-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
142 lines (132 loc) · 4.08 KB
/
Game.java
File metadata and controls
142 lines (132 loc) · 4.08 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
import java.awt.*;
import java.awt.image.BufferStrategy;
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class Game extends Canvas implements Runnable{
//dimensions for game
public static final int H = 600, W = H*9/12;
private Thread t; // thread for the game to run on - single threaded game
private boolean running = false;
private Handler h;
private KeyInput ki;
private Player p;
//pipe settings
public static int pipeSpeed = 5, pipeSpace = 75;
//death stuff
Scanner sc;
String msg = "";
public Game() throws Exception{
h = new Handler();
p = new Player(50, 50, h);
h.add(p);
ki = new KeyInput(h);
addKeyListener(ki);
sc = new Scanner(new File("death.txt"), StandardCharsets.UTF_8.name());
new Window(W, H, "REEEE", this);
}
public synchronized void start(){
t = new Thread(this);
running = true;
t.run(); //start the game on the game thread
}
public synchronized void stop(){
try{
t.join();
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
// start templated game loop
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
long timer2 = System.currentTimeMillis();
while(running){
long now = System.nanoTime();
delta += (now-lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();//custom function (we define)
delta--;
}
if(running){
render();//custom function
}
frames++;
//triggers every second
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
if(p.alive){
//randomly place the pipes every second
int y = (int)(Math.random()*350)+100;
h.add(new Pipe(y, p));
h.add(new ScoreZone(y));
}
}
if(System.currentTimeMillis() - timer2 > 35){
timer2 += 35;
//typing effect for death message
if(!p.alive){
removeKeyListener(ki);
//get the death message and update it
if(sc.hasNext()){
msg=sc.nextLine();
System.out.println(msg);
}
}
}
}
stop();
//end templated game loop
}
public void tick(){
h.tick();
}
public void render(){
//allow pre rendering of frames (to make game smoother)
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
//draw background
g.setColor(Color.black);
g.fillRect(0, 0, W, H);
h.render(g);
//fill in the floor
g.setColor(Color.green);
g.fillRect(0, 500, W, 100);
//do score board
g.setColor(Color.white);
g.drawString("Score: " + p.score, W/2-100, 50);
//render the player last so it appears on top
p.render(g);
if(!p.alive){
g.setColor(Color.red);
g.drawString(msg, p.x-(msg.length()*3/2), p.y);
}
bs.show();
g.dispose(); // clear the memory usage
}
public static int clamp(int curr, int min, int max){
curr = curr < min ? min : curr;
curr = curr > max ? max : curr;
return curr;
}
public static void main(String[] args){
try{
new Game();
}catch(Exception e){
e.printStackTrace();
}
}
}