Computer Science Canada

Turing centipede game(having trouble increasing length)

Author:  Heartseed [ Sat Jun 14, 2014 8:49 am ]
Post subject:  Turing centipede game(having trouble increasing length)

I have been making a centipede or snake game on turing.
The issue is when I try to increase the default length of the snake it would either say variable has no value or array subscript out of range.
Ive been browsing the web to see if anyone has encountered the same problem as me and I have been trying different variations of my code, so far I am still unable to grow the snake faster.



[syntax="turing"]

% Snake
% Version 6

setscreen ("graphics: 800;600")
View.Set ("offscreenonly")


%prepare keyboard input
var chars : array char of boolean


%checks if a fruit is currently present
var spawnedfruit : boolean := true

%true or false for constant snake motion
var leftflag : boolean := false
var rightflag : boolean := false
var upflag : boolean := false
var downflag : boolean := false
%fruit coordinate
var fruitX : int
var fruitY : int
%fruit random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)

%links snake growthX/Y
var g : int := 1
%size of circles
var s : int := 8
%snake movement speed
var spd : int := 10
%fruit color
var col : int := 42
%score
var score : int := 0
%snake growth distancing
var gain : int := 2
%snake length
var len : int := 1
%snake size and body
var growthX : flexible array 1 .. len of int
var growthY : flexible array 1 .. len of int
%checks if player has lost
var death : boolean := false
%map color
var mapcol : int := 0
%map
var map : int := 0
%snake/player/centipede color
var playercol : int := 0

%Startscreen
%backround
drawfillbox (maxx, maxy, 0, 0, black)

%title
%put "Snake/Centipede"(white)
%put "By Kevin"(white)
var mouseX : int := 0
var mouseY : int := 0
var mouseB : int := 0
var new_upper : int := upper (growthX)
var last_upper : int := 0
%sets up array
for b : 1 .. upper (growthX)
end for
% loop
% %backround
% %drawfillbox(maxx,maxy,0,0,black)
% %startmenu
% %startbutton
% drawfillbox(maxx div 2 - 200,maxy div 2 - 100,maxx div 2 + 150,maxy div 2 + 200,white)
% %options button
% drawfillbox(maxx div 2 - 50,maxy div 2 - 100,maxx div 2 + 50,maxy div 2 - 200,white)
% %exit button
% drawfillbox(maxx div 2 - 50,maxy div 2 - 100,maxx div 2 + 50,maxy div 2 - 200,white)
% %selector
% %if mouseX
% end loop
Input.KeyDown (chars)
mousewhere (mouseX, mouseY, mouseB)

%if
%selects start button
%elsif mb = 1 and mx > maxx div 2 - 50 and mx > maxx div 2 + 50 and my > maxy div 2 + 50 and my < maxy div 2 + 25
%then

%location of snake
var posx : int := maxx div 2
var posy : int := maxy div 2






%for moving the snake
loop
%backround
drawfillbox (maxx, maxy, 0, 0, black)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%score
put "Score ", score
%get keyboard input
Input.KeyDown (chars)


if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
%tail growth

%kills ends game if player makes contact with itself
if whatdotcolor (posx - s - 1, posy) <= playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%how to clear fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
%grows snake
new growthX, upper (growthX, 1) + 1
new growthY, upper (growthY, 1) + 1


end if

end if
if rightflag = true then
posx := posx + 2
if whatdotcolor (posx + s + 1, posy) <= playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
new growthX, upper (growthX, 1) + 1
new growthY, upper (growthY, 1) + 1
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolor (posx, posy + s + 1) <= playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
new growthX, upper (growthX, 1) + 1
new growthY, upper (growthY, 1) + 1
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolor (posx, posy - s - 1) <= playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
new growthX, upper (growthX, 1) + 1
new growthY, upper (growthY, 1) + 1
end if
end if







%snake head
drawfilloval (posx, posy, s, s, playercol)



%moves the array
for decreasing b : upper (growthX) .. gain
growthX (b) := growthX (b - g)
growthY (b) := growthY (b - g)
drawfilloval (growthX (b), growthY (b), s, s, playercol)
end for
growthX (g) := posx
growthY (g) := posy
drawfilloval (growthX (g), growthY (g), s, s, playercol)
View.Update
delay (spd)
cls
%checks if player has lost
if death = true then
Text.Color (playercol)
Text.ColorBack (black)
put "You have lost with a score of ", score, " -GAME OVER"


end if


end loop



Turing 4.1.1

Author:  Insectoid [ Sat Jun 14, 2014 10:08 am ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

What's the maximum length of the centipede? Why not make an array that long, and just keep track of how long it is. If the max length is 100 and the start length is 3, make the array size 100 right off the bat and just use the first 3 elements.

Author:  Dragon20942 [ Sat Jun 14, 2014 10:26 am ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

I haven't seen array subscript is out of range yet, however, I have seen variable has no value. The problem when you try to increase the default length happens because nothing from growthX is ever declared at the beginning, and then the code tries to manipulate the values of those undeclared segments later in the code.

You avoided this issue when len is 1 by using the variable "gain" as the lower bound of the decreasing for loop. Since gain is 2, that loop won't actually do anything until upper(growthX) is 2 or higher. This is why your game works fine when it hits that loop while growthX(1) is undeclared. You can delay declaring growthX(1). When len is 2 or higher, the for loop activates, and you get an error message because nothing in the array is declared. If you declare growthX(1), that's fine for len = 2. But if len = 3, you must declare both growthX(1) and growthX(2).

Author:  Heartseed [ Sat Jun 14, 2014 12:07 pm ]
Post subject:  Re: Turing centipede game(having trouble increasing length)

Thank you for answering, ive figured it out now.
Do you have any suggestions on how to make the snake eat its own tail ?

Author:  Heartseed [ Sat Jun 14, 2014 3:17 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

My code works now mostly, ive tried using whatdotcolor to detect if the snake made contact with its tail, so far the whatdotcolor of my x y coordinate is being displayed as black, not the snake color. Im not very familiar with whatdotcolor so suggestions would help me progress pass the fundamentals of my game.

% Snake
% Version 6

setscreen ("graphics: 800;600")
View.Set ("offscreenonly")


%prepare keyboard input
var chars : array char of boolean

%music
%defines subprocess
process PlayMusic
%the command that plays the file
Music.PlayFile ("Nujabes - Aruarian Dance.mp3")
end PlayMusic

%checks if a fruit is currently present
var spawnedfruit : boolean := true

%true or false for constant snake motion
var leftflag : boolean := false
var rightflag : boolean := false
var upflag : boolean := false
var downflag : boolean := false
%fruit coordinate
var fruitX : int
var fruitY : int
%fruit random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)

%links snake growthX/Y
var h : int := 1
%initial snake length
var len : int := 50
%size of circles
var s : int := 8
%snake movement speed
var spd : int := 10
%fruit color
var col : int := 42
%score
var score : int := 0
%snake growth distancing
var gain : int := 15
%snakes body and initial length
var growthX : flexible array 1 .. len of int
var growthY : flexible array 1 .. len of int
%grows snake in length
var grow : int := upper (growthX)
%variable for zero(since a normal zero would be constant and unchanging)
var zero : int := 0
%initializes array
for i : 1 .. upper (growthX)
growthX (i) := 0
growthY (i) := 0
end for
%checks if player has lost
var death : boolean := false
%map color
var mapcol : int := 0
%map
var map : int := 0
%snake/player/centipede color
var playercol : int := 7
%game fonts
var startscreenfont : int
startscreenfont := Font.New ("Arial:36")
var font : int
font := Font.New ("Arial:16")
var smalllettering : int
smalllettering := Font.New("Arial:14")
%options menu variables
var options : int
var winID : int
loop
%Startscreen
drawfillbox(0, 0, maxx, maxy, black)
Font.Draw("Snake Game by Kevin", 75, 400, startscreenfont, white)
Font.Draw("-The snake grows by eating fruit which also increases your score. ",100,250,font,white)
Font.Draw("-If you attempt to leave the screen or bite your tail the game will end.",100,200,font,white)
Font.Draw("- GOOD LUCK AND HAVE FUN !!",100,150,font,white)
Font.Draw("1 will start game in default settings, 2 will bring you to the option screen, 3 will exit the game.",0,8,smalllettering,white)
View.Update
%keyboard input
Input.KeyDown(chars)
if chars('1') then
exit
end if
if
chars('2') then
cls
View.Update
elsif chars('3') then
Window.Close(winID)
end if
end loop
%location of snake
var posx : int := maxx div 2
var posy : int := maxy div 2






%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, 0)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%score
put "Score ", score
put"coordinates ",posx," ",posy," ", whatdotcolour (posx, posy)
%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2


%kills ends game if player makes contact with itself
if whatdotcolour (posx, posy) = playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%how to clear fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
%grows snake(the zero keeps track of current size as the grow grows the snake)
zero := grow
%gain is how much the snake will grow by
grow := grow + gain
new growthX, grow
new growthY, grow

for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if


end if


if rightflag = true then
posx := posx + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)

%snake body
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)
cls
%checks if player has lost
if death = true then
Text.Color (playercol)
Text.ColorBack (black)
put "You have lost with a score of ", score, " -GAME OVER"


end if


end loop

Author:  Srlancelot39 [ Sat Jun 14, 2014 9:12 pm ]
Post subject:  Re: Turing centipede game(having trouble increasing length)

An alternative to whatdotcolour for the collision detection is to check if the new head position is already occupied by the snake. You would need to make your playing area a two dimensional array in order to use this method.

Author:  Heartseed [ Sat Jun 14, 2014 10:38 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Thanks for the advice Srlancelot39, ill try it tomorrow, btw what does the lancelot in your username come from ?

Author:  Heartseed [ Sun Jun 15, 2014 8:54 am ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Could you give an example of how to use the 2 dimensional array.

Im not too good with turing so im not sure what to put there.


%tracks the tail position, this helps indicate if youve made contact with the head
var taillock : flexible array ? .. ?, ?..? of int

Author:  Heartseed [ Sun Jun 15, 2014 12:41 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Can someone explain the two dimensional array ?
Im still having trouble with getting the snake to collide with itself, also does anyone know why whatdotcolor doesnt work(could it be because the turing version im using ?).

% Snake
% Version 6
%Im only commenting the left line of code, all the others are just repeats for different directions.


%prepare turing for graphics
setscreen ("graphics: 800;600")
View.Set ("offscreenonly")


%prepare keyboard input
var chars : array char of boolean

%music
%defines subprocess
process PlayMusic
%the command that plays the file
Music.PlayFile ("Nujabes - Aruarian Dance.mp3")
end PlayMusic

%checks if a fruit is currently present
var spawnedfruit : boolean := true

%true or false for constant snake motion
var leftflag : boolean := false
var rightflag : boolean := false
var upflag : boolean := false
var downflag : boolean := false
%fruit coordinate
var fruitX : int
var fruitY : int
%fruit random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)

%links snake growthX/Y
var h : int := 1
%initial snake length
var len : int := 50
%size of circles
var s : int := 8
%snake movement speed
var spd : int := 10
%fruit color
var col : int := 42
%score
var score : int := 0
%how much the score increases by will vary
var point : int := 1
%snake growth distancing
var gain : int := 15
%snakes body and initial length
var growthX : flexible array 1 .. len of int
var growthY : flexible array 1 .. len of int
%grows snake in length
var grow : int := upper (growthX)
%variable for zero(since a normal zero would be constant and unchanging)
var zero : int := 0
%initializes array
for i : 1 .. upper (growthX)
growthX (i) := 0
growthY (i) := 0
end for
%tracks the tail position, this helps indicate if youve made contact with the head
var taillock : flexible array 1 .. len of int
%checks if player has lost
var death : boolean := false
%map color
var mapcol : int := 0
%map
var map : int := 0
%snake/player/centipede color
var playercol : int := 7
%game fonts
var startscreenfont : int
startscreenfont := Font.New ("Arial:36")
var font : int
font := Font.New ("Arial:16")
var smalllettering : int
smalllettering := Font.New ("Arial:14")
%options menu variables
var options : int
var winID : int
loop
%Startscreen
drawfillbox (0, 0, maxx, maxy, black)
Font.Draw ("Snake Game by Kevin", 75, 400, startscreenfont, white)
Font.Draw ("-The snake grows by eating fruit which also increases your score. ", 100, 250, font, white)
Font.Draw ("-If you attempt to leave the screen or bite your tail the game will end.", 100, 200, font, white)
Font.Draw ("- GOOD LUCK AND HAVE FUN !!", 100, 150, font, white)
Font.Draw ("GAMEMODES: 1 default settings, 2 mapped, 3 secret settings.", 0, 8, smalllettering, white)
View.Update
%keyboard input
Input.KeyDown (chars)
if chars ('1') then
exit
end if
if
chars ('2') then
cls
View.Update
elsif chars ('3') then
Window.Close (winID)
end if
end loop
%location of snake
var posx : int := maxx div 2
var posy : int := maxy div 2






%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, 0)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%score
put "Score ", score
put "coordinates ", posx, " ", posy, " ", whatdotcolour (posx, posy)
%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
end if

%ends game if player makes contact with itself
if whatdotcolour (posx, posy) = playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%when snake makes contact with fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location for fruit
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
%adds points to previous score
score := score + point
%draws fruit
drawfilloval (fruitX, fruitY, s, s, col)
%zero keeps track of current size as the grow grows the snake, this is so the snake will grow the conssistently for each fruit
zero := grow
%gain is how much the snake will grow by, this just grows the snake
grow := grow + gain
%the new grow value increases thus adding growing the snake from its previous size in the flexible array
new growthX, grow
new growthY, grow
%the new array values become 0, this is so the snake can continue to grow within the flexible array
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if





if rightflag = true then
posx := posx + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)

%snake body, this moves the snake
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)
cls
%checks if player has lost
if death = true then
Text.Color (playercol)
Text.ColorBack (black)
put "You have lost with a score of ", score, " -GAME OVER"


end if


end loop

Author:  wtd [ Sun Jun 15, 2014 12:43 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Please for the love of everything unholy... use code tags.

Author:  Heartseed [ Sun Jun 15, 2014 1:00 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Code tags ?

Author:  Heartseed [ Sun Jun 15, 2014 2:34 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

I still havnt figured out how to make the snake collide with itself, but ive also noticed that my end screen keeps flashing, how can I make it less volatile ?

% Snake
% Version 6
%Im only commenting the left line of code, all the others are just repeats for different directions.


%prepare turing for graphics
setscreen ("graphics: 800;600")
View.Set ("offscreenonly")


%prepare keyboard input
var chars : array char of boolean

%music
%defines subprocess
process PlayMusic
%the command that plays the file
Music.PlayFile ("Nujabes - Aruarian Dance.mp3")
end PlayMusic

%checks if a fruit is currently present
var spawnedfruit : boolean := true

%true or false for constant snake motion
var leftflag : boolean := false
var rightflag : boolean := false
var upflag : boolean := false
var downflag : boolean := false
%fruit coordinate
var fruitX : int
var fruitY : int
%fruit random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)

%links snake growthX/Y
var h : int := 1
%initial snake length
var len : int := 1
%size of circles
var s : int := 8
%snake movement speed
var spd : int := 10
%fruit color
var col : int := 42
%point related stuff
%score
var score : int := 0
%how much the score increases by will vary
var point : int := 1
%the 3 highscores, if player score passes these they will replace them in the end screen
var highscore : array 1 .. 3 of int := init (9001, 80, 6)
%displays score in Font.Draw, without this there will be an synatax error
% var scoredisplay : array 1..20 of string
% scoredisplay(1) := "Your score is " + score + " congratulations, and good luck nextime"

%snake growth distancing
var gain : int := 10
%snakes body and initial length
var growthX : flexible array 1 .. len of int
var growthY : flexible array 1 .. len of int
%grows snake in length
var grow : int := upper (growthX)
%variable for zero(since a normal zero would be constant and unchanging)
var zero : int := 0
%initializes array
for i : 1 .. upper (growthX)
growthX (i) := 0
growthY (i) := 0
end for
%tracks the tail position, this helps indicate if youve made contact with the head
var taillock : flexible array 1 .. len of int
%checks if player has lost
var death : boolean := false
%map color
var mapcol : int := 0
%map
var map : int := 0
%snake/player/centipede color
var playercol : int := 0
%game fonts
var startscreenfont : int
startscreenfont := Font.New ("Arial:36")
var font : int
font := Font.New ("Arial:16")
var smalllettering : int
smalllettering := Font.New ("Arial:14")
%options menu variables
var GAMEMODE : int := 0
loop
%Startscreen
drawfillbox (0, 0, maxx, maxy, black)
Font.Draw ("Snake Game by Kevin", 75, 400, startscreenfont, white)
Font.Draw ("-The snake grows by eating fruit which also increases your score. ", 100, 250, font, white)
Font.Draw ("-If you attempt to leave the screen or bite your tail the game will end.", 100, 200, font, white)
Font.Draw ("- GOOD LUCK AND HAVE FUN !!", 100, 150, font, white)
Font.Draw ("GAMEMODES: 1 default settings, 2 mapped, 3 secret settings.", 0, 8, smalllettering, white)
View.Update
%keyboard input
Input.KeyDown (chars)
if chars ('1') then
GAMEMODE := 1
exit

elsif
chars ('2') then
GAMEMODE := 2
exit
elsif chars ('3') then
GAMEMODE := 3
exit
end if
end loop
%location of snake
var posx : int := maxx div 2
var posy : int := maxy div 2




%gamemodes
if GAMEMODE = 1
then
%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, black)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%information hud
%current score
Font.Draw("Current score:", 0, maxy - 25, font, white)
Font.Draw(intstr(score), 150, maxy - 25, font, white)
%coordinates
Font.Draw("X:",200,maxy - 25, font, white)
Font.Draw(intstr(posx),250,maxy - 25, font, white)

View.Update
% put "coordinates ", posx, " ", posy, " ", whatdotcolour (posx, posy)
%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
end if

%ends game if player makes contact with itself
if whatdotcolour (posx, posy) = playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%when snake makes contact with fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location for fruit
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
%adds points to previous score
score := score + point
%draws fruit
drawfilloval (fruitX, fruitY, s, s, col)
%zero keeps track of current size as the grow grows the snake, this is so the snake will grow the conssistently for each fruit
zero := grow
%gain is how much the snake will grow by, this just grows the snake
grow := grow + gain
%the new grow value increases thus adding growing the snake from its previous size in the flexible array
new growthX, grow
new growthY, grow
%the new array values become 0, this is so the snake can continue to grow within the flexible array
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if





if rightflag = true then
posx := posx + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)
%snake body, this moves the snake
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)

%checks if player has lost
if death = true then

Text.Color (playercol)
%displays the score
Font.Draw ("Your Score Was:", 0, 25, startscreenfont, white)
Font.Draw (intstr(score), maxx div 2, 0, startscreenfont, white)
View.Update
%changes high scores
if score > highscore (1) then %changes high scores
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := highscore (2)
highscore (2) := highscore (1)
highscore (1) := score
elsif score > highscore (2) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := highscore (2)
highscore (2) := score
elsif score > highscore (3) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := score
end if
%displays highscores
Font.Draw ("The top 3 scores are:", 8, 500, startscreenfont, white)
Font.Draw (intstr (highscore (1)), 500, 500, startscreenfont, white)
Font.Draw (intstr (highscore (2)), 500, 450, startscreenfont, white)
Font.Draw (intstr (highscore (3)), 500, 400, startscreenfont, white)

View.Update

end if


end loop
%checks if second gamemode was selected
elsif GAMEMODE = 2 then
%gameplay adjustments
playercol := 7
spd := 8
len := 80
gain := 16

%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, white)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%score

%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
end if

%ends game if player makes contact with itself
if whatdotcolour (posx, posy) = playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%when snake makes contact with fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location for fruit
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
%adds points to previous score
score := score + point
%draws fruit
drawfilloval (fruitX, fruitY, s, s, col)
%zero keeps track of current size as the grow grows the snake, this is so the snake will grow the conssistently for each fruit
zero := grow
%gain is how much the snake will grow by, this just grows the snake
grow := grow + gain
%the new grow value increases thus adding growing the snake from its previous size in the flexible array
new growthX, grow
new growthY, grow
%the new array values become 0, this is so the snake can continue to grow within the flexible array
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if





if rightflag = true then
posx := posx + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)

%snake body, this moves the snake
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)
cls
%checks if player has lost
if death = true then
Text.Color (playercol)
Text.ColorBack (black)



end if


end loop


end if

Author:  Srlancelot39 [ Sun Jun 15, 2014 2:43 pm ]
Post subject:  Re: Turing centipede game(having trouble increasing length)

Use either
code:

Code here


or

Turing:

Turing code here


As for the 2 dimensional array, the dimensions would be whatever resolution you want your playing area to be. For example, if you want there to be 30 positions horizontally and 50 vertically, your playing area would be like playing_area [30][50]. You would use a separate array for the snake itself since it has the potential to be longer than the width or height of the playing area.

Author:  Heartseed [ Sun Jun 15, 2014 2:48 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Okay thankyou, so I have to do a grid ? - can you give me an example of a program that uses this ?

Author:  Srlancelot39 [ Sun Jun 15, 2014 2:54 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Yes, precisely.
Examples:
Spreadsheets
Tetris
Chess/Checkers
...

*Your grid does not need to be visible btw. You could if you wanted to, but it'd be more work for you and would make the game less challenging.

Author:  Heartseed [ Sun Jun 15, 2014 3:10 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Okay thankyou, ill try to make it then, btw do you know how I could make my end game screen less flashy and volatile ?

Code here

% Snake
% Version 6
%Im only commenting the left line of code, all the others are just repeats for different directions.


%prepare turing for graphics
setscreen ("graphics: 800;600")
View.Set ("offscreenonly")


%prepare keyboard input
var chars : array char of boolean

%music
%defines subprocess
process PlayMusic
%the command that plays the file
Music.PlayFile ("Nujabes - Aruarian Dance.mp3")
end PlayMusic

%checks if a fruit is currently present
var spawnedfruit : boolean := true

%true or false for constant snake motion
var leftflag : boolean := false
var rightflag : boolean := false
var upflag : boolean := false
var downflag : boolean := false
%fruit coordinate
var fruitX : int
var fruitY : int
%fruit random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)

%links snake growthX/Y
var h : int := 1
%initial snake length
var len : int := 1
%size of circles
var s : int := 8
%snake movement speed
var spd : int := 10
%fruit color
var col : int := 42
%point related stuff
%score
var score : int := 0
%how much the score increases by will vary
var point : int := 1
%the 3 highscores, if player score passes these they will replace them in the end screen
var highscore : array 1 .. 3 of int := init (9001, 80, 6)
%displays score in Font.Draw, without this there will be an synatax error
% var scoredisplay : array 1..20 of string
% scoredisplay(1) := "Your score is " + score + " congratulations, and good luck nextime"

%snake growth distancing
var gain : int := 10
%snakes body and initial length
var growthX : flexible array 1 .. len of int
var growthY : flexible array 1 .. len of int
%grows snake in length
var grow : int := upper (growthX)
%variable for zero(since a normal zero would be constant and unchanging)
var zero : int := 0
%initializes array
for i : 1 .. upper (growthX)
growthX (i) := 0
growthY (i) := 0
end for
%tracks the tail position, this helps indicate if youve made contact with the head
var taillock : flexible array 1 .. len of int
%checks if player has lost
var death : boolean := false
%map color
var mapcol : int := 0
%map
var map : int := 0
%snake/player/centipede color
var playercol : int := 0
%game fonts
var startscreenfont : int
startscreenfont := Font.New ("Arial:36")
var font : int
font := Font.New ("Arial:16")
var smalllettering : int
smalllettering := Font.New ("Arial:14")
%options menu variables
var GAMEMODE : int := 0
loop
%Startscreen
drawfillbox (0, 0, maxx, maxy, black)
Font.Draw ("Snake Game by Kevin", 75, 400, startscreenfont, white)
Font.Draw ("-The snake grows by eating fruit which also increases your score. ", 100, 250, font, white)
Font.Draw ("-If you attempt to leave the screen or bite your tail the game will end.", 100, 200, font, white)
Font.Draw ("- GOOD LUCK AND HAVE FUN !!", 100, 150, font, white)
Font.Draw ("GAMEMODES: 1 default settings, 2 mapped, 3 secret settings.", 0, 8, smalllettering, white)
View.Update
%keyboard input
Input.KeyDown (chars)
if chars ('1') then
GAMEMODE := 1
exit

elsif
chars ('2') then
GAMEMODE := 2
exit
elsif chars ('3') then
GAMEMODE := 3
exit
end if
end loop
%location of snake
var posx : int := maxx div 2
var posy : int := maxy div 2




%gamemodes
if GAMEMODE = 1
then
%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, black)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%information hud
%current score
Font.Draw("Current score:", 0, maxy - 25, font, white)
Font.Draw(intstr(score), 150, maxy - 25, font, white)
%coordinates
Font.Draw("X:",175,maxy - 25, font, white)
Font.Draw(intstr(posx),200, maxy - 25, font, white)
Font.Draw("Y:",250,maxy - 25, font, white)
Font.Draw(intstr(posy), 275, maxy - 25, font, white)
Font.Draw("Fruit:", 325, maxy - 25, font, white)
Font.Draw(intstr(fruitX), 375, maxy - 25, font, white)
Font.Draw("/", 415, maxy - 25, font, white)
Font.Draw(intstr(fruitY), 420, maxy - 25, font, white)
View.Update
% put "coordinates ", posx, " ", posy, " ", whatdotcolour (posx, posy)
%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
end if

%ends game if player makes contact with itself
if whatdotcolour (posx, posy) = playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%when snake makes contact with fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location for fruit
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
%adds points to previous score
score := score + point
%draws fruit
drawfilloval (fruitX, fruitY, s, s, col)
%zero keeps track of current size as the grow grows the snake, this is so the snake will grow the conssistently for each fruit
zero := grow
%gain is how much the snake will grow by, this just grows the snake
grow := grow + gain
%the new grow value increases thus adding growing the snake from its previous size in the flexible array
new growthX, grow
new growthY, grow
%the new array values become 0, this is so the snake can continue to grow within the flexible array
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if





if rightflag = true then
posx := posx + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)
%snake body, this moves the snake
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)

%checks if player has lost
if death = true then

Text.Color (playercol)
%displays the score
Font.Draw ("Your Score Was:", 0, 25, startscreenfont, white)
Font.Draw (intstr(score), maxx div 2, 0, startscreenfont, white)
View.Update
%changes high scores
if score > highscore (1) then %changes high scores
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := highscore (2)
highscore (2) := highscore (1)
highscore (1) := score
elsif score > highscore (2) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := highscore (2)
highscore (2) := score
elsif score > highscore (3) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := score
end if
%displays highscores
Font.Draw ("The top 3 scores are:", 8, 500, startscreenfont, white)
Font.Draw (intstr (highscore (1)), 500, 500, startscreenfont, white)
Font.Draw (intstr (highscore (2)), 500, 450, startscreenfont, white)
Font.Draw (intstr (highscore (3)), 500, 400, startscreenfont, white)

View.Update

end if


end loop
%checks if second gamemode was selected
elsif GAMEMODE = 2 then
%gameplay adjustments
playercol := 7
spd := 8
len := 80
gain := 16

%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, white)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%score

%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
end if

%ends game if player makes contact with itself
if whatdotcolour (posx, posy) = playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%when snake makes contact with fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location for fruit
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
%adds points to previous score
score := score + point
%draws fruit
drawfilloval (fruitX, fruitY, s, s, col)
%zero keeps track of current size as the grow grows the snake, this is so the snake will grow the conssistently for each fruit
zero := grow
%gain is how much the snake will grow by, this just grows the snake
grow := grow + gain
%the new grow value increases thus adding growing the snake from its previous size in the flexible array
new growthX, grow
new growthY, grow
%the new array values become 0, this is so the snake can continue to grow within the flexible array
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if





if rightflag = true then
posx := posx + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)

%snake body, this moves the snake
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)
cls
%checks if player has lost
if death = true then
Text.Color (playercol)
Text.ColorBack (black)



end if


end loop


end if

Author:  Heartseed [ Sun Jun 15, 2014 3:39 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

For my 2d array could I do this ?

Code

var taillock : flexible array 0..maxx, 0..maxy of int

Author:  Insectoid [ Sun Jun 15, 2014 4:28 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

That will only really work if your centipede parts are 1 pixel by 1 pixel. You can do it with larger centipede parts too, but it takes a wee bit more work.

Author:  Heartseed [ Sun Jun 15, 2014 4:33 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

They are by 10, since my screen is 800;600 (default), should I go with 80 and 60 then ?

Author:  Heartseed [ Sun Jun 15, 2014 4:39 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

I dont understand how to do the the 2d array, is there an example program that has a similar application as this ?
Can someone help me with this please I dont know how to keep track of the tail.

My game thus far.

Code

% Snake
% Version 6
%Im only commenting the left line of code, all the others are just repeats for different directions.


%prepare turing for graphics
setscreen ("graphics: 800;600")
View.Set ("offscreenonly")


%prepare keyboard input
var chars : array char of boolean

%music
%defines subprocess
process PlayMusic
%the command that plays the file
Music.PlayFile ("Nujabes - Aruarian Dance.mp3")
end PlayMusic

%checks if a fruit is currently present
var spawnedfruit : boolean := true

%true or false for constant snake motion
var leftflag : boolean := false
var rightflag : boolean := false
var upflag : boolean := false
var downflag : boolean := false
%fruit coordinate
var fruitX : int
var fruitY : int
%fruit random location
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)

%links snake growthX/Y
var h : int := 1
%initial snake length
var len : int := 1
%size of circles
var s : int := 8
%snake movement speed
var spd : int := 10
%fruit color
var col : int := 42
%point related stuff
%score
var score : int := 0
%how much the score increases by will vary
var point : int := 1
%the 3 highscores, if player score passes these they will replace them in the end screen
var highscore : array 1 .. 3 of int := init (9001, 80, 6)
%displays score in Font.Draw, without this there will be an synatax error
% var scoredisplay : array 1..20 of string
% scoredisplay(1) := "Your score is " + score + " congratulations, and good luck nextime"

%snake growth distancing
var gain : int := 10
%snakes body and initial length
var growthX : flexible array 1 .. len of int
var growthY : flexible array 1 .. len of int
%grows snake in length
var grow : int := upper (growthX)
%variable for zero(since a normal zero would be constant and unchanging)
var zero : int := 0
%initializes array
for i : 1 .. upper (growthX)
growthX (i) := 0
growthY (i) := 0
end for
%since whatdotcolor is evil this 2 dimentional array grid tracks the tail position, this helps indicate if youve made contact with the head
var taillock : flexible array 0..80, 0..60 of int
%sets grid values, these values are small or else the array subscript becomes out of range
for x : 0 .. 80
for y : 0 .. 60
taillock (x, y) := 0
end for
end for
for x : 0 .. maxx
taillock (x, 0) := 1
taillock (x, 80) := 1
end for
for y : 0 .. maxy
taillock (0, y) := 1
taillock (60, y) := 1
end for


%checks if player has lost
var death : boolean := false

%snake/player/centipede color
var playercol : int := 0
%game fonts
var startscreenfont : int
startscreenfont := Font.New ("Arial:36")
var font : int
font := Font.New ("Arial:16")
var smalllettering : int
smalllettering := Font.New ("Arial:14")

%options menu variables
var GAMEMODE : int := 0
loop

%Startscreen
drawfillbox (0, 0, maxx, maxy, black)
Font.Draw ("Snake Game by Kevin", 75, 400, startscreenfont, white)
Font.Draw ("-The snake grows by eating fruit which also increases your score. ", 100, 250, font, white)
Font.Draw ("-If you attempt to leave the screen or bite your tail the game will end.", 100, 200, font, white)
Font.Draw ("- GOOD LUCK AND HAVE FUN !!", 100, 150, font, white)
Font.Draw ("GAMEMODES: 1 default settings, 2 mapped, 3 secret settings.", 0, 8, smalllettering, white)
View.Update
%keyboard input
Input.KeyDown (chars)
if chars ('1') then
GAMEMODE := 1
exit

elsif
chars ('2') then
GAMEMODE := 2
exit
elsif chars ('3') then
GAMEMODE := 3
exit
end if
end loop
%location of snake
var posx : int := maxx div 2
var posy : int := maxy div 2




%gamemodes
if GAMEMODE = 1
then
%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, black)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%information hud
%current score
Font.Draw("Current score:", 0, maxy - 25, font, white)
Font.Draw(intstr(score), 150, maxy - 25, font, white)
%coordinates
Font.Draw("X:",175,maxy - 25, font, white)
Font.Draw(intstr(posx),200, maxy - 25, font, white)
Font.Draw("Y:",250,maxy - 25, font, white)
Font.Draw(intstr(posy), 275, maxy - 25, font, white)
Font.Draw("Fruit:", 325, maxy - 25, font, white)
Font.Draw(intstr(fruitX), 375, maxy - 25, font, white)
Font.Draw("/", 415, maxy - 25, font, white)
Font.Draw(intstr(fruitY), 420, maxy - 25, font, white)
View.Update
%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
end if

%chekcs if player made contact with tail

for c : 2 .. upper (growthX)
if taillock (posx - 1, posx) = taillock (growthX (c), growthY (c)) then
death := true
end if
end for
%ends game if head leaves visible area
if posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%when snake makes contact with fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location for fruit
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
%adds points to previous score
score := score + point
%draws fruit
drawfilloval (fruitX, fruitY, s, s, col)
%zero keeps track of current size as the grow grows the snake, this is so the snake will grow the conssistently for each fruit
zero := grow
%gain is how much the snake will grow by, this just grows the snake
grow := grow + gain
%the new grow value increases thus adding growing the snake from its previous size in the flexible array
new growthX, grow
new growthY, grow
%the new array values become 0, this is so the snake can continue to grow within the flexible array
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if





if rightflag = true then
posx := posx + 2
for c : 2 .. upper (growthX)
if taillock (posx - 1, posx) = taillock (growthX (c), growthY (c)) then
death := true
end if
end for
if posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
end if
for c : 2 .. upper (growthX)
if taillock (posx - 1, posx) = taillock (growthX (c), growthY (c)) then
death := true
end if
end for
if posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true


elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

if downflag = true then
posy := posy - 2
for c : 2 .. upper (growthX)
if taillock (posx - 1, posx) = taillock (growthX (c), growthY (c)) then
death := true
end if
end for
if posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)
%snake body, this moves the snake
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)

%checks if player has lost
if death = true then

Text.Color (playercol)
%displays the score
Font.Draw ("Your Score Was:", 0, 25, startscreenfont, white)
Font.Draw (intstr(score), maxx div 2, 0, startscreenfont, white)
View.Update
%changes high scores
if score > highscore (1) then %changes high scores
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := highscore (2)
highscore (2) := highscore (1)
highscore (1) := score
elsif score > highscore (2) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := highscore (2)
highscore (2) := score
elsif score > highscore (3) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, white)
highscore (3) := score
end if
%displays highscores
Font.Draw ("The top 3 scores are:", 8, 500, startscreenfont, white)
Font.Draw (intstr (highscore (1)), 500, 500, startscreenfont, white)
Font.Draw (intstr (highscore (2)), 500, 450, startscreenfont, white)
Font.Draw (intstr (highscore (3)), 500, 400, startscreenfont, white)

View.Update

end if


end loop
%checks if second gamemode was selected
elsif GAMEMODE = 2 then
%gameplay adjustments
playercol := 7
spd := 5

%for moving the snake
loop
% fork PlayMusic
%backround
drawfillbox (maxx, maxy, 0, 0, white)
%fruit
drawfilloval (fruitX, fruitY, s, s, col)
%score
%information hud
%current score
Font.Draw("Current score:", 0, maxy - 25, font, black)
Font.Draw(intstr(score), 150, maxy - 25, font, black)
%coordinates
Font.Draw("X:",175,maxy - 25, font, black)
Font.Draw(intstr(posx),200, maxy - 25, font, black)
Font.Draw("Y:",250,maxy - 25, font, white)
Font.Draw(intstr(posy), 275, maxy - 25, font, black)
Font.Draw("Fruit:", 325, maxy - 25, font, black)
Font.Draw(intstr(fruitX), 375, maxy - 25, font, black)
Font.Draw("/", 415, maxy - 25, font, white)
Font.Draw(intstr(fruitY), 420, maxy - 25, font, black)
View.Update
%get keyboard input
Input.KeyDown (chars)

%determines direction pressed
if chars (KEY_LEFT_ARROW) and rightflag = false then
leftflag := true
rightflag := false
upflag := false
downflag := false

elsif chars (KEY_RIGHT_ARROW) and leftflag = false then
leftflag := false
rightflag := true
upflag := false
downflag := false

elsif chars (KEY_DOWN_ARROW) and upflag = false then
leftflag := false
rightflag := false
upflag := false
downflag := true

elsif chars (KEY_UP_ARROW) and downflag = false then
leftflag := false
rightflag := false
upflag := true
downflag := false

end if

if leftflag = true then
posx := posx - 2
end if

%ends game if player makes contact with itself
if whatdotcolour (posx, posy) = playercol then
death := true
%ends game if head leaves visible area
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
%when snake makes contact with fruit
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
%new random location for fruit
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
%adds points to previous score
score := score + point
%draws fruit
drawfilloval (fruitX, fruitY, s, s, col)
%zero keeps track of current size as the grow grows the snake, this is so the snake will grow the conssistently for each fruit
zero := grow
%gain is how much the snake will grow by, this just grows the snake
grow := grow + gain
%the new grow value increases thus adding growing the snake from its previous size in the flexible array
new growthX, grow
new growthY, grow
%the new array values become 0, this is so the snake can continue to grow within the flexible array
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if





if rightflag = true then
posx := posx + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if upflag = true then
posy := posy + 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if

end if
if downflag = true then
posy := posy - 2
if whatdotcolour (posx, posy) = playercol then
death := true
elsif posx < 0 or posx > maxx or posy < 0 or posy > maxy then
death := true
elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) <= 2 * s then
randint (fruitX, 25, maxx - 25)
randint (fruitY, 25, maxy - 25)
score := score + 1
drawfilloval (fruitX, fruitY, s, s, col)
zero := grow
grow := grow + gain
new growthX, grow
new growthY, grow
for i : zero + 1 .. grow
growthX (i) := 0
growthY (i) := 0
end for
end if
end if








%snake head
drawfilloval (posx, posy, s, s, playercol)

%snake body, this moves the snake
for decreasing i : upper (growthX) .. 2
growthX (i) := growthX (i - 1)
growthY (i) := growthY (i - 1)
drawfilloval (growthX (i), growthY (i), s, s, playercol)
end for
growthX (h) := posx
growthY (h) := posy
View.Update
delay (spd)
cls
%checks if player has lost
if death = true then

Text.Color (playercol)
%displays the score
Font.Draw ("Your Score Was:", 0, 25, startscreenfont, black)
Font.Draw (intstr(score), maxx div 2, 0, startscreenfont, black)
View.Update
%changes high scores
if score > highscore (1) then %changes high scores
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, black)
highscore (3) := highscore (2)
highscore (2) := highscore (1)
highscore (1) := score
elsif score > highscore (2) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, black)
highscore (3) := highscore (2)
highscore (2) := score
elsif score > highscore (3) then
Font.Draw ("High Score!", maxx div 2, maxy div 2, startscreenfont, black)
highscore (3) := score
end if
%displays highscores
Font.Draw ("The top 3 scores are:", 8, 500, startscreenfont, black)
Font.Draw (intstr (highscore (1)), 500, 500, startscreenfont, black)
Font.Draw (intstr (highscore (2)), 500, 450, startscreenfont, black)
Font.Draw (intstr (highscore (3)), 500, 400, startscreenfont, black)

View.Update


end if


end loop


end if

Author:  Srlancelot39 [ Mon Jun 16, 2014 10:44 am ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

My username was originally used for an online game, and I ended up using it for other accounts like this one Razz

Remember to use code tags. Your code is near impossible to read when it is in regular text form as it is.

The end(s) of the tail will always be moved to the position of the part of the tail that is ahead of it. This can be done very easily when you use an array.

Author:  Heartseed [ Mon Jun 16, 2014 5:12 pm ]
Post subject:  RE:Turing centipede game(having trouble increasing length)

Code tags, all I did was put code before pasting in my code, am I supposed to do something else ?

Author:  Dreadnought [ Mon Jun 16, 2014 7:34 pm ]
Post subject:  Re: Turing centipede game(having trouble increasing length)

If you do this
code:
[syntax="Turing"]
...
awesome code here
...
[/syntax]


Then your code will look like this
Turing:
var awesome : string := "This code is awesome"
% Comments are awesome too!
put awesome

Author:  Srlancelot39 [ Mon Jun 16, 2014 11:06 pm ]
Post subject:  Re: Turing centipede game(having trouble increasing length)

End Game Screen:
It is flashing because you do not end the game; the playing screen and end game screen are being displayed simultaneously.

2D Array:
Yes, if you want to maintain the size of 10, then you could use 80 and 60 as your array size. As Insectoid said though, you would need to do a bit of add work to "redefine" your "pixels" (snake segments) as being 10 wide.
Part of this extra work would be making your snake move by 10 pixels at a time. You would be drawing the segments at x*10-5, and y*10-5, where x and y are values from 1 to 80 and 60 respectively. The subtraction of 5 is so that you draw the circles at the centre point of the divisions of 80 and 60, since the drawfilloval command draws the shape relative to its centre.

EDIT: Using a diameter of 10 for the snake and having 80 positions across and 60 high on an 800x600 resolution will work fine, however, the segments of your snake will be drawn end to end and not overlap like they do now to appear as a solid line. To make the above appear as a line, you would need to either use 160 and 120 as you array dimensions, or simply draw circles with diameter of 20 instead of 10 (all calculations would stay the same).


: