Just posting some progress. I've been working on the ghost over the weekend and tonight. I finished all the code in the tutorial about the ghost movement and AI however there is a problem somewhere inmy code. I have spent ages lookingfor itbuti can't seem to find it. Sometimes the ghosts stop completely andother times they manage to move diagonaly throught the walls of a junction. The Ai doesnt seem to be working either as the ghosts pay no attentionto the player whatsoever. If anyone couldhavea quicklook throughmy codethen i'd be really grateful.
Cheers,
Michael
set display mode 640,480,32
sync on : sync rate 60
rem randomize timer
Randomize timer()
rem declare global variables
global playerx#
global playery#
global playerup
global playerdown
global playerleft
global playeright
global playercangoup
global playercangodown
global playercangoleft
global playercangoright
global score
dim maze#(20,16)
rem ghost array
dim ghosts#(4,11)
remstart
1 = x co-ordinate
2 = y co-ordinate
3 = going up?
4 = going down?
5 = going left?
6 = going right?
7 = can go up?
8 = can go down?
9 = can go left?
10 = can go right?
11 = state (alive,eaten,under influenece of pills, in pen etc)
remend
rem load all media for the game
loadmedia()
rem load the maze
loadmaze()
rem setup the variables
startlevel()
do
rem check if player is in the centre of a cell
centrex# = (playerx# - 174) / 16
centrey# = (playery# - 59) / 16
centrex = Int(centrex#)
centrey = Int(centrey#)
centre = 0
if centrex# = centrex and centrey# = centrey
centre = 1
endif
rem if the player is in the centre
if centre = 1
rem initilaise all possible directions
playercangoup = 0
playercangodown = 0
playercangoleft = 0
playercangoright = 0
rem check what is imediately up,down,left and right
mazeup = maze#(centrey - 1, centrex)
mazedown = maze#(centrey + 1, centrex)
mazeleft = maze#(centrey, centrex - 1)
mazeright = maze#(centrey, centrex + 1)
rem determine whether it is possible for the player tomake a move
If mazeup = 0 or mazeup >= 3
playercangoup = 1
else
playerup = 0
endif
if mazedown = 0 or mazedown >= 3
playercangodown = 1
else
playerdown = 0
endif
if mazeleft = 0 or mazeleft >= 3
playercangoleft = 1
else
playerleft = 0
endif
if mazeright = 0 or mazeright >= 3
playercangoright = 1
else
playeright = 0
endif
rem allow quick about turn
if playerup = 1 and downkey() = 1
reset_player_direction()
playerdown = 1
endif
if playerdown = 1 and upkey() = 1
reset_player_direction()
playerup = 1
endif
if playerleft = 1 and rightkey() = 1
reset_player_direction()
playeright = 1
endif
if playerright = 1 and leftkey() = 1
reset_player_direction()
playerleft = 1
endif
playerturn()
endif
showplayer()
playermove()
eatpills()
showghost()
ghostmove()
checkghost()
sync
loop
remstart
1 is wall
2 is gate
3 is pill
4 is power pill
0 is gap
remend
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data 1,4,3,3,3,3,3,3,3,3,3,3,3,3,4,1
Data 1,3,1,1,1,1,1,3,3,1,1,1,1,1,3,1
Data 1,3,1,3,3,3,3,3,3,3,3,3,3,1,3,1
Data 1,3,1,3,1,3,1,0,0,1,3,1,3,1,3,1
Data 1,3,1,3,1,3,1,2,2,1,3,1,3,1,3,1
Data 1,3,1,3,1,3,1,0,0,1,3,1,3,1,3,1
Data 1,3,1,3,1,3,1,1,1,1,3,1,3,1,3,1
Data 0,3,1,3,1,3,3,3,3,3,3,1,3,1,3,0
Data 1,3,1,3,1,1,1,1,1,1,1,1,3,1,3,1
Data 1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1
Data 1,3,1,1,1,1,1,1,1,1,1,1,1,1,3,1
Data 1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1
Data 1,3,1,3,1,3,1,1,1,1,3,1,3,1,3,1
Data 1,3,1,3,1,3,3,3,3,3,3,1,3,1,3,1
Data 1,3,1,3,1,1,1,1,1,1,1,1,3,1,3,1
Data 1,3,1,3,3,3,3,3,0,3,3,3,3,1,3,1
Data 1,3,1,1,1,3,1,1,1,1,3,1,1,1,3,1
Data 1,4,3,3,3,3,3,3,3,3,3,3,3,3,4,1
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
function loadmedia
load image "media\wall.bmp",1
load image "media\gate.bmp",2
load image "media\pill.bmp",3
load image "media\power.bmp",4
load image "media\blank.bmp",5
load image "media\pacman.bmp",6
load image "media\pacplace.bmp",7
load image "media\redghost.bmp",8
load image "media\blueghost.bmp",9
load image "media\pinkghost.bmp",10
load image "media\greenghost.bmp",11
endfunction
function loadmaze
sprite 1,190,75,5
mazewall = 10
mazepill = 200
mazepower = 500
For Y = 0 to 19
For X = 0 to 15
Read maze
Maze#(Y+1,X+1) = Maze
if maze = 1
sprite mazewall,190+x*16,75+y*16,1
inc mazewall
endif
if maze = 2
sprite mazewall,190+x*16,75+y*16,2
inc mazewall
endif
if maze = 3
sprite mazepill,190+x*16,75+y*16,3
inc mazepill
endif
if maze = 4
sprite mazepower,190+x*16,75+y*16,4
inc mazepower
endif
Next X
Next Y
endfunction
function startlevel
playerx# = 318
playery# = 331
playerup = 0
playerdown = 0
playerleft = 0
playeright = 0
resetghosts()
endfunction
function showplayer
sprite 2,playerx#,playery#,6
sprite 3,playerx#,playery#,7
endfunction
function playermove
if playerup then playery# = playery# - 1
if playerdown then playery# = playery# + 1
if playerleft then playerx# = playerx# - 1
if playeright then playerx# = playerx# + 1
rem if player leaves the maze
if playerx# < 190 and playerleft = 1
playerx# = 439
endif
if playerx# > 428 and playeright = 1
playerx# = 190
endif
endfunction
function playerturn
if upkey() and playercangoup =1
reset_player_direction()
playerup = 1
endif
if downkey() and playercangodown = 1
reset_player_direction()
playerdown = 1
endif
if leftkey() and playercangoleft= 1
reset_player_direction()
playerleft = 1
endif
if rightkey() and playercangoright = 1
reset_player_direction()
playeright = 1
endif
endfunction
function reset_player_direction
playerup = 0
playerdown = 0
playerleft = 0
playeright = 0
endfunction
function eatpills
for pillstate = 200 to 343
if spritepixelcollision (2,pillstate) > 0 then delete sprite pillstate : score = score + 10
next pillstate
set cursor 310,400
print score
endfunction
function resetghosts
rem reset ghost directions
for ghost = 1 to 4
ghosts#(ghost,3) = 0
ghosts#(ghost,4) = 0
ghosts#(ghost,5) = 0
ghosts#(ghost,6) = 0
ghosts#(ghost,7) = 0
ghosts#(ghost,8) = 0
ghosts#(ghost,9) = 0
ghosts#(ghost,10) = 0
next ghost
rem red ghost starts at top of pen
ghosts#(1,1) = 286
ghosts#(1,2) = 123
ghosts#(1,11) = 0
rem blue ghost starts in pen
ghosts#(2,1) = 302
ghosts#(2,2) = 171
ghosts#(2,11) = 1
rem pink ghost starts in pen
ghosts#(3,1) = 318
ghosts#(3,2) = 171
ghosts#(3,11) = 1
rem green ghost starts at top of pen
ghosts#(4,1) = 334
ghosts#(4,2) = 123
ghosts#(4,11) = 0
rem give each ghost a starting direction
Ghosts#(1,5) = 1
Ghosts#(2,3) = 1
Ghosts#(3,3) = 1
Ghosts#(4,6) = 1
endfunction
function showghost
for ghost = 1 to 4
ghostsprite = 1000 + ghost
ghostx# = ghosts#(ghost,1)
ghosty# = ghosts#(ghost,2)
ghostimage = ghost + 7
sprite ghostsprite,ghostx#,ghosty#,ghostimage
next ghost
endfunction
function ghostmove
for ghost = 1 to 4
ghostx# = ghosts#(ghost,1)
ghosty# = ghosts#(ghost,2)
ghostup = ghosts#(ghost,3)
ghostdown = ghosts#(ghost,4)
ghostleft = ghosts#(ghost,5)
ghostright = ghosts#(ghost,6)
rem check to see if the ghost is at the edge of the maze
if ghostx# < 190
ghostx# = 428
endif
if ghostx# > 428
ghostx# = 190
endif
if ghostup = 1 then ghosty# = ghosty# - 1
if ghostdown = 1 then ghosty# = ghosty# + 1
if ghostleft = 1 then ghostx# = ghostx# - 1
if ghostright = 1 then ghostx# = ghostx# + 1
ghosts#(ghost,1) = ghostx#
ghosts#(ghost,2) = ghosty#
next ghost
endfunction
function checkghost
for ghost = 1 to 4
ghostx# = ghosts#(ghost,1)
ghosty# = ghosts#(ghost,2)
ghostup = ghosts#(ghost,3)
ghostdown = ghosts#(ghost,4)
ghostleft = ghosts#(ghost,5)
ghostright = ghosts#(ghost,6)
rem check if the ghost is in the centre of a cell
centrex# = (ghostx# - 174) / 16
centrey# = (ghosty# - 59) / 16
centrex = Int(centrex#)
centrey = Int(centrey#)
centre = 0
if centrex# = centrex and centrey# = centrey
centre = 1
endif
rem if ghost is in centre of cell
if centre = 1
rem initialise all possible directions
ghostcangoup = 0
ghostcangodown = 0
ghostcangoleft = 0
ghostcangoright = 0
rem check what is imediately up,down,left and right
mazeup = maze#(centrey - 1, centrex)
mazedown = maze#(centrey + 1, centrex)
mazeleft = maze#(centrey, centrex - 1)
mazeright = maze#(centrey, centrex + 1)
rem store current directions
ghostwasup = ghostup
ghostwasdown = ghostdown
ghostwasleft = ghostleft
ghostwasright = ghostright
rem determine whether it is possible for the player tomake a move
If mazeup = 0 or mazeup >= 3
ghostcangoup = 1
else
ghostup = 0
endif
if mazedown = 0 or mazedown >= 3
ghostcangodown = 1
else
ghostdown = 0
endif
if mazeleft = 0 or mazeleft >= 3
ghostcangoleft = 1
else
ghostleft = 0
endif
if mazeright = 0 or mazeright >= 3
ghostcangoright = 1
else
ghostright = 0
endif
ghosts#(ghost,3) = ghostup
ghosts#(ghost,4) = ghostdown
ghosts#(ghost,5) = ghostleft
ghosts#(ghost,6) = ghostright
ghosts#(ghost,7) = ghostcangoup
ghosts#(ghost,8) = ghostcangodown
ghosts#(ghost,9) = ghostcangoleft
ghosts#(ghost,10) = ghostcangoright
rem if ghost isn't moving
if ghostup + ghostdown + ghostright + ghostleft = 0
turnghost(ghost)
endif
endif
if ghostwasup = 1
if ghostcangoleft = 1 or ghostcangoright = 1
ghostcangodown = 0
ghostup = 0
endif
endif
if ghostwasdown = 1
if ghostcangoleft = 1 or ghostcangoright = 1
ghostcangoup = 0
ghostdown = 0
endif
endif
if ghostwasleft = 1
if ghostcangoup = 1 or ghostcangodown = 1
ghostcangoright = 0
ghostleft = 0
endif
endif
if ghostwasright = 1
if ghostcangoup = 1 or ghostcangodown = 1
ghostcangoleft = 0
ghostright = 0
endif
endif
next ghost
endfunction
function turnghost(ghost)
dim Turn$(4)
Turn = 0
ghostcangoup = ghosts#(ghost,7)
ghostcangodown = ghosts#(ghost,8)
ghostcangoleft = ghosts#(ghost,9)
ghostcangoright = ghosts#(ghost,10)
if ghostup + ghostdown + ghostleft + ghostright = 0
if ghostcangoup = 1
inc turn
Turn$(turn) = "U"
endif
if ghostcangodown = 1
inc turn
Turn$(turn) = "D"
endif
if ghostcangoleft = 1
inc turn
Turn$(turn) = "L"
endif
if ghostcangoright = 1
inc turn
Turn$(turn) = "R"
endif
Direction$ = Turn$(Rnd(Turn) + 1)
if Direction$ = "U" then ghostup = 1
if Direction$ = "D" then ghostdown = 1
if Direction$ = "L" then ghostleft = 1
if Direction$ = "R" then ghostright = 1
endif
rem see where the player is in relation to the ghost
playerxdiff# = playerx# - ghosts#(ghost,1)
playerydiff# = playery - ghosts#(ghost,2)
rem see whether the ghost should be goin horizontally or vertically
if abs(playerxdiff#) > abs(playerydiff#)
xbias = 1
ybias = 0
else
xbias = 0
ybias = 1
endif
if xbias = 1
if playerxdiff# > 0
if ghostcangoleft = 1
ghostleft = 1
endif
else
if ghostcangoright = 1
ghostright = 1
endif
endif
endif
if ybias = 1
if playerydiff# > 0
if ghostcangodown = 1
ghostdown = 1
endif
else
if ghostcangoup = 1
ghostup = 1
endif
endif
endif
ghosts#(ghost,3) = ghostup
ghosts#(ghost,4) = ghostdown
ghosts#(ghost,5) = ghostleft
ghosts#(ghost,6) = ghostright
endfunction
rem pixel perfect collision code taken from the codebase
function SpritePixelCollision(sprite1 as integer, sprite2 as integer)
local Sprite1Base as dword
local Sprite2Base as dword
local Sprite1Addr as dword
local Sprite2Addr as dword
local V as dword
if sprite collision (sprite1, sprite2) > 0
make memblock from image 255,sprite image(sprite1)
make memblock from image 256,sprite image(sprite2)
Sprite1Base=get memblock ptr(255)
Sprite2Base=get memblock ptr(256)
XPos1=sprite x(sprite1)-sprite offset x(sprite1)
YPos1=sprite y(sprite1)-sprite offset y(sprite2)
Width1=*Sprite1Base
inc Sprite1Base, 4
Height1=*Sprite1Base
inc Sprite1Base, 11
XPos2=sprite x(sprite2)-sprite offset x(sprite2)
YPos2=sprite y(sprite2)-sprite offset y(sprite2)
Width2=*Sprite2Base
inc Sprite2Base, 4
Height2=*Sprite2Base
inc Sprite2Base, 11
if XPos2 > XPos1
XStart1=XPos2-XPos1
XStart2=0
XOverlapSize=(Width1-XStart1)-1
if XOverlapSize >= Width2 then XOverlapSize=Width2-1
else
XStart2=XPos1-XPos2
XStart1=0
XOverlapSize=(Width2-XStart2)-1
if XOverlapSize >= Width1 then XOverlapSize=Width1-1
endif
if YPos2 > YPos1
YStart1=YPos2-YPos1
YStart2=0
YOverlapSize=(Height1-YStart1)-1
if YOverlapSize >= Height2 then YOverlapSize=Height2-1
else
YStart2=YPos1-YPos2
YStart1=0
YOverlapSize=(Height2-YStart2)-1
if YOverlapSize >= Height1 then YOperlapSize=Height1-1
endif
XStart1=XStart1*4
XStart2=XStart2*4
Width1=Width1*4
Width2=Width2*4
for YOffset=YOverlapSize to 0 step -1
Sprite1Addr=Sprite1Base + ((YOffset+YStart1)*Width1) + XStart1
Sprite2Addr=Sprite2Base + ((YOffset+YStart2)*Width2) + XStart2
for XOffset=XOverlapSize to 0 step -1
v=*Sprite1Addr
if (v && 0xff000000) <> 0
v=*Sprite2Addr
if (v && 0xff000000) <> 0
delete memblock 255
delete memblock 256
exitfunction 1
endif
endif
inc Sprite1Addr, 4
inc Sprite2Addr, 4
next XOffset
next YOffset
delete memblock 255
delete memblock 256
endif
endFunction 0