繁体   English   中英

在简单的游戏中创建子弹

[英]bullets creation in a simple game

我正在创建一个简单的游戏,其中形状下降并且玩家射击它们,但是我在每次点击鼠标时都会遇到问题。 我已经尝试了各种逻辑,没有任何帮助,所以我只是将代码放在这里,这样你们就可以看看它并帮助我。

我创建的子弹是在每次单击时都没有创建的,只有一个是创建的,它会在每次点击时移动,这是错误的........我希望每次点击创建一个子弹。

// My main class: mousework2

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.geom.*;

public class mousework2 extends JFrame
{
    public static int Width = 300;
    public static int Height = 400;
    private JPanel p1;
    private Image pixMage,gunMage;

    public mousework2()
    {
        super("shoot-em-up");

        this.setSize(Width, Height);
        this.setResizable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Dimension pos = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (pos.width - Width) / 2;
        int y = (pos.height - Height) / 2;

        this.setLocation(x, y);

        p1 = new CreateImage();
        this.add(p1);
        this.getContentPane();

        Thread t = new recMove(this);
        t.start();
    }

    class recMove extends Thread
    {
        JFrame b;

        public recMove(JFrame b)
        {
            this.b = b;
        }

        public void run()
        {
            while (true) {
                b.repaint();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }
            }
        }
    }

    class CreateImage extends JPanel implements MouseListener
    {
        ArrayList<fallShape> rect = new ArrayList<fallShape>();
        int x_pos = mousework.Width / 2;
        int y_pos = mousework.Height - 50;
        int bx_pos = mousework.Width / 2;
        int by_pos = mousework.Height;
        int y_speed = -10;
        boolean clicked;

        public CreateImage()
        {
            for (int i = 0; i < 10; i++) {
                rect.add(new fallShape(15, 15, rect));
            }

            Toolkit picx = Toolkit.getDefaultToolkit();
            gunMage = picx.getImage("gunner.jpg");
            gunMage = gunMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);

            Toolkit pic = Toolkit.getDefaultToolkit();
            pixMage = pic.getImage("ballfall3.jpg");
            pixMage = pixMage.getScaledInstance(200, -1, Image.SCALE_SMOOTH);

            addMouseListener(this);
            addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseMoved(MouseEvent e)
                {
                    x_pos = e.getX() - 5;
                }
            });
        }

        public void mousePressed(MouseEvent e)
        {
            if (e.getButton() == 1) {
                clicked = true;
            }
        }

        public void mouseReleased(MouseEvent e)
        {
            if (e.getButton() == 1) {
                clicked = false;
            }
        }

        public void mouseExited(MouseEvent e)
        {
        }

        public void mouseEntered(MouseEvent e)
        {
        }

        public void mouseClicked(MouseEvent e)
        {
        }

        public void paint(Graphics g)
        {
            super.paint(g);

            g.drawImage(pixMage, 0, 0, Width, Height, null);

            Graphics2D g2 = (Graphics2D) g;

            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawImage(gunMage,x_pos,y_pos,10,20,null);

            if (clicked) {
                by_pos += y_speed;
                Shape bullet = new Rectangle2D.Float(bx_pos, by_pos, 3, 10);
                g2.setColor(Color.BLACK);
                g2.fill(bullet);
                g2.draw(bullet);
            }

            g2.setColor(Color.RED);

            for (fallShape b : rect) {
                b.move();
                g2.fill(b);
            }
        }
    }

    public static void main(String[] args)
    {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run()
            {
                new mousework2().setVisible(true);
            }
        });
    }
}

和:

// My falling shapes class: fallShape

import java.awt.geom.*;
import java.util.*;

public class fallShape extends Rectangle2D.Float
{
    public int x_speed, y_speed;
    public int l, b;
    public int height = mousework.Height;
    public int width = mousework.Width;
    public ArrayList<fallShape> fall;

    public fallShape(int breadth, int length, ArrayList<fallShape> fall)
    {
        super((int) (Math.random() * (mousework.Width - 20) + 1), 0, breadth, length);
        this.b = breadth;
        this.l = length;
        this.x_speed = (int) Math.random() * (10) + 1;
        this.y_speed = (int) Math.random() * (10) + 1;
        this.fall = fall;
    }

    public void move()
    {
        Rectangle2D rec = new Rectangle2D.Float(super.x, super.y, b, l);

        for (fallShape f : fall) {
            if (f != this && f.intersects(rec)) {
                int rxspeed = x_speed;
                int ryspeed = y_speed;
                x_speed = f.x_speed;
                y_speed = f.y_speed;
                f.x_speed = rxspeed;
                f.y_speed = ryspeed;
            }
        }

        if (super.x < 0) {
            super.x =+ super.x;
            //super.y =+ super.y;
            x_speed = Math.abs(x_speed);
        }

        if (super.x> mousework.Width - 30) {
            super.x =+ super.x;
            super.y =+ super.y;
            x_speed =- Math.abs(x_speed);
        }

        if (super.y < 0) {
            super.y = 0;
            y_speed = Math.abs(y_speed);
        }

        super.x += x_speed;
        super.y += y_speed;
    }
}
if(clicked){
    by_pos+=y_speed;

此代码仅在鼠标停止时绘制子弹。 这是因为你设置clickedfalsemouseReleased方法:

public void mouseReleased(MouseEvent e){
    if(e.getButton()==1)
        clicked=false;
}

如果您要删除mouseReleased方法的主体,您的子弹将正常移动。


但是,假设您想要的不仅仅是一颗子弹。 目前,您的paint方法一次只能绘制一个项目符号。 要绘制多个项目符号,您需要创建项目符号的坐标列表,并在单击时向列表中添加新的坐标对。 然后,在paint方法中,只需更新for循环中的每个位置。

 ArrayList<Integer> by_poss = new ArrayList<>(); 

by_poss是子弹的所有y位置的列表。

 public void mousePressed(MouseEvent e){ if(e.getButton() == 1) by_poss.add(mousework.Height); } 

mousePressed方法将一个新的“项目符号”以y位置的形式添加到坐标中。

 public void mouseReleased(MouseEvent e){ //do nothing } 

mouseReleased方法不需要发生任何事情。

 //update the bullets public void paint(Graphics g){ ... g2.setColor(Color.BLACK); Shape bullet; for(int i = 0; i < by_poss.size(); i++){ by_poss.set(i, by_poss.get(i) + y_speed); //move the bullet bullet = new Rectangle2D.Float(bx_pos, by_poss.get(i), 3, 10); g2.fill(bullet); g2.draw(bullet); } ... } 

paint方法中的for循环逐个绘制所有项目符号,使用by_poss列表中的y位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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