gpt4 book ai didi

java - 从同一类射击多发子弹

转载 作者:行者123 更新时间:2023-11-29 20:56:06 25 4
gpt4 key购买 nike

我创建的以下代码 fragment 有一个发射子弹的矩形。我希望每当空格键“召唤”一个新的项目符号时调用项目符号类,并使其独立于最后一个项目符号。在屏幕上同时创建多个项目符号实例的第一步是什么?

绘图组件类:

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





}

另外,如果我可以提出另一个问题,如果我想从 swing 开始学习 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 方法。

关于java - 从同一类射击多发子弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27715053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com