Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 1968 times)
0 Members and 1 Guest are viewing this topic.
Offline everbeard

Senior Newbie





« Posted 2012-02-20 02: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 03: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 03: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 07:08:19 »

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

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-02-20 07: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.

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

Offline everbeard

Senior Newbie





« Reply #5 - Posted 2012-02-20 07: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!
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


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

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

Offline everbeard

Senior Newbie





« Reply #7 - Posted 2012-02-20 07: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 14: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 20: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 21: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (30 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!