Last Active: Oct 26, 2024
Threads:
615
Posts:
7,941
Reputation:
86
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?
Last Active: Mar 04, 2024
Threads:
1
Posts:
18
Reputation:
1
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