Computer Science Canada Help With a snake game? |
Author: | szym3n [ Fri Apr 15, 2011 10:02 pm ] | ||||
Post subject: | Help With a snake game? | ||||
Hi, I'm doing a Snake game in turing for a grade 12 computer science course, and I'm having trouble making the snake increase in size, I've been trying to use arrays, but have a lot of problems trying to make the array be in range. If someone could figure out what is wrong or what I should do to improve, it would be greatly appreciated I know I can shorten the code but I still will need help figuring out how to make the extra boxes follow the snake. This is the version without me trying to make the snake increase:
This is the version where I tried to make the snake increase in size:
|
Author: | Tony [ Fri Apr 15, 2011 11:09 pm ] |
Post subject: | Re: Help With a snake game? |
szym3n @ Fri Apr 15, 2011 10:02 pm wrote: what is wrong or what I should do to improve[?] Perhaps you should... szym3n @ Fri Apr 15, 2011 10:02 pm wrote: make the array be in range. |
Author: | szym3n [ Fri Apr 15, 2011 11:58 pm ] |
Post subject: | Re: Help With a snake game? |
Tony @ Fri Apr 15, 2011 11:09 pm wrote: szym3n @ Fri Apr 15, 2011 10:02 pm wrote: what is wrong or what I should do to improve[?] Perhaps you should... szym3n @ Fri Apr 15, 2011 10:02 pm wrote: make the array be in range. Well, that's the core of my problem, I don't know how, because I am trying to take the value of the array at a specific time, and thoguht that by taking the PREVIOUS array, I could determine the coordinates of the box, however For some strange reason, it will not let me draw the figure with the previous coordinates, I really need help, tried everything I know, and none worked ;/ What piece of code would you add/change? |
Author: | Tony [ Sat Apr 16, 2011 12:17 am ] |
Post subject: | RE:Help With a snake game? |
I would start with a fixed-size snake (lets say an array of size 10), and get that working, before worrying about growth. |
Author: | szym3n [ Sun Apr 17, 2011 3:42 pm ] |
Post subject: | RE:Help With a snake game? |
I'm still gettin the same error, anyone else able to help? I keep getting "Variable has no value" on ary(i-1) and arx(i-1) and I don't understand why it has no value, i clearly am getting ary(i) and arx(i) inside a loop, and it starts to take ary(i-1)and arx(i-1) after more than just 1 loop, so that value should exist. |
Author: | Tony [ Sun Apr 17, 2011 3:57 pm ] |
Post subject: | RE:Help With a snake game? |
so now you need to figure out why it doesn't have a value. The only way to not have an initialized value is to create a new variable and to not put anything into it. |
Author: | szym3n [ Sun Apr 17, 2011 4:09 pm ] |
Post subject: | Re: RE:Help With a snake game? |
Tony @ Sun Apr 17, 2011 3:57 pm wrote: so now you need to figure out why it doesn't have a value. The only way to not have an initialized value is to create a new variable and to not put anything into it.
I am able to make ONE box follow me without the use of arrays, however When I try to replace that code with arrays, the error continues. I have no clue why, because The growth loop only starts after I have taken a bunch of variables into the array already, and It should figure out what array(i-1) is, because IT HAS ALREADY BEEN TAKEN. im sitting here for the past like 3 days trying to figure this out, and get nothing. Man, i really need someone to either explain whats wrong or provide me with the code which I do not expect to happen, as I wanna be able to understand this. |
Author: | Tony [ Sun Apr 17, 2011 4:30 pm ] |
Post subject: | RE:Help With a snake game? |
But the compiler is telling you that this is not the case, so something is not adding up to where you expect it to be. It could be a number of things: -- (i-1) is evaluating to a different value than you think it does -- array itself is different, perhaps because of scoping rules --- or because it's been cleared since last use. use of put statements could help you trace through what variable contains what value, at what time in your program. |
Author: | szym3n [ Sun Apr 17, 2011 4:47 pm ] |
Post subject: | Re: RE:Help With a snake game? |
Tony @ Sun Apr 17, 2011 4:30 pm wrote: But the compiler is telling you that this is not the case, so something is not adding up to where you expect it to be.
It could be a number of things: -- (i-1) is evaluating to a different value than you think it does -- array itself is different, perhaps because of scoping rules --- or because it's been cleared since last use. use of put statements could help you trace through what variable contains what value, at what time in your program. WELL.... it has not been cleared since last use, so that option is gone, and I duno what else. Could you at least like give me a hint on what to change, or do you not know urself? :S |
Author: | Tony [ Sun Apr 17, 2011 4:54 pm ] |
Post subject: | RE:Help With a snake game? |
Maybe if you were to post the relevant part of the code... |
Author: | szym3n [ Sun Apr 17, 2011 4:59 pm ] |
Post subject: | Re: Help With a snake game? |
Quote: %% Snake game : Szymon Wyborny View.Set ("graphics:600,600") View.Set ("offscreenonly") colour (white) colorback (black) var tempx, tempy : int := -100 var x, y : int := 0 var x2, y2 : int := 0 var playagain : string (1) var font1 : int var scoreint : int := 00 var scorestring : string var k := 100 var i : int := 0 font1 := Font.New ("Dauphin:17") var z : string (1) var foodx, foody : int randint (foodx, 24, 576) randint (foody, 24, 576) loop if foodx mod 12 not= 0 then randint (foodx, 20, 568) end if if foody mod 12 not= 0 then randint (foody, 20, 568) end if exit when foodx mod 12 = 0 and foody mod 12 = 0 end loop drawfill (0, 0, black, black) drawfillbox (12, 12, 588, 576, white) View.Update loop var ary, arx : array 0 .. k of int k := k + 1 i := i + 1 put "Your score: ", scoreint % Font.Draw ("Your score: ", 100, 580, font1, black) % % scorestring := intstr (scoreint) % Font.Draw (scorestring, 210, 580, font1, black) if hasch then getch (z) locate (1, 1) if ord (z) = 200 and y not= -12 and y not= 12 then % put "Up Arrow Key" y := 12 x := 0 end if if ord (z) = 203 and x not= 12 and x not= -12 then % put "Left Arrow Key" x := -12 y := 0 end if if ord (z) = 205 and x not= -12 and x not= 12 then % put "Right Arrow Key" x := 12 y := 0 end if if ord (z) = 208 and y not= 12 and y not= -12 then % put "Down Arrow Key" y := -12 x := 0 end if end if arx (scoreint) := x2 ary (scoreint) := y2 drawfillbox (12, 12, 588, 576, white) drawfillbox (foodx, foody, foodx + 10, foody + 10, black) drawfillbox (foodx + 2, foody + 2, foodx + 8, foody + 8, yellow) View.Update drawfillbox (60 + x2, 60 + y2, 70 + x2, 70 + y2, black) drawfillbox (62 + x2, 62 + y2, 68 + x2, 68 + y2, green) tempx := x2 tempy := y2 View.Update x2 := x2 + x y2 := y2 + y % GROWTH for s : 1 .. scoreint drawfillbox (60 + arx (scoreint-1), 60 + ary (scoreint-1), 70 + arx (scoreint-1), 70 + ary (scoreint-1), red) end for View.Update if 60 + x2 = foodx and 60 + y2 = foody then scoreint := scoreint + 1 randint (foodx, 24, 568) randint (foody, 24, 568) loop if foodx mod 12 not= 0 then randint (foodx, 20, 568) end if if foody mod 12 not= 0 then randint (foody, 20, 568) end if exit when foodx mod 12 = 0 and foody mod 12 = 0 end loop end if if 60 + x2 > 576 or 60 + x2 < 12 or 60 + y2 >= 576 or 60 + y2 < 12 then locate (1, 23) put "GAME OVER" locate (1, 37) put "Press ANY key to play again" View.Update getch (playagain) cls scoreint := 0 x := 0 y := 0 x2 := 0 y2 := 0 drawfillbox (12, 12, 588, 576, white) View.Update end if locate (1, 1) delay (60) end loop and the problem is this: Quote: drawfillbox (60 + arx (scoreint-1), 60 + ary (scoreint-1), 70 + arx (scoreint-1), 70 + ary (scoreint-1), red) error: Variable has no value is this what you were asking for? |
Author: | szym3n [ Sun Apr 17, 2011 5:18 pm ] |
Post subject: | RE:Help With a snake game? |
I know the problem itself lies in arx(scoreint-1) and ary(scoreint-1), and that apparently there is no value assigned to it, but I dunno how to change that |
Author: | szym3n [ Sun Apr 17, 2011 5:45 pm ] |
Post subject: | Re: Help With a snake game? |
I think im at a breakthrough, I figured out how to make the tail, however I deleted the score and the food, and made the snake just a # of boxes long. I am still trying to perfect this, and If i still need help, I'll ask more |
Author: | Tony [ Sun Apr 17, 2011 5:53 pm ] | ||
Post subject: | RE:Help With a snake game? | ||
I've taken a look at your code. You assign some value to scoreint'th position
but the rest of the array is not-initialized, and doesn't have any values (just like the compiler says). |
Author: | szym3n [ Sun Apr 17, 2011 6:08 pm ] |
Post subject: | Re: Help With a snake game? |
YES! Thank god, I got it to work! Thanks for the help, I kinda had to redo a large part of this, but thats ok as long as it works! :] If someone wants to see the code, just PM me. |
Author: | hongyousan [ Fri Dec 13, 2013 9:12 am ] |
Post subject: | Re: Help With a snake game? |
Can I please see the code for your snake game? |
Author: | Raknarg [ Fri Dec 13, 2013 10:25 am ] |
Post subject: | RE:Help With a snake game? |
Should have read the date on this thread. Its been dead for some time. |