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

Username:   Password: 
 RegisterRegister   
 I need help with snake game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cpu_hacker4.0




PostPosted: Wed Oct 17, 2007 5:57 pm   Post subject: I need help with snake game

Ok I am making a snake game for turing and this is what I have so far. Basically, you control one ball with the arrow keys and have to "eat" other circles that get added on to the tail. I'm trying to use collision detection (to "eat" other balls), but it doesn't seem to be working. I put the code that's causing problems in green, just so you can see what I am trying to do. Fiirst run it as is, then remove the /* and */ to see what the problem is. Help would be greatly appreciated.
Turing:

const RADIUS : int := 10

var chars : array char of boolean
var num : int := 1
var x, y : array 1 .. num of int
var xInc, yInc : int := 0
var rx, ry : int

proc RandBall
    randint (rx, 0 + RADIUS, maxx - RADIUS)
    randint (ry, 0 + RADIUS, maxy - RADIUS)
end RandBall

for a : 1 .. num
    x (a) := maxx div 2
    y (a) := maxy div 2
end for

View.Set ("Offscreenonly")
RandBall
loop
    for a : 1 .. num
        x (a) := x (a) + xInc
        y (a) := y (a) + yInc
        drawfilloval (x (a), y (a), RADIUS, RADIUS, black)
        drawfilloval (rx, ry, RADIUS, RADIUS, black)
        View.Update
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) then
            xInc := 0
            yInc := 1
        elsif chars (KEY_DOWN_ARROW) then
            xInc := 0
            yInc := -1
        elsif chars (KEY_RIGHT_ARROW) then
            xInc := 1
            yInc := 0
        elsif chars (KEY_LEFT_ARROW) then
            xInc := -1
            yInc := 0
        end if
    end for
    /*if x(a) > rx - 10 or x(a) < rx + 10 and y(a) > ry - 10 or y(a) < ry + 10 then
        RandBall
     end if*/

    cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Wed Oct 17, 2007 6:26 pm   Post subject: Re: I need help with snake game

the commented part is not in your for loop Shocked
that's the "problem", and change the conditions to = rather than bigger or less, although that doesnt fix the dectection.
try searching through the turing forum, im sure there are a lot of snake games example >>snake game<<
Saad




PostPosted: Wed Oct 17, 2007 6:26 pm   Post subject: RE:I need help with snake game

The 'or's in if statement should be an "and" and that should be inside the for loop
HeavenAgain




PostPosted: Wed Oct 17, 2007 6:29 pm   Post subject: RE:I need help with snake game

yea, forget the equal to, just change the or to and. Very Happy
cpu_hacker4.0




PostPosted: Wed Oct 17, 2007 6:42 pm   Post subject: Re: I need help with snake game

Alright thanks! That seems to have worked...but why is it and? I know now that or doesn't work, but I used or because my ball can either be on the left or right side of the other ball...it can't be on both.

Also, I need help with the next part. When I touch the other ball, I want it to act as a tail that follows my ball around, but I don't know how to use it. I know I need to make an array for the x and y values, but I'm not sure how to use it in the program. Thanks for helping!

Here is the updated code:
Turing:

const RADIUS : int := 10

var chars : array char of boolean
var num : int := 1
var x, y : array 1 .. num of int
var xInc, yInc : int := 0
var rx, ry : int

proc RandBall
    randint (rx, 0 + RADIUS, maxx - RADIUS)
    randint (ry, 0 + RADIUS, maxy - RADIUS)
end RandBall

for a : 1 .. num
    x (a) := maxx div 2
    y (a) := maxy div 2
end for

View.Set ("Offscreenonly")
RandBall
loop
    for a : 1 .. num
        x (a) := x (a) + xInc
        y (a) := y (a) + yInc
        drawfilloval (x (a), y (a), RADIUS, RADIUS, black)
        drawfilloval (rx, ry, RADIUS, RADIUS, black)
        View.Update
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) then
            xInc := 0
            yInc := 1
        elsif chars (KEY_DOWN_ARROW) then
            xInc := 0
            yInc := -1
        elsif chars (KEY_RIGHT_ARROW) then
            xInc := 1
            yInc := 0
        elsif chars (KEY_LEFT_ARROW) then
            xInc := -1
            yInc := 0
        end if
        if x (a) > rx - 15 and x (a) < rx + 15 and y (a) > ry - 15 and y (a) < ry + 15 then
            RandBall
        end if
    end for
    cls
end loop
Nick




PostPosted: Thu Oct 18, 2007 6:56 am   Post subject: RE:I need help with snake game

just make the x and y = the old x and y of ur snake and have each attaching part = the old x and y of the precedding part so:

Turing:
old x:=x
oldy:=y
x+=5
y+=0
%snake is moving right
oldballx(1):=ballx(1)
oldbally(1):=bally(1)
ballx(1):=oldx
bally(1):=oldy
ballx(2):=oldballx(1)
bally(2):=oldbally(1)


i havent actually tried this nor made a snake game but i did make a trailing string bounce program ill post so u can see how this works

Turing:
var word : array 1 .. 5 of string
put "Enter the word or sentence to bounce:"
get word (1) : *
var row, col : array 1 .. 5 of int
var len : int := length (word (1))
var a, b : int := 1
var timer : int
put "how fast (1,2 or 3)"
get timer
if timer = 1 then
    timer := 10
elsif timer = 2 then
    timer := 30
elsif timer = 3 then
    timer := 50
else
    timer := 1000
end if

for i : 1 .. 5
    word (i) := word (1)
    row (i) := 10
    col (i) := 10
end for
cls
loop
    locate (row (1), col (1))
    color (red)
    put word (1) ..
    locate (row (2), col (2))
    color (green)
    put word (2) ..
    locate (row (3), col (3))
    color (brown)
    put word (3) ..
    locate (row (4), col (4))
    color (black)
    put word (4) ..
    locate (row (5), col (5))
    color (blue)
    put word (5) ..
    col (5) := col (4)
    col (4) := col (3)
    col (3) := col (2)
    col (2) := col (1)
    col (1) := col (1) + a
    row (5) := row (4)
    row (4) := row (3)
    row (3) := row (2)
    row (2) := row (1)
    row (1) := row (1) + b
    if row (1) >= 25 then
        b := -1
    end if
    if col (1) >= 80 - len then
        a := -1
    end if
    if row (1) <= 1 then
        b := +1
    end if
    if col (1) <= 1 then
        a := +1
    end if
    delay (timer)
    cls
end loop
cpu_hacker4.0




PostPosted: Thu Oct 18, 2007 10:38 am   Post subject: Re: I need help with snake game

Ok thanks...I think I understand how it works, but I'm using a variable for the last number of the array, because it depends on how many times the ball disappears. So I don't really know how to put that in my code...how would I do it?[/quote]
HeavenAgain




PostPosted: Thu Oct 18, 2007 11:40 am   Post subject: RE:I need help with snake game

i cant rememebr who it was, but someone made a nice snake game a while back, and it was only a few lines (and it was great)
like i said, go and search for it, its in the turing sections somewhere.
and it seems like you dont know hwo does the array works? you can have a nice look at the tutorials section, here
Sponsor
Sponsor
Sponsor
sponsor
cpu_hacker4.0




PostPosted: Thu Oct 18, 2007 5:55 pm   Post subject: Re: I need help with snake game

No I know how to use arrays, just don't know what to do with the code
CodeMonkey2000




PostPosted: Thu Oct 18, 2007 9:07 pm   Post subject: Re: I need help with snake game

There is a ridiculously easy way to make a snake game. I'm too lazy to explain it so meh. Ther a re a lot of ways you can optimize this though.
code:
View.Set ("graphics:330;330")
type node :
    record
        x : int
        y : int
    end record
var square : array 1 .. 30, 1 .. 30 of int
var nodes : flexible array 0 .. 0 of node
var key : array char of boolean
var xInc : int := 1
var yInc : int := 0
nodes (0).x := 15
nodes (0).y := 15
for x : 1 .. 30
    for y : 1 .. 30
        square (x, y) := 0
    end for
end for
square (Rand.Int (1, 30), Rand.Int (1, 30)) := 2
proc reDraw
    for x : 1 .. 30
        for y : 1 .. 30
            Draw.FillBox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, black)
            if square (x, y) = 2 then
                Draw.FillOval (x * 10 + 4, y * 10 + 4, 5, 5, yellow)
            end if
            if square (x, y) = 1 then
                Draw.FillOval (x * 10 + 4, y * 10 + 4, 5, 5, green)
            end if
        end for
    end for
end reDraw
proc addNode
    new nodes, upper (nodes) + 1
    nodes (upper (nodes)).x := nodes (upper (nodes) - 1).x
    nodes (upper (nodes)).y := nodes (upper (nodes) - 1).y
    square (Rand.Int (1, 30), Rand.Int (1, 30)) := 2
end addNode
loop
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        xInc := 0
        yInc := 1
    elsif key (KEY_DOWN_ARROW) then
        xInc := 0
        yInc := -1
    elsif key (KEY_RIGHT_ARROW) then
        xInc := 1
        yInc := 0
    elsif key (KEY_LEFT_ARROW) then
        xInc := -1
        yInc := 0
    end if
    square (nodes (0).x, nodes (0).y) := 0
    for decreasing x : upper (nodes) .. 0
        if x > 0 then
            square (nodes (x).x, nodes (x).y) := 0
            nodes (x).x := nodes (x - 1).x
            nodes (x).y := nodes (x - 1).y
        end if
    end for
    if nodes (0).x + xInc > 0 and nodes (0).x + xInc < 31 then
        nodes (0).x += xInc
    end if
    if nodes (0).y + yInc > 0 and nodes (0).y + yInc < 31 then
        nodes (0).y += yInc
    end if
    if square (nodes (0).x, nodes (0).y) = 2 then
        addNode
    end if
    for x : 0 .. upper (nodes)
        square (nodes (x).x, nodes (x).y) := 1
    end for
    reDraw
    delay (100)
end loop
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  [ 10 Posts ]
Jump to:   


Style:  
Search: