Computer Science Canada Snake Help! |
Author: | dervy [ Tue May 20, 2008 12:39 pm ] |
Post subject: | Snake Help! |
I am making a snake game and i cant seem to get the tail to follow it properly. If you see the code and try it you'll see what i mean. Any suggestions? var x, y : array 1 .. 999 of int var lastx, lasty:array 1 .. 999 of int var dx, dy : int var direction : int var count : int := 1 var ch : string (1) var folx, foly : int x (1) := 100 y (1) := 100 View.Set ("offscreenonly") var chars : array char of boolean color (red) put "Welcome to Mr. Line! Press a key." getch (ch) direction := 1 loop randint (dx, 0, maxx) randint (dy, 0, maxy) loop drawfilloval (x (1), y (1), 4, 4, blue) if count > 1 then for i : 2 .. count x (i) := x (i - 1) + folx y (i) := y (i - 1) + foly drawfilloval (x (i), y (i), 4, 4, blue) end for end if drawfilloval (dx, dy, 4, 4, red) exit when View.WhatDotColour (x (1), y (1) - 4) = red or View.WhatDotColour (x (1), y (1) + 4) = red or View.WhatDotColour (x (1) + 4, y (1)) = red or View.WhatDotColour (x (1) - 4, y (1)) = red View.Update Input.KeyDown (chars) if chars (KEY_UP_ARROW) and direction not= 4 then direction := 2 %y (1)+= 4 end if if chars (KEY_LEFT_ARROW) and direction not= 3 then %x (1):= x(1) - 4 direction := 1 end if if chars (KEY_RIGHT_ARROW) and direction not= 1 then %x (1):= x (1)+ 4 direction := 3 end if if chars (KEY_DOWN_ARROW) and direction not= 2 then %y (1) := y (1)- 4 direction := 4 end if if direction = 1 and x (1) <= maxx then x (1) := x (1) - 4 folx := -8 foly := 0 elsif direction = 2 and y (1) <= maxy then y (1) += 4 foly := -8 folx := 0 elsif direction = 3 and x (1) >= 0 then x (1) := x (1) + 4 folx := 8 foly := 0 elsif direction = 4 and y (1) >= 0 then y (1) := y (1) - 4 foly:=8 folx:=0 else Music.Sound (1000, 50) Music.SoundOff x (1) := maxx div 2 y (1) := maxy div 2 count := 1 end if delay (25) cls end loop count += 1 end loop |
Author: | minime [ Tue Apr 28, 2009 7:58 pm ] |
Post subject: | RE:Snake Help! |
well i was bored and tried making it as well, and i got most of it, all except when the tail hits itself it ends the game, so just see what you can get from my code. var balls, space : int space := 10 balls := 1 * space var sx : flexible array 1 .. balls of int var sy : flexible array 1 .. balls of int var sr : int var x,y,r : int var speedy : array 1 .. balls of int var speedx : array 1 .. balls of int var keys : array char of boolean var score : int View.Set ("graphics:600;600,offscreenonly") sx (1) := 300 sy (1) := 300 sr := 10 r := 7 speedy (1) := 0 speedx (1) := 0 score := 0 randint (x, 0 + r, maxx - r) randint (y, 0 + r, maxy - r) for i : 1 .. balls - 1 speedx (i + 1) := speedx (i) speedy (i + 1) := speedy (i) end for for i : 2 .. balls sx (i) := sx (i - 1) - speedx (i - 1) sy (i) := sy (i - 1) - speedy (i - 1) end for loop cls put score if sx (1) + sr >= x - r and sx (1) - sr <= x + r and sy (1) + sr >= y - r and sy (1) - sr <= y + r then randint (x, 0 + r, maxx - r) randint (y, 0 + r, maxy - r) score := score + 1 for i : 1 .. 20 balls := balls + 1 new sx, balls new sy, balls sx (balls) := 0 sy (balls) := 0 end for end if for i : 1 .. balls Draw.FillOval (sx (i), sy (i), sr, sr, black) end for Draw.FillOval (x, y, r, r, red) Input.KeyDown (keys) if keys (KEY_DOWN_ARROW) then speedy (1) := -2 speedx (1) := 0 elsif keys (KEY_UP_ARROW) then speedy (1) := 2 speedx (1) := 0 elsif keys (KEY_LEFT_ARROW) then speedx (1) := -2 speedy (1) := 0 elsif keys (KEY_RIGHT_ARROW) then speedx (1) := 2 speedy (1) := 0 end if exit when sx (1) <= 0 + r or sx (1) >= maxx - r or sy (1) <= 0 + r or sy (1) >= maxy - r sy (1) := sy (1) + speedy (1) sx (1) := sx (1) + speedx (1) for decreasing i : balls .. 2 sy (i) := sy (i - 1) sx (i) := sx (i - 1) end for View.Update () end loop cls put "You got ", score, " points!" |