# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### 2D All the way! / Simple Bricks Game - Bricks array not working properly ?

Message
Posted: 3rd Jun 2014 16:29
I am working on a project for a course in college and im having troubles with some coding.
nvm the background tho ...
What im working on is a simple Bricks game.
You have a platform to move about, a ball which bounces of the platform and an array of bricks that you need to destroy with the ball.
The problem is, when the ball hits a brick weird things happen. For example, the ball collides with a brick (the ball should bounce of and the brick should be destroyed), the ball does destroy the brick but also goes through the brick (and sometimes more than just one brick). Another weird thing that is happening upon collision ball-brick is that the brick that the ball collides with ... gets destroyed but so does some other random brick which does not get hit at all.
As well as those two problems ... I have an array set up to be 15x3 bricks/15 columns x 3 rows yet almost half or the array is not displayed.

REM Project: The Bricks Game
REM Created: 13/05/2014 13:30:15
REM
REM ***** Main Source File *****
REM Dawid Michalowicz

REM Declare and Initialise Global Variables and data statements

Global BallX AS Integer = 0
Global BallY AS Integer = 0

Global SpeedX AS Integer = 1
Global SpeedY AS Integer = -1

Global PaddleX AS word = 5
Global PaddleY AS word = 20

Global BackGroundImage AS Integer = 1
Global BallImage AS Integer = 2
Global BrickImage AS Integer = 3
Global PaddleImage AS Integer = 6

Global BallSprite AS Integer = 2
Global BrickSprite AS Integer = 3
Global PaddleSprite AS Integer = 5

Global GameState\$ AS String = "Initialise"
Global PlayerTurns AS Integer = 3

Dim BricksArray(5,15) AS Integer

Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Data 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Data 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

REM Main Processing Section

PlayBricksGame()

End

REM Procedure Section

Sync ON
Sync Rate 60
Hide Mouse
Randomize Timer()

Set Text Font "Arial"
Set Text Size 16
Set Text To Bold
Ink 0, 0

Sprite BallSprite, 999, 999, BallImage
Sprite BrickSprite, 999, 999, BrickImage

PaddleY = Screen Height() - 25

EndFunction

For j = 1 TO 5
For i = 1 to 15
Next i
Next j

EndFunction

Function PlayBricksGame()

Do
Paste Image BackGroundImage, 0, 0

Select GameState\$

Case "Play":
DisplayBricks()
BounceBall()
EndCase

Case "Initialise":
DisplayBricks()

Restore
If UpKey() = 1
GameState\$ = "Play"
EndIf
EndCase

Case "Over":
Center Text Screen Width() /2, Screen Height() /2, "Game Over"
Center Text Screen Width() /2, Screen Height() /2 + 30, "Press the Up Key to play a new game."

If UpKey() = 1
PlayerTurns = 3
Restore
GameState\$ = "Initialise"
EndIf
EndCase
EndSelect

If GameState\$ <> "Over"
ProcessPlayerInput()
Text 25, 25, "Balls Remaining: " + Str\$(PlayerTurns)
EndIf

Sync
Loop

EndFunction

Function DisplayBricks()

Width = Sprite Width(BrickSprite) + 2
Height = Sprite Height(BrickSprite) + 2

BlockCount = 0

For i = 1 to 15
For j = 1 to 5
if BricksArray(i,j) = 1
Inc BlockCount

Sprite BrickSprite, 65 + Width * i, 75 + Height * j, BrickImage
Paste Sprite BrickSprite, 65 + Width * i, 75 + Height * j
If Sprite Collision(BrickSprite, BallSprite) = 1
BricksArray(i,j) = 0
DeflectBallOfBricks()
EndIF
EndIf
Next j
Next i

If BlockCount = 0
GameState\$ = "Initialise"
EndIf

EndFunction

Function BounceBall()

BallX = BallX + SpeedX
BallY = BallY + SpeedY

ProcessSideOfScreenCollisions()
ProcessTopOfScreenCollisions()
ProcessMisses()
Sprite BallSprite, BallX, BallY, BallImage

EndFunction

Function ProcessSideOfScreenCollisions()

If BallX < 1
BallX = 1
SpeedX = SpeedX * -1
EndIf

If BallX > Screen Width() -14
BallX = Screen Width() -14
SpeedX = SpeedX * -1
EndIf

EndFunction

Function ProcessTopOfScreenCollisions()

If BallY < 0
BallY = 0
SpeedY = SpeedY * -1
EndIf

EndFunction

Function ProcessMisses()

If BallY > Screen Height() -6
PlayerTurns = PlayerTurns -1
If PlayerTurns < 1
GameState\$ = "Over"
Else
GameState\$ = "Initialise"
EndIf
EndIf

EndFunction

If Sprite Collision(PaddleSprite, BallSprite) = 1
EndIf
EndFunction

SpeedX = rnd(2) + 1
SpeedY = rnd(2) - 6

Sprite BallSprite, BallX, BallY, BallImage
EndFunction

Function ProcessPlayerInput()

EndIf

If LeftKey()
EndIf

If RightKey()
EndIf

EndFunction

CenterOfBall = sprite x(BallSprite) + sprite width(BallSprite) /2

If CenterOfBall < LeftSideOfPaddle + 15
SpeedX = -rnd(4) - 1
EndIf

If CenterOfBall > RightSideOfPaddle - 15
SpeedX = rnd(4) - 1
EndIf

SpeedY = -rnd(2) -3
EndFunction

Function DeflectBallOfBricks()

CenterOfBall = Sprite x(BallSprite) + Sprite Width(BallSprite) /2
LeftSideOfBlock = Sprite x(BallSprite)
RightSideOfBlock = Sprite x(BallSprite) + Sprite Width(BallSprite)

If CenterOfBall < LeftSideOfBlock or CenterOfBall > RightSideOfBlock
SpeedX = SpeedX* -1
Else
SpeedY = SpeedY* -1
EndIf
EndFunction
Posted: 7th Jun 2014 04:53 Edited at: 8th Jul 2014 16:44
edit: I should have known better than to try and help a newb on this board.

So many games to code.....so little time.