Computer Science Canada

Snake program

Author:  white_dragon [ Sat Feb 07, 2004 5:13 pm ]
Post subject:  Snake program

tis is my snake program but i dunno how to make it so than 2 ppl can press a key at the same time and the snake moves. also, i dunno how i make it so when a person collides with the other person, it stops. can someone help me? Confused . i am hopelessly confused. i only spent about 3 months in turing............

Author:  Andy [ Sat Feb 07, 2004 5:14 pm ]
Post subject: 

read up on Input.Keydown

Author:  white_dragon [ Sat Feb 07, 2004 5:18 pm ]
Post subject: 

tks. but i still dunno how 2 put it back into da program

Author:  santabruzer [ Sat Feb 07, 2004 5:37 pm ]
Post subject: 

... enjoy:

code:
setscreen ("graphics:400;400,nocursor")
var chars : array char of boolean
var cellsize := 20
var ch : string (1)
var x, y := 3
var leftright, updown : int := 0 % 1 for first word, 2 for second, 0 for nothing
var lsnake := 3
var count := 1

proc drawboard
    drawbox (0, 0, maxx, maxy, 7)
    for i : 1 .. 20
        drawline (i * 20, 0, i * 20, maxy, 7)
        drawline (0, i * 20, maxx, i * 20, 7)
    end for
end drawboard

proc drawcell (row, col, clr : int)
    var temprow := row - 1
    var tempcol := col - 1
    drawfillbox (temprow * cellsize, tempcol * cellsize, row * cellsize, col * cellsize, clr)
end drawcell

loop

    Input.KeyDown (chars)
    delay (50)
    if chars (KEY_UP_ARROW) then
        y += 1
    elsif chars (KEY_DOWN_ARROW) then
        y -= 1
    elsif chars (KEY_LEFT_ARROW) then
        x -= 1
    elsif chars (KEY_RIGHT_ARROW) then
        x += 1
    end if
        drawcell (x, y, 7)
    drawboard
    if count = lsnake then
        count := 1
    end if
end loop

Author:  Paul [ Sat Feb 07, 2004 5:39 pm ]
Post subject: 

This is the basic movement with Input.KeyDown
code:

%Snake game
setscreen ("nocursor")
setscreen ("noecho")
var chars : array char of boolean
var x, y : int := 100
var d, e : int := 150
var a : string (1)
var b : string (1)
loop
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        x := x + 2
elsif chars (KEY_LEFT_ARROW) then
        x := x - 2
elsif chars (KEY_UP_ARROW) then
        y := y + 2
elsif chars (KEY_DOWN_ARROW) then
        y := y - 2
end if
    delay (30)
    drawfilloval (x, y, 2, 2, 50)

end loop

Edit: Darn it, santabruzer is faster again... I was experimenting with erasing its tail after its gone a bit.

Author:  white_dragon [ Sat Feb 07, 2004 5:39 pm ]
Post subject: 

lol tks. i got some lines from ur program and got da idea. but i still don't how 2 make some borders so it doesn't go past it.

Author:  santabruzer [ Sat Feb 07, 2004 5:43 pm ]
Post subject: 

the hardest thing in this program.. was the fact that you have to keep the snake size.. i was thinking of using a flexible array, when i attacked the challenge.. and then i got bored.. was i on the right path?

Author:  Paul [ Sat Feb 07, 2004 5:45 pm ]
Post subject: 

Hehe, I have no idea, I thought about storing the co-ordinates in an array or something, then when the snake went a certain distance, you'd draw white dots at those previous coordinates, but my idea is probably stupid.

Author:  Cervantes [ Sat Feb 07, 2004 5:52 pm ]
Post subject: 

do it the whatdotcolour way Smile

in the Input.KeyDown part, whenever an arrow key is hit, a counter goes up. you'd need an array to hold the x and y values of the snake's body circles thingys. then when the counter = 10 or whatever, make the counter go down by 1 (so it = 9) and draw a white dot over the last x and the last y in the array.

for collisions, use whatdotcolour Smile

Author:  white_dragon [ Sat Feb 07, 2004 6:02 pm ]
Post subject: 

thank u all sincerely for your help! i come here very often 2 look for help n i always get it!!!!!!!!!!1 Very Happy Very Happy Very Happy Very Happy u guys are all so nice. i'm only 11 years old n my class mates n teacher hate me. i guess tat's how life is.......... *sigh* tks for all the help

Author:  recneps [ Sun Feb 08, 2004 4:08 pm ]
Post subject: 

you can take turing at 11 yrs old?! and for the keydow with more than one person, you gotta put each if statement separately (if blah
end if
if blah
end if)

Author:  the_short1 [ Sun Feb 08, 2004 6:07 pm ]
Post subject: 

nice... Post as a .exe when its done... it willl be a hit with my sis.. shes addicted to that Game.....

Author:  white_dragon [ Mon Feb 09, 2004 4:37 pm ]
Post subject: 

yeah. i did skip tat many grades. lol

Author:  the_short1 [ Mon Feb 09, 2004 5:03 pm ]
Post subject: 

crazy.... i think it would kind of suck to be that young in high school.... everyone would think u a weak little sh!t cuz ur younger and start beating you up....... but then u got braging rights to be smarter then people that are like 4 years older then you.. hahhahah....

Anyway.... it would be kinda cool if you made you steps in incriments

Maybe integrate into this....
I think it is a Wicked Grid program and the movement rocks....
Also a idea....<<ask the user to input player 1 Color,name, and player 2 name and color... then when you crash you can say who lost... and to colorfy it too....Have FUn.,.,.,.

Author:  Paul [ Mon Feb 09, 2004 5:04 pm ]
Post subject: 

Ah, my eyes, you can't look at that program for too long, the lines are too close together. And it says battleship Razz.

Author:  the_short1 [ Fri Feb 13, 2004 5:18 pm ]
Post subject: 

i got it off compsci while back,.,...i like it cuz of smooth movement...

i forget who made it....


: