Snake Collision Help
Author |
Message |
PaPaJi
|
Posted: Tue May 13, 2008 8:32 am Post subject: Snake Collision Help |
|
|
i cant get the collision to work for when the snake head hits the snake body
could you tell me how i could do that?
heres my game program:
Turing: | import GUI
var name : string
var LEFT_ARROW, RIGHT_ARROW, DOWN_ARROW, UP_ARROW : string
var ss1, ss2 : int
var x1, y1 : int := 300
var N, E, S, W : int := 0
var response : string
var timerunning : int := 0
var counter : int := 0
var score : int := 0
var level : string
var music : int
var font1, font2, font3, font4 : int
font1 := Font.New ("serif:12")
assert font1 > 0
font2 := Font.New ("sans serif:18:bold")
assert font2 > 0
font3 := Font.New ("mono:9")
assert font3 > 0
font4 := Font.New ("Palatino:18:bold,italic")
assert font4 > 0
LEFT_ARROW := chr (203)
RIGHT_ARROW := chr (205)
UP_ARROW := chr (200)
DOWN_ARROW := chr (208)
% The introduction
Font.Draw ("Welcome to Humza's Personal Snake Game!", 60, 379, font2, red)
delay (1000)
Font.Draw ("What is your name?", 280, 350, font1, magenta)
locate(5, 40)
get name: * %user enters his or hers name
Font.Draw("Thank you for choosing to play Humza's game", 50, 300,font4, blue)
delay (1000)
cls
colourback(red)
colour(brightred)
put "Please Select the Background music you would like to play ", name
put "1 = Kiss Kiss (Chris Brown ft. T-Pain)"
put "2 = I Wanna Love You (Akon ft. Snoop Dogg)"
put "3 = Good Life (Kanye West ft. T-Pain)"
get music
if music = 1 then
Music.PlayFileReturn ("G:/kisskiss.mp3")
elsif music = 2 then
Music.PlayFileReturn ("G:/i wanna love you.mp3")
elsif music = 3 then
Music.PlayFileReturn ("G:/Kanye West Good Life.mp3")
end if
cls
put name, " ,please enter the level you would like to play at (1-3)"
get level % lets the user enter the level they want to play
if level > "3" then
level := "3"
elsif level <= "0" then
level := "1"
end if
put "Please wait a moment......."
colourback (green)
delay (1000)
cls
drawbox (0, 0, maxx, maxy, black)
setscreen ("nocursor") % removes the cursor from the screen
setscreen ("graphics") % sets the screen to "graphics"
ss1 := 100 % gives the variables a value
ss2 := 100
ss1 := 100
ss2 := 100
var xx1 : array 1 .. 50 of int % the position
var yy1 : array 1 .. 50 of int
var a : int := 0
for i : 1 .. 50
xx1 (i ) := 0
yy1 (i ) := 0
end for
procedure game
cls
loop
colourback (green)
drawbox (10, 10, maxx - 10, maxy - 10, brightgreen) % draws the border
drawfilloval (x1, y1, 5, 5, red) % draws the apple
var key : array char of boolean
drawfillbox (ss1, ss2, ss1 + 10, ss2 + 10, black) % draws the head of snake
Input.KeyDown (key )
if key (KEY_LEFT_ARROW) then % moves the snake left
N := 0
E := 0
S := 0
W := 1
put "" ..
elsif key (KEY_RIGHT_ARROW) then % moves the snake right
N := 0
E := 1
S := 0
W := 0
put "" ..
elsif key (KEY_UP_ARROW) then % moves the snake up
N := 1
E := 0
S := 0
W := 0
put "" ..
elsif key (KEY_DOWN_ARROW) then % moves the snake down
N := 0
E := 0
S := 1
W := 0
put "" ..
elsif key ('p') then % pauses the game using "p"
N := 0
E := 0
S := 0
W := 0
put "" ..
end if
% below is the different levels and their hardness
if level = "1" then
delay (7)
elsif level = "2" then
delay (5)
elsif level = "3" then
delay (3)
end if
a := a + 1
if a = 11 then
for decreasing i : 50 .. 2
xx1 (i ) := xx1 (i - 1)
yy1 (i ) := yy1 (i - 1)
end for
xx1 (1) := ss1
yy1 (1) := ss2
locate (1, 1)
a := 0
end if
for i : 1 .. counter
Draw.FillBox (xx1 (i + 1), yy1 (i + 1), xx1 (i + 1) + 10, yy1 (i + 1) + 10, green)
end for
for i : 1 .. counter
Draw.FillBox (xx1 (i ), yy1 (i ), xx1 (i ) + 10, yy1 (i ) + 10, black)
end for
drawfillbox (ss1, ss2, ss1 + 10, ss2 + 10, green)
if N = 1 then
ss2 := ss2 + 1
elsif E = 1 then
ss1 := ss1 + 1
elsif S = 1 then
ss2 := ss2 - 1
elsif W = 1 then
ss1 := ss1 - 1
end if
% below is the code of the collision between the snake and the apple
if ss1 + 10 > x1 and ss1 - 10 < x1 and y1 > ss2 - 10 and y1 < ss2 + 10 then
drawfilloval (x1, y1, 5, 5, green) % the apple goes away into the background
randint (x1, 10, maxx - 10) % places the new snake in random place
randint (y1, 10, maxy - 10)
delay (5)
drawfilloval (x1, y1, 5, 5, brightred)
counter := counter + 1
% the below code adds up the score
if level = "1" then
score := score + 40
elsif level = "2" then
score := score + 80
elsif level = "3" then
score := score + 120
end if
end if
% the below code counts the time
timerunning := Time.Elapsed
if timerunning < 10000 then
score := score + 2000
elsif timerunning > 50000 then
score := score + 4000
elsif timerunning > 100000 then
score := score + 6000
elsif timerunning > 200000 then
score := score + 8000
elsif timerunning > 400000 then
score := score + 10000
end if
% below code ends the game when the snake hits the borders
if ss1 >= maxx - 10 or ss1 < 10 or ss2 >= maxy - 10 or ss2 < 10 then
var font14 := Font.New ("Impact :20")
var text14 := "Game Over"
var width14 := Font.Width (text14, font14 )
Font.Draw (text14, 230, 230, font14, red)
exit
end if
end loop
delay (1000)
% below code is the conclusion of the program and tallies up the persons score
cls
colour(blue)
put ""
put "You ate ", counter, " apples"
put ""
put "Your time taken in seconds was ", timerunning div 1000
put "You scored, ", score, ", ", name, "!"
put "Thank you for playing Humza's game! He greatly appreciates it"
end game
% below code gets the buttons before the game starts
var button1 : int := GUI.CreateButton (230, 1, 200, "Play Game", game )
procedure instructions
colourback (green)
color (white)
locate (2, 1)
% below are the instructions of the game when the user clicks instructions
put "*********************************Instructions*********************************"
put "1. Use your arrow keys to move the snake."
put "2. Try to get the apples to make the snake grow."
put "3. Use P to pause the game."
put "4. If you have paused the game, then use the arrows keys to resume."
put "5. Try to get the apples to increase your score."
put "6. Try not to collide with the wall or the body of the snake."
% below ends everything
end instructions
var button2 : int := GUI.CreateButton (230, 380, 200, "Instructions", instructions )
loop
exit when GUI.ProcessEvent
end loop |
Mod says: Use code tags! Topic split to Turing Help section. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
riveryu
|
Posted: Sat May 17, 2008 6:36 pm Post subject: RE:Snake Collision Help |
|
|
One way I think is using whatdotcolor, which returns the color int value of the a pixel (x,y) specified. Learn from Turing Help [F10].
You can compare the snake head to the color of its body.
Furthermore, correct me if Im wrong but normally in snake you can't go towards your body(backward). You can do this by using if and then comparing which direction has been set and which direction should not be available at the time.
ex.
if (righted) then
(CanGoLeft) := false
end if |
|
|
|
|
|
gitoxa
|
Posted: Sat May 17, 2008 6:43 pm Post subject: RE:Snake Collision Help |
|
|
Procedures go at the top of your code. |
|
|
|
|
|
|
|