Computer Science Canada

Help with Breakout Game

Author:  ncmitchell [ Tue Jan 17, 2012 4:30 pm ]
Post subject:  Help with Breakout Game

This is my breakout game, I'm close to being done, but I'm having trouble with how I would go about different slopes of the ball when it bounces, and with making bounce off the top of the bricks.
All help is appreciated, thanks!

Turing:

View.Set ("offscreenonly")

const PaddleColour := white
const EscKey := chr (27)
const up := 0
const down := 1
const BallRad := 6
const BallColour := 29

setscreen ("graphics:800,800")

% DECLARE VARIABLES
var map : array 1 .. 20, 1 .. 20 of int
var NumRows, NumCols, font1, font2, font3, colourb : int
var mx, my, button : int
var x, y, Paddlex1, Paddlex2, Paddley1, Paddley2 : int
var Ballx, Bally, direction : int
var level, lives : int
var hit, inc : int
var width, height, i, NumBlocks : int
var Blockx1, Blockx2, Blocky1, Blocky2 : array 1 .. 126 of int
var BlockVisible : array 1 .. 126 of boolean
var xb, yb : int

level := 0
button := up

% FONTS
font1 := Font.New ("Arial:25:bold")
font2 := Font.New ("Arial:30:bold")
font3 := Font.New ("Arial:60:bold")

% PADDLE COORDINATES
Paddlex1 := 370
Paddley1 := 10
Paddlex2 := 430
Paddley2 := 20

% BALL COORDINATES
Ballx := 400
Bally := 26

% Y COORDINATE
x := 86
y := maxy - 100

% LEVEL AND LIVES SETTING
level := 1
lives := 3

% XB AND YB COORDINATES
xb := Ballx - BallRad - 4
yb := Bally + BallRad + 4

direction := 0

NumBlocks := 28
hit := 0

inc := 5
hit := 0

for f : 1 .. NumBlocks
    BlockVisible (f) := true
end for

procedure ReadMap (filename : string, var map : array 1 .. *, 1 .. * of int, var NumRows, NumCols : int)
    var stream : int

    % OPEN FILE
    open : stream, filename, get
    if stream not= 0 then
        get : stream, NumRows
        get : stream, NumCols

        for row : 1 .. NumRows
            for col : 1 .. NumCols
                get : stream, map (row, col)
            end for
        end for

        % CLOSE THE FILE
        close : stream
    end if
end ReadMap

procedure DrawMap (map : array 1 .. *, 1 .. * of int, NumRows, NumCols : int)
    var x, y : int
    y := maxy - 40
    for row : 1 .. NumRows
        x := 0
        for col : 1 .. NumCols
            % BLUE BORDER
            if map (row, col) = 54 then
                drawfillbox (x, y, x + 40, y + 40, map (row, col))
                % BLACK BACKGROUND
            elsif map (row, col) = 7 then
                drawfillbox (x, y, x + 40, y + 40, map (row, col))
            end if
            x := x + 40
        end for
        y := y - 40
    end for
end DrawMap

colourb := 12
NumBlocks := 28

% BOXES
procedure Level
    for i : 1 .. NumBlocks
        % DRAW EACH BLOCK
        if BlockVisible (i) then
            drawfillbox (Blockx1 (i), Blocky1 (i), Blockx2 (i), Blocky2 (i), colourb)
        else
            drawfillbox (Blockx1 (i), Blocky1 (i), Blockx2 (i), Blocky2 (i), black)
        end if
    end for
end Level

ReadMap ("map.txt", map, NumRows, NumCols)

DrawMap (map, NumRows, NumCols)

% BEGINNING SCREEN
% Font.Draw ("B", 200, maxy div 3 * 2, font3, 12)
% Font.Draw ("R", 250, maxy div 3 * 2, font3, 52)
% Font.Draw ("E", 300, maxy div 3 * 2, font3, 47)
% Font.Draw ("A", 350, maxy div 3 * 2, font3, 42)
% Font.Draw ("K", 400, maxy div 3 * 2, font3, 14)
% Font.Draw ("O", 450, maxy div 3 * 2, font3, 35)
% Font.Draw ("U", 500, maxy div 3 * 2, font3, 46)
% Font.Draw ("T", 550, maxy div 3 * 2, font3, 80)
%
% Font.Draw ("PLAY", 360, maxy div 2, font1, white)
% Font.Draw ("INSTRUCTIONS", 285, maxy div 2 - 30, font1, white)
%
% mousewhere (mx, my, button)
%
% if mx > 360 and mx < 460 and my > 370 and my < 400 then
% Font.Draw ("PLAY", 360, maxy div 2, font1, 12)
% if button = down then

% MAIN SCREEN
Font.Draw ("B", 40, maxy - 30, font2, 12)
Font.Draw ("R", 65, maxy - 30, font2, 52)
Font.Draw ("E", 90, maxy - 30, font2, 47)
Font.Draw ("A", 115, maxy - 30, font2, 42)
Font.Draw ("K", 140, maxy - 30, font2, 14)
Font.Draw ("O", 165, maxy - 30, font2, 35)
Font.Draw ("U", 190, maxy - 30, font2, 46)
Font.Draw ("T", 215, maxy - 30, font2, 80)

Font.Draw ("LEVEL:", 420, maxy - 25, font1, white)
Font.Draw ("LIVES:", 620, maxy - 25, font1, white)

setscreen ("nocursor")

% SET LEVELS
if level = 1 then
    Font.Draw ("1", 540, maxy - 25, font1, 12)
elsif level = 2 then
    Font.Draw ("2", 540, maxy - 25, font1, 52)
elsif level = 3 then
    Font.Draw ("1", 540, maxy - 25, font1, 47)
elsif level = 4 then
    Font.Draw ("1", 540, maxy - 25, font1, 42)
elsif level = 5 then
    Font.Draw ("1", 540, maxy - 25, font1, 14)
elsif level = 6 then
    Font.Draw ("1", 540, maxy - 25, font1, 35)
elsif level = 7 then
    Font.Draw ("1", 540, maxy - 25, font1, 46)
elsif level = 8 then
    Font.Draw ("1", 540, maxy - 25, font1, 80)
end if

% LIVES SETTING
if lives = 3 then
    Font.Draw ("3", 730, maxy - 25, font1, colourb)
elsif lives = 2 then
    Font.Draw ("2", 730, maxy - 25, font1, colourb)
elsif lives = 1 then
    Font.Draw ("1", 730, maxy - 25, font1, colourb)
end if

% DRAW PADDLE AND BALL
drawfillbox (Paddlex1, Paddley1, Paddlex2, Paddley2, PaddleColour)

drawfilloval (Ballx, Bally, BallRad, BallRad, BallColour)

% LEVEL 1 ---------------------------------------------------------------------
width := 40
height := 15
i := 1

for row : 1 .. 2
    for col : 1 .. 14
        Blockx1 (i) := 87 + (col - 1) * 45
        Blocky1 (i) := 628 + (row - 1) * 20
        Blockx2 (i) := Blockx1 (i) + width
        Blocky2 (i) := Blocky1 (i) + height
        BlockVisible (i) := true
        i := i + 1
    end for
end for

Level

loop
    View.Update
    % GET MOUSE SIGNAL
    mousewhere (mx, my, button)

    % ERASE THE PADDLE AND BALL
    drawfillbox (Paddlex1, Paddley1, Paddlex2, Paddley2, black)

    drawfilloval (Ballx, Bally, BallRad, BallRad, black)

    Paddlex1 := mx - 30
    Paddlex2 := mx + 30

    if direction = 0 then
        Ballx := mx
    end if

    % PADDLE BOUNDARIES
    if Paddlex1 <= 80 then
        Paddlex1 := 80
    end if

    if Paddlex2 <= 140 then
        Paddlex2 := 140
    end if

    if Paddlex2 >= maxx - 80 then
        Paddlex2 := maxx - 80
    end if

    if Paddlex1 >= maxx - 140 then
        Paddlex1 := maxx - 140
    end if

    % BALL BOUNDARIES
    if Ballx <= 110 and direction = 0 then
        Ballx := 110
    end if

    if Ballx >= maxx - 110 and direction = 0 then
        Ballx := maxx - 110
    end if

    % DIRECTION SETTINGS
    if direction = 1 then
        Ballx := Ballx + 5
        Bally := Bally + 5
    elsif direction = 2 then
        Ballx := Ballx + 5
        Bally := Bally - 5
    elsif direction = 3 then
        Ballx := Ballx - 5
        Bally := Bally - 5
    elsif direction = 4 then
        Ballx := Ballx - 5
        Bally := Bally + 5
    end if

    % BLUE BOUNDARY SETTINGS
    if Ballx + BallRad >= maxx - 84 then
        if direction = 1 then
            direction := 4
        elsif direction = 2 then
            direction := 3
        end if
    elsif Ballx - BallRad <= 84 then
        if direction = 4 then
            direction := 1
        elsif direction = 3 then
            direction := 2
        end if
    elsif Bally + BallRad >= maxy - 84 then
        if direction = 1 then
            direction := 2
        elsif direction = 4 then
            direction := 3
        end if
    end if

    % BEGINNING SETTINGS
    if button = down and Ballx = mx then
        direction := 1
    end if

    % BALL BOUNCE OF PADDLE SETTINGS
    if Ballx <= Paddlex2 and Ballx >= Paddlex1 and Bally <= Paddlex2 then
        if direction = 2 and Bally <= 20 then
            direction := 1
        elsif direction = 3 and Bally <= 20 then
            direction := 4
        end if
    end if

    % CHECK IF BALL HITS BRICK
    for j : 0 .. inc - 1
        if whatdotcolour (Ballx - 1, Bally + j + 1) = colourb then
            hit := 1
        end if
        if whatdotcolour (Ballx + 1, Bally - j - 1) = colourb then
            hit := 1
        end if
    end for

    for j : 1 .. NumBlocks
        xb := Ballx - j + 4
        yb := Bally + BallRad + 4
        if xb > Blockx1 (j) and xb < Blockx2 (j) and yb > Blocky1 (j) and yb < Blocky2 (j) and BlockVisible (j) then
            hit := j
        end if
        if hit > 0 then
            BlockVisible (j) := false
            Level
            hit := 0
            if direction = 1 then
                direction := 2
            elsif direction = 4 then
                direction := 3
            elsif direction = 2 then
                direction := 1
            elsif direction = 3 then
                direction := 4
            end if
        end if
    end for

    % WHEN USER MISSES THE PADDLE
    if lives = 3 and Bally < 0 then
        direction := 0
        Bally := 26
        lives := 2
        drawfillbox (730, maxy - 25, 750, maxy, black)
        % LIVES SETTING
        if lives = 2 then
            Font.Draw ("2", 730, maxy - 25, font1, colourb)
        end if
        Font.Draw ("YOU LOST A LIFE", maxx div 3, maxy div 2, font1, colourb)
    elsif lives = 2 and Bally < 0 then
        direction := 0
        Bally := 26
        lives := 1
        drawfillbox (730, maxy - 25, 750, maxy, black)
        % LIVES SETTING
        if lives = 1 then
            Font.Draw ("1", 730, maxy - 25, font1, colourb)
        end if
        Font.Draw ("YOU LOST A LIFE", maxx div 3, maxy div 2, font1, colourb)
    elsif lives = 1 and Bally < 0 then
        direction := 0
        Bally := 26
        lives := 0
        drawfillbox (730, maxy - 25, 755, maxy, black)
        % LIVES SETTING
        if lives = 0 then
            Font.Draw ("0", 730, maxy - 25, font1, colourb)
        end if
        Font.Draw ("YOU LOSE", maxx div 3 + 50, maxy div 2, font1, colourb)
        drawfillbox (81, 450, 719, 719, black)
        for f : 1 .. NumBlocks
            BlockVisible (f) := true
        end for
        Level
    end if

    if button = down then
        drawfillbox (maxx div 3, maxy div 2, 550, 450, black)
    end if

    if button = down and lives = 0 then
        lives := 3
        drawfillbox (730, maxy - 25, 755, maxy, black)
        % LIVES SETTING
        if lives = 3 then
            Font.Draw ("3", 730, maxy - 25, font1, colourb)
        end if
    end if

    % RE-DRAW THE PADDLE AND BALL
    drawfillbox (Paddlex1, Paddley1, Paddlex2, Paddley2, PaddleColour)

    drawfilloval (Ballx, Bally, BallRad, BallRad, BallColour)

    % TIME DELAY
    delay (20)
end loop
% end if
% end if


: