-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameplay.java
207 lines (158 loc) · 4.97 KB
/
Gameplay.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
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
205
206
207
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class Gameplay extends JPanel implements ActionListener
{
// Game properties
private Timer timer;
private int delay = 8;
private boolean play = true;
// Event properties
private GameListener gameListener;
private BrickGenerator br;
public java.util.List<Tank> tankHolder = new java.util.ArrayList<Tank>();
public Gameplay()
{
br = new BrickGenerator();
Tank player1;
int MoveKeys[] = new int[]{KeyEvent.VK_D, KeyEvent.VK_W, KeyEvent.VK_A, KeyEvent.VK_S};
player1 = new Tank("assets\\tanks\\", 1, 0, 550, 5, "1", br, KeyEvent.VK_U, MoveKeys);
Tank player2;
int MoveKeys1[] = new int[]{KeyEvent.VK_RIGHT, KeyEvent.VK_UP, KeyEvent.VK_LEFT, KeyEvent.VK_DOWN};
player2 = new Tank("assets\\tanks\\", 1, 600, 0, 5, "2", br, KeyEvent.VK_ENTER, MoveKeys1);
tankHolder.add(player1);
tankHolder.add(player2);
setFocusable(true);
for(int i = 0; i < tankHolder.size(); i++){
addKeyListener(tankHolder.get(i).tankListener);
}
gameListener = new GameListener();
GameListener gameListener = new GameListener();
addKeyListener(gameListener);
setFocusTraversalKeysEnabled(false);
timer=new Timer(delay,this);
timer.start();
}
public boolean isColliding(Transform bullet, Transform tank){
if(new Rectangle(bullet.x, bullet.y, 5, 5)
.intersects(new Rectangle(tank.x, tank.y, 50, 50)))
{
return true;
}
return false;
}
public void paint(Graphics g)
{
// play background
g.setColor(new Color(248, 234, 144));
g.fillRect(0, 0, 650, 600);
// right side background
g.setColor(Color.DARK_GRAY);
g.fillRect(660, 0, 140, 600);
br.draw(this, g);
if(play)
{
g.setColor(Color.blue);
//for(int i = 0; i < tankHolder.size(); i++){
// tankHolder.get(i).DrawColRectangle(g);
//}
for(int i = 0; i < tankHolder.size(); i++){
for(int j = 0; j < tankHolder.size(); j++){
if(i != j && tankHolder.get(i).bullet != null){
if(isColliding(tankHolder.get(i).bullet.transform, tankHolder.get(j).transform)){
tankHolder.get(i).score += 10;
tankHolder.get(j).hp--;
tankHolder.get(i).bullet = null;
tankHolder.get(i).loaded = true;
}
}
}
}
for(int i = 0; i < tankHolder.size(); i++){
tankHolder.get(i).updateDir();
tankHolder.get(i).tankImage.paintIcon(this, g, tankHolder.get(i).transform.x, tankHolder.get(i).transform.y);
if (tankHolder.get(i).bullet != null)
{
tankHolder.get(i).shooting(g);
}
}
}
// the scores
g.setColor(Color.white);
g.setFont(new Font("serif", Font.BOLD, 15));
g.drawString("Scores", 700, 30);
int cursor = 60;
for(int i = 0; i < tankHolder.size(); i++){
g.drawString("Player " + (i + 1) + " : " + tankHolder.get(i).score, 670, cursor);
cursor += 30;
g.drawString("Player " + (i + 1) + " HP : " + tankHolder.get(i).hp, 670, cursor);
cursor += 30;
}
if(tankHolder.get(0).hp <= 0)
{
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 60));
g.setColor(Color.blue);
ImageIcon gameOverImage = new ImageIcon("assets\\gameover.png");
gameOverImage.paintIcon(this, g, 100, 100);
//g.fillRect(100, 100, 500, 400);
g.setColor(Color.white);
g.drawString("Game Over!", 200, 200);
g.drawString("Player 1 Won", 180,280);
play = false;
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 30));
g.drawString("(Space to Restart)", 230,430);
}
if(tankHolder.get(1).hp <= 0)
{
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 60));
g.setColor(Color.blue);
ImageIcon gameOverImage = new ImageIcon("assets\\gameover.png");
gameOverImage.paintIcon(this, g, 100, 100);
//g.fillRect(100, 100, 500, 400);
g.setColor(Color.white);
g.drawString("Game Over!", 200, 200);
g.drawString("Player 2 Won", 180,280);
play = false;
g.setColor(Color.white);
g.setFont(new Font("serif",Font.BOLD, 30));
g.drawString("(Space to Restart)", 230,430);
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
repaint();
}
public void Restart(){
br = new BrickGenerator();
tankHolder.get(0).transform.setPos(0, 550);
tankHolder.get(1).transform.setPos(600, 0);
for(int i = 0; i < tankHolder.size(); i++){
tankHolder.get(i).direction = 1;
tankHolder.get(i).updateDir();
tankHolder.get(i).score = 0;
tankHolder.get(i).hp = 5;
tankHolder.get(i).br = br;
tankHolder.get(i).bullet = null;
tankHolder.get(i).loaded = true;
}
play = true;
repaint();
}
private class GameListener implements KeyListener{
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_SPACE && (tankHolder.get(0).hp <= 0 || tankHolder.get(1).hp <= 0) )
{
Restart();
}
}
}
}