-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamePanel.java
99 lines (73 loc) · 2.34 KB
/
GamePanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main;
import entity.Player;
import tile.TileManager;
import javax.swing.JPanel;
import java.awt.*;
public class GamePanel extends JPanel implements Runnable{
//screen
final int originalTileSize = 16;
final int scale = 3;
public final int tileSize = originalTileSize * scale;
public final int maxScreenCol = 16;
public final int maxScreenRow = 12;
public final int screenWidth = tileSize * maxScreenCol;
public final int screenHeight = tileSize * maxScreenRow;
//world setting
public final int maxWorldCol = 50;
public final int maxWorldRow = 50;
public final int worldWidth = tileSize * maxWorldCol;
public final int worldHeight = tileSize * maxWorldRow;
int FPS = 60;
TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler();
Thread gameThread;
public CollisionCheck cChecker = new CollisionCheck(this);
public Player player = new Player(this, keyH);
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() + drawInterval;
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 g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
tileM.draw(g2);
player.draw(g2);
g2.dispose();
}
}