-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameBoard.java
108 lines (71 loc) · 2.39 KB
/
gameBoard.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
100
101
102
103
104
105
106
107
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class gameBoard extends JPanel implements Runnable{
//Screen Settings
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;
public final int gameOverState = 4;
public final int charachterState = 4;
tileManager tiles = new tileManager(this);
KeyHandler keyH = new KeyHandler();
public Collision checker = new Collision(this);
Thread gameThread;
player player = new player(this, keyH);
public gameBoard(){
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() {
//important game loop
double drawInterval=1000000000/60; // FPS = 60
double delta = 0;
long 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();// update info such as charachter positions
repaint();
delta--;
drawCount++;
}
if(timer >= 1000000000){
System.out.println("FPS" + drawCount);
drawCount = 0;
timer = 0;
}
}
}
public void update(){ //update
player.update();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
tiles.draw(g2);
player.draw(g2);
g2.dispose();
}
}