movement of pacman
Author |
Message |
Waked
|
Posted: Fri Apr 06, 2012 3:10 pm Post subject: movement of pacman |
|
|
i am making a pacman game as my final project. im just having some trouble making pacman move all the way until he hits a wall. i can only make him move as long as u hold the arrow.
this is what i have so please help:
var command : array char of boolean
var frame : int := 0
var Pacman : array 1 .. 4 of array 1 .. 4 of int
Pacman (1) (1) := Pic.FileNew ("pics/pClosed.bmp")
Pacman (1) (2) := Pic.FileNew ("pics/pUp1.bmp")
Pacman (1) (3) := Pic.FileNew ("pics/pUp2.bmp")
Pacman (1) (4) := Pacman (1) (2)
Pacman (2) (1) := Pic.FileNew ("pics/pClosed.bmp")
Pacman (2) (2) := Pic.FileNew ("pics/pLeft1.bmp")
Pacman (2) (3) := Pic.FileNew ("pics/pLeft2.bmp")
Pacman (2) (4) := Pacman (2) (2)
Pacman (3) (1) := Pic.FileNew ("pics/pClosed.bmp")
Pacman (3) (2) := Pic.FileNew ("pics/pDown1.bmp")
Pacman (3) (3) := Pic.FileNew ("pics/pDown2.bmp")
Pacman (3) (4) := Pacman (3) (2)
Pacman (4) (1) := Pic.FileNew ("pics/pClosed.bmp")
Pacman (4) (2) := Pic.FileNew ("pics/pRight1.bmp")
Pacman (4) (3) := Pic.FileNew ("pics/pRight2.bmp")
Pacman (4) (4) := Pacman (4) (2)
var pacman : int := Pacman (2) (2)
%---------------------------------------------------------------------------------
var font := Font.New ("comicsansms:19:bold")
var stage : int := Pic.FileNew ("pics/stage1.bmp")
proc Visuals ( px, py, score : int)
Pic.Draw (stage, 0, 0, picCopy)
Font.Draw ("GAME", 30, maxy - 50, font, 84)
Font.Draw ("SCORE", 24, maxy - 75, font, 84)
Font.Draw (intstr (score), 12, maxy - 110, font, 84)
Pic.Draw (pacman, px, py, picMerge)
for i : 1 .. 4
if frame > 4 then
frame := 1
end if
if command (KEY_UP_ARROW) then
pacman := Pacman (1) (frame)
end if
if command (KEY_DOWN_ARROW) then
pacman := Pacman (3) (frame)
end if
if command (KEY_RIGHT_ARROW) then
pacman := Pacman (4) (frame)
end if
if command (KEY_LEFT_ARROW) then
pacman := Pacman (2) (frame)
end if
end for
View.Update
end Visuals
%----------------------------------------------------------------------------------
proc PacmanMove (var px, py : int)
Input.KeyDown (command)
if command (KEY_UP_ARROW) and whatdotcolor (px + 26, py + 26) = 7 and whatdotcolor (px + 15, py + 32) = 7 and whatdotcolor (px + 3, py + 26) = 7 then
py += 1
frame := frame + 1
end if
if command (KEY_DOWN_ARROW) and whatdotcolor (px + 26, py + 3) = 7 and whatdotcolor (px + 15, py - 5) = 7 and whatdotcolor (px + 3, py + 3) = 7 then
py -= 1
frame := frame + 1
end if
if command (KEY_RIGHT_ARROW) and whatdotcolor (px + 26, py + 3) = 7 and whatdotcolor (px + 32, py + 15) = 7 and whatdotcolor (px + 26, py + 26) = 7 then
px += 1
frame := frame + 1
end if
if command (KEY_LEFT_ARROW) and whatdotcolor (px + 3, py + 3) = 7 and whatdotcolor (px - 5, py + 15) = 7 and whatdotcolor (px + 3, py + 26) = 7 then
px -= 1
frame := frame + 1
end if
end PacmanMove
%----------------------------------------------------------------------------------
proc Teleport (var px, py : int)
if px <= 132 and py >= 340 and py <= 376 then
px := 687
elsif px >= 697 and py >= 340 and py <= 376 then
px := 134
end if
end Teleport
%----------------------------------------------------------------------------------
View.Set ("graphics: 800;645,offscreenonly")
var score : int := 0
var px, py : int
px := 250
py := 49
loop
Input.KeyDown (command)
Visuals ( px, py, score)
PacmanMove (px, py)
Teleport (px, py)
end loop |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dreadnought
|
Posted: Fri Apr 06, 2012 3:57 pm Post subject: Re: movement of pacman |
|
|
Rather than continuously check if the user is pressing a key and move pacman accordingly, why not give pacman a velocity when a key is pressed? Then you just change his position based on his velocity on every iteration of your main loop.
If you think about it, this is similar to what happens in the real world (ignoring friction). |
|
|
|
|
|
Waked
|
Posted: Sat Apr 07, 2012 11:00 am Post subject: RE:movement of pacman |
|
|
could you please explain to me how would i get the velocity to work? cuz i dont really get the velocity idea that well. |
|
|
|
|
|
2goto1
|
Posted: Sat Apr 07, 2012 11:43 am Post subject: RE:movement of pacman |
|
|
Pacman essentially has 2 speeds - stopped, or moving. Pacman also has 4 directions - left, up, right, down. Imagine if Pacman is stopped, and you want the user to make Pacman move. When the user presses an arrow key, say arrow up, you could use 2 variable - one indicating that Pacman is facing the up direction, and one indicating that Pacman is moving.
Pacman's movement can normally stop in two different ways. If Pacman is moving up but runs into a wall or a ghost, his movement stops. If he runs into a wall, his movement stops but his direction still points up.
If you use 2 variables to track whether Pacman is moving, and what direction Pacman is facing, you can then set those variables when your user presses an arrow key. So then your loop code can look at the value of those variables instead of whether or not the user is actually pressing a key at this moment. |
|
|
|
|
|
|
|