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

Username:   Password: 
 RegisterRegister   
 snake game - the body (part 2)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xHoly-Divinity




PostPosted: Wed Jan 05, 2005 3:32 pm   Post subject: snake game - the body (part 2)

In my code, the snake's body does not fully disappear when he moves. I was wondering if anyone can give me a hand. Thank you.

code:

var speed : int
put "Welcome to SNAKE!!!", skip, skip
put "Enter the speed you would like (1)Slow  (2)Fast  (3)Insane", skip
put "***NOTE That the faster the speed the more points you earn per 'bite'"
get speed
cls
drawbox (10, 10, maxx - 10, maxy - 10, black)
var score : int := 0
var x, y, xx, yy : int
x := 100
y := 100
xx := 200
yy := 100
drawline (x+50, y, xx, yy, brightgreen)
var h, k : int
randint (h, 15, maxx - 15)
randint (k, 15, maxy - 15)
drawfilloval (h, k, 5, 5, brightred)
loop
    delay (10)
    var chars : array char of boolean
    var chars2 : array char of boolean
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        loop
            delay (10)
            y := yy
            xx := xx + speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx-50, yy, white)
            if whatdotcolour (xx + speed, yy + speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if xx > maxx - 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_UP_ARROW) or chars2 (KEY_DOWN_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_UP_ARROW) then
        loop
            delay (10)
            x := xx
            yy := yy + speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx, yy-50, white)
            if whatdotcolour (xx + speed, yy + speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if yy > maxy - 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_RIGHT_ARROW) or chars2 (KEY_LEFT_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_LEFT_ARROW) then
        loop
            delay (10)
            y := yy
            xx := xx - speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx+50, yy, white)
            if whatdotcolour (xx - speed, yy - speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if xx < 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_UP_ARROW) or chars2 (KEY_DOWN_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_DOWN_ARROW) then
        loop
            delay (10)
            x := xx
            yy := yy - speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx, yy+50, white)
            if whatdotcolour (xx - speed, yy - speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if yy < 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_RIGHT_ARROW) or chars2 (KEY_LEFT_ARROW) then
                exit
            end if
        end loop
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Neo




PostPosted: Wed Jan 05, 2005 4:50 pm   Post subject: (No subject)

I looked briefly at your code, and from what I can see, you are using a white line that trails the green line to erase the trail. It works fine if you go in a straight line but your problem is when turning. When your turning, say you are moveing east and then turn north, the white line that was supposed to erase the green line horizontally doesnt get to finish erasing because you turned and so the horizintal white line dissapears and you start drawing a vertical white line to erase the trail vertically. You follow?

To fix it let the white line run a little bit longer, after you make a change in direction, so that it will erase the bit of green that was left behind after the turn.
xHoly-Divinity




PostPosted: Wed Jan 05, 2005 4:59 pm   Post subject: (No subject)

This can work, however, once I successfully manage to fix it, I want to extend the length of the snake, so every time the amount of time the white line would have to run would be longer. Is there not an easier way? I would really appreciate it if someone could just fill in the lines they think I'm missing (you will be credited of course!). Thank you
Neo




PostPosted: Wed Jan 05, 2005 5:09 pm   Post subject: (No subject)

Well your method of drawing the snake is not very effective. I think you need a new technique. I've never made a snake game but if i were to do so, I think I would make the snake out of a series of squares and put their coords in an array. Every time a snake eats a food add a square to the end of the snake. Or something similar.
Cervantes




PostPosted: Wed Jan 05, 2005 5:19 pm   Post subject: (No subject)

You're code is.. less than ideal. For a snake game, you will probably want to use arrays to store the position of each segment of the snake, and have a variable to store the current direction of the snake.
I suggest you read up on arrays once or twice to get the handle of them, if you aren't good with them already, and then recode your program and ask questions along the way.

EDIT: wow, four posts while I was typing that. Confused
xHoly-Divinity




PostPosted: Wed Jan 05, 2005 6:16 pm   Post subject: (No subject)

I was hoping that perhaps I could keep the same code... however, if it is absolutely impossible to do it using my code, then i will have to change it....
Cervantes




PostPosted: Wed Jan 05, 2005 6:22 pm   Post subject: (No subject)

I highly suggest you do. Using whatdotcolour with Snake is a nono. Only Andy is allowed to do that Wink
xHoly-Divinity




PostPosted: Wed Jan 05, 2005 6:40 pm   Post subject: (No subject)

I've fixed that problem (sort of). If you press lets say up, then after 5 secs you press left then it works fine. However, if you move to another direction before the thing is finished erasing, then there is a problem (if you don't understand what i'm talking about, test the game and i'm sure you will identify it). So, as you can see, I have made an attempt at fixing the problem and have successfuly done so to a certain extent (which comes to prove that the code can work). Please, if you could elaborate on what I have so far to make it flawless then I would forever be in your debt. Thank you.

code:

var speed : int
put "Welcome to SNAKE!!!", skip, skip
put "Enter the speed you would like (1)Slow  (2)Fast  (3)Insane", skip
put "***NOTE That the faster the speed the more points you earn per 'bite'"
get speed
cls
drawbox (10, 10, maxx - 10, maxy - 10, black)
var score : int := 0
var x, y, xx, yy : int
x := 100
y := 100
xx := 200
yy := 100
drawline (x + 50, y, xx, yy, brightgreen)
var h, k : int
randint (h, 15, maxx - 15)
randint (k, 15, maxy - 15)
drawfilloval (h, k, 5, 5, brightred)
loop
    delay (10)
    var chars : array char of boolean
    var chars2 : array char of boolean
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        var d, e, f, g, dd, ee, ff, gg, u, v : int
        d := y
        e := yy + 50
        dd := y
        ee := yy + 50
        f := x
        g := xx
        u := yy - 50
        v := yy - 50
        loop
            delay (10)
            ee := ee - speed
            u := u + speed
            drawline (f, u, g, v, white)
            drawline (f, e, g, ee, white)
            y := yy
            xx := xx + speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx - 50, yy, white)
            if whatdotcolour (xx + speed, yy + speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if xx > maxx - 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_UP_ARROW) or chars2 (KEY_DOWN_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_UP_ARROW) then
        var d, e, f, g, dd, ee, ff, gg, u, v : int
        d := y
        e := yy
        ff := xx - 50
        dd := y
        gg := xx - 50
        f := x
        g := xx
        u := xx + 50
        v := xx + 50
        loop
            delay (10)
            gg := gg + speed
            u := u - speed
            drawline (ff, d, gg, e, white)
            drawline (u, d, v, e, white)
            x := xx
            yy := yy + speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx, yy - 50, white)
            if whatdotcolour (xx + speed, yy + speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if yy > maxy - 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_RIGHT_ARROW) or chars2 (KEY_LEFT_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_LEFT_ARROW) then
        var d, e, f, g, dd, ee, ff, gg, u, v : int
        d := y
        e := yy + 50
        dd := y
        ee := yy + 50
        f := x
        g := xx
        u := yy - 50
        v := yy - 50
        loop
            delay (10)
            ee := ee - speed
            u := u + speed
            drawline (f, u, g, v, white)
            drawline (f, e, g, ee, white)
            y := yy
            xx := xx - speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx + 50, yy, white)
            if whatdotcolour (xx - speed, yy - speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if xx < 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_UP_ARROW) or chars2 (KEY_DOWN_ARROW) then
                exit
            end if
        end loop
    elsif chars (KEY_DOWN_ARROW) then
    var d, e, f, g, dd, ee, ff, gg, u, v : int
        d := y
        e := yy
        ff := xx - 50
        dd := y
        gg := xx - 50
        f := x
        g := xx
        u := xx + 50
        v := xx + 50
        loop
            gg := gg + speed
            u := u - speed
            drawline (ff, d, gg, e, white)
            drawline (u, d, v, e, white)
            delay (10)
            x := xx
            yy := yy - speed
            drawline (x, y, xx, yy, brightgreen)
            drawline (x, y, xx, yy + 50, white)
            if whatdotcolour (xx - speed, yy - speed) = brightgreen then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            if yy < 12 then
                delay (200)
                cls
                put "YOUR FINAL SCORE WAS: ", score
                return
            end if
            Input.KeyDown (chars2)
            if Math.DistancePointLine (h, k, x, y, xx, yy) < 7 then
                score := score + speed
                drawfilloval (h, k, 5, 5, white)
                randint (h, 15, maxx - 15)
                randint (k, 15, maxy - 15)
                loop
                    if whatdotcolour (h + 6, k + 6) not= white then
                        randint (h, 15, maxx - 15)
                        randint (k, 15, maxy - 15)
                    else
                        exit
                    end if
                end loop
                drawfilloval (h, k, 5, 5, brightred)
            end if
            if chars2 (KEY_RIGHT_ARROW) or chars2 (KEY_LEFT_ARROW) then
                exit
            end if
        end loop
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Wed Jan 05, 2005 7:22 pm   Post subject: (No subject)

I'm telling you, learning to use arrays will make your life easier.
I find people are often afraid of learning new things. It's common, but not really understandable. Delve in! There will be plenty of assistance for you when you have your questions Smile
xHoly-Divinity




PostPosted: Wed Jan 05, 2005 7:54 pm   Post subject: (No subject)

THat's not the problem, it's that I really dun want to write a brand new code Confused ! I think that it is possisble to write what I'm trying to accomplish using this code, so if you can spare me, then please do.
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: