我试图创建一个在键入键“空格”时会发射子弹的游戏

时间:2019-04-01 12:07:48

标签: java swing

我有一个名为Field(扩展JPanel的类)的Jpanel,并且我要在List和player-class中作为对象添加对象项目符号。

请帮助。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;


import javax.swing.JPanel;
import javax.swing.Timer;

public class Field extends JPanel implements ActionListener
{

Player p1 = new Player();
    Move m = new Move(this);
    Shoot shoot = new Shoot(this);

    List<Bullet> bullets = new ArrayList<Bullet>(); 

    //position of the player
    public int x ;
    public int y ;

    Timer tm = new Timer(20,this); 

    public Field(Player p , ArrayList<Chicken> chickens)
    {
        p1 = p ;
        setBackground(Color.black);
        addMouseMotionListener(m);
        addKeyListener(shoot);

        tm.start();
    }

    class Move implements MouseMotionListener 
    {
        public Move(Field f) 
        {
            f.addMouseMotionListener(this);

        }

        @Override
        public void mouseDragged(MouseEvent e) 
        {
            // TODO Auto-generated method stub
            p1.shuttlex = e.getXOnScreen()-15;
            p1.shuttley = e.getYOnScreen()-50;
            p1.wingLx = p1.shuttlex - p1.shuttleWidth + 2;
            p1.wingY = p1.shuttley + 40;
            p1.wingRx = p1.shuttlex + p1.shuttleWidth - 2;
            p1.wingWid = 15;
            p1.wingLen = 40;

            x = e.getXOnScreen()-15;
            y = e.getYOnScreen()-25;
            repaint();


        }

        @Override
        public void mouseMoved(MouseEvent e) 
        {
            // TODO Auto-generated method stub
            p1.shuttlex = e.getXOnScreen()-15;
            p1.shuttley = e.getYOnScreen()-50;
            p1.wingLx = p1.shuttlex - p1.shuttleWidth + 2;
            p1.wingY = p1.shuttley + 40;
            p1.wingRx = p1.shuttlex + p1.shuttleWidth - 2;
            p1.wingWid = 15;
            p1.wingLen = 40;

            x = e.getXOnScreen()-15;
            y = e.getYOnScreen()-25;
            repaint();
        }
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        p1.paintComponent(g);

        for(Bullet fire : bullets)
        {
            fire.paintComponent(g);
        }
    }

    private void addFire(Bullet fire)
    {
        bullets.add(fire);
    }

    class Shoot implements KeyListener
    {
        Field field;
         public Shoot(Field field) 
         {
            field.addKeyListener(this);
            this.field=field;

         }
        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
            char key = e.getKeyChar();
            if(key == ' ') 
            {
                field.addFire(new Bullet(x, y));

            }
        }

        @Override
        public void keyPressed(KeyEvent e) {


        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

    }
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        // TODO Auto-generated method stub
        for(Bullet fire : bullets)
        {
            fire.y-=10;

        }
        repaint();
    }
    public void movePlayer(Player p , MouseEvent e)
    {
              //just moves the Player
        p.shuttlex = e.getXOnScreen()-15;
        p.shuttley = e.getYOnScreen()-50;
        p.wingLx = p1.shuttlex - p1.shuttleWidth + 2;
        p.wingY = p1.shuttley + 40;
        p.wingRx = p1.shuttlex + p1.shuttleWidth - 2;
        p.wingWid = 15;
        p.wingLen = 40;

        x = e.getXOnScreen()-15;
        y = e.getYOnScreen()-25;
        repaint();
    }
}

这是我的子弹班:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class Bullet 
{
    public int x , y,width =20, length=50;
    public int damage = 10;

    public Bullet(int x , int y)
    {
        this.x = x ;
        this.y = y ;

    }

    public void paintComponent(Graphics g)
    {


        g.setColor(Color.red);
        g.drawRect(x, y, width, length);


    }


}

以及我的玩家职业:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

public class Player 
{
    public Player() 
    {

    }

    private int HP;

    public int shuttlex = 740, shuttley = 650, shuttleWidth = 15, shuttleLength = 70;

    public int wingLx = shuttlex - shuttleWidth + 2, wingY = shuttley + 40;
    public int wingRx = shuttlex + shuttleWidth - 2;

    public int wingWid = 15, wingLen = 40;

    public void paintComponent(Graphics g) 
    {


        g.setColor(Color.RED);
        g.fillOval(shuttlex + 2, shuttley + 60, 11, 33);
        g.setColor(Color.ORANGE);
        g.fillOval(shuttlex + 2, shuttley + 60, 11, 20);

        g.setColor(Color.BLUE);
        g.fillRoundRect(shuttlex, shuttley, shuttleWidth, shuttleLength, 30, 100);
        g.fillOval(wingLx, wingY, wingWid, wingLen);
        g.fillOval(wingRx, wingY, wingWid, wingLen);
//      g.fillRect(x, y, width, height);


    }
    public void gotDamaged(int damage)
    {
        HP-=damage;
    }

    public boolean isAlive()
    {
        if(HP>0)
            return true;

        return false;
    }



}

它会绘制播放器并移至鼠标所在的位置。 问题是当我键入空格键时,它不会添加项目符号。 我的代码怎么了?

它没有显示必须绘制的项目符号

1 个答案:

答案 0 :(得分:0)

您可以尝试使用KeyCode

{{1}}

或SPACE的Unicode值,而不是''。