Pacman help
Author |
Message |
sarah
|
Posted: Thu Mar 22, 2007 7:45 pm Post subject: Pacman help |
|
|
I was wondering if anyone could help me figure out how to make my pacman game run properly. I can't get the pacman to stop when he hits a wall, or get him to eat the dots. I've looked through quite a few tutorials and nothing seems to be helping. Any advice at all would really be appriciated.
Here is the code:
code: |
View.Set ("graphics;offscreenonly")
var x, y : int := 20
var rad : int
var key : string (1)
var dx, dy : int % direction Pacman moves
dx := 2 % Pacman will move to the right
dy := 0 % Pacman will not be going up or down
x := 30
y := 30
rad := 20
var mouth : int := 1 % size of mouth
var dir : int := 0 % mouth direction, 0=right, 90=up, etc.
var timeRunning : int
var pillx, pilly, pillrad : int
pillrad := 5
pillx := 400
pilly := 300
%borders and pacman
Draw.FillBox (0,0,10,480,magenta)
Draw.FillBox (0,0,640,10,magenta)
Draw.FillBox (640,480,0,390,magenta)
Draw.FillBox (640,480,630,0, magenta)
Draw.FillBox (310,480,330,250,magenta)
Draw.FillBox (310,0,330,160,magenta)
drawfillarc(x, y, rad, rad, mouth + dir, -mouth + dir, magenta)
clock (timeRunning)
loop
Draw.FillBox (0,0,10,480,magenta)
Draw.FillBox (0,0,640,10,magenta)
Draw.FillBox (640,480,0,390,magenta)
Draw.FillBox (640,480,630,0, magenta)
Draw.FillBox (310,480,330,250,magenta)
Draw.FillBox (310,0,330,160,magenta)
drawfilloval (pillx, pilly, pillrad, pillrad, black)
delay (10)
mouth := mouth mod 45 + 1 % change size of the mouth
drawfillarc(x, y, rad, rad, mouth + dir, -mouth + dir, magenta)
delay(10)
x := x + dx
y := y + dy
View.Update
cls
if hasch then
getch(key)
put "key ", key
if ord(key) = 205 then %right arrow
if whatdotcolor (x+20, y+20) not = white then
dx := 0
dy := 0
dir := 0
elsif ord(key) = 205 then
dx := 4
dy := 0
dir := 0
end if
elsif ord(key) = 203 then %left arrow
if whatdotcolor (x, y) not = white then
dx := 0
dy := 0
dir := 180
elsif ord(key) = 203 then
dx := -4
dy := 0
dir := 180
end if
elsif ord(key) = 200 then %up arrow
if whatdotcolor (x, y) not = white then
dx := 0
dy := 0
dir := 90
elsif ord(key) = 200 then
dx := 0
dy := 4
dir := 90
end if
elsif ord(key) = 208 then %down arrow
if whatdotcolor (x, y-20) not = white then
dx := 0
dy := 0
dir := 270
elsif ord(key) = 208 then
dx := 0
dy := -4
dir := 270
end if
end if
locate (1,1)
if Math.Distance (dx, dy, pillx, pilly) < rad + pillrad then
put "The pacman ate the pill"
else
put "Keep trying!"
end if
end if
end loop
put "It took you ", timeRunning, " milliseconds to complete the level" |
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
actualgame.t |
Filesize: |
2.64 KB |
Downloaded: |
146 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
samuelkyl
|
Posted: Thu Mar 22, 2007 8:39 pm Post subject: Re: Pacman help |
|
|
I just started turing, and need to get familiar with it to help your wall question, but for the pill, I could see the problem.
You put dx and dy in the Math.Distance function, change those to x and y, and also, put an exit statement after the "pacman ate the pill". Then move that whole block out of that if statement and right above the end of the loop statement.
Good job with the drawing of the picture, tho. Even though it could be improved, it's pretty creative.
Also check your co-ord +- for your whatdotcolours, some of them are erroneous..
|
|
|
|
|
![](images/spacer.gif) |
sarah
|
Posted: Thu Mar 22, 2007 9:09 pm Post subject: Re: Pacman help |
|
|
thank you so much. The math.distance works great now
|
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Fri Mar 23, 2007 11:16 pm Post subject: Re: Pacman help |
|
|
There is a different approach that works great for a game like pacman that is more simplistic. You can use it if you want to make life easier, or stay with your own.
Mentally split the screen up into squares (for example, 10 X 10). In each square, there will be something different. Either it will be a dot, pacman (or a piece of pacman), a piece of a wall, an enemy, or nothing at all. Basically, we can just have a 2 dimensional array represent each of these squares and assign the array elements with 0 for nothing, 1 for pacman, 2 for a dot, 3 for a piece of wall, 4 for an enemy, etc. This way, collision detection becomes so easy, because you can just check if the square pacman is about to move to has a dot, enemy, or wall and act accordingly.
Here is a quick example of what I mean.
Turing: | View.Set ("offscreenonly")
var keys : array char of boolean
%a 30 X 30 array. each element represents a 10 X 10 square on the screen
%0 = nothing, 1 = pacman, 2 = dot, 3 = piece of a wall
var grid : array 0 .. 30, 0 .. 30 of int
%x, y, is the grid position of pacman
var x, y, score := 0
%initialize all grid spaces to 0
for i : 0 .. 30
for j : 0 .. 30
grid (i, j ) := 0
end for
end for
%put some walls in. walls are better hardcoded
for n : 10 .. 18
grid (4, n ) := 3
grid (5, n ) := 3
grid (n + 10, 4) := 3
grid (n + 10, 5) := 3
grid (n, 20) := 3
grid (n, 21) := 3
end for
%put some random dots in and make sure they aren't on top of an existing wall
for i : 0 .. 30
for j : 0 .. 30
if Rand.Int (1, 100) > 90 and grid (i, j ) = 0 then
grid (i, j ) := 2
end if
end for
end for
colour (0)
colourback (7)
loop
Input.KeyDown (keys )
grid (x, y ) := 0
%move pacman unless there's a wall there
if keys (chr (200)) and y < 30 and grid (x, y + 1) not= 3 then
y + = 1
elsif keys (chr (208)) and y > 0 and grid (x, y - 1) not= 3 then
y - = 1
elsif keys (chr (205)) and x < 30 and grid (x + 1, y ) not= 3 then
x + = 1
elsif keys (chr (203)) and x > 0 and grid (x - 1, y ) not= 3 then
x - = 1
else
grid (x, y ) := 1
end if
%before we delete the dot from the grid array because pacman has got it, put the score up by 10
if grid (x, y ) = 2 then
score + = 10
end if
%now change delete the dot from the grid array (and replace it with pacman)
grid (x, y ) := 1
cls
for i : 0 .. 30
for j : 0 .. 30
%draw depending on what's in the grid space
if grid (i, j ) = 1 then
Draw.FillOval (i * 10 + 5, j * 10 + 5, 5, 5, yellow)
elsif grid (i, j ) = 2 then
Draw.FillOval (i * 10 + 5, j * 10 + 5, 3, 3, white)
elsif grid (i, j ) = 3 then
Draw.FillBox (i * 10, j * 10, i * 10 + 10, j * 10 + 10, red)
end if
end for
end for
put score
View.Update
delay (50)
end loop |
|
|
|
|
|
![](images/spacer.gif) |
tenniscrazy
![](http://compsci.ca/v3/uploads/user_avatars/16168420744a9c16d226a1c.jpg)
|
Posted: Thu Apr 19, 2007 10:58 am Post subject: Re: Pacman help |
|
|
I might use that idea for my pacman game im making thanks
|
|
|
|
|
![](images/spacer.gif) |
|
|