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

Username:   Password: 
 RegisterRegister   
 Snake Game
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
LiquidDragon




PostPosted: Fri May 14, 2004 5:38 pm   Post subject: Snake Game

Well it seems to be alomst the end of the year and our teachers are beginning to assign the end of the year projects. For mine i have decided to make a snake game Dance. My teacher was suprised that i wanted to take the "challenge" although i dont find it to challenging myself. Just difficult to increase the length of the snake.

If you have been assigned your end of the year project feel free to post it or tell me what its about because i would like to no.

I will also update you on my progress for my snake game, but it will be pretty basic because of the basic knowledge our teacher taught us.
Sponsor
Sponsor
Sponsor
sponsor
LiquidDragon




PostPosted: Fri May 14, 2004 6:20 pm   Post subject: (No subject)

Ok i have some problems so far. i need to get my oranges (the things the snake collects) to be on some sort of grid so the snake will get it in line

Here is my code:

code:

setscreen ("graphics:640;480")

proc snake_up (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y - 30, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_up

proc snake_down (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y + 30, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_down

proc snake_left (var x, y : int)

    drawfillbox (x, y - 10, x + 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_left

proc snake_right (var x, y : int)

    drawfillbox (x, y - 10, x - 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_right

%boundaries
drawfillbox (115, 40, 128, 440, 7) %left wall
drawfillbox (120, 430, 520, 440, 7) %top wall
drawfillbox (511, 40, 520, 440, 7) %right wall
drawfillbox (114, 35, 520, 48, 7) %bottom wall

var x, y : int
var key : string (1)
var orange_x, orange_y : int

x := maxx div 2
y := maxy div 2

randint (orange_x, 150, 510)
randint (orange_y, 50, 430)

loop
    getch (key)

    %drawing the orange
    drawfilloval (orange_x, orange_y, 10, 10, 8)

    if key = KEY_UP_ARROW and whatdotcolor (x, y + 20) not= 7 then
        y += 20
        snake_up (x, y)
    elsif key = KEY_DOWN_ARROW and whatdotcolor (x, y - 20) not= 7 then
        y -= 20
        snake_down (x, y)
    elsif key = KEY_LEFT_ARROW and whatdotcolor (x - 20, y) not= 7 then
        x -= 20
        snake_left (x, y)
    elsif key = KEY_RIGHT_ARROW and whatdotcolor (x + 20, y) not= 7 then
        x += 20
        snake_right (x, y)
    end if
end loop
gamer




PostPosted: Fri May 14, 2004 7:23 pm   Post subject: (No subject)

dont use getch , use Input.KeyDown instead
Tony




PostPosted: Fri May 14, 2004 9:56 pm   Post subject: (No subject)

first of all this belongs in [turing help]

second - maybe you should take a look at an existing snake. Maybe you'll get some ideas for variables setup and types of procedures needed.

Don't copy though, cuz that would be apparent
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
LiquidDragon




PostPosted: Sat May 15, 2004 8:30 am   Post subject: (No subject)

see the problem is i cant use Input.KeyDown becuase we have never learned that in class, so my teacher will get angry. Although i dont understand why Hit Wall She also said that if she finds something we havent learned it will be considered "cheating" and can't have cheating Naughty. So i have to keep it simple.

Well... i can add Input.KeyDown if someone explained how it worked and then i could explain to the teacher Smile
MyPistolsIn3D




PostPosted: Sat May 15, 2004 12:48 pm   Post subject: (No subject)

Tpye Input.KeyDown in a turing window and press F9.
white_dragon




PostPosted: Sat May 15, 2004 4:27 pm   Post subject: (No subject)

it basically lets u use more than one key at a time. though i think there is a limit to how many keys u can use.
LiquidDragon




PostPosted: Sat May 15, 2004 6:17 pm   Post subject: (No subject)

Ok well im gonna stick with the getch because otherwise it gets all weird. So now i have two problems

1. When my snake eats an orange he increases in length. I have tried many things for this but haven't figured it out.

2. getting the orange to be on the same grid as the snake. Right now the snake isnt technically on a grid it just looks like it because i set the x and y increases to 20.

Here is what i have so far
code:

setscreen ("graphics:640;480")

var snake_length : int

snake_length := 1

proc snake_up (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y - 30, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_up

proc snake_down (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y + 30, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_down

proc snake_left (var x, y : int)

    drawfillbox (x, y - 10, x + 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_left

proc snake_right (var x, y : int)

    drawfillbox (x, y - 10, x - 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)

end snake_right

%boundaries
drawfillbox (115, 40, 128, 440, 7) %left wall
drawfillbox (120, 430, 520, 440, 7) %top wall
drawfillbox (511, 40, 520, 440, 7) %right wall
drawfillbox (114, 35, 520, 48, 7) %bottom wall

var x, y : int
var key : string (1)
var orange_x, orange_y : int

x := maxx div 2
y := maxy div 2

randint (orange_x, 150, 510)
randint (orange_y, 50, 430)

loop
    getch (key)

    %drawing the orange
    drawfilloval (orange_x, orange_y, 10, 10, 9)

    if key = KEY_UP_ARROW and whatdotcolor (x, y + 20) not= 7 then
        y += 20
        snake_up (x, y)
    elsif key = KEY_DOWN_ARROW and whatdotcolor (x, y - 20) not= 7 then
        y -= 20
        snake_down (x, y)
    elsif key = KEY_LEFT_ARROW and whatdotcolor (x - 20, y) not= 7 then
        x -= 20
        snake_left (x, y)
    elsif key = KEY_RIGHT_ARROW and whatdotcolor (x + 20, y) not= 7 then
        x += 20
        snake_right (x, y)
    end if
end loop
SuperGenius




PostPosted: Sat May 15, 2004 9:56 pm   Post subject: (No subject)

If you are making a snake game you really need to have at least a few shapes in a 'train' to start with. In the real snake, the head is controlled by the user and each trailing piece will turn in the same direction at the same location that the head did. Also, if you do this you're going to have to add collision detection to see if the snake has run over itself.
LiquidDragon




PostPosted: Sun May 16, 2004 9:33 am   Post subject: (No subject)

Yes i am still working on all that but one small bit at a time.
LiquidDragon




PostPosted: Sun May 16, 2004 10:31 am   Post subject: (No subject)

Ok so i got the snake to follow me but now it tends to leave ovals behind when i switch directions. Could someone help me out with that and maybe help me with Input.KeyDown like give an exmaple in my program.



code:

setscreen ("graphics:640;480")

var snake_length : int

snake_length := 4

proc snake_up (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y - 30, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x - 4, y + 4, 2, 2, green)
    drawfilloval (x + 4, y + 4, 2, 2, green)

end snake_up

proc snake_up_f (var x, y : int)

    if whatdotcolor (x, y - (20 * snake_length)) not= 7 then
        drawfilloval (x, y - (20 * snake_length), 10, 10, 0)
    end if
    drawfilloval (x, y - 20, 10, 10, 12)

end snake_up_f

proc snake_down (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y + 30, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x - 4, y - 4, 2, 2, green)
    drawfilloval (x + 4, y - 4, 2, 2, green)

end snake_down

proc snake_down_f (var x, y : int)

    if whatdotcolor (x, y + (20 * snake_length)) not= 7 then
        drawfilloval (x, y + (20 * snake_length), 10, 10, 0)
    end if
    drawfilloval (x, y + 20, 10, 10, 12)

end snake_down_f

proc snake_left (var x, y : int)

    drawfillbox (x, y - 10, x + 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x - 4, y + 4, 2, 2, green)
    drawfilloval (x - 4, y - 4, 2, 2, green)

end snake_left

proc snake_left_f (var x, y : int)

    if whatdotcolor (x + (20 * snake_length), y) not= 7 then
        drawfilloval (x + (20 * snake_length), y, 10, 10, 0)
    end if
    drawfilloval (x + 20, y, 10, 10, 12)

end snake_left_f

proc snake_right (var x, y : int)

    drawfillbox (x, y - 10, x - 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x + 4, y + 4, 2, 2, green)
    drawfilloval (x + 4, y - 4, 2, 2, green)

end snake_right

proc snake_right_f (var x, y : int)

    if whatdotcolor (x - (20 * snake_length), y) not= 7 then
        drawfilloval (x - (20 * snake_length), y, 10, 10, 0)
    end if
    drawfilloval (x - 20, y, 10, 10, 12)

end snake_right_f


%boundaries
drawfillbox (115, 40, 128, 440, 7)     %left wall
drawfillbox (120, 430, 520, 440, 7)     %top wall
drawfillbox (511, 40, 520, 440, 7)     %right wall
drawfillbox (114, 35, 520, 48, 7)     %bottom wall

var x, y : int
var key : string (1)
var orange_x, orange_y : int

x := maxx div 2
y := maxy div 2

randint (orange_x, 150, 510)
randint (orange_y, 50, 430)

loop
    getch (key)

    %drawing the orange
    drawfilloval (orange_x, orange_y, 10, 10, 9)

    if key = KEY_UP_ARROW and whatdotcolor (x, y + 20) not= 7 then
        y += 20
        snake_up (x, y)
        for i : 1 .. snake_length
            snake_up_f (x, y)
        end for
    elsif key = KEY_DOWN_ARROW and whatdotcolor (x, y - 20) not= 7 then
        y -= 20
        snake_down (x, y)
        for i : 1 .. snake_length
            snake_down_f (x, y)
        end for
    elsif key = KEY_LEFT_ARROW and whatdotcolor (x - 20, y) not= 7 then
        x -= 20
        snake_left (x, y)
        for i : 1 .. snake_length
            snake_left_f (x, y)
        end for
    elsif key = KEY_RIGHT_ARROW and whatdotcolor (x + 20, y) not= 7 then
        x += 20
        snake_right (x, y)
        for i : 1 .. snake_length
            snake_right_f (x, y)
        end for
    end if
end loop
LiquidDragon




PostPosted: Sun May 16, 2004 3:18 pm   Post subject: (No subject)

Come on i really need help on this. I just need to know how to make the circles follow behind. I can do it but as soon as i change directions it messes up.
LiquidDragon




PostPosted: Sun May 16, 2004 3:38 pm   Post subject: (No subject)

YAY!!!! I finally got it to work. Now its time for collision detection and stuff. I'm so happy i finally got it Dance Dance Dance Dance

Look how i got it to work

code:

setscreen ("graphics:640;480")

var snake_length : int

snake_length := 4

proc snake_up (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y - 30, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x - 4, y + 4, 2, 2, green)
    drawfilloval (x + 4, y + 4, 2, 2, green)

end snake_up

proc snake_up_f (var x, y : int)

    drawfilloval (x, y - 20, 10, 10, 12)

end snake_up_f

proc snake_down (var x, y : int)

    drawfillbox (x - 10, y, x + 10, y + 30, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x - 4, y - 4, 2, 2, green)
    drawfilloval (x + 4, y - 4, 2, 2, green)

end snake_down

proc snake_down_f (var x, y : int)

    drawfilloval (x, y + 20, 10, 10, 12)

end snake_down_f

proc snake_left (var x, y : int)

    drawfillbox (x, y - 10, x + 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x - 4, y + 4, 2, 2, green)
    drawfilloval (x - 4, y - 4, 2, 2, green)

end snake_left

proc snake_left_f (var x, y : int)

    drawfilloval (x + 20, y, 10, 10, 12)

end snake_left_f

proc snake_right (var x, y : int)

    drawfillbox (x, y - 10, x - 30, y + 10, 0)
    drawfilloval (x, y, 10, 10, 12)
    drawfilloval (x + 4, y + 4, 2, 2, green)
    drawfilloval (x + 4, y - 4, 2, 2, green)

end snake_right

proc snake_right_f (var x, y : int)

    drawfilloval (x - 20, y, 10, 10, 12)

end snake_right_f


%boundaries
drawfillbox (115, 40, 128, 440, 7)     %left wall
drawfillbox (120, 430, 520, 440, 7)     %top wall
drawfillbox (511, 40, 520, 440, 7)     %right wall
drawfillbox (114, 35, 520, 48, 7)     %bottom wall

var x, y : int
var key : string (1)
var count : int
var orange_x, orange_y : int
var erase_x, erase_y : array 1 .. 1000 of int

x := maxx div 2
y := maxy div 2

count := 0

randint (orange_x, 150, 510)
randint (orange_y, 50, 430)

loop
    getch (key)

    count += 1

    erase_x (count) := x
    erase_y (count) := y

    %drawing the orange
    drawfilloval (orange_x, orange_y, 10, 10, 9)

    if key = KEY_UP_ARROW and whatdotcolor (x, y + 20) not= 7 then

        y += 20
        snake_up (x, y)
        snake_up_f (x, y)

    elsif key = KEY_DOWN_ARROW and whatdotcolor (x, y - 20) not= 7 then

        y -= 20
        snake_down (x, y)
        snake_down_f (x, y)

    elsif key = KEY_LEFT_ARROW and whatdotcolor (x - 20, y) not= 7 then

        x -= 20
        snake_left (x, y)
        snake_left_f (x, y)

    elsif key = KEY_RIGHT_ARROW and whatdotcolor (x + 20, y) not= 7 then

        x += 20
        snake_right (x, y)
        snake_right_f (x, y)
    end if

%This erases the last part of the snake :D
    if count >= snake_length then
        drawfilloval (erase_x (count - snake_length + 1), erase_y (count - snake_length + 1), 10, 10, 0)
    end if

end loop


I think some bits are in order Laughing
white_dragon




PostPosted: Sun May 16, 2004 4:54 pm   Post subject: (No subject)

good job! the collision and detection should be really easy. just say that if the snake is beyond or equal to an x or y value then just fork it to a exit statement and say "u suk" lol. jk jk
LiquidDragon




PostPosted: Sun May 16, 2004 4:56 pm   Post subject: (No subject)

LOL! yes that sounds like an excellent thing to say. But what's fork. I've never used that before Question
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 2  [ 29 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: