OOP Pong

BlitzMax Forums/BlitzMax Beginners Area/OOP Pong

po(Posted 2006) [#1]
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?


Byteemoz(Posted 2006) [#2]
Hi.
1) The easiest thing is to give the paddle type an "up_key" and a "down_key" field, to initialise these values when you create the type and then use them within your drawing-method:
... = paddle.Create(790,265,4,False,KEY_UP,KEY_DOWN)

...
If cpu Then
    If KeyDown(up_key) then y=y-4
    If KeyDown(down_key) Then y=y+4
End If
...

2) I'd probably use a Function that does all the drawing (background, menu-entries, cursor, ...) manages the cursor control ("If KeyDown(KEY_UP) Then cursor=cursor-1") and returns an int when the user selects an entry (1=single player, 2=multiplayer, 3=highscores, ...).

3) You could either let the paddles draw the scores or give the background type a reference to the two paddle types.

4) It's personal preference and doesn't change how your prog works.

--Byteemoz


po(Posted 2006) [#3]
Ok, thanks for the help. I'll try all that stuff.