I've been using John J's awesome OOP tutorial: http://www.blitzbasic.com/Community/posts.php?topic=59233
To try and understand OOP better, I dicided to make OO pong. Here is my code thus far:
First off, does anyone see me doing anything wrong? The above code should just show a static pong game. This is where I need help though. I'm trying to get everything to update. The best place to update each type would be in each of their Draw() methods would it not? Stuff like x=x+1 and player controls. The problem is, if I put "If KeyDown(KEY_UP) Then y=y-4" inside of the paddle type's Draw() method, then it would move both of the paddles rather than just one of them moving. How do I distinguish one paddle(obj1) from the other paddle(obj2), yet keep the code in the Draw() method?
EDIT: Another thing. If I were to make a menu for this pong, would I make the menu another type which extends entity or would I just not make it a type at all?
EDIT2: You may have noticed also that in the background type's Draw() method, the scores(text) which appear on the screen are set to strings ("0"), rather than obj1.score and obj2.score. I couldn't figure out how to let those fields have access to the background type without messing up the way everything is drawn in the main loop. Any ideas?
EDIT3: Also, I put all of my types below my main loop. I just like them there(like my functions). Is there any difference between putting types above your main loop or putting them below? Or is it just personal preference?
To try and understand OOP better, I dicided to make OO pong. Here is my code thus far:
SuperStrict Graphics 800,600,0,60 SeedRnd MilliSecs() Local obj1:paddle=paddle.Create(0,265,4,True) Local obj2:paddle=paddle.Create(790,265,4,False) Local obj3:ball=ball.Create(395,295,1) Local obj4:background=background.Create() While Not KeyHit(KEY_ESCAPE) Local ent:entity For ent=EachIn entity.EntityList ent.Draw() Next Flip;Cls Wend End 'TYPES Type entity Field x#,y# Global EntityList:TList=New TList Method Draw() Abstract Method New() EntityList.AddLast(Self) End Method End Type Type paddle Extends entity Field sp#,cpu:Int,score:Int Method Draw() SetColor(255,255,255) DrawRect(x,y,10,75) End Method Method New() sp#=4 cpu=0 End Method Function Create:paddle(x#,y#,sp#,cpu:Int) Local ent:paddle=New paddle ent.sp#=sp# ent.x#=x# ent.y#=y# ent.score=0 Return ent End Function End Type Type ball Extends entity Field sp#,dirx#,diry# Method Draw() SetColor(255,255,255) DrawRect(x,y,10,10) End Method Method New() sp#=1 End Method Function Create:ball(x#,y#,sp#) Local ent:ball=New ball ent.sp#=sp# ent.x#=x# ent.y#=y# Return ent End Function End Type Type background Extends entity Method Draw() SetColor(255,255,255) DrawRect(0,0,800,10) DrawRect(0,590,800,10) Local n:Int=10 For Local i:Int=0 To 10 DrawRect(395,n,10,35) n=n+55 Next SetScale(5,5) DrawText("0",250,20) DrawText("0",510,20) SetScale(1,1) End Method Function Create:background() Local ent:background=New background Return ent End Function End Type
First off, does anyone see me doing anything wrong? The above code should just show a static pong game. This is where I need help though. I'm trying to get everything to update. The best place to update each type would be in each of their Draw() methods would it not? Stuff like x=x+1 and player controls. The problem is, if I put "If KeyDown(KEY_UP) Then y=y-4" inside of the paddle type's Draw() method, then it would move both of the paddles rather than just one of them moving. How do I distinguish one paddle(obj1) from the other paddle(obj2), yet keep the code in the Draw() method?
EDIT: Another thing. If I were to make a menu for this pong, would I make the menu another type which extends entity or would I just not make it a type at all?
EDIT2: You may have noticed also that in the background type's Draw() method, the scores(text) which appear on the screen are set to strings ("0"), rather than obj1.score and obj2.score. I couldn't figure out how to let those fields have access to the background type without messing up the way everything is drawn in the main loop. Any ideas?
EDIT3: Also, I put all of my types below my main loop. I just like them there(like my functions). Is there any difference between putting types above your main loop or putting them below? Or is it just personal preference?