-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
173 lines (120 loc) · 2.98 KB
/
Copy pathGame.java
File metadata and controls
173 lines (120 loc) · 2.98 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package remake2048;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Game extends JPanel implements KeyListener, Runnable {
//board size
public static int WIDTH = 370;
public static int HEIGHT = 370;
//Font
public static final Font main = new Font("absender1.ttf", Font.PLAIN, 28);
private Thread game;
private boolean running;
private GameBoard board;
private CodeRunner runner;
//constructs game and sets starting requirements
public Game(CodeRunner runner){
this.runner = runner;
setLayout(null);
setFocusable(true);
setPreferredSize(new Dimension(WIDTH,HEIGHT));
addKeyListener(this);
runner.startGame();
board = new GameBoard(WIDTH/2 - GameBoard.BOARD_WIDTH/2, HEIGHT - GameBoard.BOARD_HEIGHT, runner.board.getArray());
}
//updates
private void update(){
if(Key.typed(KeyEvent.VK_LEFT)){
runner.excecuteTurn(2);
}else if(Key.typed(KeyEvent.VK_RIGHT)){
runner.excecuteTurn(3);
} else if(Key.typed(KeyEvent.VK_UP)) {
runner.excecuteTurn(0);
} else if(Key.typed(KeyEvent.VK_DOWN)) {
runner.excecuteTurn(1);
}
board.update(runner.board.getArray());
Key.update();
}
private void render(){
Graphics2D g2d = (Graphics2D) getGraphics();
board.render(g2d);
g2d.dispose();
}
//the main game loop
@Override
public void run() {
long fpsTimer = System.currentTimeMillis();
double nsPerUpdate = 1000000000.0 / 60;
double then = System.nanoTime();
double unprocessed = 0;
while(running){
boolean shouldRender = false;
double now = System.nanoTime();
unprocessed += (now - then) / nsPerUpdate;
then = now;
if(runner.gameOverTest()){
running = false;
}
while(unprocessed >=1 ){
update();
unprocessed--;
shouldRender = true;
}
if(shouldRender){
render();
shouldRender = false;
}else{
try{
Thread.sleep(1);
}catch(Exception e){
e.printStackTrace();
}
}
}
if(System.currentTimeMillis() - fpsTimer > 1000){
fpsTimer += 1000;
System.out.println();
System.out.println("Game Over");
System.out.println();
System.out.println("thanks for playing");
}
}
// starts the game
public synchronized void start(){
if (running)
return;
running = true;
game = new Thread(this, "game");
game.start();
}
//stops the game
public synchronized void stop(){
if(!running)
return;
running = false;
System.exit(0);
}
@Override
//records a key code for a key that is pressed
public void keyPressed(KeyEvent e) {
Key.KeyPressed(e);
}
@Override
//records the key code for a key that is released
public void keyReleased(KeyEvent e) {
Key.keyReleased(e);
}
@Override
//records previosly typed keys
public void keyTyped(KeyEvent e) {
}
}