Strict ' -----------------SETUP GAME CONDITIONS-------------------- Const PLAYER_LAYER:Int=1 Const PLAY:Int=1 Const PAUSE:Int=2 Const START:Int=3 Const WAIT:Int=4 Const GAMEOVER:Int=5 Global xx:Int=0 Global GameState:Int=START Global GameObjectList:TList=CreateList() Global Width:Int=640 'Our Game Screen Width Global Height:Int=480 'Our Game Screen Height Global URL:String=BlitzMaxPath()+"/samples/Breakout/media/" Graphics Width,Height AutoMidHandle True HideMouse() Global Bricks:TImage=LoadAnimImage(URL+"tiles.png",32,20,0,5) Local FontURL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/breakout/" Global Font:TImageFont=LoadImageFont(FontURL+"BorisBlackBloxx.ttf",24) Global Font72:TImageFont=LoadImageFont(FontURL+"BorisBlackBloxx.ttf",72) '========================================================= ' ---------------------MAIN LOOP------------------------- Repeat Cls UpdateGameState() For Local o:TGameObject=EachIn GameObjectList o.DrawSelf() o.UpdateSelf() Next Flip Forever End '========================================================== ' ---------------THE MOTHER OF ALL OBJECT, TYPE GAMEOBJECT------------- Type TGameObject Field X:Int Field Y:Int Field XSpeed:Float=3 Field YSpeed:Float=-3 Field Image:TImage Field XScale:Float=1.0 Field YScale:Float=1.0 Field Rotation:Int=0 Method DrawSelf() SetScale XScale, YScale SetRotation Rotation SetColor 255,255,255 DrawImage Image,X,Y End Method Method UpdateSelf() Abstract End Type ' ---------------------DEFINE REQUIRED GAME OBJECTS------------------------- '------------------------------------------------------------------------------ 'Defines the Bouncing Ball Type TBall Extends TGameObject Function Create:TBall(Image:TImage,xstart:Int,ystart:Int) Local B:TBall=New TBall CreateObject(B,Image,xstart,ystart,2.0) Return B End Function Method UpdateSelf() If GameState<>PLAY Then Return '-------Update new X,Y Position by its respective speed per frame X :+ XSpeed Y :+ YSpeed '-------If Ball at left or Right Edge, set direction to opposite way If x>Width Or x<0 Then XSpeed=-XSpeed EndIf '-------If Ball at Top or Bottom Edge, set direction to opposite way If Y>Height Or Y<0 Then YSpeed=-YSpeed EndIf Rotation :+ 10 'increment rotation If Rotation >= 360 Then Rotation=0 'reset once full turn reached CheckCollision() End Method Method CheckCollision() '------Set the correct scale and rotation before collision checking SetScale XScale, YScale SetRotation Rotation If CollideImage(Image,X,Y,0,PLAYER_LAYER,0) YSpeed=-YSpeed EndIf For Local b:TBricks=EachIn GameObjectList If ImagesCollide2(Image,X,Y,0,Rotation,XScale,YScale, b.Image, b.X, b.Y, 0, 0, 1.0, 1.0) ListRemove(GameObjectList,b) YSpeed=-YSpeed Exit EndIf Next End Method End Type '-------------------------------------------------------------------------- Type TBricks Extends TGameObject Function Create:TBricks(Image:TImage,xstart:Int,ystart:Int) Local B:TBricks=New TBricks CreateObject(B,Image,xstart,ystart) Return B End Function Method UpdateSelf() 'do nothing End Method EndType '--------------------------------------------------------------------------- 'Defines the Score Object Type TScore Extends TText Field score:Int Function Create:TScore(Text:String, Font:TImageFont,xstart:Int,ystart:Int) Local B:TScore=New TScore B.Text=Text B.Font=Font B.X=XStart B.Y=YStart ListAddLast GameObjectList, B Return B End Function Method UpdateSelf() If GameState<>PLAY Then Return 'Do not continue if not in PLAY state score=0 For Local B:TBricks=EachIn GameObjectList score :+ 1 Next If score<90 Then GameState=GAMEOVER End Method Method drawself() SetImageFont(Font) SetRotation 0 SetScale 0.75,0.75 SetColor 255,0,0 DrawText Text+Score,X,Y End Method EndType '--------------------------------------------------------------------------- 'Defines the player controlled paddle Type TPaddle Extends TGameObject Function Create:TPaddle(Image:TImage,xstart:Int,ystart:Int) Local B:TPaddle=New TPaddle CreateObject(B,Image,xstart,ystart) B.XSpeed :* 1.5 'need a slightly faster speed than the balls B.YSpeed :* 1.5 'otherwise we cannot catch them Return B End Function Method UpdateSelf() If GameState<>PLAY Then Return '-------Move X direction depending on which key was pressed If KeyDown(KEY_LEFT) X :- XSpeed If KeyDown(KEY_RIGHT) X :+ XSpeed Y = height-60 'Y remains the same '-------Make sure paddle cannot move beyond the gameplay area If X(Width-ImageWidth(Image)/2) X=(Width-ImageWidth(Image)/2) '-------Reset Collision Layer and Write image to collision layer ResetCollisions(PLAYER_LAYER) CollideImage(Image,X,Y,0,0,PLAYER_LAYER,Self) End Method End Type '--------------------------------------------------------------------- Type TText Extends TGameObject Field Text:String Field Font:TImageFont Function Create:TText(Text:String, Font:TImageFont,xstart:Int,ystart:Int) Local B:TText=New TText B.Text=Text B.Font=Font B.X=XStart B.Y=YStart ListAddLast GameObjectList, B Return B End Function Method UpdateSelf() 'DO NOTHING End Method Method drawself() SetImageFont(Font) SetRotation 0 SetScale 1.0,1.0 SetColor 255,255,255 DrawText Text,X,Y End Method EndType '------------------------------HELPER FUNCTIONS------------------------ Function CreateBricks() For Local x:Int=0 To 15 For Local y:Int=0 To 5 TBricks.Create(Bricks,50+x*36,50+y*22) Next Next End Function '----------------------------------------------------------------- Function CreateObject(Obj:TGameObject, Image:TImage,xstart:Int,ystart:Int,Scale:Float=1.0) Obj.X=xstart Obj.Y=ystart Obj.XScale=Scale Obj.YScale=Scale Obj.Image=Image If Obj.Image=Null Print "Not able to load image file. Program aborting" End EndIf '-------Always add our object to the GameList, to be processed by the MAIN LOOP ListAddLast GameObjectList, Obj End Function '----------------------------------------------------------------- Function UpdateGameState() Select GameState Case START CreateBricks() TText.Create("BREAKOUT",Font72,80,50) TText.Create("PRESS To START",Font,110,250) TText.Create("PRESS TO EXIT",Font,110,300) GameState=WAIT Case WAIT 'To Start If KeyDown(KEY_ESCAPE) Or AppTerminate() End EndIf If KeyDown(KEY_ENTER) '-------------Remove all Objects ClearList GameObjectList '--------------Now Create the game objects that we need TBall.Create(LoadImage(URL+"ball.png"),Width/2,400) TPaddle.Create(LoadImage(URL+"paddle.png"),Width/2,0) TScore.CREATE("SCORE:",Font,10,5) CreateBricks() GameState=PLAY EndIf FlushKeys() Case PLAY If KeyDown(KEY_ESCAPE) Or AppTerminate() TText.Create("PRESS To RE-START",Font,80,250) TText.Create("PRESS TO EXIT",Font,110,300) FlushKeys() GameState=PAUSE EndIf Case PAUSE If KeyDown(KEY_ESCAPE) Or AppTerminate() End EndIf If KeyDown(KEY_ENTER) GameState=PLAY '-------------Remove all of the Text Objects For Local o:TText=EachIn GameObjectList ListRemove(GameObjectList,o) Next EndIf FlushKeys() Case GAMEOVER TText.Create("GAMEOVER",Font72,60,50) TText.Create("PRESS TO RE-START",Font,80,250) TText.Create("PRESS TO EXIT",Font,110,300) GameState=WAIT EndSelect EndFunction