Source Code: Wall Bounce
rating: 0+x

Description

This is a demo of the wall bounce used in ball and paddle games such as Pong and Breakout.

Instructions

Use the Space Key to change the speed and direction of the ball.

Source Code

// Screen

SetFPS 60    // Set game speed to 60 frames per second
SCREEN_WIDTH =    GetScreenWidth()
SCREEN_HEIGHT = GetScreenHeight()

// Font

LoadFont "Arial", 1, 16, 0

// Starting Values

ballRadius = 5                        // radius of ball
x# = SCREEN_WIDTH  / 2            // horizontal position of ball
y# = SCREEN_HEIGHT / 2            // vertical position of ball
xSpeed# = RndRange#(-2,2)        // horizontal speed of ball
ySpeed# = RndRange#(-2,2)        // vertical speed of ball

// Main Loop

Do            // beginning of main loop

    // Game Logic

    // change speed / direction on Space Key

    If SpaceKey()
        xSpeed# = RndRange#(-4,4)
        ySpeed# = RndRange#(-4,4)
        FlushKeys
    EndIf

    // move ball
    x# = x# + xSpeed#
    y# = y# + ySpeed#

    // bounce
    If x# < 0 + ballRadius / 2 Then xSpeed# = xSpeed# * -1
    If x# >  SCREEN_WIDTH - ballRadius / 2 Then xSpeed# = xSpeed# * -1
    If y# < 0 + ballRadius / 2 Then ySpeed# = ySpeed# * -1
    If y# >  SCREEN_HEIGHT - ballRadius / 2 Then ySpeed# = ySpeed# * -1

    // Draw Graphics
    Cls 0                                // clear screen to black
        Print "Hit Space to change speed / direction of ball." 
        Circle x#,y#, 5, False    // draw circle
    Sync                                // display screen to player
Loop        // end if main loop

Related Pages

Backlinks

These pages link back to this one. You may find them helpful.

Categories: Beginner Source Code : Movement : Physics : Physics Source Code : Source Code