Strict ' -----------------SETUP GAME CONDITIONS-------------------- Const PLAYER_LAYER:Int=1 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) CreateBricks() TBall.Create(LoadImage(URL+"ball.png"),Width/2,400) 'Creates our Bouncing Ball TPaddle.Create(LoadImage(URL+"paddle.png"),Width/2,0) '========================================================= ' ---------------------MAIN LOOP------------------------- Repeat Cls For Local o:TGameObject=EachIn GameObjectList o.DrawSelf() o.UpdateSelf() Next Flip Until AppTerminate() Or KeyDown(KEY_ESCAPE) 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 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() '-------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 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() '-------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 '------------------------------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