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 Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SuperGenius




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

fork is used to call a process. It works the same way as a procedure except it will run concurrently with the code below the fork statement. Be careful about process that use global variables though, because it can have problems.
Sponsor
Sponsor
Sponsor
sponsor
white_dragon




PostPosted: Sun May 16, 2004 5:00 pm   Post subject: (No subject)

just make a procedure then after ur done say

[code]
proc lose
*stuff u wanna put in here to say "u lose" n such*
end lose

fork lose
[/code]


BTW, who r u in WCI?
omni




PostPosted: Sun May 16, 2004 5:05 pm   Post subject: (No subject)

its is used to call a process. Processes run alongside with your program
white_dragon




PostPosted: Sun May 16, 2004 5:07 pm   Post subject: (No subject)

but there's two kinds.....well sorta..... there's procedures and processes.

their really different

btw, how do i put a code in cause it never works when i do it
SuperGenius




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

white_dragon wrote:
just make a procedure then after ur done say

code:

proc lose
*stuff u wanna put in here to say "u lose" n such*
end lose

fork lose




dont do this cause it's wrong. Procedures are called by a single word, which is the name of the procedure.

When a procedure is called the stack that called it will pause while the procedure is executing, and then when the procedure is finished the origional stack that called it will resume.

A process is called by saying "fork nameofprocess" When a stack calls a process it will keep going WHILE the process executes at the same time.

To accomplish this it would be best to use a procedure because if the game is over there is not much for the computer to do still, so this is what it should look like:
code:

proc lose
*stuff u wanna put in here to say "u lose" n such*
end lose
if loosecondition = true then
lose
end if
end lose
LiquidDragon




PostPosted: Sun May 16, 2004 6:08 pm   Post subject: (No subject)

well all that forkin' fork stuff is forkin' confusing the fork outta me Thinking
LiquidDragon




PostPosted: Mon May 17, 2004 2:53 pm   Post subject: (No subject)

Well i have run into another problem. I am trying to put my oranges onto a grid but it is not working.

this is the method im using to draw the orange on a grid.

code:

setscreen ("graphics:640;480")

var snake_length : int

snake_length := 4

proc orange_grid

    var orange_x_ar, orange_y_ar : array 1 .. 19 of int
    var count : int
    count := 0

    for pick_x : 139 .. 499 by 20
        count += 1
        orange_x_ar (count) := pick_x
    end for

    count := 0

    for pick_y : 59 .. 419 by 20
        count += 1
        orange_y_ar (count) := pick_y
    end for

end orange_grid

proc snake_up (var x, y : int)

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

        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)
        drawfilloval (x, y - 20, 10, 10, 12) %Snake following
    end if

end snake_up

proc snake_down (var x, y : int)

    if whatdotcolor (x, y - 20) not= 7 then
        y -= 20

        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)
        drawfilloval (x, y + 20, 10, 10, 12) %Snake following
    end if

end snake_down

proc snake_left (var x, y : int)

    if whatdotcolor (x - 20, y) not= 7 then
        x -= 20

        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)
        drawfilloval (x + 20, y, 10, 10, 12) %Snake following
    end if

end snake_left

proc snake_right (var x, y : int)

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

        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)
        drawfilloval (x - 20, y, 10, 10, 12) %Snake following
    end if

end snake_right


proc draw_orange (var x, y : int)

    var orange_x, orange_y : int
    var count : int

    orange_grid

    randint (orange_x, 1, 19)
    randint (orange_y, 1, 19)

    %draws orange
    drawfilloval (orange_x_ar (orange_x), orange_y_ar (orange_y), 10, 10, 41)


end draw_orange

proc orange_hit (var orange_x, orange_y, x, y : int)

    orange_grid

    if orange_x_ar (2) = x and orange_y_ar (2) = y then     %if snake gets orange
        put "SCORE"
        draw_orange (x, y)
    end if

end orange_hit


%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 orange_x, orange_y : int
var key : string (1)
var move : string
var count : int
var erase_x, erase_y : array 1 .. 1000 of int

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

count := 0
move := "NONE"

draw_orange (x, y)

loop

    getch (key)

    loop

        count += 1

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

        if key = KEY_UP_ARROW then

            if move not= "DOWN" then
                snake_up (x, y)
                move := "UP"
            elsif move = "DOWN" then
                snake_down (x, y)
            end if

        elsif key = KEY_DOWN_ARROW then

            if move not= "UP" then
                snake_down (x, y)
                move := "DOWN"
            elsif move = "UP" then
                snake_up (x, y)
            end if

        elsif key = KEY_LEFT_ARROW then

            if move not= "RIGHT" then
                snake_left (x, y)
                move := "LEFT"
            elsif move = "RIGHT" then
                snake_right (x, y)
            end if

        elsif key = KEY_RIGHT_ARROW then

            if move not= "LEFT" then
                snake_right (x, y)
                move := "RIGHT"
            elsif move = "LEFT" then
                snake_left (x, y)
            end if

        end if

        delay (150)

        if count >= snake_length then
            drawfilloval (erase_x (count - snake_length + 1), erase_y (count - snake_length + 1), 10, 10, 0)
        end if

        orange_hit (orange_x, orange_y, x, y)

        exit when hasch

    end loop

end loop


EDIT: I fixed the orange grid drawing but now it has problems checking if it hit the orange. My code is really messed up right now so dont mind some of it but if you could plze help me fix it i will give you some bits Smile
SuperGenius




PostPosted: Mon May 17, 2004 3:50 pm   Post subject: (No subject)

I'm a little confused as to exactly what you're asking, but anyways...

This was a point to consider during the making of my connect 4 game. The simplest way to keep track of the board was to have a 2d array. This effectively turns the board into a cartesian plane. So to take a set of coordinates you need to do something like this.(This is code from my connect 4)

code:

proc drawboard
    for h : 1 .. 8
        for c : 1 .. 8
            if board (h, c) not= " " then
                if board (h, c) = "RED" then
                    Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, red)
                elsif board (h, c) = "GREEN" then
                    Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, green)
                end if
            end if
        end for
    end for
end drawboard


You need to have a base number (in my case 130) which is the bottom of the grid minus 1/2 the size of the grid square. (if you are drawing circles because you need to give the x,y of the center) and then you add to the base the product of the x or y value on the cartesian plane times the width of each grid square.
Sponsor
Sponsor
Sponsor
sponsor
LiquidDragon




PostPosted: Mon May 17, 2004 5:27 pm   Post subject: (No subject)

So ar you trying to say that i should move my snake on an array instead of just drawing them?

What i was asking was to help me with drawing the orange and checking if the snake runs over it. In that code i devised a way to draw the orange but now its just a case of only drawing it once and checking if the snake runs over it.

EDIT: LOL!! looks like i solved my own problem again Embarassed Now i only need to check if the snake has run into itself. I'm also going to try to shorten my main program.
SuperGenius




PostPosted: Mon May 17, 2004 7:26 pm   Post subject: (No subject)

oh dear. I seem to have gotten confused and posted in the wrong thread. Disregard that please. If any of you mods want to delete that post go ahead.
LiquidDragon




PostPosted: Tue May 18, 2004 2:28 pm   Post subject: (No subject)

Ok so now the oranges are working but i can get it to check if my head hits the snake

EDIT: OK i got it to check if the snake hits itself. But i cant figure out anything else to add. Anyone got some ideas? So far im gonna have levels that have different speeds. I may add a wall in the middle for one level. But i still need a few more ideas.
LiquidDragon




PostPosted: Thu May 20, 2004 2:46 pm   Post subject: (No subject)

Ok so things are coming along nicely but now im stuck again. Ok let me explain. I have that sword thing on the side and as your score increases the sword thing will fill up. When the score reaches 500 I want to sword to be completely coloured. Everytime the score increases i also want to colour to increment up the sword.
LiquidDragon




PostPosted: Thu May 20, 2004 2:49 pm   Post subject: (No subject)

Heres the pictures


lives.jpg
 Description:
 Filesize:  14.29 KB
 Viewed:  1969 Time(s)

lives.jpg



snake_back.jpg
 Description:
 Filesize:  168.24 KB
 Viewed:  1969 Time(s)

snake_back.jpg


LiquidDragon




PostPosted: Mon May 24, 2004 6:15 pm   Post subject: (No subject)

YAY i'm pretty much done my program now Dance I just need to add some losing screens and instructions and extra stuff

You can take a look if you like. I'll repost it when its completely finished. I'm just posting it now so you can have a quick peek before its completely finished. The pictures are posted above



high_score.txt
 Description:
the high score text file

Download
 Filename:  high_score.txt
 Filesize:  113 Bytes
 Downloaded:  119 Time(s)


snake.t
 Description:

Download
 Filename:  snake.t
 Filesize:  8.77 KB
 Downloaded:  133 Time(s)

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 2 of 2  [ 29 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: