[英]shooting multiple bullets from the same class
我创建的以下代码段具有一个发射子弹的矩形。 我希望每次在空格键“召唤”一个新的项目符号时调用该项目符号类,并使它独立于最后一个项目符号。 一次在屏幕上创建多个项目符号实例的第一步是什么?
drawingComponent类:
package scratch;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.*;
public class drawingComponent extends JComponent implements KeyListener {
private Timer timer;
public int count = 0;
public Rectangle hello = new Rectangle(100, 300, 50, 50);
Integer x1 = hello.x;
Integer y1 = hello.y;
public Bullet bullet = new Bullet(hello.x, hello.y);
boolean fired = false;
public drawingComponent() {
addKeyListener(this);
}
public class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
count++;
bullet.fired();
repaint();
System.out.println(count + "seconds.");
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (fired == true) {
bullet.drawBullet(g);
}
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(255, 25, 0));
g2.setFont(new Font("monospace", Font.BOLD + Font.ITALIC, 30));
g2.drawString("nothing yet", 300, 320);
g2.fill(hello);
setFocusable(true);
requestFocus();
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
hello.y = hello.y - 1;
hello.setLocation(hello.x, hello.y);
repaint();
System.out.println(hello.y);
}
if (e.getKeyCode() == KeyEvent.VK_S) {
hello.y = hello.y + 1;
hello.setLocation(hello.x, hello.y);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_A) {
hello.x = hello.x - 1;
hello.setLocation(hello.x, hello.y);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_D) {
hello.x = hello.x + 1;
hello.setLocation(hello.x, hello.y);
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
bullet.update(hello.x, hello.y);
fired = true;
if (count > 5) {
timer.stop();
}
timer = new Timer(50, new TimerListener());
timer.start();
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
// fired = false;
repaint();
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
子弹类:
package scratch;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
public class Bullet {
Rectangle BulletRect = new Rectangle(20, 20, 20, 20);
public Bullet(int x, int y2) {
BulletRect.y = y2;
BulletRect.x = x;
}
public void fired() {
BulletRect.y = BulletRect.y + 50;
}
public void update(int x1, int y4) {
BulletRect.x = x1;
BulletRect.y = y4;
}
public void drawBullet(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.fill(BulletRect);
}
// get bullets position
//increment bullets position every so often
//when bullet goes off screen, clear it from game
}
另外,如果我可能要提出另一个问题,如果我想从挥杆学习android的话,最好走哪条路线? 谢谢。
以下是一些适合您的指针(我假设您正在使用Java 8)。
public Bullet bullet = new Bullet(hello.x, hello.y);
使用private List<Bullet> bullets = new ArrayList<>();
bullets.add(new Bullet(x, y));
paintComponent
绘制所有项目符号时,可以使用bullets.stream().forEach(bullet -> bullet.drawBullet(g));
fired
字段:空的项目符号列表与未触发的列表相同。 bullets.removeIf(Bullet::isOffScreen);
假设您实现了isOffScreen方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.