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 ?

Author
Message
ApocalypseFPS
9
Years of Service
User Offline
Joined: 3rd Jun 2014
Location:
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

ReadyGameForPlay()
LoadBricksArray()
PlayBricksGame()

End

REM Procedure Section

Function ReadyGameForPlay()

Sync ON
Sync Rate 60
Hide Mouse
Randomize Timer()

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

Load Image "Background.bmp", BackGroundImage
Load Image "Paddle.bmp", PaddleImage
Load Image "GameBall.bmp", BallImage
Load Image "Brick.bmp", BrickImage

Sprite PaddleSprite, 999, 999, PaddleImage
Sprite BallSprite, 999, 999, BallImage
Sprite BrickSprite, 999, 999, BrickImage

PaddleX = (Screen Width() /2) - (Sprite Width(PaddleSprite) /2)
PaddleY = Screen Height() - 25

EndFunction

Function LoadBricksArray()

For j = 1 TO 5
For i = 1 to 15
Read BricksArray(i,j)
Next i
Next j

EndFunction

Function PlayBricksGame()

Do
Paste Image BackGroundImage, 0, 0

Select GameState$

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

Case "Initialise":
ReadyBallAndPaddle()
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"
LoadBricksArray()
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"
LoadBricksArray()
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

Function LookForCollisionWithPaddle()

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

Function ReadyBallAndPaddle()

BallX = PaddleX + (Sprite Width(PaddleSprite) /2) - (Sprite Width(BallSprite) /2)
BallY = PaddleY -14

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

Sprite BallSprite, BallX, BallY, BallImage
EndFunction

Function ProcessPlayerInput()

If PaddleX < 6 Then PaddleX = 6

RightSideOfPaddle = PaddleX + Sprite Width(PaddleSprite)

If RightSideOfPaddle > Screen Width()
PaddleX = Screen Width() - Sprite Width(PaddleSprite)
EndIf

If LeftKey()
Dec PaddleX, 6
EndIf

If RightKey()
Inc PaddleX, 6
EndIf

Sprite PaddleSprite, PaddleX, PaddleY, PaddleImage
EndFunction

Function DeflectBallOfPaddle()

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

LeftSideOfPaddle = sprite x(PaddleSprite)
RightSiodeOfPaddle = sprite x(PaddleSprite) + sprite width(PaddleSprite)

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
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
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.

Login to post a reply

Server time is: 2024-04-24 04:51:02
Your offset time is: 2024-04-24 04:51:02