繁体   English   中英

射击同一班的多发子弹

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM