Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Kiepsko napisany kod


Rekomendowane odpowiedzi

Opublikowano

Siemka wszyscy. Jeśli ktoś miałby na tyle czasu oraz chęci prosiłbym o przeanalizowanie mojego (nie wiem czy mogę to tak nazwać) silnika 2d. W javie nie jestem jakimś super programistą lecz staram się jak najwięcej uczyć dlatego proszę o napisanie mi co zrobiłem źle, co mógłbym poprawić oraz co jest zupełnie do wywalenia. :) Z własnych obserwacji zauważyłem straszne skakanie kwadracika podczas przesuwania kamerą oraz raz na jakiś czas ramka rozszerza się i nie pozostaje przy podanych wymiarach.
Jest to mój pierwszy większy projekt dlatego za każdą okazaną pomoc będę bardzo wdzięczny. :)

Plik: Game.java


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel implements Runnable {

// Ctrl + Shift + f formatowanie kodu ;)

public static final String TITLE = "Cube And Stone";
public static final int WIDTH = 480;
public static final int HEIGHT = 315;

public static World world;
public static Camera camera;

public static KeyboardListener keyboard;

private Thread thread;


public boolean gameRunning = false;

public Game() {
setFocusable(true);
keyboard = new KeyboardListener();
addKeyListener(keyboard);

camera = new Camera();

world = new World();
world.createWorld();
}

public void start() {
if (thread == null) {
thread = new Thread(this, "Game Thread");
thread.start();

gameRunning = true;
}
}

public void stop() {
if (thread != null) {
thread = null;

gameRunning = false;
}
}

public void run() {
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
int lastFpsTime = 0;
int fps = 0;

while (gameRunning) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);

lastFpsTime += updateLength;
fps++;

if (lastFpsTime >= 1000000000) {
System.out.println("(FPS: " + fps + ")");
lastFpsTime = 0;
fps = 0;
}

update(delta);

repaint();

try {
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void update(double delta) {
world.update(delta);
}

@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponents(g);

g2d.setColor(new Color(255, 0, 255));
g2d.fillRect(0, 0, WIDTH, HEIGHT);

world.render(g2d);
}

public static void main(String[] args) {
Game game = new Game();

Dimension size = new Dimension(WIDTH, HEIGHT);

game.setMaximumSize(size);
game.setMinimumSize(size);
game.setPreferredSize(size);

JFrame frame = new JFrame(TITLE);
frame.add(game);
frame.setResizable(false);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocation((int) Toolkit.getDefaultToolkit().getScreenSize()
.getWidth()
/ 2 - WIDTH / 2, (int) Toolkit.getDefaultToolkit()
.getScreenSize().getHeight()
/ 2 - HEIGHT / 2);

game.start();
}

}

 

 

Plik: GameObject.java

 

import java.awt.Graphics2D;

public abstract class GameObject {

private boolean removed;

public GameObject() {
removed = false;
}


public void remove() {
removed = true;
}

public boolean isRemoved() {
return removed;
}


public abstract void update(double delta);
public abstract void render(Graphics2D g);
}

 

 

 

Plik: GameObjectManager.java

 

import java.awt.Graphics2D;
import java.util.ArrayList;

public class GameObjectManager extends ArrayList<GameObject> {

public void update(double delta) {
for (int i = 0; i < size(); i++) {

GameObject o = (GameObject) get(i);

if (!o.isRemoved()) {
o.update(delta);
} else {
remove(o);
}
}

for (int i = 0; i < size(); i++) {
for (int j = 0; j < size(); j++) {

GameObject o1 = (GameObject) get(i);
GameObject o2 = (GameObject) get(j);

if (o1 instanceof Entity && o2 instanceof Entity) {
Entity me = (Entity) get(i);
Entity him = (Entity) get(j);

if (me.checkCollision(him) && him.checkCollision(me)) {
me.collidedWith(him);
him.collidedWith(me);
}
}
}
}
}

public void render(Graphics2D g) {
for (int i = 0; i < size(); i++) {
GameObject o = (GameObject) get(i);
o.render(g);
}
}
}

 

 

 

Plik: KeyboardListener.java

 

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyboardListener implements KeyListener {

private boolean[] keys = new boolean[256];

//metoda wywoływana, gdy wciśniemy przycisk
@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}

//metoda wywoływana, gdy puścimy przycisk
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;

}

//w zasadzie to samo co keyPressed, tyle, że obsługuje jedynie klawisze posiadające odpowiednik Unicode. Nie zareaguje np. na przyciski funkcyjne
@Override
public void keyTyped(KeyEvent e) {
}

public boolean isKeyPressed(int key) {
return keys[key];
}

public boolean isKeyReleased(int key) {
return !keys[key];
}
}

 

 

 

Plik: TestEntity.java

 

 

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

public class TestEntity extends Entity {

private double speed = 1.9;

public TestEntity(double x, double y, double width, double height) {
super(x, y, width, height);
}

@Override
public void collidedWith(Entity e) {
}

@Override
public void update(double delta) {

if(x + width - Game.camera.getX() >= Game.WIDTH - 70) {
Game.camera.moveRight(speed * delta);
}

if(x - Game.camera.getX() <= 70) {
Game.camera.moveLeft(speed * delta);
}

if(y - Game.camera.getY() <= 70) {
Game.camera.moveUp(speed * delta);
}

if(y + height - Game.camera.getY() >= Game.HEIGHT - 70) {
Game.camera.moveDown(speed * delta);
}

if(Game.keyboard.isKeyPressed(KeyEvent.VK_A))
x -= speed * delta;
if(Game.keyboard.isKeyPressed(KeyEvent.VK_W))
y -= speed * delta;
if(Game.keyboard.isKeyPressed(KeyEvent.VK_S))
y += speed * delta;
if(Game.keyboard.isKeyPressed(KeyEvent.VK_D))
x += speed * delta;
}

@Override
public void render(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect((int) x - (int) Game.camera.getX(), (int) y - (int) Game.camera.getY(), (int) width, (int) height);
}
}

 

 

Plik: World.java

 

 

import java.awt.Graphics2D;

public class World {

private GameObjectManager gom;

public World() {
gom = new GameObjectManager();
}

public void createEntiy(Entity e) {
if(e != null)
gom.add(e);
}

public void createWorld() {
createEntiy(new TestEntity(10, 10, 25, 25));
}

public void update(double delta) {
gom.update(delta);
}

public void render(Graphics2D g) {
gom.render(g);
}
}

 

 

 

Plik: Camera.java

 

 

import java.util.Random;


public class Camera {

private static double x, y;

public Camera() {
x = 0;
y = 0;
}

public void moveRight(double value) {
x += value;
}

public void moveLeft(double value) {
x -= value;
}

public void moveUp(double value) {
y -= value;
}

public void moveDown(double value) {
y += value;
}

public void setX(double x) {
this.x = x;
}

public void setY(double y) {
this.y = y;
}

public double getX() {
return x;
}

public static double getY() {
return y;
}
}

 

 

Plik: Entity.java

 

 

import java.awt.Graphics2D;
import java.awt.Rectangle;

public abstract class Entity extends GameObject {

public double x;
public double y;

public double width;
public double height;

public Entity(double x, double y, double width, double height) {
this.x = x;
this.y = y;

this.width = width;
this.height = height;
}

public boolean checkCollision(double x, double y, double width, double height) {
if(new Rectangle((int) this.x, (int) this.y, (int) this.width, (int) this.height).intersects(x, y, width, height))
return true;
return false;
}

public boolean checkCollision(Entity e) {
if(new Rectangle((int) x, (int) y, (int) width, (int) height).intersects(e.x, e.y, e.width, e.height))
return true;
return false;
}

public abstract void collidedWith(Entity e);

public abstract void update(double delta);
public abstract void render(Graphics2D g);
}

 

2mmtzeu.jpg

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...