Computer Science Canada I need help with snake game! |
Author: | PaPaJi [ Tue May 13, 2008 7:41 am ] |
Post subject: | I need help with snake game! |
here is the code, i am having trouble with getting the game to end when the snake hits itself if someone could type the code that would work with my program, I would greatly appreciate it thanks 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 LEFT_ARROW := chr (203) RIGHT_ARROW := chr (205) UP_ARROW := chr (200) DOWN_ARROW := chr (208) % The introduction put "Welcome to Humza's Personal Snake Game!" delay (2000) put "What is your name?" get name %user enters his or hers name put "Thank you for choosing to play Humza's game, ", name delay (1000) 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 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 |
Author: | S_Grimm [ Tue May 13, 2008 11:35 am ] |
Post subject: | RE:I need help with snake game! |
try whatdotcolor |
Author: | Asherel [ Tue May 13, 2008 12:02 pm ] | ||
Post subject: | RE:I need help with snake game! | ||
I believe you will need to compare the ss1 value to see if it equals the end of your snake. If it does then.
|