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

Username:   Password: 
 RegisterRegister   
 moving an animation with keyboard
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Mon Apr 28, 2008 6:16 pm   Post subject: moving an animation with keyboard

I am trying to write a simple program for my self-learning where there is a pacman (Very original, no?) whose mouth opens and closes while you move him about the screen.
However, currently, he does not move. I know why, just, not how to fix it. Note-This is my first time trying out procedures, so no things like "Dude, that's not even close to how procedures work" or any similar things.
And without further ado, Code!

Turing:

var x : int := 100
var y : int := 100
var chars : array char of boolean


procedure pacman
    var anglex : int := 135
    var angley : int := 225
    loop


        loop
            exit when anglex = 180
            Draw.FillOval (x, y, 10, 10, yellow)
            Draw.FillArc (x, y, 10, 10, anglex, angley, white)
            anglex := anglex + 1
            angley := angley - 1
            delay (20)
        end loop
        loop
            exit when anglex = 135 and angley = 225
            Draw.FillOval (x, y, 10, 10, yellow)
            Draw.FillArc (y, x, 10, 10, anglex, angley, white)
            anglex := anglex - 1
            angley := angley + 1
            delay (20)
        end loop
    end loop
end pacman


loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and y < 390 then
        y := y + 5
    end if
    if chars (KEY_DOWN_ARROW) and y > 10 then
        y := y - 5
    end if
    if chars (KEY_RIGHT_ARROW) and x < 630 then
        x := x + 5
    end if
    if chars (KEY_LEFT_ARROW) and x > 10 then
        x := x - 5
    end if
    delay (1)
    cls
    pacman
end loop
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Mon Apr 28, 2008 6:23 pm   Post subject: RE:moving an animation with keyboard

Look carefully at your program flow....
Insectoid




PostPosted: Mon Apr 28, 2008 6:28 pm   Post subject: RE:moving an animation with keyboard

Yes, I know it will loop pacman indefinitley. I don't know how to 'fork' a procedure (The whole 'this for 1 frame, that for the next frame' thing) so, to enhance my learning, please explain how this works.
CodeMonkey2000




PostPosted: Mon Apr 28, 2008 6:48 pm   Post subject: RE:moving an animation with keyboard

You shouldn't have all those nested loops for the animation, since in those loops you aren't checking for input.
Insectoid




PostPosted: Mon Apr 28, 2008 6:56 pm   Post subject: RE:moving an animation with keyboard

Well, to make the animation work, I have to!
But I see where your going with this and have an Idea about how to maybe make it work...
Insectoid




PostPosted: Mon Apr 28, 2008 6:57 pm   Post subject: RE:moving an animation with keyboard

wait, never mind! It won't work
CodeMonkey2000




PostPosted: Mon Apr 28, 2008 7:40 pm   Post subject: RE:moving an animation with keyboard

Sigh...you should be doing frame by frame animation. remember what pacman looks like at each frame.
code:

var x : int := 100
var y : int := 100
var chars : array char of boolean
var anglex : int := 135
var angley : int := 225
var tmp : boolean := true
procedure pacman
    if tmp then
        %exit when anglex = 180
        Draw.FillOval (x, y, 10, 10, yellow)
        Draw.FillArc (x, y, 10, 10, anglex, angley, white)
        anglex := anglex + 1
        angley := angley - 1
        delay (20)
        if anglex = 180 then
            tmp := false
        end if
    else
        %exit when anglex = 135 and angley = 225
        Draw.FillOval (x, y, 10, 10, yellow)
        Draw.FillArc (y, x, 10, 10, anglex, angley, white)
        anglex := anglex - 1
        angley := angley + 1
        delay (20)
        if anglex = 135 and angley = 225 then
            tmp := true
        end if
    end if
end pacman


loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and y < 390 then
        y := y + 5
    end if
    if chars (KEY_DOWN_ARROW) and y > 10 then
        y := y - 5
    end if
    if chars (KEY_RIGHT_ARROW) and x < 630 then
        x := x + 5
    end if
    if chars (KEY_LEFT_ARROW) and x > 10 then
        x := x - 5
    end if
    delay (1)
    cls
    pacman
end loop
Insectoid




PostPosted: Mon Apr 28, 2008 7:59 pm   Post subject: RE:moving an animation with keyboard

Yes, well, We haven't been taught boolean yet, and by the way my teacher's going, I'll have to find out for myself.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Apr 28, 2008 8:05 pm   Post subject: RE:moving an animation with keyboard

Found a glitch in your code. After you move Mr. Pacman, The mouth doesn't open smoothly anymore. it's instant. It closes fine, though.
CodeMonkey2000




PostPosted: Mon Apr 28, 2008 8:13 pm   Post subject: RE:moving an animation with keyboard

Meh I don't really care Razz. And you should be using pictures anyway, it's much easier and it looks nicer. I think it might be faster too, but I'm not sure. And there's not much to know about boolean types. It just holds true/false. If you want you can just use an integer and use 0 for false and 1 for true.
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  [ 10 Posts ]
Jump to:   


Style:  
Search: