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

Username:   Password: 
 RegisterRegister   
 Unable to make "food" disappear in a game of "Snake"
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nismail




PostPosted: Thu Jan 21, 2016 10:37 pm   Post subject: Unable to make "food" disappear in a game of "Snake"

What is it you are trying to achieve?
To create a game of Snake


What is the problem you are having?
Don't know how to make the "Food" disappear when being touched by the snake
Also want to make it so they snake moves by itself once a key is pressed, not that I have to hold the key to make it move


Describe what you have tried to solve this problem
I've checked other programs to observe the coding yet I am unable to decipher what and where the code is that makes the "food" disappear


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Just some quicknotes:
fx and fy are the variables for the food

Turing:


setscreen ("graphics:max;max,nobuttonbar")     
var chars : array char of boolean             
var button : string (1)                       
var xposition : int := 650                     
var yposition : int := 500                     
var xdirection, ydirection : int := 0           
var fx, fy : int                               
var score : int:= 00                           

%locatexy (550, 450)                                                       
%put "Use the arrow keys to move and don't touch the red!"                   
%locatexy (500, 425)
%put "Try to get the longest time possible and fill the entire screen"

cls                                           

randint (fx, 24, 578)                       
randint (fy, 24, 578)                         
 
loop                                           

var x, y : int                                 
x := 650                                     
y := 400                                       
loop
    Input.KeyDown (chars)                     
    if chars (KEY_UP_ARROW) then               
        y := y + 3                             
    end if
    if chars (KEY_RIGHT_ARROW) then
        x := x + 3
    end if
    if chars (KEY_LEFT_ARROW) then
        x := x - 3
    end if
    if chars (KEY_DOWN_ARROW) then
        y := y - 3
    end if
   
    drawfilloval (x, y, 10, 10, brightgreen)
    delay (10)
    drawfilloval (x, y, 10, 10, brightred)
   
   
    drawfillbox (fx, fy, fx + 10, fy + 10, purple)
    drawfillbox (fx + 2, fy + 2, fx + 8, fy + 8, brightblue)
   
    View.Update
 
    end loop
end loop




Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Jan 22, 2016 6:29 am   Post subject: RE:Unable to make "food" disappear in a game of "Snake"

You need to detect if the snake collides with the food. There are plenty of collision detection tutorials on this site.

As for movement, well, just make it move all the time. Use the arrow keys to control direction, not movement.
nismail




PostPosted: Fri Jan 22, 2016 2:17 pm   Post subject: Re: RE:Unable to make "food" disappear in a game of "Snake"

Insectoid @ Fri Jan 22, 2016 6:29 am wrote:
You need to detect if the snake collides with the food. There are plenty of collision detection tutorials on this site.

As for movement, well, just make it move all the time. Use the arrow keys to control direction, not movement.


Thanks for the feed back, but how do I make it control the direction but not the movement?
Insectoid




PostPosted: Fri Jan 22, 2016 5:56 pm   Post subject: RE:Unable to make "food" disappear in a game of "Snake"

A very basic implementation would look something like this:
code:
if (up arrow pressed) then
    direction := 'up'
end if

if direction = up then
    y := y + 1
end if

%repeat for all keys/directions
nismail




PostPosted: Sat Jan 23, 2016 3:41 pm   Post subject: Re: RE:Unable to make "food" disappear in a game of "Snake"

Insectoid @ Fri Jan 22, 2016 5:56 pm wrote:
A very basic implementation would look something like this:
code:
if (up arrow pressed) then
    direction := 'up'
end if

if direction = up then
    y := y + 1
end if

%repeat for all keys/directions


I'm still somewhat confused. I've looked at other programs I notice that it does not say
Turing:


if chars (KEY_LEFT_ARROW) then
        x := x - 3



but it says something like this

Turing:


if chars (KEY_LEFT_ARROW) then
        x := - 3



Yet when I try this, it just makes the box disappear. Is there something I have to type beforehand?

The code you've given me, I don't understand exactly what it is doing. can you explain it with more detail (I started using turing about a month ago, I'm not the best)

I'm also having trouble making the snake hit itself. I've managed to add a few things to somewhat finish the program, here is the code:

Turing:


setscreen ("graphics:800;600")
var chars : array char of boolean
var button : string (1)
var xposition : int := 650
var yposition : int := 500
var xdirection, ydirection : int := 0
var x2, y2 : int
var fx, fy : int
var score : int := 00

Input.KeyDown (chars)
locatexy (200, 325)
put "Use the arrow keys to move and don't touch the red!"
locatexy (200, 290)
put "Use space bar to slightly slow down time!"
locatexy (200, 270)
put "This message will disappear once touching the first piece of food."
locatexy (200, 250)
put "The food is the blue/purple square."
loop
    if chars (KEY_UP_ARROW) then
        cls
    end if
    randint (fx, 24, 490)
    randint (fy, 24, 490)

    var x, y : int
    x := 200
    y := 100
    x2 := 210
    y2 := 110
    loop
        Input.KeyDown (chars)

        if chars (KEY_UP_ARROW) then
            y := y + 2
            y2 := y2 + 2
        end if
        if chars (KEY_RIGHT_ARROW) then
            x := x + 2
            x2 := x2 + 2
        end if
        if chars (KEY_LEFT_ARROW) then
            x := x - 2
            x2 := x2 - 2
        end if
        if chars (KEY_DOWN_ARROW) then
            y := y - 2
            y2 := y2 - 2
        end if

        locatexy (1, 585)
        put "Score:"
        locatexy (1, 575)
        put score

        drawfillbox (x, y, x2, y2, brightgreen)
        delay (10)
        drawfillbox (x, y, x2, y2, brightgreen)


        drawfillbox (fx, fy, fx + 10, fy + 10, purple)
        drawfillbox (fx + 2, fy + 2, fx + 8, fy + 8, brightblue)
        if x2 > fx and x < fx + 10 and y2 > fy and y < fy + 10 then
            cls
            randint (fx, 24, 490)
            randint (fy, 24, 490)
            score := score + 1
            View.Update
        end if
        drawfillbox (-10, 0, 30, 565, red)
        drawfillbox (0, 0, 950, 30, red)
        drawfillbox (765, 0, 950, 565, red)
        drawfillbox (0, 555, 950, 565, red)

        if chars (' ') then
            drawfillbox (x, y, x2, y2, brightred)
            delay (10)
            drawfillbox (x, y, x2, y2, brightred)
        end if

        if 30 > x and 0 < x2 and 565 > y and 0 < y2 then
            cls
            locatexy (350, 300)
            put "You lose!"
            score := 00
            exit
        elsif 950 > x and 0 < x2 and 30 > y and 0 < y2 then
            cls
            locatexy (350, 300)
            put "You lose!"
            score := 00
            exit
        elsif 950 > x and 765 < x2 and 950 > y and 0 < y2 then
            cls
            locatexy (350, 300)
            put "You lose!"
            score := 00
            exit
        elsif 950 > x and 0 < x2 and 565 > y and 555 < y2 then
            cls
            locatexy (350, 300)
            put "You lose!"
            score := 00
            cls
            exit
        end if
    end loop
    exit
end loop


Insectoid




PostPosted: Sun Jan 24, 2016 8:14 am   Post subject: RE:Unable to make "food" disappear in a game of "Snake"

The example you posted is a very slightly more advanced version of the same thing I posted. My example is simplified to make it easier to understand, and it will work just fine.

The x variable in your example is not actually an x coordinate. It is an x velocity (and should be called velX or something similar). It is a direction and speed all in one. When you tried it, your picture disappeared because you were using it as an x coordinate, and setting it to -3, which places it off-screen (where you can't see it).

It should look like this:
code:
if keys (KEY_LEFT_ARROW) then
    velX := -3
end if %repeat for all keys

x := x + velX
y := y + velY


You can see that we add velX to x no matter what. It always happens, so x is always moving. We change velX/velY to change the speed/direction only. A negative value of velX makes the character move left (because adding a negative number is the same as subtracting) and a positive velX makes it move right. Therefore, to move left, switch velX to negative and to move right, switch it to positive. This code removes one if statement (the 2nd one in my first example) and streamlines your code a bit so you can do more with less.

Making the snake hit itself (and actually move like a snake) is the most involved part of the program, so I'm going to let you figure it out on your own. I'll give you a few hints though.

-The snake isn't one big piece, it's a whole bunch of similar segments joined together.
-Arrays are really useful for keeping track of and controlling a bunch of similar things.
-The whole snake doesn't necessarily have to move. None of it really has to move (at least in the classic grid-based version). Only the head (first segment) and tail (last segment) actually have to do anything and keeping this in mind can simplify your program quite a bit.
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 1  [ 6 Posts ]
Jump to:   


Style:  
Search: