Hallo Leute, habe mich gestern an die Erstellung der Oberfläche für ein 2D Spiel (so wie Counterstrike 2D) gemacht. Nur habe ich noch ein paar Probleme nämlich erstens ich weiß nicht ob das so alles richtig ist wie ich das gemacht habe (Bufferstrategy etc) zweitens ist Awt überhaupt das richtige dafür? Wollte in mein Spiel Licht und Schatten einbauen so wie bei dem Spiel Left 4k Dead ist das so wie ich das im Moment aufgebaut habe überhaupt möglich, weil ich noch nicht verstanden habe wie ich ein Transparents Bild Lade etc? Und Drittens was ist der Unterschied zwischen Graphics und Graphics2D, habe gesehen das es bei Graphics2D ein paar Methoden mehr gibt, allerdings weiß ich nicht wie ich jetzt in meinem Code Graphics2D statt Graphics benutzen kann.
Java:
package Game;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import Controls.Keyboard;
import Controls.Mouse;
import Level.Level;
import Player.Player;
import Textures.Textures;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private static Properties properties = new Properties();
boolean running = false;
Player player;
Keyboard keyboard;
Mouse mouse;
Display display;
Level level;
Thread thread;
JFrame frame = new JFrame();
public Game(){
Dimension size = new Dimension(properties.Width()*properties.Scale(),properties.Height()*properties.Scale());
setPreferredSize(size);
display = new Display();
player = new Player();
keyboard = new Keyboard();
level = new Level();
mouse = new Mouse();
addKeyListener(keyboard);
addMouseMotionListener(mouse);
}
public static void main(String [] args){
Game game = new Game();
game.frame.setResizable(true);
game.frame.setTitle("CounterStrike 4D");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
public void start(){
running = true;
thread = new Thread(this, "Game");
thread.start();
}
public synchronized void stop(){
running = false;
try {
thread.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
final double ns = 1000000000.0 / properties.UPS(); // Hier Updates pro Sekunde einstellen
double delta = 0;
int frames = 0;
int updates = 0;
requestFocus();
while(running){
long now = System.nanoTime();
delta += (now-lastTime) / ns;
lastTime = now;
while (delta >= 1){
update();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000){
timer += 1000;
frame.setTitle( " | " + updates + " ups, " + frames + " fps");
updates = 0;
frames = 0;
}
}
stop();
}
public void update(){
keyboard.update();
double temp = frame.getWidth()/ properties.Width();
mouse.update(temp);
player.update(keyboard, level);
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0 , 0, properties.Width()*properties.Scale(), properties.Height()*properties.Scale());
level.render(player.posX(), player.posY(), g);
g.dispose();
bs.show();
}
}