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

Username:   Password: 
 RegisterRegister   
 Squished up Snake
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RedRogueXIII




PostPosted: Sun Jan 15, 2006 1:52 pm   Post subject: Squished up Snake

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 := 5
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 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
    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
end loop



arrrgh ive got it to work except that the snake shifts to fast so that what should be 8 block long snake appears as a 4 block snake.
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sun Jan 15, 2006 3:12 pm   Post subject: (No subject)

From what I can ellucidate...
Say you have a body length of 4. If your snake moves Right, then you get a set up like this:
code:

_ _ _ o

Where '_' is a body piece, and 'o' is the head. Now, if we turn and move Left:
code:

o _ _

What has happened here is that one of the body pieces has been drawn at the same place as the head. This has to do with the way you're assigning locations, i.e.:
code:

bodyx (i) := bodyx (i - 1)
bodyy (i) := bodyy (i - 1)


Take some time with this and try figure out what you should be doing so that it sets the right amount in whatever direction the snake travels. You may want to consider switching from an absolute x-y coords system to a grid-like system, where a point like (2,3) signifies an actual block as opposed to a point. You have started that already with your drawing commands, but now try expand that to actual data sets.
Confusing? Perhaps. Do-able? Absolutely.
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  [ 2 Posts ]
Jump to:   


Style:  
Search: