-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
104 lines (68 loc) · 1.84 KB
/
GamePanel.java
File metadata and controls
104 lines (68 loc) · 1.84 KB
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
100
101
102
103
104
//lala lalai lalai lala lai lai
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable {
final int originalTileSize = 16;
final int scale = 3;
public final int tileSize = originalTileSize * scale;
final int maxScreenCol = 30;
final int maxScreenRow = 20;
final int screenWidth = tileSize * maxScreenCol;
final int screenHeigth = tileSize * maxScreenRow;
int FPS = 50;
TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler();
Thread gameThread;
Monster monster = new Monster(this);
Player player = new Player(this, keyH);
Axe axe = new Axe(this);
public UI ui = new UI(this);
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeigth));
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 nextDrawTime = System.nanoTime() + drawInterval;
while(gameThread != null) {
update();
repaint();
try {
double remainingTime = nextDrawTime - System.nanoTime();
remainingTime = remainingTime / 1000000;
if (remainingTime < 0)
remainingTime = 0;
Thread.sleep((long)remainingTime);
nextDrawTime += drawInterval;
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void update() {
player.update();
axe.update();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
tileM.draw(g2);
player.draw(g2);
//ui.draw_sad(g2);
monster.draw(g2, true);
axe.draw(g2);
g2.dispose();
}
}