Computer Science Canada

Start Of Game - Help With Controls,Movement

Author:  Rasta Fella [ Sat Jan 07, 2006 3:05 pm ]
Post subject:  Start Of Game - Help With Controls,Movement

Ok, I am taking Computer Science at school and it is my first year taking it. I am in grade 10 learning Turing. At school they teach us the basics loops, for, end, exit when, etc. Now summatives are approaching or for some have started. My teacher wants each student to create a game using 5 inputs; up, down, right, centre and button. I decieded to do a game in which you are a small dot (filled circle) and you must reach a certain goal (coloured block). But there are obstacles you must face to get through to reach the "goal". I started and have a good startup page with GUI.

Here is the problem:

I want to make the dot as like those "snake games". The dot starts going in a "up" direction until another direction is pressed. When you push right it turn right and goes at a steady pace until another direction is put in. The problem with my code right now is that you have to hold the up button for it to continuously go up. Now any help is "good help" but remember, I just started turing so not to complex. And if you do help and it is complex just briefly explain so I can understand it.

Here is the part of a code which I started.

code:
var x,y: int
x:=20
y:=20
var chars: array char of boolean

%Draw Backgorund Colour
    drawfillbox(0,0,560,350,black)

        loop
       
            Input.KeyDown (chars)
            if chars (KEY_UP_ARROW) then
            if y <= 348 then
                y:=y+1
                end if
            end if
            if chars (KEY_RIGHT_ARROW) then
            if x <=559 then
                x:=x+1
                end if
            end if
            if chars (KEY_LEFT_ARROW) then
            if x >= 1 then
                x:=x-1
                end if
            end if
            if chars (KEY_DOWN_ARROW) then
            if y >= 1 then
                y:=y-1
                end if
            end if
           
            drawfilloval(x, y, 1, 1,white)
            delay(10)
            drawfilloval(x, y, 1, 1,black)
           
        end loop

Author:  cool dude [ Sat Jan 07, 2006 3:41 pm ]
Post subject: 

just make a for loop where u are drawing your snake and in the coordinates stick the variable u used for the for loop!

i actually made snake it turned out really good but i had a lot of problems so beware. i strongly suggest to make 4 procedure called move_up, move_down, move_left, and move_right. then have a procedure move snake. then in the procedure move snake have something like this

code:

if key = KEY_UP_ARROW and move not= "DOWN" then
        snake_up    %calls procedure
elsif key = KEY_UP_ARROW and move = "DOWN" then
        snake_down    %calls procedure


continue the if statements for all the procedures. since u will have a procedure for each movement it will make your life much easier! Smile

Author:  RedRogueXIII [ Sun Jan 08, 2006 10:45 am ]
Post subject: 

boolean flags anyone?
4 boolean switchs for 4 directions
when a key is pressed it turns all of the other ones off and turns the flag corresponding to the direction on.
then an if statement inside the main loop that checks to see which flag is on and then moves accordingly. ( Then this could also probably be accomplished by a variable that stores the last arrow key pressed....)
eg
if goUpFlag = true then
y += 1
end if

Author:  cool dude [ Sun Jan 08, 2006 10:48 am ]
Post subject: 

The reason i didn't suggest boolean flags is that when it comes around to programming the whole game it will be much easier to have a procedure for each movement so u can just call it. this will save him a lot of code and headachs.

Author:  RedRogueXIII [ Sun Jan 08, 2006 11:00 am ]
Post subject: 

well you can do it like that but when using the flags the procedures become unneccesary, its also pretty simple, just call it after a key is pressed and an moving condition is fufilled. if moving = false and keydown arrow then moving = true.
if moving = true then
move
end if


Also i know it is easier to understand like that but how would you keep the guy moving if you attached the movement to the buttons, because anytime you let go the snake stops moving then.

Author:  Cervantes [ Sun Jan 08, 2006 11:02 am ]
Post subject: 

RedRogueXIII wrote:
boolean flags anyone?
4 boolean switchs for 4 directions
when a key is pressed it turns all of the other ones off and turns the flag corresponding to the direction on.
then an if statement inside the main loop that checks to see which flag is on and then moves accordingly. ( Then this could also probably be accomplished by a variable that stores the last arrow key pressed....)
eg
if goUpFlag = true then
y += 1
end if


Why would you create four variables when you could do it with one? Sure, applying the movement to the object with only one variable might be a little tougher, but it will save you a lot of lines, and a lot of
code:

up := true
down := false
right := false
left := false

Author:  cool dude [ Sun Jan 08, 2006 11:16 am ]
Post subject: 

RedRogueXIII wrote:


Also i know it is easier to understand like that but how would you keep the guy moving if you attached the movement to the buttons, because anytime you let go the snake stops moving then.


actually if u use a for loop and make the snake move in there it will be moving at all times. and thats wat u want! so no matter wat the user will press the snake will still move. now wat cervenas says with the one variable obviously if u want to have less lines of code go for it but if your new to turing i suggest making it as easy as u can with having a bit more variables than one!

Author:  Rasta Fella [ Sun Jan 08, 2006 12:36 pm ]
Post subject: 

Thanks 'cool dude' for the reply but I don't understand. Let me reconfirm, you want me to create a procedure called "move snake" in that procedure put your code (you only made one for move up, so I will make the rest). And thats all there is to it? And I am not that fond of the procedure command...it's almost like a stating the variable thing right? It's confusing for me so if i stated anthing wrong please correct me. Thanks for everyone who replied. I am testing your code right now.

Author:  chrispminis [ Sun Jan 08, 2006 10:49 pm ]
Post subject: 

Personally, I prefer Red Rogue XIII's and Cervantes way, mostly Cervantes. Why would you use booleans? Could you not use just one variable say called direction where direction := 1 is up etc. So just set some if's that decide which keys give direction which values, preferably the right ones but there are mirror games and such. So instead of

code:

if goUpFlag = true then
y += 1
end if


You could have :
[code]
if direction = 1 then %1 meaning up, perhaps 2 for right, 3 for left, etc.

So for you Rasta Fella
y += 1
end if

I think that all works, and i tihnk its what Cervantes meant. Instead of:
[code]
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if y <= 348 then
y:=y+1
end if
end if
if chars (KEY_RIGHT_ARROW) then
if x <=559 then
x:=x+1
end if
end if
if chars (KEY_LEFT_ARROW) then
if x >= 1 then
x:=x-1
end if
end if
if chars (KEY_DOWN_ARROW) then
if y >= 1 then
y:=y-1
end if
end if
[/code]

You could do this:
[code]
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
direction := 1
end if
if chars (KEY_RIGHT_ARROW) then
direction := 2
end if
if chars (KEY_LEFT_ARROW) then
direction := 3
end if
if chars (KEY_DOWN_ARROW) then
direction := 4
end if
[/code]

And add this as well into the loop:

[code]
if direction := 1 then
y += 1 %Or wutever, depending on speed, applies to all that follow
elsif direction := 2 then
x += 1
elsif direction := 3 then
x -= 1
elsif direction := 4 then
y-= 1
end if
[/code]

This could be put into a procedure say called move or something if you want. I hope you understand now, plus i've basically given you the source, although you'll have to improve it. Don't forget boundaries. We don't want him to run off the screen.

Author:  chrispminis [ Sun Jan 08, 2006 10:51 pm ]
Post subject: 

Whoa did I do do something wrong or are code tags not working? And why does the edit button disappear sometimes? Is it not in the mood for working?

Author:  Cervantes [ Sun Jan 08, 2006 11:33 pm ]
Post subject: 

chrispminis wrote:

code:

if direction := 1 then
y += 1 %Or wutever, depending on speed, applies to all that follow
elsif direction := 2 then
x += 1
elsif direction := 3 then
x -= 1
elsif direction := 4 then
y-= 1
end if


Just to throw this out there: Instead of making that big if statement, you can store the angle of the snake's movement, then use some trig and rounding to generalize his movement. You have to be sure to incriment the angle by +/- 90 (or pi / 4).

chrispminis: I think you missed a "[/code]" at the end of your if direction = 1 then %1 meaning up, perhaps 2 for right, 3 for left, etc., which threw everything off. As for the edit button, it's disabled in Turing Help, since users were posting their question then removing them after it was answered.

Author:  chrispminis [ Sun Jan 08, 2006 11:47 pm ]
Post subject: 

Oh, Ok, that explains everything. But moderators can edit it? If so, wanna add that little end code tab for me? Smile Also it seems in my code there were a few typos. Correct me if im wrong it shouldn't be if direction := 1 but if direction = 1. Also thats not such a large if statement IMO, i've seen and used MUCH larger, but then again im probably not nearly as advanced as you are in Turing, it seems that your trig function would take longer than an if statement would it not? Since he is using a grid, and not like movement where he could move in angles other than right angles or multiples of 90 degrees.

Author:  Rasta Fella [ Mon Jan 09, 2006 4:36 pm ]
Post subject: 

Thanks chrispminis and crevantes for your reply but when i do the code it still doesnt work. You still have to hold the up arrow key to make it move.

but I was wondering if a code something like this will work...
This code does not work...but is it possible to do something along the lines of this like loop the input until another direction is pressed?
code:


        loop
       
            Input.KeyDown (chars)
            if chars (KEY_UP_ARROW) then
            if y <= 348 then

                loop
               
                y:=y+1
                exit when %like exit when a different direction is pressed
               
                loop
           
            drawfilloval(x, y, 1, 1,white)
            delay(10)
            drawfilloval(x, y, 1, 1,black)
 
        end loop

Author:  chrispminis [ Mon Jan 09, 2006 4:47 pm ]
Post subject: 

"y += 1" = "y:= y +1"

So your basically talking about my code but adding in the exit. I meant for you to loop it anyways, I didn't know it wasn't clear. In order for you to continue moving you must loop it.

Author:  RedRogueXIII [ Tue Jan 10, 2006 8:49 pm ]
Post subject: 

To keep moving all you have to do is keep calling in the part that moves over. Like i said in my 2nd post just make moveing flag that checks if the game (or a direction has been started) Once that codition has been met then you just keep calling in this part of code
Quote:
You could do this:
code:

Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
direction := 1
end if
if chars (KEY_RIGHT_ARROW) then
direction := 2
end if
if chars (KEY_LEFT_ARROW) then
direction := 3
end if
if chars (KEY_DOWN_ARROW) then
direction := 4
end if



Which will keep on going on from the last direction.

Author:  Rasta Fella [ Wed Jan 11, 2006 6:08 pm ]
Post subject: 

RedRogueXIII I understand what you mean but does this mean I can add your previous code to the one I currently have? And the variable direction..what do I declare it as?? integer??

Thanks for your help so far[/code]

Author:  RedRogueXIII [ Wed Jan 11, 2006 7:49 pm ]
Post subject: 

my mistake i quoted the wrong code the part lemme just make it up
code:
if moving = true then
        if dir = 1 then
            y += 10
% up
        elsif dir = 2 then
            y -= 10
% down
        elsif dir = 3 then
            x -= 10
% left
        elsif dir = 4 then
            x += 10
% right
        end if
    end if


that part can be added on anywhere in the main loop as long as you check for the moving flag, it will move the character as long as the game is in that loop.

Author:  Rasta Fella [ Tue Jan 17, 2006 4:47 pm ]
Post subject: 

I didn't want to bring an old topic back up but I also didn't want to create a brand new help topic regarding the same thing. The "help question" I posted before stated I needed help with a movement issue. Well, i was troubleshooting and figured it out but now i have a problem exiting the loop. All the variables are stated in my real document but the problem is I do not know how to exit the loop where y+1 is.
I want to make it so if any other direction is pressed then exit loop.The 'error' I have written in the code below in programmers comments.
So I am thinking something along the lines of this...
code:

if input_value=up then
        if y <= 337 then
            loop
            y := y + 1
                drawfilloval (x, y, 1, 1, white)
                delay (5)
                drawfilloval (x, y, 1, 1, black)
%***PROBLEM HERE
            exit when input value=left or down or right or jbutton%like this??
%***PROBLEM HERE
           end loop
        elsif y>= 337 then
            boom_death_reset           
            x := 20
            y := 60
            tdeaths:= tdeaths +1
            ldeaths:= ldeaths +1   
        end if


If its hard to understand what I mean post and I will explain more thoroughly(spelling?)

Although i do appreciate other alternatives to moving the dot like getch etc. I want to keep it this way because I understand it but if its not possible then I will change it.

Author:  Clayton [ Tue Jan 17, 2006 11:49 pm ]
Post subject: 

Rasta Fella wrote:
I didn't want to bring an old topic back up but I also didn't want to create a brand new help topic regarding the same thing. The "help question" I posted before stated I needed help with a movement issue. Well, i was troubleshooting and figured it out but now i have a problem exiting the loop. All the variables are stated in my real document but the problem is I do not know how to exit the loop where y+1 is.
I want to make it so if any other direction is pressed then exit loop.The 'error' I have written in the code below in programmers comments.
So I am thinking something along the lines of this...
code:

if input_value=up then
        if y <= 337 then
            loop
            y := y + 1
                drawfilloval (x, y, 1, 1, white)
                delay (5)
                drawfilloval (x, y, 1, 1, black)
%***PROBLEM HERE
            exit when input value=left or down or right or jbutton%like this??
%***PROBLEM HERE
           end loop
        elsif y>= 337 then
            boom_death_reset           
            x := 20
            y := 60
            tdeaths:= tdeaths +1
            ldeaths:= ldeaths +1   
        end if


If its hard to understand what I mean post and I will explain more thoroughly(spelling?)

Although i do appreciate other alternatives to moving the dot like getch etc. I want to keep it this way because I understand it but if its not possible then I will change it.


okay when you are trying to get out of a loop with many possible exit conditions ie
code:

loop
exit when exitCond1=1 or exitCond=2 or exitCond=3
end loop

u have to restate what you are comparing to so that you can exit, you cant just go
code:

loop
exit when exitCond=1 or 2 or 3
end loop

this wont work because for the 2 and 3 there is nothing to compare it to, try that and see if that is your problem

Author:  Rasta Fella [ Wed Jan 18, 2006 6:58 pm ]
Post subject: 

Thanks SuperFreak82 I got It too work. I appreciate your help.

Author:  Rasta Fella [ Wed Jan 18, 2006 7:08 pm ]
Post subject: 

Ok, I found out how to move, now when you press the arrow keys I want it to constantly check the x,y. So know when I press right arrow key it will remember THAT x,y cordinate, while the dot is STILL moving to the right and goes through objects because it only remember the LAST x,y cordinate. Now the problem is how can I fix it so the x,y values keep checking even while the dot is moving toward the right. Part of the code is below, I need to find a way how to constantly check the x,y value even though you are not pressing a key.
Code Below:
code:
proc movedot                                                                                       
    Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) then
            if y <= 337 then
                loop
                    y := y + 1
                    drawfilloval (x, y, 1, 1, white)
                    delay (5)
                    drawfilloval (x, y, 1, 1, black)
                    Input.KeyDown (chars)
                    exit when chars (KEY_DOWN_ARROW) or chars (KEY_RIGHT_ARROW) or chars (KEY_LEFT_ARROW)
                    if y>= 337 then
                        boom_death_reset
                        movedot
                    elsif whatdotcolour(x,y)=red then
                        boom_death_reset
                    elsif whatdotcolour(x,y)=yellow then
                        exit
                    end if
                end loop
            end if
        end if
        if chars (KEY_RIGHT_ARROW) then
            if x <= 547 then
                loop
                    x := x + 1
                    drawfilloval (x, y, 1, 1, white)
                    delay (5)
                    drawfilloval (x, y, 1, 1, black)
                    Input.KeyDown (chars)
                    exit when chars (KEY_DOWN_ARROW) or chars (KEY_UP_ARROW) or chars (KEY_LEFT_ARROW)
                    if x >= 547 then
                        boom_death_reset
                    end if
                end loop
            end if
        end if
        if chars (KEY_LEFT_ARROW) then
            if x >= 13 then
                loop
                    x := x - 1
                    drawfilloval (x, y, 1, 1, white)
                    delay (5)
                    drawfilloval (x, y, 1, 1, black)
                    Input.KeyDown (chars)
                    exit when chars (KEY_DOWN_ARROW) or chars (KEY_UP_ARROW) or chars (KEY_RIGHT_ARROW)
                    if x <= 13 then
                        boom_death_reset
                    end if
                end loop       
            end if
        end if
        if chars (KEY_DOWN_ARROW) then
            if y >= 53 then
                loop
                    y := y - 1
                    drawfilloval (x, y, 1, 1, white)
                    delay (5)
                    drawfilloval (x, y, 1, 1, black)
                    Input.KeyDown (chars)
                    exit when chars (KEY_LEFT_ARROW) or chars (KEY_UP_ARROW) or chars (KEY_RIGHT_ARROW)
                    if y<= 53 then
                        boom_death_reset
                    end if
                end loop
            end if
        end if
        if chars (KEY_ENTER) then
            x:=20
            y:=60
                delay(100)
    end if
        drawfilloval (x, y, 1, 1, white)
    delay (5)
        drawfilloval (x, y, 1, 1, black)
    locatexy(90,14)
        colourback(red)
            colour(white) 
                put tdeaths..
    locatexy(163,14)
        colourback(red)
            colour(white) 
                put score..
end movedot


Any help is appreciated considering the due date for this project is coming closer.

Author:  Rasta Fella [ Thu Jan 19, 2006 4:32 pm ]
Post subject: 

Is it possible to constantly check the x,y value even though you are not pressing a button and the object is moving? I need it for the code above. If so how? Explanation with some source would be helpful. I need this today or tommorow. I will award bits for my procrastination (last minute question).

Author:  Drakain Zeil [ Thu Jan 19, 2006 5:57 pm ]
Post subject: 

Store an ascending numerated list in an array, and a second variable (since, creating a true/false for every value on the list isn't worth it) which stores the number of movements; length of snake. From there, you can simply tell the entire list to jump back one, and add on a new guy to the list when you hit the apple. The list would store the x/y position, as a list of 1 to length.

Author:  Rasta Fella [ Thu Jan 19, 2006 6:30 pm ]
Post subject: 

Thanks, for explaining(+Bits) but the thing is I don't fully understand. Can you post a small source for me. If you can't thats fine but thanks for helping.

Author:  Drakain Zeil [ Thu Jan 19, 2006 7:49 pm ]
Post subject: 

Okay, so do something like...

type snakelist
record:
length:int %Length of snake
x:array 1..999 of int %x position for this segment
y:array 1..999 of int %y position for this segment
end record

Then for your snake, you assign each value in the array a number, this represents which part of the snake it is. So, you can draw the snake by going

for i:1..snake.length
Draw.Dot (snake.x,snake.y,blue) %(or watever your draw code is)
end for

When the snake moves, give each next section on the snake the previous section's value, so part 1 gains a new value, part 2 get's part 1's, part 3 get's what part 2 had and so on till we reach snake.length, where we just drop off the old value. (you might get problems here, watch out the order you're doing these things in! If you assign everything the same value then... yeah)

Also, for gaining new segments, we simply give it the first position, add one to the length, and move all of the exisiting segments back.

Beyond this, I don't think I can get into much more detail, I'm just not that good at answering specific questions, but sometimes I have my moments, so who knows.

Author:  Drakain Zeil [ Thu Jan 19, 2006 8:23 pm ]
Post subject: 

Okay, I have a decent example of what I'm talking about after you requested it via PM, I'm sure there's likely to be an error or two, but it works.

code:

View.Set ("graphics:300;300,nobuttonbar")
colourback (black)
colour (brightgreen)
cls
type snakelist :
    record
        x : array 1 .. 999 of int
        y : array 1 .. 999 of int
        l : int
    end record

var snake : snakelist
snake.l := 1
snake.x (1) := 10
snake.y (1) := 10
var x, y := 0 %Temp holder values
loop

    if Rand.Int (0, 4) = 1 then %Random, just to add to snake length
        snake.l += 1
        x := snake.x (1)
        y := snake.y (1)
        for decreasing i : snake.l -11 .. 2
            snake.x (i) := snake.x (i +1)
            snake.y (i) := snake.y (i +1)
        end for
    end if

    if Rand.Int (0, 1) = 1 then %Random movement, right or up?
        for decreasing i : (snake.l) .. 2
            snake.x (i) := snake.x (i - 1)
            snake.y (i) := snake.y (i - 1)
        end for
        snake.x (1) += 1
    else
        for decreasing i : (snake.l) .. 2
            snake.x (i) := snake.x (i - 1)
            snake.y (i) := snake.y (i - 1)
        end for
        snake.y (1) += 1
    end if
Input.Pause
    cls
    for n : 1 .. snake.l
        Draw.Dot (snake.x (n), snake.y (n), white)
    end for
    exit when snake.l = 25

end loop

Author:  Rasta Fella [ Thu Jan 19, 2006 8:33 pm ]
Post subject: 

Thanks, Drakain Zeil...but doesn't seem to work I'm sure it is correct but I'm having a problem with it. Was wondering any other simpler way to accomplish this. Oh well...

Thanks Drakain Zeil (+ Bits) For typing that code out. Hopefully it will help other users.

Author:  Drakain Zeil [ Thu Jan 19, 2006 10:11 pm ]
Post subject: 

What problems are you having?

Author:  Rasta Fella [ Thu Jan 19, 2006 10:13 pm ]
Post subject: 

I don't want to use arrays... I need a easier way that just keeps checking the x,y cordinates.

Author:  Drakain Zeil [ Thu Jan 19, 2006 10:28 pm ]
Post subject: 

Well, the only quick thing I can think of is to fork a process, which I don't suggest you do. Anyway, sorry I can't help anymore, I have to goto bed. Good luck with that program.

Author:  RedRogueXIII [ Mon Jan 23, 2006 8:51 pm ]
Post subject: 

Well i can say this, your posts made me want to do snake, so here an example of what i did,
code:
View.Set ("graphics:550,400, offscreenonly, nobuttonbar, title:Snake")
var x, y, dir, font, score, foodx, foody, speed, speedfactor, bodycount : int
var chars : array char of boolean
var moving, pauser, gameOver, setfood : boolean
var bodyx, bodyy : flexible array 0 .. 0 of int
x := 28
y := 18
dir := 1
score := 0
moving := false
pauser := false
gameOver := false
setfood := false
speed := 0
speedfactor := 10
bodycount := 0
font := Font.New ("Impact:14")
% lets say 1 in terms of direction = up
proc drawgrid
    % Draw a 10x10 grid background
    for i : 1 .. maxx by 10
        for j : 1 .. maxy by 10
            drawbox (i - 1, j - 1, i + 9, j + 9, black)
        end for
    end for
    drawfillbox (0, maxy - 49, maxx, maxy, grey)
end drawgrid

proc controls
    % This is the main game loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and dir not= 2 then
        dir := 1
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_DOWN_ARROW) and dir not= 1 then
        dir := 2
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_LEFT_ARROW) and dir not= 4 then
        dir := 3
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_RIGHT_ARROW) and dir not= 3 then
        dir := 4
        if moving = false then
            moving := true
        end if
    end if
    if chars (KEY_ENTER) then
        if pauser = false then
            pauser := true
        elsif pauser = true then
            pauser := false
        end if
    end if

end controls

proc move
    % controls should go first and then check if moving has begun then
    % Snake Shifting code here
    if bodycount > 0 then
        bodyx (1) := x
        bodyy (1) := y
        if bodycount > 1 then
            for decreasing i : bodycount .. 2
                bodyx (i) := bodyx (i - 1)
                bodyy (i) := bodyy (i - 1)
            end for
        end if
    end if


    if moving = true then
        speed += speedfactor
        if speed mod 10 = 0 then
            if dir = 1 then
                y += 1
            elsif dir = 2 then
                y -= 1
            elsif dir = 3 then
                x -= 1
            elsif dir = 4 then
                x += 1
            end if
            speed := 0

        end if
    end if
end move

proc ohnoes
    % Handles Game Over/ Restart
    if x <= 0 or x * 10 >= maxx or y <= 0 or y * 10 >= maxy - 50 then
        x := 28
        y := 18
        moving := false
        %pauser := true
        gameOver := true
        score := 0
        dir := 0
        bodycount := 0
        new bodyx, bodycount
        new bodyy, bodycount
    end if
end ohnoes

proc otherside
    if x < 0 then
        x := maxx div 10
    end if
    if x * 10 >= maxx then
        x := 0
    end if
    if y < 0 then
        y := maxy div 10 - 5
    end if
    if y * 10 >= maxy - 50 then
        y := 0
    end if
end otherside

proc HUD
    Font.Draw ("SCORE :" + intstr (score), 20, maxy - 40, font, black)
    if pauser = true then
        Font.Draw ("PAUSED", maxx div 2 - 100, maxy - 40, font, red)
    end if
end HUD

proc food
    % Food Code
    if setfood = false then
        setfood := true
        randint (foodx, 1, maxx div 10)
        randint (foody, 1, maxy div 10 - 5)
    end if
    drawfilloval (foodx * 10 + 5, foody * 10 + 5, 5, 5, red)
    if x = foodx and y = foody then
        bodycount += 1
        new bodyx, bodycount
        new bodyy, bodycount
        score += 1
        setfood := false
    end if
end food





loop
otherside
    %ohnoes
    drawgrid
    HUD
    controls

    food
    if pauser = false then
        move
    end if

    locate (3, 18)
    put x, " ", y, " = ", foodx, " ", foody, " ", bodycount, " ", upper (bodyx), " " ..
    View.Update
    cls
    delay (5)
    drawfillbox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, 1)
    for decreasing i : bodycount .. 1
        drawfillbox (bodyx (i) * 10, bodyy (i) * 10, bodyx (i) * 10 + 10, bodyy (i) * 10 + 10, i)
    end for
    delay(10)
end loop

basically
you need an x,y, and direction variable which can all be integers. then a flexible array or just a huge array thats predetermined to store the body x and y. Also the food x and y. So using a main loop your code should be like this.

loop

keyboardInput % not actually moves but changes the direction variable

move%takes the direction and then moves 1 to that direction

if snake x, y = food x,y then
+ 1 to the flexible snake x and y array.

for decreasing loop
*for decreasing i:upper(snakex)..1
snakex(i):=snakex(i-1)
%Y-Coordinates can be in the same for loop
end for*

then check to see if the snake hit a wall or if it hit itself

*for i:1..upper(snakex)
if x(the front dot) = snakex(i) (one of the snake body)
end for

end loop

Remeber your not trying to make the whole game execute in only one run of the loop but to make it repeat small continous actions over and over.

learn from my snake example, - [you cant die because i was to lazy to keep on restarting to playtest. Razz]


: