-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGame.java
More file actions
204 lines (170 loc) · 4.26 KB
/
Game.java
File metadata and controls
204 lines (170 loc) · 4.26 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
class line extends JComponent {
static int y = 370;
@Override
protected void paintComponent(Graphics arg0) {
super.paintComponent(arg0);
arg0.setColor(Color.LIGHT_GRAY);
arg0.drawLine(0,400 , 800, 400);
arg0.drawRect(90, y, 30, 30);
arg0.fillRect(90,y,30,30);
}
}
class RectEnemies extends JComponent {
static Random r = new Random();
static int x1 = 800;
static int x2 = x1 + r.nextInt(250) + 200;
static int x3 = x2 + r.nextInt(250) + 200;
static int x4 = x3 + r.nextInt(250) + 200;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(g.getFont().deriveFont(g.getFont().getSize() * 1.6F));
g.drawString("Score:" + Game.score, 600, 50);
g.setColor(Color.ORANGE);
g.drawRect(x1, 330, 25, 70);
g.fillRect(x1, 330, 25, 70);
g.setColor(Color.red);
g.drawRect(x2, 330, 25, 70);
g.fillRect(x2, 330, 25, 70);
g.setColor(Color.MAGENTA);
g.drawRect(x3, 330, 25, 70);
g.fillRect(x3, 330, 25, 70);
g.setColor(Color.GREEN);
g.drawRect(x4, 330, 25, 70);
g.fillRect(x4, 330, 25, 70);
}
}
class enemy extends Thread {
static Random r = new Random();
enemy() {
super("enemies");
start();
}
void moveLeft() {
RectEnemies.x1-=5;
RectEnemies.x2-=5;
RectEnemies.x3-=5;
RectEnemies.x4-=5;
}
void checkCollision() {
if(RectEnemies.x1<125 && RectEnemies.x1>75)
if(line.y>=330)
Game.gameOver = true;
if(RectEnemies.x2<125 && RectEnemies.x2>75)
if(line.y>330)
Game.gameOver = true;
if(RectEnemies.x3<125 && RectEnemies.x3>75)
if(line.y>330)
Game.gameOver = true;
if(RectEnemies.x4<125 && RectEnemies.x4>75)
if(line.y>330)
Game.gameOver = true;
}
void resetEnemy() {
if(RectEnemies.x1<0) {
RectEnemies.x1 = 800 + (360+RectEnemies.x4);
Game.score++;
}
if(RectEnemies.x2<0) {
RectEnemies.x2 = 800 + (400+RectEnemies.x1) + r.nextInt(250) + 100;
Game.score++;
}
if(RectEnemies.x3<0) {
RectEnemies.x3 = 800 + (600+RectEnemies.x4) + r.nextInt(250) + 100;
Game.score++;
}
if(RectEnemies.x4<0) {
RectEnemies.x4 = 800 + (200+RectEnemies.x3) + r.nextInt(250) + 100;
Game.score++;
}
}
@Override
public void run() {
while(!Game.gameOver) {
try {
sleep(18);
} catch(Exception e) {
System.out.print(e);
}
checkCollision();
resetEnemy();
moveLeft();
Main.frame.getContentPane().add(new RectEnemies());
Main.frame.setVisible(true);
}
new GameOver();
}
}
class move extends Thread {
move() {
new Thread(this,"move box");
}
@Override
public void run() {
if(!Game.gameOver) {
while(line.y<=370 && line.y>230) {
line.y-=7;
Main.frame.getContentPane().add(new line());
Main.frame.setVisible(true);
try {
sleep(25);
} catch(Exception e) {
System.out.print(e);
}
}
while(line.y<370) {
Main.frame.getContentPane().add(new line());
Main.frame.setVisible(true);
line.y+=7;
try {
sleep(25);
} catch(Exception e) {
System.out.print(e);
}
}
line.y = 370;
Game.threadRunning = false;
}
}
}
public class Game {
static boolean threadRunning;
static boolean gameOver;
static int score;
Game() {
Main.gameplaying = true;
gameOver = false;
threadRunning = false;
score = 0;
Main.frame.getContentPane().removeAll();
Main.frame.repaint();
new enemy();
Main.frame.getContentPane().add(new line());
Main.frame.setVisible(true);
Main.frame.addKeyListener(new KeyListener(){
@Override
public void keyTyped(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyPressed(KeyEvent arg0) {
move m = new move();
if(Main.gameplaying) {
if(!gameOver) {
if(!threadRunning) {
threadRunning = true;
m.start();
}
}
}
}
});
}
}