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

Username:   Password: 
 RegisterRegister   
 Start Of Game - Help With Controls,Movement
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2, 3
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RedRogueXIII




PostPosted: Mon Jan 23, 2006 8:51 pm   Post subject: (No subject)

Well i can say this, your posts made me want to do snake, so here an example of what i did,
code:
View.Set ("graphics:550,400, offscreenonly, nobuttonbar, title:Snake")
var x, y, dir, font, score, foodx, foody, speed, speedfactor, bodycount : int
var chars : array char of boolean
var moving, pauser, gameOver, setfood : boolean
var bodyx, bodyy : flexible array 0 .. 0 of int
x := 28
y := 18
dir := 1
score := 0
moving := false
pauser := false
gameOver := false
setfood := false
speed := 0
speedfactor := 10
bodycount := 0
font := Font.New ("Impact:14")
% lets say 1 in terms of direction = up
proc drawgrid
    % Draw a 10x10 grid background
    for i : 1 .. maxx by 10
        for j : 1 .. maxy by 10
            drawbox (i - 1, j - 1, i + 9, j + 9, black)
        end for
    end for
    drawfillbox (0, maxy - 49, maxx, maxy, grey)
end drawgrid

proc controls
    % This is the main game loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and dir not= 2 then
        dir := 1
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_DOWN_ARROW) and dir not= 1 then
        dir := 2
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_LEFT_ARROW) and dir not= 4 then
        dir := 3
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_RIGHT_ARROW) and dir not= 3 then
        dir := 4
        if moving = false then
            moving := true
        end if
    end if
    if chars (KEY_ENTER) then
        if pauser = false then
            pauser := true
        elsif pauser = true then
            pauser := false
        end if
    end if

end controls

proc move
    % controls should go first and then check if moving has begun then
    % Snake Shifting code here
    if bodycount > 0 then
        bodyx (1) := x
        bodyy (1) := y
        if bodycount > 1 then
            for decreasing i : bodycount .. 2
                bodyx (i) := bodyx (i - 1)
                bodyy (i) := bodyy (i - 1)
            end for
        end if
    end if


    if moving = true then
        speed += speedfactor
        if speed mod 10 = 0 then
            if dir = 1 then
                y += 1
            elsif dir = 2 then
                y -= 1
            elsif dir = 3 then
                x -= 1
            elsif dir = 4 then
                x += 1
            end if
            speed := 0

        end if
    end if
end move

proc ohnoes
    % Handles Game Over/ Restart
    if x <= 0 or x * 10 >= maxx or y <= 0 or y * 10 >= maxy - 50 then
        x := 28
        y := 18
        moving := false
        %pauser := true
        gameOver := true
        score := 0
        dir := 0
        bodycount := 0
        new bodyx, bodycount
        new bodyy, bodycount
    end if
end ohnoes

proc otherside
    if x < 0 then
        x := maxx div 10
    end if
    if x * 10 >= maxx then
        x := 0
    end if
    if y < 0 then
        y := maxy div 10 - 5
    end if
    if y * 10 >= maxy - 50 then
        y := 0
    end if
end otherside

proc HUD
    Font.Draw ("SCORE :" + intstr (score), 20, maxy - 40, font, black)
    if pauser = true then
        Font.Draw ("PAUSED", maxx div 2 - 100, maxy - 40, font, red)
    end if
end HUD

proc food
    % Food Code
    if setfood = false then
        setfood := true
        randint (foodx, 1, maxx div 10)
        randint (foody, 1, maxy div 10 - 5)
    end if
    drawfilloval (foodx * 10 + 5, foody * 10 + 5, 5, 5, red)
    if x = foodx and y = foody then
        bodycount += 1
        new bodyx, bodycount
        new bodyy, bodycount
        score += 1
        setfood := false
    end if
end food





loop
otherside
    %ohnoes
    drawgrid
    HUD
    controls

    food
    if pauser = false then
        move
    end if

    locate (3, 18)
    put x, " ", y, " = ", foodx, " ", foody, " ", bodycount, " ", upper (bodyx), " " ..
    View.Update
    cls
    delay (5)
    drawfillbox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, 1)
    for decreasing i : bodycount .. 1
        drawfillbox (bodyx (i) * 10, bodyy (i) * 10, bodyx (i) * 10 + 10, bodyy (i) * 10 + 10, i)
    end for
    delay(10)
end loop

basically
you need an x,y, and direction variable which can all be integers. then a flexible array or just a huge array thats predetermined to store the body x and y. Also the food x and y. So using a main loop your code should be like this.

loop

keyboardInput % not actually moves but changes the direction variable

move%takes the direction and then moves 1 to that direction

if snake x, y = food x,y then
+ 1 to the flexible snake x and y array.

for decreasing loop
*for decreasing i:upper(snakex)..1
snakex(i):=snakex(i-1)
%Y-Coordinates can be in the same for loop
end for*

then check to see if the snake hit a wall or if it hit itself

*for i:1..upper(snakex)
if x(the front dot) = snakex(i) (one of the snake body)
end for

end loop

Remeber your not trying to make the whole game execute in only one run of the loop but to make it repeat small continous actions over and over.

learn from my snake example, - [you cant die because i was to lazy to keep on restarting to playtest. Razz]
Sponsor
Sponsor
Sponsor
sponsor
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 3 of 3  [ 31 Posts ]
Goto page Previous  1, 2, 3
Jump to:   


Style:  
Search: