Skip to main content
gamedev.net
🔒 Locked

Simple OOP-Problem...

Started by Roderik Jul 8, 2000 at 6:50 AM 0 replies 550+ views
Original Post
Roderik
Roderik
Ok, so I have been trying to learn OOP recently, I read books, I tried some simple sample programs. A few days ago I thought I knew enough about C++ to make a little Pong-game in OOP. So at first, I thought, how would I do it? I came up with the following plan: I made a class for the Bats, which has a "draw()" and a "move()"-method, a "Control" Method, which accepts Keyboardinput according to wether it is the right or the left bat and some interface methods to acces and set the position; then I had a Ball-class, again with drawing, moving and Interfacemethods. In the main program file (which uses the modules where the classes are declared and defined) I made two global instances of the bats and one of the ball. Then I ran into a problem I haven''t foreseen; how does the Ball know where the Bats are, when he moves? I mean, there are 2 instances of bats and a Ball-instance, but they aren''t really "connected" (I put them into seperate modules, and there the methods of the ball knew of course nothing about the 2 bats); so my question is, how do I solve this problem in an elegant an "OOP" way?
--------------------------Ghosts crowd the young child's fragile eggshell mind...
JOL
JOL
A simple solution would be to let them both inherit from the same class, and call that class a sprite or something. That class will have methods for collisiondetection, something like this.

Bat bt = new Bat()
Ball bl = new Ball()

if(bt.collidesWith(bl))
COLLISION
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.