-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwoPlayerShooterGamee.java
More file actions
181 lines (137 loc) · 4.41 KB
/
TwoPlayerShooterGamee.java
File metadata and controls
181 lines (137 loc) · 4.41 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
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class TwoPlayerShooterGamee extends JPanel{
int WINDOW_WIDTH = 1300;
int WINDOW_HEIGHT = 720;
int playerScore = 0;
int bossScore = 0;
int winCount = 0;
Boss boss = new Boss(this);
Player player = new Player(this);
ArrayList<Bullet> playerBulletList = new ArrayList<Bullet>();
ArrayList<Bullet> bossBulletList = new ArrayList<Bullet>();
public TwoPlayerShooterGamee(){
addKeyListener(new KeyListener() {
//boolean activeKey = true; //activeKey set up so can only move in one direction at a time
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_UP ||
code == KeyEvent.VK_DOWN)
player.keyReleased(e);
else if(code == KeyEvent.VK_W || code == KeyEvent.VK_A || code == KeyEvent.VK_S ||
code == KeyEvent.VK_D)
boss.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
boss.keyPressed(e);
}
});
setFocusable(true);
}
public void addPlayerBullet(Bullet bullet){
playerBulletList.add(bullet);
}
public void addBossBullet(Bullet bullet){
bossBulletList.add(bullet);
}
private void move() {
if(playerScore >= winCount)
playerWon();
else if(bossScore >= winCount)
bossWon();
player.move();
boss.move();
for(int i = 0; i < playerBulletList.size(); i++){
playerBulletList.get(i).move();
}
for(int i = 0; i < bossBulletList.size(); i++){
bossBulletList.get(i).move();
}
}
public void playerWon(){
JOptionPane.showMessageDialog(null, player.getName()+" has won!", "Game Over!", JOptionPane.YES_NO_OPTION);
System.exit(0);
}
public void bossWon(){
JOptionPane.showMessageDialog(null, boss.getName()+" has won!", "Game Over!", JOptionPane.YES_NO_OPTION);
System.exit(0);
}
public void resetGame(){
player.reset();
boss.reset();
}
public void addOneToScore(){
playerScore += 1;
}
public void getPlayerNames()
{
String player1Name = JOptionPane.showInputDialog("Enter The Name of First Player!");
player.setName(player1Name);
String player2Name = JOptionPane.showInputDialog("Enter The Name of Second Player!");
boss.setName(player2Name);
}
public int getWinScore(){
int score = 0;
try{
score = Integer.parseInt(JOptionPane.showInputDialog(null, "What would you like the winning score to be?", "Winning Score"));
return score;
}catch(Exception e){
return getWinScore();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
this.setBackground(Color.WHITE);
g2d.setColor(Color.ORANGE);
g2d.fillRect(3, WINDOW_HEIGHT/2 - 10, WINDOW_WIDTH, 20);
player.paint(g2d);
boss.paint(g2d);
for(int i = 0; i < playerBulletList.size(); i++){
playerBulletList.get(i).paint(g2d);
}
for(int i = 0; i < bossBulletList.size(); i++){
bossBulletList.get(i).paint(g2d);
}
g2d.setColor(Color.GRAY);
g2d.setFont(new Font("Verdana", Font.BOLD, 30));
g2d.drawString(String.valueOf(boss.getName()), 10, 30);
g2d.drawString(String.valueOf(bossScore), 25, 55);
g2d.setFont(new Font("Verdana", Font.BOLD, 30));
g2d.drawString(String.valueOf(player.getName()), 10, WINDOW_HEIGHT - 75);
g2d.drawString(String.valueOf(playerScore), 25, WINDOW_HEIGHT - 50);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Shooter");
TwoPlayerShooterGamee game = new TwoPlayerShooterGamee();
frame.add(game);
frame.setSize(game.WINDOW_WIDTH, game.WINDOW_HEIGHT);
frame.setVisible(true);
game.getPlayerNames();
game.winCount = game.getWinScore();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
}