renamed package
This commit is contained in:
86
src/de/miaurizius/jgame2d/core/GamePanel.java
Normal file
86
src/de/miaurizius/jgame2d/core/GamePanel.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package de.miaurizius.jgame2d.core;
|
||||
|
||||
import de.miaurizius.jgame2d.entity.Player;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class GamePanel extends JPanel implements Runnable {
|
||||
|
||||
// SCREEN SETTINGS
|
||||
final int originalTileSize = 16; //16x16 tile
|
||||
final int scale = 3;
|
||||
|
||||
public final int tileSize = originalTileSize * scale; //48x48 tile
|
||||
// 4:3 ratio
|
||||
final int maxScreenCol = 16;
|
||||
final int maxScreenRow = 12;
|
||||
final int screenWidth = tileSize * maxScreenCol; // 768 pixels
|
||||
final int screenHeight = tileSize * maxScreenRow; // 576 pixels
|
||||
|
||||
//FPS
|
||||
int FPS = 60;
|
||||
|
||||
KeyHandler keyH = new KeyHandler();
|
||||
Thread gameThread;
|
||||
Player player = new Player(this, keyH);
|
||||
|
||||
//Default position
|
||||
int playerX = 100;
|
||||
int playerY = 100;
|
||||
int playerSpeed = 4;
|
||||
|
||||
public GamePanel() {
|
||||
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
|
||||
this.setBackground(Color.black);
|
||||
this.setDoubleBuffered(true);
|
||||
this.addKeyListener(keyH);
|
||||
this.setFocusable(true);
|
||||
}
|
||||
|
||||
public void startGameThread() {
|
||||
gameThread = new Thread(this);
|
||||
gameThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
double drawInterval = 1000000000 / FPS;
|
||||
double delta = 0;
|
||||
double lastTime = System.nanoTime();
|
||||
long currentTime;
|
||||
long timer = 0;
|
||||
int drawCount = 0;
|
||||
|
||||
while(gameThread != null) {
|
||||
currentTime = System.nanoTime();
|
||||
delta += (currentTime - lastTime) / drawInterval;
|
||||
timer += currentTime - lastTime;
|
||||
lastTime = currentTime;
|
||||
|
||||
if(delta >= 1) {
|
||||
update();
|
||||
repaint();
|
||||
delta--;
|
||||
drawCount++;
|
||||
}
|
||||
if(timer >= 1000000000) {
|
||||
System.out.println("FPS: " + drawCount);
|
||||
drawCount = 0;
|
||||
timer = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
player.update();
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics graphics) {
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2d = (Graphics2D) graphics;
|
||||
player.draw(graphics2d);
|
||||
graphics.dispose();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user