Having a problem with making my snake grow in the game of snake
Author |
Message |
legosz
|
Posted: Tue Jun 16, 2015 8:53 am Post subject: Having a problem with making my snake grow in the game of snake |
|
|
What is it you are trying to achieve?
I am trying to make my snake grow
What is the problem you are having?
The snake will not grow
Describe what you have tried to solve this problem
I have tried many things,i added a body to the snake head made is pawn everytime i got the food, but it woud not follow the snake
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
import GUI
var winID : int
winID := Window.Open ("graphics:600;645")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%variables
var menubutton, snakebutton, helpbutton, quitbutton, restartbutton : int
var LeftMainScreen : boolean := false
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Forwarding procedures
forward proc MENU
forward proc SNAKE
forward proc HELP
forward proc QUIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Play game
body proc SNAKE
cls
GUI.Dispose (snakebutton )
GUI.Dispose (helpbutton )
GUI.Dispose (quitbutton )
%Making grid
Draw.FillBox (0, 0, 600, 600, black)
var grid : array 0 .. 59, 0 .. 59 of int
var width : int := 10
var score : int := 0
var arrows : array char of boolean
var direction : int := 0
var playerx, playery : int := 2
var foodx, foody : int := Rand.Int (3, 56)
var playagain : string (1)
var counter : int := 55
%grid
for x : 0 .. 59
for y : 0 .. 59
grid (x, y ) := 0
end for
end for
for x : 0 .. 59
grid (x, 0) := 1
grid (x, 59) := 1
end for
for y : 0 .. 59
grid (0, y ) := 1
grid (59, y ) := 1
end for
%actualk grid
loop
for x : 0 .. 59
for y : 0 .. 59
if grid (x, y ) = 1 then
Draw.FillBox (x * width, y * width, x * width + width, y * width + width, yellow)
end if
end for
end for
%score
locate (1, 1)
put "Score: ", score
%instructions
locate (2, 1)
put "Use 'w', 'a', 's', 'd', to move!"
%directions
Input.KeyDown (arrows )
if arrows ('a') and direction not= 2 then
direction := 1
elsif arrows ('d') and direction not= 1 then
direction := 2
elsif arrows ('s') and direction not= 4 then
direction := 3
elsif arrows ('w') and direction not= 3 then
direction := 4
end if
%movement
if direction = 1 and grid (playerx - 1, playery ) = 0 then
playerx - = 1
elsif direction = 2 and grid (playerx + 1, playery ) = 0 then
playerx + = 1
elsif direction = 3 and grid (playerx, playery - 1) = 0 then
playery - = 1
elsif direction = 4 and grid (playerx, playery + 1) = 0 then
playery + = 1
end if
%hitting walls
if playerx = 1 then
locate (14, 35)
put "GAME OVER"
locate (15, 30)
put "YOU GOT A SCORE OF ", score
locate (16, 30)
put "press 'w' to restart"
View.Update
getch (playagain )
score := 0
playerx := 5
playery := 5
counter := 55
elsif
playery = 1 then
locate (14, 35)
put "GAME OVER"
locate (15, 30)
put "YOU GOT A SCORE OF ", score
locate (16, 30)
put "press 'd' to restart"
View.Update
getch (playagain )
score := 0
playerx := 5
playery := 5
counter := 55
elsif
playerx = 58 then
locate (14, 35)
put "GAME OVER"
locate (15, 30)
put "YOU GOT A SCORE OF ", score
locate (16, 30)
put "press 'w' to restart"
View.Update
getch (playagain )
score := 0
playerx := 5
playery := 5
counter := 55
elsif
playery = 58 then
locate (14, 35)
put "GAME OVER"
locate (15, 30)
put "YOU GOT A SCORE OF ", score
locate (16, 30)
put "press 'w' to restart"
View.Update
getch (playagain )
score := 0
playerx := 5
playery := 5
counter := 55
end if
%food
Draw.FillBox (foodx * width, foody * width, foodx * width + width,
foody * width + width, 5)
%snake
Draw.FillBox (playerx * width, playery * width, playerx * width + width,
playery * width + width, black)
%score
if playerx = foodx and playery = foody then
score + = 1
counter := counter - 1
foodx := Rand.Int (3, 56)
foody := Rand.Int (3, 56)
end if
View.Update
delay (counter )
cls
end loop
end SNAKE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Help menu
body proc HELP
LeftMainScreen := true
cls
GUI.Dispose (snakebutton )
GUI.Dispose (quitbutton )
GUI.Dispose (helpbutton )
var any : int := Rand.Int (8, 15)
Draw.FillBox (0, 0, 800, 645, any )
var writing : int := Font.New ("arial:12")
Draw.Text ("Welcome to the help menu, this will teach you about the game", 0, 600, writing, any - 1)
Draw.Text ("Use 'W', 'A', 'S', 'D' to move the snake around", 0, 550, writing, any - 2)
Draw.Text ("You move the snake around to collect food within the grid to grow", 0, 500, writing, any - 3)
Draw.Text ("To lose the game you must run into the walls, or yourself", 0, 450, writing, any - 4)
Draw.Text ("To win the game you must eat enough food to have nowhere to move", 0, 400, writing, any - 5)
Draw.Text ("It is a simple game to play, hope you enjoy, and have fun!", 0, 350, writing, any - 6)
quitbutton := GUI.CreateButton (175, 25, 0, "Quit", QUIT )
menubutton := GUI.CreateButton (235, 25, 0, "Menu", MENU )
GUI.Refresh
end HELP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Quit menu
body proc QUIT
cls
var any : int := Rand.Int (8, 15)
Draw.FillBox (0, 0, 800, 645, any )
var writing : int := Font.New ("arial:24")
Draw.Text ("Thanks for playing!", 50, 200, writing, any - 1)
delay (1000)
GUI.Quit
Window.Close (winID )
end QUIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%main menu
body proc MENU
cls
var any : int := Rand.Int (8, 15)
Draw.FillBox (0, 0, 600, 645, any )
var snakename : int := Font.New ("Arial:74:bold")
Font.Draw ("SNAKE", 150, 300, snakename, any - 1)
snakebutton := GUI.CreateButton (25, 25, 0, "Play game", SNAKE )
helpbutton := GUI.CreateButton (115, 25, 0, "Help", HELP )
quitbutton := GUI.CreateButton (175, 25, 0, "Quit", QUIT )
if LeftMainScreen = true then
GUI.Dispose (menubutton )
Draw.FillBox (235, 25, 295, 48, any )
end if
GUI.Refresh
end MENU
MENU
loop
exit when GUI.ProcessEvent
end loop
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Sat Jun 27, 2015 4:05 am Post subject: RE:Having a problem with making my snake grow in the game of snake |
|
|
Use a flexible array for your snakes body. http://compsci.ca/holtsoft/doc/flexible.html
as he eats the berries, increase his size by 1.
set those x,y values after the head moves to the old position.
handle the movements and /win |
|
|
|
|
|
|
|