-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
148 lines (130 loc) · 4.42 KB
/
Copy pathGame.java
File metadata and controls
148 lines (130 loc) · 4.42 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
import java.awt.Canvas;
import java.awt.image.BufferStrategy;
import java.awt.Graphics;
import java.awt.Color;
import java.util.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Font;
public class Game extends Canvas implements Runnable{
public static final int WIDTH = 960, HEIGHT = 720;
private Handler handler;
private Random r;
private HUD hud;
private Thread thread;
private boolean running = false;
public Game(){
r = new Random();
handler = new Handler();
this.addKeyListener(new KeyInput(handler));
new Window(WIDTH, HEIGHT, "Walid's Asteroid Game", this);
hud = new HUD();
handler.addObject(new Player(WIDTH / 2 - 12, HEIGHT / 2 - 12, ID.Player, handler));
//handler.addObject(new Powerup(r.nextInt(WIDTH - 16), r.nextInt(HEIGHT - 16), ID.Enemy, handler, Color.green));
//handler.addObject(new Player2(WIDTH / 2 - 24, HEIGHT / 2 - 12, ID.Player, handler));
spawnEnemies();
}
public void spawnEnemies() {
r = new Random();
for(int i = 0; i < 12; i++){
handler.addObject(new Enemy(r.nextInt(WIDTH - 16), r.nextInt(HEIGHT - 16), ID.Enemy, handler, Color.red));
//handler.addObject(new Enemy(r.nextInt(WIDTH - 16), r.nextInt(HEIGHT - 16), ID.Enemy, handler, Color.orange));
//handler.addObject(new Enemy(r.nextInt(WIDTH - 16), r.nextInt(HEIGHT - 16), ID.Enemy, handler, Color.red));
handler.addObject(new Enemy(r.nextInt(WIDTH - 16), r.nextInt(HEIGHT - 16), ID.Enemy, handler, Color.magenta));
//handler.addObject(new Enemy(r.nextInt(WIDTH - 16), r.nextInt(HEIGHT - 16), ID.Enemy, handler, Color.white));
//handler.addObject(new Enemy(r.nextInt(WIDTH - 16), r.nextInt(HEIGHT - 16), ID.Enemy, handler, Color.green));
}
}
public synchronized void start(){
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop(){
try {
thread.join();
running = false;
} catch(Exception e) {
e.printStackTrace();
}
}
// game loop
public void run(){
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
//System.out.println("FPS: " + frames);
}
}
stop();
}
private void tick() {
handler.tick();
hud.tick();
if(HUD.HEALTH == 0){
running = false;
Graphics g = getGraphics();
endScreen(g);
}
}
// creates 3 buffers
private void render() {
r = new Random();
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.darkGray);
g.fillRect(0, 0, WIDTH, HEIGHT);
// creates background with moving stars effect
g.setColor(Color.white);
g.fillOval(r.nextInt(WIDTH), r.nextInt(HEIGHT), 4, 4);
handler.render(g);
hud.render(g);
g.dispose();
bs.show();
}
public static int clamp(int var, int min, int max){
if(var >= max){
return var = max;
} else if(var <= min){
return var = min;
} else{
return var;
}
}
public void endScreen(Graphics g){
g.setColor(Color.darkGray);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.red);
Font lFont = new Font ("Calibri", 1, 40);
g.setFont(lFont);
g.drawString("L", WIDTH / 2 - 20, HEIGHT / 2);
Font scoreFont = new Font ("Calibri", 1, 20);
g.setFont(scoreFont);
g.setColor(Color.white);
g.drawString("score:" + (int)HUD.score, 5, 20);
}
public static void main (String args[]) {
new Game();
}
}