How should I split my project in different classes?
#1
When you code your project and you use object-oriented programming concepts, how would you split up your classes?

I'm trying to code a simple game demo and I am looking to use Java to make a GUI. So far, I am having trouble splitting up classes because I really have no organisational
skills whatsoever!

I am currently making a shooter where I control a crosshair on the screen via mouse and shoot flying disembodied heads. I understand how to program to register a shot, but what I don't know about is how I should split it up. I understand, though, that there will be more than one time at which I have to split up the work into different classes.

My question, if not made clearly through this thread, is, how to organise?
Reply
#2
Well, in my opinion, the Readability and maintainability of your code can be significantly increased by structuring it in an object-oriented manner. In the context of a simple game like your shooter, you can consider organizing your classes based on different components and responsibilities.
Here is the simple code for your kind reference.

// Game.java
public class Game {
public static void main(String[] args) {
GameWindow gameWindow = new GameWindow();
Player player = new Player();
Enemy enemy = new Enemy();
Bullet bullet = new Bullet();
CollisionManager collisionManager = new CollisionManager();

// Game loop
while (true) {
player.update();
enemy.update();
bullet.update();

collisionManager.checkCollisions(player, enemy, bullet);

gameWindow.render(player, enemy, bullet);
}
}
}

// Player.java
public class Player {
private int x, y; // player position

public void update() {
// handle player input and update position
}

public void shoot() {
// logic for shooting
}
}

// Enemy.java
public class Enemy {
private int x, y; // enemy position

public void update() {
// logic for enemy movement and behavior
}
}

// Bullet.java
public class Bullet {
private int x, y; // bullet position

public void update() {
// logic for bullet movement
}
}

// GameWindow.java
public class GameWindow {
public void render(Player player, Enemy enemy, Bullet bullet) {
// rendering logic
}
}

// CollisionManager.java
public class CollisionManager {
public void checkCollisions(Player player, Enemy enemy, Bullet bullet) {
// collision detection logic
}
}
// Game.java
public class Game {
public static void main(String[] args) {
GameWindow gameWindow = new GameWindow();
Player player = new Player();
Enemy enemy = new Enemy();
Bullet bullet = new Bullet();
CollisionManager collisionManager = new CollisionManager();

// Game loop
while (true) {
player.update();
enemy.update();
bullet.update();

collisionManager.checkCollisions(player, enemy, bullet);

gameWindow.render(player, enemy, bullet);
}
}
}

// Player.java
public class Player {
private int x, y; // player position

public void update() {
// handle player input and update position
}

public void shoot() {
// logic for shooting
}
}

// Enemy.java
public class Enemy {
private int x, y; // enemy position

public void update() {
// logic for enemy movement and behavior
}
}

// Bullet.java
public class Bullet {
private int x, y; // bullet position

public void update() {
// logic for bullet movement
}
}

// GameWindow.java
public class GameWindow {
public void render(Player player, Enemy enemy, Bullet bullet) {
// rendering logic
}
}

// CollisionManager.java
public class CollisionManager {
public void checkCollisions(Player player, Enemy enemy, Bullet bullet) {
// collision detection logic
}
}

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Torrentii Project - Search, Download, Protect! Qarizma 0 16,250 Dec 16, 2013, 13:28 pm
Last Post: Qarizma
  Auto-torrenting Program - Should I make this? slicedt 1 17,037 Nov 07, 2013, 06:16 am
Last Post: NIK



Users browsing this thread: 1 Guest(s)