とりあえず自機の移動(A,Dキー)と弾発射だけしてみました。
参考に・・・なるかどうか分かりません。
敵機は入れるとまた大きくなりすぎるので、やめました。
java
1package teratail_java.q370706;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.util.ArrayList;
6import java.util.Iterator;
7import java.util.List;
8
9import javax.swing.JFrame;
10import javax.swing.JPanel;
11import javax.swing.Timer;
12
13public class Q370706 extends JFrame {
14 public static void main(String[] args) {
15 new Q370706().setVisible(true);
16 }
17 Q370706() {
18 super("Q370706");
19 setDefaultCloseOperation(EXIT_ON_CLOSE);
20
21 GamePanel panel = new GamePanel();
22 add(panel);
23 pack();
24
25 panel.start();
26 }
27}
28
29class GamePanel extends JPanel {
30 public static final int WIDTH = 300;
31 public static final int HEIGHT = 300;
32
33 private Jiki jiki;
34 private Controller controller;
35
36 private List<Tama> tamaList = new ArrayList<Tama>();
37 GamePanel() {
38 super(null);
39 setPreferredSize(new Dimension(WIDTH,HEIGHT));
40 setMinimumSize(getPreferredSize());
41 setFocusable(true);
42 setBackground(Color.BLACK);
43
44 jiki = new Jiki(this, (WIDTH-Jiki.WIDTH)/2, HEIGHT-50);
45
46 controller = new Controller();
47 addMouseListener(controller);
48 addKeyListener(controller);
49 }
50
51 void start() {
52 new Timer(100, new ActionListener() {
53 public void actionPerformed(ActionEvent evt) {
54 int dx = controller.getDeltaX();
55 jiki.setDeltaX(dx);
56 jiki.move();
57 if(intersect(jiki)) { //外に出そうなら戻す
58 jiki.setDeltaX(-dx);
59 jiki.move();
60 }
61 if(controller.fire) {
62 jiki.fire();
63 controller.fire = false;
64 }
65 synchronized (tamaList) {
66 for(Iterator<Tama> ite=tamaList.iterator(); ite.hasNext(); ) {
67 Tama tama = ite.next();
68 tama.move();
69 if(!contains(tama)) ite.remove();
70 }
71 }
72 repaint();
73 }
74 }).start();
75 }
76 //一部でもパネルの中
77 boolean contains(GameObject obj) {
78 return 0 <= obj.x+obj.w && obj.x < WIDTH &&
79 0 <= obj.y+obj.h && obj.y < HEIGHT;
80 }
81 //一部(以上)パネルの外
82 boolean intersect(GameObject obj) {
83 return obj.x < 0 || WIDTH < obj.x+obj.w ||
84 obj.y < 0 || HEIGHT < obj.y+obj.h;
85 }
86
87 int countTama(IFF iff) {
88 int count = 0;
89 for(Tama tama : tamaList) if(tama.iff == iff) count++;
90 return count;
91 }
92 void add(Tama tama) {
93 tamaList.add(tama);
94 }
95 void remove(Tama tama) {
96 tamaList.remove(tama);
97 }
98
99 @Override
100 public void paintComponent(Graphics g) {
101 super.paintComponent(g);
102
103 Graphics2D g2 = (Graphics2D)g;
104 jiki.draw(g2);
105 synchronized (tamaList) {
106 for(Tama tama : tamaList) tama.draw(g2);
107 }
108 }
109
110 class Controller extends MouseAdapter implements KeyListener {
111 private static final int LEFT_KEY = KeyEvent.VK_A;
112 private static final int RIGHT_KEY = KeyEvent.VK_D;
113 private static final int DELTA = 10;
114
115 private boolean left, right;
116 private boolean fire;
117
118 void setLeft(boolean left) { this.left = left; }
119 void setRight(boolean right) { this.right = right; }
120 void setFire(boolean fire) { this.fire = fire; }
121
122 int getDeltaX() {
123 return left == right ? 0 : right ? DELTA : -DELTA;
124 }
125
126 @Override
127 public void mouseClicked(MouseEvent e) {
128 setFire(true);
129 }
130
131 @Override
132 public void keyTyped(KeyEvent e) {} //ignore
133 @Override
134 public void keyReleased(KeyEvent e) {
135 if(e.getKeyCode() == LEFT_KEY) setLeft(false);
136 if(e.getKeyCode() == RIGHT_KEY) setRight(false);
137 }
138 @Override
139 public void keyPressed(KeyEvent e) {
140 if(e.getKeyCode() == LEFT_KEY) setLeft(true);
141 if(e.getKeyCode() == RIGHT_KEY) setRight(true);
142 }
143 }
144
145 enum IFF {
146 A, B;
147 }
148
149 abstract class GameObject {
150 protected GamePanel world;
151 protected int x, y; //左上座標
152 protected int w, h; //幅, 高さ
153 protected int dx, dy; //移動距離
154 protected IFF iff; //敵味方識別
155
156 GameObject(GamePanel world, IFF iff, int x, int y, int w, int h) {
157 this.world = world;
158 this.iff = iff;
159 this.x = x;
160 this.y = y;
161 this.w = w;
162 this.h = h;
163 }
164
165 void move() {
166 x += dx;
167 y += dy;
168 }
169
170 abstract void draw(Graphics2D g);
171 }
172
173 abstract class GameTarget extends GameObject {
174 GameTarget(GamePanel world, IFF iff, int x, int y, int w, int h) {
175 super(world, iff, x, y, w, h);
176 }
177
178 void fire() {
179 Tama tama = createTama(world.countTama(iff));
180 if(tama != null) world.add(tama);
181 }
182
183 abstract Tama createTama(int count);
184 }
185
186 class Jiki extends GameTarget {
187 public static final int WIDTH = 20;
188 public static final int HEIGHT = 20;
189 private static final int TAMA_MAX = 5;
190
191 private int nPoints = 3;
192 private int[] xPoints = new int[nPoints];
193 private int[] yPoints = new int[nPoints];
194
195 Jiki(GamePanel world, int x, int y) {
196 super(world, IFF.A, x, y, WIDTH, HEIGHT);
197 setPolygon();
198 }
199
200 void setDeltaX(int dx) {
201 this.dx = dx;
202 }
203
204 void setPolygon() {
205 xPoints[0] = x+10;
206 yPoints[0] = y;
207 xPoints[1] = x;
208 yPoints[1] = y+20;
209 xPoints[2] = x+20;
210 yPoints[2] = y+20;
211 }
212
213 @Override
214 Tama createTama(int count) {
215 if(count >= TAMA_MAX) return null;
216 return new Tama(world, iff, x+(WIDTH-Tama.WIDTH)/2, y-Tama.HEIGHT, -10);
217 }
218 @Override
219 void draw(Graphics2D g) {
220 g.setColor(Color.GREEN);
221 setPolygon();
222 g.fillPolygon(xPoints, yPoints, nPoints);
223 }
224 }
225
226 //class Teki extends GameTarget {
227 //}
228
229 class Tama extends GameObject {
230 public static final int WIDTH = 5;
231 public static final int HEIGHT = 10;
232
233 Tama(GamePanel world, IFF iff, int x, int y, int dy) {
234 super(world, iff, x, y, WIDTH, HEIGHT);
235 this.dy = dy;
236 }
237
238 @Override
239 void draw(Graphics2D g) {
240 g.setColor(Color.YELLOW);
241 g.fillOval(x, y, w, h);
242 }
243 }
244}