Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help Getting Started with OOP Game Programming  (Read 2236 times)
0 Members and 1 Guest are viewing this topic.
Offline everbeard

Senior Newbie

« Posted 2012-02-20 01:32:23 »

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 Smiley
Offline ReBirth
« Reply #1 - Posted 2012-02-20 02:34:16 »

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.

Offline everbeard

Senior Newbie

« Reply #2 - Posted 2012-02-20 02:37:00 »

Brilliant Smiley Thanks very much.

I'll be sticking around with more questions. I'll try passing the graphics object around and see if it works.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline everbeard

Senior Newbie

« Reply #3 - Posted 2012-02-20 06:08:19 »

Hmmmm.... did you mean drawing all of the objects inside the paint method of the JPanel?
Online ra4king

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

« Reply #4 - Posted 2012-02-20 06:28:27 »

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)

Offline everbeard

Senior Newbie

« Reply #5 - Posted 2012-02-20 06:29:45 »

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!
Online ra4king

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

« Reply #6 - Posted 2012-02-20 06:32:37 »

If it's simple and it's efficient = it's right Smiley

Offline everbeard

Senior Newbie

« Reply #7 - Posted 2012-02-20 06:38:37 »

 Tongue Woohoo Smiley 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 Smiley

Offline krasse
« Reply #8 - Posted 2012-02-20 13:26:45 »

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.

Offline everbeard

Senior Newbie

« Reply #9 - Posted 2012-02-20 19:38:11 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #10 - Posted 2012-02-20 20:16:33 »

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.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Riven (24 views)
2015-07-27 16:38:00

Riven (14 views)
2015-07-27 15:35:20

Riven (19 views)
2015-07-27 12:26:13

Riven (9 views)
2015-07-27 12:23:39

BurntPizza (24 views)
2015-07-25 00:14:37

BurntPizza (36 views)
2015-07-24 22:06:39

BurntPizza (20 views)
2015-07-24 06:06:53

NoxInc (23 views)
2015-07-22 22:16:53

NoxInc (15 views)
2015-07-22 22:13:39

Jesse (36 views)
2015-07-22 03:10:36
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!