Help needed with snake game
Author |
Message |
kytes
|
Posted: Sat May 03, 2008 11:26 am Post subject: Help needed with snake game |
|
|
i have this much of the game so far...im having trouble with the snake eating the apple and then the apple coming up in a new spot. if someone could please tell me how to do this, i would greatly appreciate it. just reply with the new code that will work with my program. thanks alot
ps. the apple code is between the lines of stars
const LEFT_ARROW := chr (203)
const RIGHT_ARROW := chr (205)
const UP_ARROW := chr (200)
const DOWN_ARROW := chr (208)
var BLOCKER_ROW := 12
var ss1, ss2 : int
var Blocker_Column := 38
var Key : string (1)
var x2, y2 : int
var x1, y1 : int
var diff : int
var U, R, L, D : int := 0
var arrows : array char of boolean
ss1 := 20
ss2 := 20
x2 := ss1
y2 := ss2
colourback (green)
setscreen ("graphics:600;400")
setscreen ("nocursor")
randint (x1, 1, maxx - 10)
randint (y1, 1, maxy - 10)
loop
drawfillbox (x2, y2, x2 + 10, y2 + 10, white)
drawfillbox (ss1, ss2, ss1 + 10, ss2 + 10, black)
x2 := ss1
y2 := ss2
delay (50)
Input.KeyDown (arrows)
locate (BLOCKER_ROW, Blocker_Column)
if arrows (KEY_RIGHT_ARROW) then
L := 0
D := 0
R := 1
U := 0
elsif arrows (KEY_LEFT_ARROW) then
L := 1
D := 0
R := 0
U := 0
elsif arrows (KEY_DOWN_ARROW) then
L := 0
D := 1
R := 0
U := 0
elsif arrows (KEY_UP_ARROW) then
L := 0
D := 0
R := 0
U := 1
end if
if U = 1 then
ss2 := ss2 + 10
elsif R = 1 then
ss1 := ss1 + 10
elsif D = 1 then
ss2 := ss2 - 10
elsif L = 1 then
ss1 := ss1 - 10
end if
if ss1 >= maxx - 10 then
exit
elsif ss1 < 1 then
exit
elsif ss2 >= maxy - 10 then
exit
elsif ss2 < 1 then
exit
end if
%*************************************
drawfillbox (x1, y1, x1+10, y1+10, red)
for i : 1..10
if ss1 + i = x1 then
drawfillbox (x1, y1, x1+10, y1+10, white)
drawfillbox (x1, y1, x1+10, y1+10, red)
elsif ss2 + i = y1 then
drawfillbox (x1, y1, x1+10, y1+10, white)
drawfillbox (x1, y1, x1+10, y1+10, red)
end if
end for
%***************************************
end loop
cls
locate (12, 35)
put "GAME OVER" |
|
|
|
|
|
Sponsor Sponsor
|
|
|
josh_65665
|
Posted: Sat May 03, 2008 1:41 pm Post subject: try this |
|
|
give this a try
code: |
drawfillbox (x1, y1, x1+10, y1+10, red)
if ss1 > x1-10 and ss1 < x1+10 and ss2 >y1-10 and ss2 <y1+10 then
drawfillbox (x1, y1, x1+10, y1+10, white)
x1 := Rand.Int (1,maxx)
y1 := Rand.Int (1,maxy)
end if
|
|
|
|
|
|
|
Zampano
|
Posted: Sat May 03, 2008 2:28 pm Post subject: Re: Help needed with snake game |
|
|
You may want to note that the setup with the L,D,R,U variables is just a proxy. When a button is pressed, these variables are modified, only to modify ss1 and ss2 later, creating a redundancy. Try eliminating the middleman, and place the modification of ss1 and ss2 right under the conditions with the constant chars.
Note also that you have unused variables, a locate procedure with no text to right, several logical flaws (if and elsif which do the same thing), and many inconsistencies in your variable names . . .
Also, note that you are recommended to not use randint (int, int lit, int lit), but rather int Rand.Int (int lit, int lit). |
|
|
|
|
|
Tony
|
Posted: Sat May 03, 2008 3:06 pm Post subject: RE:Help needed with snake game |
|
|
@Zampano - well a common element of a snake game is that the snake keeps on going even if no keys are pressed (otherwise on the 2nd frame the Input.KeyDown will set the entire keymap to false and the snake would stop).
@kytes
code: |
drawfillbox (x1, y1, x1+10, y1+10, white)
drawfillbox (x1, y1, x1+10, y1+10, red)
|
This seems like a problem. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Asherel
|
Posted: Sat May 03, 2008 8:11 pm Post subject: Re: Help needed with snake game |
|
|
Alright, since I saw your post in your old thread, while I was answering it was taken down.
However, I luckly remember your question
code: |
drawfillbox (ss1, ss2, ss1 + 10, ss2 + 10, black)
|
This is your snake. To make it grow, you will need to manipulate the ss1 + 10 variable by adding an increment of some number. This will cause your snake to grow length wise, but remain the same height.
To do this you need an if statement to check to see if it collides, the have it increase the ss1 + 10 by a certain amount. |
|
|
|
|
|
Tony
|
Posted: Sat May 03, 2008 8:43 pm Post subject: RE:Help needed with snake game |
|
|
don't bother. kytes' been bluntly disregarding the forum rules and landed himself a "banned" status. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Asherel
|
Posted: Sat May 03, 2008 8:48 pm Post subject: Re: Help needed with snake game |
|
|
Oh, alright.
Posted it before I knew of his banned status. My mistake. |
|
|
|
|
|
isaiahk9
|
Posted: Mon May 05, 2008 5:27 am Post subject: Re: Help needed with snake game |
|
|
IT is not that hard to respawn apples. All you need to do is make coordinates for your snake's head - x and y corrdinates. If the user presses a key then the coordinates change and the picture is drawn on those new coordinates. If the snake position is the same as the apple position, then the apple dissapears, score = score + 1 and a new apple is drawn at rand.int (1, 600), rand.int (1, 400).
I think thats all. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
isaiahk9
|
Posted: Mon May 05, 2008 4:37 pm Post subject: Re: Help needed with snake game |
|
|
woops! didn't see the ban sign |
|
|
|
|
|
|
|