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

Username:   Password: 
 RegisterRegister   
 cannonball game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hossack




PostPosted: Tue Feb 17, 2009 10:53 am   Post subject: cannonball game

I need to know how to make my arc go up and then down. This is what i've gotten so far
Turing:

View.Set ("graphic:640;480")

var x, y, angle, power : int

x := 20
y := 5

put "Enter Gun Powder"
get power
put "Enter the arc |Lower = Steep | Higher = Shallower"
get angle
delay (100)
cls


loop
    drawfillbox (580, 0, 480, 20, brightred)
    drawfilloval (x, y, 5, 5, blue)
    delay (10)
    if x = 600 + 20 or x = 600 - 20 and y = 360 - 20 or y = 360 -20 then
    x := x
    y := y
    cls
    locatexy (300,200)
    put "YOU WIN!!!"
    elsif x = maxx or y = maxy then
    locatexy (300,200)
    put "YOU LOSE!!!!"
    end if
    drawfilloval (x, y, 5, 5, 0)
    x := x + power
    y := round (x ** 2 / angle) + 5
end loop
Sponsor
Sponsor
Sponsor
sponsor
Ktomislav




PostPosted: Tue Feb 17, 2009 11:23 am   Post subject: Re: cannonball game

It's simple physics. You just have to set speed in x direction an in y direction and acceleration. And after that you just increase your x coordinate for x speed and y coordinate for y speed and increase your y speed for acceleration. You repeat that until x reaches maxx or y reaches 0.

code:
procedure arc
    var x, y, ay, vx, vy: real;
    ay := -0.2;
    x := 0;
    y := 0;
    vx := 10;
    vy := 10;
    loop
        exit when (x > maxx) or (y < 0);
        Draw.Dot (round(x), round(y), blue);
        x += vx;
        y += vy;
        vy += ay;
        delay (100);
    end loop
end arc
hossack




PostPosted: Tue Feb 17, 2009 8:47 pm   Post subject: Re: cannonball game

We haven't learned Procedures yet. Is there any other way to do it?
DemonWasp




PostPosted: Tue Feb 17, 2009 9:22 pm   Post subject: RE:cannonball game

Learn procedures.

If you want to just run his code, copy everything between "procedure arc" and "end arc" and run that as a Turing program. You should see what he's talking about.

Learn procedures anyway.
hossack




PostPosted: Wed Feb 18, 2009 9:30 am   Post subject: RE:cannonball game

I got it work. Thanks

edit nvm
hossack




PostPosted: Wed Feb 18, 2009 10:06 am   Post subject: Re: cannonball game

How do i get it so like when it hits the target you win/ if you miss you lose? Also how do you get it so the target is the same size each time?

Turing:
var x, y, ay, vx, vy: real;
 var randx, randx2: int
    ay := -0.2;
    x := 0;
    y := 0;
    vx := 7;
    vy := 7;
   
   randint (randx, 125, maxx-125)
   randint (randx2, 125, maxx-125)
   
    drawfillbox (randx, 0, randx2, 20, brightred)
    put "Enter the angle"
    get vy
    put "Enter the speed"
    get vx
    delay(10)
   
   
    loop
        exit when (x > maxx) or (y < 0);
        drawfilloval (round(x), round(y), round(5), round (5), blue);
        delay (10);
        View.Update
        drawfilloval (round(x), round(y), round(5), round (5), white);
        x += vx;
        y += vy;
        vy += ay;
        if x <= randx or x <= randx2 then
        put "YOU WIN"
        elsif y <= 0 then
        put "YOU LOSE"
        end if

    end loop
Ktomislav




PostPosted: Wed Feb 18, 2009 11:18 am   Post subject: Re: cannonball game

You don't have to randomize randx2.
code:
randx2 := randx + 100;


You have to ask if the cannonball is inside the target.
code:
if (x >= randx) and (x <= randx2) and (y >= 0) and (y <= 20) then
   put "You win!"
end if


And when cannonball goes out of screen you lose (when y <=0 or x >= maxx). Write the code yourself.

And I missed it.. You got it wrong..
Angle is not speed. You could write "y direction speed" or something like that instead of "angle" or
you could do something with sin and cos or something like that. Mr. Green
hossack




PostPosted: Thu Feb 19, 2009 9:48 am   Post subject: Re: cannonball game

How do I restart the program after you win?

Like

Turing:
if (x >= randx) and (x <= randx2) and (y >= 0) and (y <= 20) then
        put "You win!"
        vx := 0
        vy := 0
        cls
        delay(10)
        put "Play Again(Y/N)"
        get ans
        if ans = "Y" or ans = "y" then

*I don't Know how to restart it from here*
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Feb 19, 2009 10:34 am   Post subject: RE:cannonball game

code:

loop
    % Game code

    exit when donePlaying % where donePlaying is some condition, such as (ans not= "y" and ans not="Y")
end loop
A.J




PostPosted: Thu Feb 19, 2009 10:51 am   Post subject: Re: cannonball game

DemonWasp wrote:

code:

loop
    % Game code

    exit when donePlaying % where donePlaying is some condition, such as (ans not= "y" and ans not="Y")
end loop



See, since you are going to have you game code in the main loop, you might want to use procedure
hossack




PostPosted: Thu Feb 19, 2009 10:59 am   Post subject: RE:cannonball game

thanks for all the help, I got it working and its complete Smile
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  [ 11 Posts ]
Jump to:   


Style:  
Search: