Computer Science Canada Snake not appearing |
Author: | jellysausage [ Sat May 26, 2007 4:47 pm ] |
Post subject: | Snake not appearing |
hmm... so ive been working on my snake game-thing-a-ma-bobber.... and my previous program was rather.. eh.. un-optimized so ive optimized the best i could. someone with more experience than me told me to have all the algorithms and all first, then the animating last... they also recommended using View.Update.. which i figured out how to incorperate into my program. anymway, the topic is rather self explanitory--my snake is not appearing. i havent set up all the stuff about hitting yourself and you die or hitting the wall and you die... but those are easy, im still focusing on getting the snake to: -eat and grow eat and grow -be able to turn multiple times without the program crashing here's my code: %SETSCREEN setscreen ("graphics, nocursor,") %DECLARING VARIABLES var bod : array 1 .. 2, 1 .. 100 of int %first coordinate determines if variable %is the x or y coordinate %"1" is x coordinate, "2" is y coordinate %second coordinate determines how many %x and y coordinate there are; how many %body pieces there are var food : array 1 .. 2 of int var key : string (1) var bodycount, randomdirection, speed : int var gotfood : boolean %ALGORITHMS process movehead loop if hasch then getch (key) %right if key = KEY_RIGHT_ARROW then loop bod (1, 1) += 1 bod (2, 1) := bod (2, 1) exit when hasch end loop %left elsif key = KEY_LEFT_ARROW then loop bod (1, 1) -= 1 bod (2, 1) := bod (2, 1) exit when hasch end loop %up elsif key = KEY_UP_ARROW then loop bod (1, 1) := bod (1, 1) bod (2, 1) += 1 exit when hasch end loop %down elsif key = KEY_DOWN_ARROW then loop bod (1, 1) := bod (1, 1) bod (2, 1) -= 1 exit when hasch end loop end if end if end loop end movehead process movebody loop if bodycount > 0 then for j : 2 .. bodycount for i : 1 .. 2 bod (i, j) := bod (i, j - 1) delay (50) end for end for end if end loop end movebody process foodonfield loop %putting food on field, only puts food if %snake has gotten food if (bod (1, 1) >= food (1) - 9 and food (1) + 9 >= bod (1, 1)) or (bod (2, 1) >= food (2) - 9 and food (2) + 9 >= bod (2, 1)) then gotfood := true end if if gotfood = true then randint (food (1), 15, maxx - 15) randint (food (2), 15, maxy - 15) bodycount += 1 gotfood := false end if end loop end foodonfield %MAINSTREAM %setting all beginning parameters gotfood := false bod (1, 1) := 300 bod (2, 1) := 200 speed := 50 bodycount := 0 food (1) := 400 food (2) := 100 %starting the snake off in a direction randint (randomdirection, 1, 4) if randomdirection = 1 then %if 1, then go right loop bod (1, 1) += 10 bod (2, 1) := bod (2, 1) exit when hasch end loop elsif randomdirection = 2 then %if 2, then go left loop bod (1, 1) -= 10 bod (2, 1) := bod (2, 1) exit when hasch end loop elsif randomdirection = 3 then %if 3, then go up loop bod (1, 1) := bod (1, 1) bod (2, 1) += 10 exit when hasch end loop else %if not 1,2,3, then down loop bod (1, 1) := bod (1, 1) bod (2, 1) -= 10 exit when hasch end loop end if %Preparing all coordinates fork movehead fork movebody fork foodonfield %animating head and body loop drawfilloval (bod (1, 1), bod (2, 1), 10, 10, blue) if bodycount > 0 then for j : 2 .. bodycount drawfilloval (bod (1, j), bod (2, j), 10, 10, blue) end for end if View.Update cls delay (speed) end loop one otherthing i was wondering about was that if i wanted my snake to wrap around onto the otherside.. well.. basically.. if i wanted the snake to come out from the opposite side if it were to pass through one of the walls.. the code would be something like... if bod(1,1)>=635 then bod(1,1):=0 etc etc etc with the x coordinate.. my second question is... how would i get the thing to continue moving? suggestions are much appreciated. ill update as needed |
Author: | DIIST [ Sat May 26, 2007 5:27 pm ] | ||
Post subject: | Re: Snake not appearing | ||
First you should probably consider creating a flexible array of record. It will make your life a lot easier. This will also help to add and delete elements easily. Perfect for a snake game. Look into it.
I was tracing your execution, and i found the blue line going berserk.Please refrain from using processes, they are hard to control and could result in a crash the way your using them. Just create a giant loop and use procedures. This will make our lives easier to debug. Cleaning your code might solve your problem! ![]() |
Author: | Ultrahex [ Sat May 26, 2007 5:48 pm ] | ||
Post subject: | Re: Snake not appearing | ||
also using a class might help of type snake... for example:
|
Author: | jellysausage [ Thu May 31, 2007 9:51 pm ] |
Post subject: | Re: Snake not appearing |
AHA! it works! setscreen ("graphics, nocursor,offscreenonly") %DECLARING VARIABLES var bodyx, bodyy : flexible array 1 .. 0 of int var curx, cury : int := 0 var bodyspeed : int := 10 var food : array 1 .. 2 of int var key, snakemenu : string (1) var bodycount, randomdirection, speed, t : int var direction : string var death : boolean proc right direction := "right" bodyx (1) += bodyspeed bodyy (1) := bodyy (1) end right proc left direction := "left" bodyx (1) -= bodyspeed bodyy (1) := bodyy (1) end left proc up direction := "up" bodyx (1) := bodyx (1) bodyy (1) += bodyspeed end up proc down direction := "down" bodyx (1) := bodyx (1) bodyy (1) -= bodyspeed end down proc foodonfield %putting food on field, only puts food if %snake has gotten food if bodyx (1) >= food (1) - 20 and bodyx (1) <= food (1) + 20 and bodyy (1) >= food (2) - 20 and food (2) + 20 >= bodyy (1) then randint (food (1), 50, maxx - 50) randint (food (2), 50, maxy - 50) new bodyx, bodycount + 1 new bodyy, bodycount + 1 bodycount += 1 bodyx (bodycount) := curx bodyy (bodycount) := cury end if end foodonfield %ALGORITHMS proc movehead if hasch then getch (key) %right if key = KEY_RIGHT_ARROW and direction ~= "left" then right %left elsif key = KEY_LEFT_ARROW and direction ~= "right" then left %up elsif key = KEY_UP_ARROW and direction ~= "down" then up %down elsif key = KEY_DOWN_ARROW and direction ~= "up" then down end if else if direction = "up" then up elsif direction = "down" then down elsif direction = "right" then right elsif direction = "left" then left end if end if end movehead proc movebody curx := bodyx (bodycount) cury := bodyy (bodycount) if bodycount > 1 then for decreasing j : bodycount .. 2 bodyx (j) := bodyx (j - 1) bodyy (j) := bodyy (j - 1) end for end if end movebody %player lost proc lose if bodyx (1) >= maxx - 10 or bodyx (1) <= 10 or bodyy (1) >= maxy - 10 or bodyy (1) <= 10 then death := true end if end lose %MAINSTREAM %Setting all starting parameters new bodyy, 1 new bodyx, 1 bodyx (1) := 300 bodyy (1) := 200 speed := 40 bodycount := 1 food (1) := 400 food (2) := 100 death := false %starting the snake off in a direction randint (randomdirection, 1, 4) if randomdirection = 1 then %if 1, then go right right elsif randomdirection = 2 then %if 2, then go left left elsif randomdirection = 3 then %if 3, then go up up else %if not 1,2,3, then down down end if %animating head and body setscreen ("graphics") loop clock (t) put "Body Coordinates:", bodyx (1), bodyy (1) put "speed: ", speed put "points: ", bodycount put "Time: ", t lose movebody movehead foodonfield %Drawing head drawfilloval (bodyx (1), bodyy (1), 10, 10, blue) %Drawing body if bodycount > 1 then for j : 2 .. bodycount drawfilloval (bodyx (j), bodyy (j), 10, 10, blue) end for end if %Drawing food drawfilloval (food (1), food (2), 5, 5, red) View.Update delay (speed) cls if bodyx (1) >= maxx - 10 or bodyx (1) <= 10 or bodyy (1) >= maxy - 10 or bodyy (1) <= 10 then put "GAME OVER!" drawfilloval (bodyx (1), bodyy (1), 10, 10, blue) end if exit when death = true end loop thanks for help guys! |