Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing centipede game(having trouble increasing length)
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Heartseed




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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.
Dragon20942




PostPosted: 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).
Heartseed




PostPosted: 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 ?
Heartseed




PostPosted: 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
Srlancelot39




PostPosted: 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.
Heartseed




PostPosted: 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 ?
Heartseed




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Heartseed




PostPosted: 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
wtd




PostPosted: 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.
Heartseed




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

Code tags ?
Heartseed




PostPosted: 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
Srlancelot39




PostPosted: 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.
Heartseed




PostPosted: 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 ?
Srlancelot39




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 24 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: