
-----------------------------------
Heartseed
Sat Jun 14, 2014 8:49 am

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)  maxx or posy < 0 or posy > maxy then
            death := true
            %how to clear fruit
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
            %how to clear fruit
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
            %when snake makes contact with fruit
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
            death := true
            %when snake makes contact with fruit
        elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
                death := true
            elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
                death := true
            elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
                death := true
            elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  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)  maxx or posy < 0 or posy > maxy then
                death := true
            elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
                death := true
            elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2)  maxx or posy < 0 or posy > maxy then
                death := true
            elsif sqrt ((posx - fruitX) ** 2 + (posy - fruitY) ** 2) 