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

Username:   Password: 
 RegisterRegister   
 Moving two balls at once
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SunsFan13




PostPosted: Sat Apr 05, 2008 8:55 pm   Post subject: Moving two balls at once

Hello there, I've been fighting with this for sometime now, and I've got no idea where I've gone wrong..
Basically, I'm only able to move the green ball (process ball2) using WASD.
I'm supposed to be able to move black ball (process ball1) using the arrow keys AND the green ball at the same time.
I can move the black ball without the green ball code, and I can move the green ball without the black ball code.

ANY help is greatly appreciated

code:
put "Use the arrow keys to move the ball around, press any other key to stop."

delay (2000)
cls  % Clear Screen

process ball1
    setscreen ("graphics")

    var key : string (1)

    var x, y, r : int

    x := maxx div 2 % Start at center
    y := maxy div 2 % Start at center
    r := 10 % Ball radius


    loop

        % Draw circle
        drawfilloval (x, y, r, r, black)

        getch (key)

        % Move Left
        if key = (KEY_LEFT_ARROW) then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x - r

            % Move Right
        elsif key = (KEY_RIGHT_ARROW) then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x + r

            % Move Up
        elsif key = (KEY_UP_ARROW) then
            %elsif ord (key) = 200 then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y + r

            % Move Down
        elsif key = (KEY_DOWN_ARROW) then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y - r

            % If any other key is pressed, clear screen and exit
            %else

            %cls

            %exit


        end if
    end loop
end ball1 %End process ball1

process ball2 %Begin process ball2
    %setscreen ("graphics")

    var key : string (1)

    var x, y, r : int

    x := maxx div 3
    y := maxy div 3
    r := 10 % Ball radius


    loop

        % Draw circle
        drawfilloval (x, y, r, r, green)

        getch (key)

        % Move Left
        if key = ('a') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x - r

            % Move Right
        elsif key = ('d') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x + r

            % Move Up
        elsif key = ('w') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y + r

            % Move Down
        elsif key = ('s') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y - r

            %If any other key is pressed, clear screen and exit
            %else

            %cls

            %exit


        end if
    end loop
end ball2

fork ball1
fork ball2
[/quote]

Thanks in advance.
-Chris
Sponsor
Sponsor
Sponsor
sponsor
andrew.




PostPosted: Sat Apr 05, 2008 9:02 pm   Post subject: Re: Moving two balls at once

Well, first you don't ever want to use fork especially for this kind of thing. The only thing fork should be used for is maybe background music in a game. Second, get a pen and paper and organize your code. Group each set of code in a logical order and put it in one main loop. This is a great way to solve any programming problem. I would give you a little more help but I can't really run the code because I'm on my Linux machine. Just try my suggestion and then post it here or PM it and I'll help you with it.
Tony




PostPosted: Sat Apr 05, 2008 10:12 pm   Post subject: RE:Moving two balls at once

andrew is completely right. To illustrate an example:

Lets say you press KEY_LEFT_ARROW.

getch (key) is called from ball2 (try pick more descriptive names).

The said block of code doesn't know what to do with this particular key.

------

Also, getch reads one character at a time, from the buffer. Which is clearly now what you are trying to do in your program. Look into Input.KeyDown() instead.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
SunsFan13




PostPosted: Sat Apr 05, 2008 10:14 pm   Post subject: Re: Moving two balls at once

Thanks for the advice Andrew, unfortunately "forking it" is how my teacher wants it to be done..
Another kid in the class has his working VERY similar to mine, I think I'm just missing something little, or have something that doesn't need to be there, that kind of thing. The teach wants them organized in procedures, then to use fork in the end to call them.
SunsFan13




PostPosted: Sat Apr 05, 2008 10:53 pm   Post subject: Re: Moving two balls at once

Alright Tony, tried your way

code:
put "Use the arrow keys to move the ball around, press any other key to stop."

delay (2000)
cls  % Clear Screen

process ball1
    setscreen ("graphics")

    var key : array char of boolean

    var x, y, r : int

    x := maxx div 2 % Start at center
    y := maxy div 2 % Start at center
    r := 10 % Ball radius


    loop

        % Draw circle
        drawfilloval (x, y, r, r, black)

        Input.KeyDown (key)
        % Move Left
        if key (KEY_LEFT_ARROW) then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x - r

            % Move Right
        elsif key  (KEY_RIGHT_ARROW) then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x + r

            % Move Up
        elsif key  (KEY_UP_ARROW) then
            %elsif ord (key) = 200 then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y + r

            % Move Down
        elsif key (KEY_DOWN_ARROW) then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y - r

            % If any other key is pressed, clear screen and exit
            %else

            %cls

            %exit


        end if
    end loop
end ball1 %End process ball1

process ball2 %Begin process ball2
    %setscreen ("graphics")

    var key :array char of boolean

    var x, y, r : int

    x := maxx div 3
    y := maxy div 3
    r := 10 % Ball radius


    loop

        % Draw circle
        drawfilloval (x, y, r, r, green)

        Input.KeyDown (key)

        % Move Left
        if key  ('a') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x - r

            % Move Right
        elsif key  ('d') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            x := x + r

            % Move Up
        elsif key  ('w') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y + r

            % Move Down
        elsif key  ('s') then
            drawfilloval (x, y, r, r, white)
            % or use cls
            y := y - r

            %If any other key is pressed, clear screen and exit
            %else

            %cls

            %exit


        end if
    end loop
end ball2

fork ball1
fork ball2


The ball however just disappears, this is probably because I've got it drawing a white circle when I press said key.
How do I get it to redraw its self in the new position?
SunsFan13




PostPosted: Sat Apr 05, 2008 11:36 pm   Post subject: RE:Moving two balls at once

You guys can delete this thread if need be.
I've figured it out.
I needed to add a delay after each key stroke.
Civik




PostPosted: Sun Apr 06, 2008 2:16 pm   Post subject: RE:Moving two balls at once

Why erase it? Someone else might find this useful later.
Prince Pwn




PostPosted: Mon Apr 07, 2008 1:15 pm   Post subject: RE:Moving two balls at once

andrew: Turing and Ready run under emulation in Linux, but the fonts are a bit cut off so you have to mess with them.
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 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: