Help Getting Started with OOP Game Programming

Hi! I’m new to the whole “Game Programming” scene here.

Well, here’s the thing.

I’ve already made one game. It works just fine, but the code is sloppy as anything… it’s pretty much all written in one class. For my next game, still very simple, I wanted to separate the common game items (the player, enemy, powerups, bullets, etc) into different classes, as a lot of games do.

My main question is: How can I paint each of the class’s sprites into one JPanel?

Also, should there be one central class where all the collisions are computed?

I know that’s absolute “Bare Basics” of OOP Game Programming, but I just need some help getting started.

Thanks in advance for your help :slight_smile:

  1. Get Graphics object from the active JPanel and pass it around each entity when updating.
  2. Collision, you can use distributed checking or single. On single, each entity should have method as interface to interact.

Brilliant :slight_smile: Thanks very much.

I’ll be sticking around with more questions. I’ll try passing the graphics object around and see if it works.

Hmmmm… did you mean drawing all of the objects inside the paint method of the JPanel?

No he meant that inside paintComponent method of JPanel, you loop through a list of all Entities and calling their “draw(Graphics2D g)” method, passing the Graphics object.


public void paintComponent(Graphics g) {
    for(Entity e : entityList)
        e.draw((Graphics2D)g);
}

Excellent. Is that efficient? That’s the way I did it in my last game. I’m going through the “If it’s simple, it can’t be right” mentality right now, lol!

If it’s simple and it’s efficient = it’s right :slight_smile:

:stuck_out_tongue: Woohoo :slight_smile: Thank you! I’m going to go clumsily stumble around my code for a while.

And I’m sure I’ll be back here within the hour :slight_smile:

I haven’t looked at it myself but the latest game from Mojang Catacomb Snatch might be a good example for developing games using OOP in Java.
It was the result of the 60 hour Mojam event this weekend.

Haha, I’m playing it right now. I’ve already looked through a lot of Notch’s old projects, and it’s been really helpful, but he can be a bit… cryptic… at times.

I think he prefers to obfuscate.

Or, at least that he believes that the shortest variable name is the best. Which you can understand after you read things on his blog. One of his older (As in a month or two ago) is talking about how he applied for a job at a gaming company, and they said that he was obviously self-taught. I think that’s one of the places where it really shows.

Most of the people who’ve been edjamadcated in computer programming, and especially Java, have a lot of stuff beaten into them. And the need for descriptive variable names is one of them. That, and not exposing class fields, etc. >.> Which makes things very difficult to read at times, especially if you’re expecting all of those conventions.

I’ve read the source of a bunch of his Ludum Dare projects, which probably aren’t the best for checking readability (For those who don’t know, Ludum Dare is a 48 Hour competition where the subject is made known at the start of the competition. Competitors have to design and code the game in a 48 hour time period, and they’re graded on several criteria, including how well their idea conforms to the subject matter. So a fully functioning game in those conditions probably is going to have readability issues.)

Something that I’ve noticed, at least on here, is that while a lot of projects end up using OOP Principles, but often ignore some of the more difficult/costly to apply ones in favor of writing workable code. Things like the Open-Closed Principle, and Data-Hiding get discarded because they tend to reduce performance/add boiler plate code.