-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpaceInvadersPanel.java
182 lines (150 loc) · 4.35 KB
/
SpaceInvadersPanel.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
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.awt.Graphics;
import javax.swing.*;
//git test git test
import java.util.*;
public class SpaceInvadersPanel extends JPanel
{
public static SpaceInvadersPanel panel;
public Shot shot;
public static Player player;
public long milis = 0;
public Graphics graphics;
public static ArrayList<Shot> shots = new ArrayList<Shot>();
public static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
public static MoveEnemies moveEnemies;
public Image playerImage;
public static Stopwatch stopwatch;
public static JFrame f;
public static ArrayList<EnemyShot> enemyShots = new ArrayList<EnemyShot>();
public SpaceInvadersPanel()
{
setBackground(Color.white);
shot = new Shot(this);
player = new Player(this);
setFocusable(true);
addKeyListener(new KL());
player.start();
stopwatch = new Stopwatch();
stopwatch.start();
for(int i = 0; i < 10; i++)
{
enemies.add(new Squid(50 * i + 50, 100, this));
}
for(int i = 0; i < 10; i++)
{
enemies.add(new Squid(50 * i + 50, 150, this));
}
for(int i = 0; i < 10; i++)
{
enemies.add(new Crab(50 * i + 50, 200, this));
}
for(int i = 0; i < 10; i++)
{
enemies.add(new Octopus(50 * i + 50, 250, this));
}
moveEnemies = new MoveEnemies(enemies, this);
moveEnemies.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//shot.draw(g);
try {
player.draw(g);
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!shots.isEmpty())
{
for(int i = 0; i < shots.size(); i++)
{
shots.get(i).draw(g);
}
}
if(!enemies.isEmpty())
{
for(int i = 0; i < enemies.size(); i++)
{
enemies.get(i).draw(g);
}
}
if(!enemyShots.isEmpty())
{
for(int i = 0; i < enemyShots.size(); i++)
{
enemyShots.get(i).draw(g);
}
}
g.drawImage(playerImage, 200, 600, 100, 100, null);
}
public static void main(String[] args)
{
f = new JFrame("SpaceInvadersV0.1");
panel = new SpaceInvadersPanel();
f.add(panel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600,700);
f.setResizable(false);
f.setVisible(true);
f.setFocusable(false);
}
class KL extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
if(stopwatch.hundredthes > 25)
{
Shot shot1 = new Shot(player.x + (player.width)/2, 600, 5, 10, 10, panel);
Shot.makeSound();
shots.add(shot1);
shot1.start();
stopwatch.reset();
stopwatch.start();
}
panel.repaint();
}
if(e.getKeyCode() == KeyEvent.VK_D)
{
player.dir = 1;
}
if(e.getKeyCode() == KeyEvent.VK_A)
player.dir = -1;
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_A)
player.dir = 0;
}
}
public static void endDialog()
{
if(JOptionPane.showConfirmDialog(null, "would you like to play again?", "Game over, play time: ", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
f.dispose();
main(null);
}
else
{
System.exit(0);
}
}
public static void println()
{
System.out.println();
}
public static void print(String str)
{
System.out.print(str);
}
public static void println(String str)
{
System.out.println(str);
}
}