My BreakOut Game

Started by markel422, November 12, 2009, 08:21:07 PM

Previous topic - Next topic

markel422

Here is a Breakout Game that I made by myself with countless Trail & Error.

It took me a while to finally get the Arrays of Blocks Right as that was the Hardest Part of me making this Game.

I'm glad of how it looks so far but am somewhat disappointed that I couldn't solve the collision of each of the Blocks manually without using a automatic built-in Collision detection such as the "PointInBox" word.

Here is the Code:

PlayBASIC Code: [Select]
; PROJECT : BreakoutProject
; AUTHOR : Michael
; CREATED : 11/6/2009
; EDITED : 11/12/2009
; ---------------------------------------------------------------------

openscreen 800,600,32,1

setfps 60

Mouse Off

Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
BallSize =8

gosub Ball_Restart

Dim bl(160)

Menu()

Do
Cls 0

;Create Movements of Player
Playerx = MouseX()

;Create Collision for Ball on Player
if playery+height > bally and playery < bally
if playerx+width > ballx and playerx < ballx
ballspeedy=ballspeedy * -1
endif
endif

;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + width > getscreenwidth() then playerx = getscreenwidth()-width
;Create Ball Movement
Ballx = ballx + ballspeedx


;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
ballx = getscreenwidth() - ballsize
ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1
endif

Bally = bally + ballspeedy

/*if bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -1
endif*/


if bally - ballsize < 0
bally = 0 + ballsize
ballspeedy=ballspeedy * -1
endif


;Create Blocks
for blocky = 100 to 280 step 20
for blockx = 1 to 751 step 50
if bl(ba)=0 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,100,0)
if PointInBox (ballx,bally,blockx,blocky,blockx+48,blocky+18) and bl(ba)=0
bl(ba)=1
PlayerScore=PlayerScore+50
ballspeedy=ballspeedy * -1
Endif
Inc BA
next
next

if BA=160 then BA=0 ;"BA" can Represent the word "Break Away"

;Creat Player
Boxc Playerx,playery,playerx+width,playery+height,1,RGB(180,0,0)

;Create Ball
Circlec ballx,bally,ballsize,1,rgb(0,0,200)

//Display Messages
text 350,10,"PlayerScore: "+Str$(PlayerScore)
text 350,30,"PlayerLives: "+Str$(PlayerLives)

If Bally > getscreenheight()
cls 0
Dec PlayerLives
text 320,getscreenheight()/2,"Player Has Missed Ball!"
sync
for lp=1 to 3
wait 1000
next
gosub Ball_Restart
endif

if PlayerLives = 0
cls 0
End
Endif

Sync
Loop

Function Menu()
Repeat
cls 0
text 330,280,"Let's Play Breakout!"
text 265,310,"Left Click the Mouse Button to Play!"
Until LeftMouseButton()=true

EndFunction

Ball_Restart:

ballspeedx = 2
ballspeedy = 3
ballx = 400
bally = 300

return



I am very aware that there is still some things that needs to be either added or fixed but as you can see so far is all that I know because when ever I attempt to go farther, my game ends up not looking right, maybe it's my current level of Computer Programming Skill or it's just that I don't know all the word Functions within PlayBasic.

Here are the things I wanted to do but could never find out how:

-How to make the Intro Messages appear on the Screen when on Fullscreen mode (I've tried this and it will only show the Intro Messages when in Window Mode but not in Fullscreen).

-Create the Event of making a Message Appear when you break all the boxes and starting a new level higher difficulty.

-Create an Event when items will fall to you every once in a while to either give you a good or bad boost of Power.

-Creating that Original Effect of when the ball will move on the same side of which the ball touches the paddle.

-Creating the Effect of having you the option to hold the Ball after you lost a life when you restart your current game.

-Create the Event when you lost all your lives, you are given the choice to either Quit the Game or Continue.

kevin

#1
Quote-How to make the Intro Messages appear on the Screen when on Fullscreen mode (I've tried this and it will only show the Intro Messages when in Window Mode but not in Fullscreen).

    By SYNC'ing - If you don't sync, the end user wont see it.  You can get away with this in windows modes (on most computers) but you should always SYNC....


Quote-Create the Event of making a Message Appear when you break all the boxes and starting a new level higher difficulty.

    Whenever  an interaction between game characters occurs, you need program the response.    So if you want messages to be displayed, then you'll need to set up some code can draw the messages (however many you want, and in whatever type, position,  etc ) you want..

   Display Messages (login required)



Quote-Create an Event when items will fall to you every once in a while to either give you a good or bad boost of Power.
Quote--Create the Event when you lost all your lives, you are given the choice to either Quit the Game or Continue.


    There are really all the same the question as above.       So if want more character types in game then we'll need to write some code to display and control them.  If you want to display messages, then you need to write some code to handle displaying them.  

   For example, a common is handling explosion.    So imagine we have two game characters that have collided (a bullet and a bad guy).    If the bullet kills the bad guy, then we need to delete both bullet and the bad guy.   However, if we do this, the player will see them both just vanish from the display.   So we need to show that some action has occured by spawning an explosion animation or some projectiles (fragments).     What i'd do, is write some functions to manage how explosions & particles are going to behave in my game.     So any time I need them, I just call my "CreateExplosion" (for example) function.


Quote-Creating that Original Effect of when the ball will move on the same side of which the ball touches the paddle.

      I don't really remember the game well enough,  but if you want the ball to rebound differently of the bat.   Then you'll to take more into account when you detect the collision.   So when  the ball hits the bat,  you could check how close this impact is to the middle to the bat.   The closer to the middle, could mean the more true the rebound could be.  So the further away could skew the rebound angle just a  little. This would make the ball behave a little less predictable.


Quote-Creating the Effect of having you the option to hold the Ball after you lost a life when you restart your current game.

    Use a variable to mark when the player is just starting the level.   This variable could count down to zero,  so only when it's zero (or bellow) should the ball logic be executed.  When it's above we set the balls position to be attached to the bat.  (offset from the bats position)     So in other words we use STATES to represent what actions our program should take and what logic should be executed.

    Personally I tend to use constants to represent the various state values of any given character.   The set of states will be grouped together using the same State Group Name with different state at the end.   So these states are really used to clearly present to the programmer that particular behavior is required.  So the program needs to choose and run the required action.     So the characters have a variable (or variables) that represent their current current frame of mind, so to speak.

    example.

PlayBASIC Code: [Select]
   setfps 60

Constant PlayerState_Dead =1
Constant PlayerState_StartingLevel =2
Constant PlayerState_Playing =3


; Variables for the player
PlayerX =400
PlayerY =580
PlayerState =PlayerState_StartingLevel ; players current STATE
PlayerPauseTime = 100



Do

Cls 0

; Execute the requires logic based upon thr player current state.


playerX=Mousex()

Select PlayerState

; ---------------------------
Case PLayerState_Dead
; ---------------------------
exitdo ; tell PB to jump out of the DO/LOOP

; ---------------------------
Case PLayerState_StartingLevel
; ---------------------------

BallX=PlayerX
BallY=PlayerY-16


dec PlayerPauseTime
if PlayerPauseTime=0
PlayerState =PLayerState_PLaying

endif


; ---------------------------
Case PLayerState_PLaying
; ---------------------------


BallY=BallY-2


if BallY<0
PlayerState =PLayerState_Dead

endif


EndSelect


circlec ballx,bally,16,true,$00ff00

box PlayerX-50,playery,playerX+50,playery+10,true


Sync
Loop


cls 255
centertext 400,300, "Ball Left Screen, Game Over"
Sync
waitkey





markel422

Wow, I am surprised on how flexible this coding can really become. I looked at your other codes due to my questions from the "Resources" section and am learning more of how Functions can personally be used for creating your own code when ever you want to request it from within the Function to make it do something. It is very slowly but surely coming to me right now on how programming code works. ;)

Alright!...I am going to resend a modified version of this BreakOut game and see what I can pull off, using these methods in the best that I can understand.:)

kevin


    Yeah, you can think of functions as a way of making your own commands.   They're not essential, but through the clever use of them,  you can build up a set of your 'customized' environment.   If you look closely, programmers often write functions that can be re-used between projects even.  So it's not uncommon to see the same code popping up in various projects from the same author.  I do it all the time.. :)

    In game programming, programmers often aim at creating a game engine.  This is nothing more than a collection of functions that perform the required game tasks.   So there's functions to spawn objects, draw them, handle collisions,  etc etc etc.

     What this does, is it abstracts the programmer from the lower level functionality of the whatever language your using.    This in turn simplifies the code sitting above it and makes it easier to locate issues (bugs) with game engine as it grows in size...    It's a lot like a deck of cards really. 

   There's a lot of  theories on code design.  This thread Planning your projectis probably worth a mind boggling read.


micky4fun

Hi markel422

well great starting game for you first project , maybe in the update of this game have differant colour bricks as they go up the screen , dont think there's any need to cls the screen when you lose ball , just a message saying player lost ball , while bricks are still visible ,
add some sound and title and end screen and its done..

great stuff , look forward to end results

mick ;)


markel422

#5
Thanks micky4fun :)

I'm going to try and do that thing with the blocks as you said, but as you may know, my skills are currently very small and limited right now and will try to figure out how to even do that. :-\

Now with the sound, I can't just simply get a random sound clip from a website somewhere can I? Wouldn't that not be acceptable when it comes to me making the entire project from sound I haven't legitly created? If I knew, I could use a program on conducting my own music and sound as use that? Or is there a way I can somehow give credit to where I've got the sound and music from I finished the project?

I'm having a bit of a hiccup right now that I am having trouble solving with the opening "Menu Code" as well as to figure out what is the code to activate my "Win Message" when you clear all of the blocks.

Here is the small main part of the code of the problem:

PlayBASIC Code: [Select]
Global Win = 0


Function Menu(x,y,show$)
repeat
cls 0
message = new tmessage
message.x = x
message.y = y
message.show$ = show$
message.colour = Green
until LeftMouseButton()=true
EndFunction


Function WonMessage(x,y,show$,EndDisplayTime)
If Win = 1
cls 0
message = new tmessage
message.x = x
message.y = y
message.show$ = show$
message.enddisplaytime = timer() + TimeToDisplay
message.colour = Green
sync
Endif
EndFunction

Function DrawMessage()

CurrentTime=timer()
for each Message()
if CurrentTime<message.EndDisplayTime
ink Message.colour
centertext message.x,message.y,message.show$
else
Message = Null
endif
next
ink Green

EndFunction



And here's with the Codes in use when it comes for the Menu screen that is put BEFORE the Do/Loop Function

Menu(330,280,"Let's Play Breakout!")
Menu(265,310,"Left Click the Mouse Button To Play!")



And Now the Win Screen within the Loop:

PlayBASIC Code: [Select]
If BA = 160-1
Win = 1
x = 350
y = 300
show$ = "HOORAY YOU'VE WON!"
TimeToDisplay=5000
WonMessage(x,y,show$,TimeToDisplay)
sync
endif



markel422

#6
I am having a huge problem right now,  with the item code, when I tried many switches on it for some reason It's saying there's something wrong with the Menu Function. ???

I'm stuck...

Here is the Code:

PlayBASIC Code: [Select]
openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

IWidth=5
IHeight=5
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
BallSize =8

Constant StartGame = 1
Constant Playing = 2
Constant Win = 3
Constant Gameover = 4


GameTimer=5000


Global Green=RGB(0,255,0)

;Item Spawns and the like
ItemMax=2
T1=timer()
T2=timer()
Dim IAppear(ItemMax)
Dim IposX(ItemMax)
Dim IposY(ItemMax)

gosub Ball_Restart

type tmessage
x,y
Show$
EndDisplaytime
Colour
endtype
Dim message as tmessage list

Dim bl(160)

Menu()

Do
Cls 0
GameState=StartGame
;Create Movements of Player
Playerx = MouseX()

Bally = bally + ballspeedy
;Create Collision for Ball on Player
if PointInBox(ballx,bally,playerx,playery,playerx+width,playery+height)
ballspeedy=ballspeedy * -1
endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + width > getscreenwidth() then playerx = getscreenwidth()-width
;Create Ball Movement
Ballx = ballx + ballspeedx


;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
ballx = getscreenwidth() - ballsize
ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1
endif



/*if bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -1
endif*/


if bally - ballsize < 0
bally = 0 + ballsize
ballspeedy=ballspeedy * -1
endif
each
;Create Blocks
for blocky = 100 to 280 step 20
for blockx = 1 to 751 step 50
if bl(ba)=0 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,100,0)
if PointInBox (ballx,bally,blockx,blocky,blockx+48,blocky+18) and bl(ba)=0
bl(ba)=1
ballspeedy=ballspeedy * -1
PlayerScore=PlayerScore+50
endif
Inc BA
next
next

if BA=160 then BA=0 ;"BA" can Represent the word "Break Away"

Elapsed=(timer()-t1)/250
;Create Item
If Rnd(500)=20
gosub Item_Appear
if Elapsed=1
boxc IposX,IposY,IposX+IWidth,IposY+IHeight,1,RGB(255,255,255)
Elapsed=0
T1=timer()
endif

If ItemCount > 0 then gosub Move_Item

;Creat Player
Boxc Playerx,playery,playerx+width,playery+height,1,RGB(180,0,0)

;Create Ball
Circlec ballx,bally,ballsize,1,rgb(0,0,200)

//Display Messages
text 345,10,"PlayerScore: "+Str$(PlayerScore)
text 350,40,"PlayerLives: "+Str$(PlayerLives)

//=============================================================================
//DISPLAY IN-GAME MESSAGES
//=============================================================================


If PlayerScore=8000
text 310,280,"HOORAY YOU WIN!"
GameState=Win
sync
for lp=1 to 4
wait 1000
next lp
endif

If Bally > getscreenheight()
Dec PlayerLives
text 310,getscreenheight()/2,"Player Has Missed Ball!"
sync
Login required to view complete source code



kevin


   The error means that at some point prior to the  function declaration,  your missing a closing statement from some statement pair.   IT could be, 

    If -> EndIF 
 
    Do -> Loop     

    Repeat  -> Until
 
   etc
 
   so good back up through the code and check everything is paired..


kevin


  Hint:  Look at about line 120

markel422

Ok, I just found the code that didn't have the endif statement, but now it is saying there is a Fatal Error on line 107. Which is my Creation of the Array of Blocks.

Is it the same Problem?

kevin



What is each doing on line 101 ?

markel422

#11
Sorry, that was taken care of, here is the updated code:

PlayBASIC Code: [Select]
openscreen 800,600,32,2
screenvsync on
setfps 60

loadfont "Arial",1,24,0

Mouse Off

IWidth=5
IHeight=5
Width = 90
Height = 15
PlayerScore = 0
PlayerLives = 3
Playerx = 355
Playery = 550
BallSize =8

Constant StartGame = 1
Constant Playing = 2
Constant Win = 3
Constant Gameover = 4


GameTimer=5000


Global Green=RGB(0,255,0)

;Item Spawns and the like
ItemMax=2
T1=timer()
T2=timer()
Dim IAppear(ItemMax)
Dim IposX(ItemMax)
Dim IposY(ItemMax)

gosub Ball_Restart

type tmessage
x,y
Show$
EndDisplaytime
Colour
endtype
Dim message as tmessage list

Dim bl(160)

Menu()

Do
Cls 0
GameState=StartGame
;Create Movements of Player
Playerx = MouseX()

Bally = bally + ballspeedy
;Create Collision for Ball on Player
if PointInBox(ballx,bally,playerx,playery,playerx+width,playery+height)
ballspeedy=ballspeedy * -1
endif


;Create Player Collision on Screen
If playerx < 0 then playerx = 0
If playerx + width > getscreenwidth() then playerx = getscreenwidth()-width
;Create Ball Movement
Ballx = ballx + ballspeedx


;Create Collision for Ball on Screen
if ballx + ballsize > getscreenwidth()
ballx = getscreenwidth() - ballsize
ballspeedx = ballspeedx * -1
endif

if ballx - ballsize < 0
ballx = 0 + ballsize
ballspeedx=ballspeedx * -1
endif



/*if bally + ballsize > getscreenheight()
bally = getscreenheight() - ballsize
ballspeedy=ballspeedy * -1
endif*/


if bally - ballsize < 0
bally = 0 + ballsize
ballspeedy=ballspeedy * -1
endif

;Create Blocks
for blocky = 100 to 280 step 20
for blockx = 1 to 751 step 50
if bl(ba)=0 then BoxC blockx,blocky,blockx+48,blocky+18,1,RGB(180,100,0)
if PointInBox (ballx,bally,blockx,blocky,blockx+48,blocky+18) and bl(ba)=0
bl(ba)=1
ballspeedy=ballspeedy * -1
PlayerScore=PlayerScore+50
endif
Inc BA
next
next

if BA=160 then BA=0 ;"BA" can Represent the word "Break Away"

Elapsed=(timer()-t1)/250
;Create Item
If Rnd(500)=20
gosub Item_Appear
if Elapsed=true
boxc IposX,IposY,IposX+IWidth,IposY+IHeight,1,RGB(255,255,255)
Elapsed=false
T1=timer()
endif
endif

If ItemCount > 0 then gosub Move_Item

;Creat Player
Boxc Playerx,playery,playerx+width,playery+height,1,RGB(180,0,0)

;Create Ball
Circlec ballx,bally,ballsize,1,rgb(0,0,200)

//Display Messages
text 345,10,"PlayerScore: "+Str$(PlayerScore)
text 350,40,"PlayerLives: "+Str$(PlayerLives)

//=============================================================================
//DISPLAY IN-GAME MESSAGES
//=============================================================================


If PlayerScore=8000
text 310,280,"HOORAY YOU WIN!"
GameState=Win
sync
for lp=1 to 4
wait 1000
next lp
endif

If Bally > getscreenheight()
Dec PlayerLives
text 310,getscreenheight()/2,"Player Has Missed Ball!"
Login required to view complete source code



kevin


   It compiles & seem to runs here.  The error is probably due to some falty logic.  I suspect what may be happening is that's possible for BA variable to get outside of arrays size.  If this occurs, then PB will stop your program and spit it out a fatal warning.   It does this to stop you writing data in memory that your program doesn't own.   




micky4fun

Hi markel422

coming along nicely now , but did see a few errors when running game , only minor 2 orange dots seem to appear now and then as shown in pic
also ball does go through bat now and then as well when bat is far right of screen

i did not have time to look through code as long day with one thing or another so off to bed , but will av a look tomorrow

keep it up
mick :)

markel422

Yeah, I have noticed the 2 orange dots on the screen, and that is fixed now. :)

Also with the ball going through the paddle I have also noticed that and I can't really find out what is really causing that glitch. :-\

By the way, you never answered the questions I asked for you on my earlier post (Replay#5) when you first posted to me on this topic. If you can answer that, thanks... :)