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

Username:   Password: 
 RegisterRegister   
 Gravityball
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zampano




PostPosted: Fri Nov 30, 2007 10:13 am   Post subject: Gravityball

Here's a small yet fun gravity game.
Hold your mouse over the black ball to hit it and send it flying. Hit the ball on one side to send it in the opposite direction.
Try to hit the coloured balls with the black one to gain points.
Use the 'n' key to stop the ball if it travels erratically.
Beware that every miss deducts points from your score, and if you reach 0 points, it's game over (you won't be able to shoot).

Turing:
setscreen ("offscreenonly") %eliminatflickering
var x, y, xinc, yinc, mousex, mousey, button, score, radius, ballx, bally : int %declare all variables
var chars : array char of boolean %creates an array to store keyboard input
%initialize values
score := 10
x := 50
y := 50
xinc := 3
yinc := 15
radius := 25
ballx := 200
bally := 200

%instruction
put "Shoot the ball with the space button and the mouse to gain more points and experiment with gravity."
put "Points count as bullets. More are awarded for hitting the coloured ball witht the black one."
put "Press space to shoot with your mouse over the ball. Press 'n' to stop the ball if it gets out of control."
put "Press any key to start."
View.Update %update screen with instructions
Input.Pause
cls %clear
drawfilloval (ballx, bally, 20, 20, Rand.Int (1, 15))
loop
    locatexy (1, maxy - 10) %locate the score text at a sing point consistently
    put score % put the score
    Input.KeyDown (chars) %indicate to the computer to use the chars array for input
    drawfilloval (x, y, radius, radius, 0) %draw an erase circle
    %move the ball coordinates
    x := x + xinc
    y := y + yinc
    %collision detection with walls
    if x - radius <= 0 then
        xinc := xinc * -1 - 1
        x := radius
    elsif x + radius >= maxx then
        xinc := xinc * -1 + 1
        x := maxx - radius
    end if
    if y - radius <= 0 then
        y := radius
        yinc := yinc * -1 - 1
    elsif y + radius >= maxy then
        yinc := yinc * -1
        y := maxy - radius
    end if
    drawfilloval (x, y, 25, 25, 7) %draw a new circle
    View.Update %update with the ball
    if Math.Distance (x, y, ballx, bally) <= 45 then
        score := score + 50
        drawfilloval (ballx, bally, 20, 20, 0)
        ballx := Rand.Int (25, 600)
        bally := Rand.Int (25, 375)
        drawfilloval (ballx, bally, 20, 20, Rand.Int (1, 15))
    end if
    delay (50) %stop the program from going to quickly
    /*gravity: this statement changes the amount the y will change each time
     yinc is added to y each time
     when this eventually becomes negative, the y decreases
     yinc is restored to positivity when the ball falls to the ground
     this is an important line in the program*/

    yinc := yinc - 1
    /* if the space is held down, the program checks if the mouse is on the ball
    if it is, the program checks what part of the ball it hit, and reacts accordingly*/

    if chars (' ') then
        Mouse.Where (mousex, mousey, button)
        if score > 0 then
            if whatdotcolour (mousex, mousey) = 7 then
                score := score + 5
                if mousex > x then
                    xinc := xinc - Rand.Int (1, 5)
                else
                    xinc := xinc + Rand.Int (1, 5)
                end if
                if mousey > y then
                    yinc := yinc - Rand.Int (1, 10)
                else
                    yinc := yinc + Rand.Int (1, 10)
                end if
            elsif score >= 1 then
                score := score - 1
            end if
        end if
    elsif chars ('n') then
        xinc := 0
        yinc := 0
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
Degensquared




PostPosted: Fri Nov 30, 2007 10:40 pm   Post subject: RE:Gravityball

pretty good! I like how simply implemented it is Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: