Java-继续对象时刻 - 我应该只添加一个方法而不是一个类吗?

时间:2017-12-16 06:21:56

标签: java image user-interface keyboard move

所以我正在制作街机射击游戏,目前我正在拍摄一个射击课。这个类创建一个新的子弹并使用椭圆类触发它。我有另一个名为playervsplayer的课程,当按下空格键时,它会调用射击课程。无论是否再次按下空格键,此子弹都会继续移动。请帮助我使我当前的代码工作,因为我将使用该项目的关键监听器而不是键绑定。如果您不想使用密钥绑定,请不要回复。我知道上下方向都有2,当它只有1和1时,我会稍后解决,所以现在就忽略那个愚蠢的事情。我曾尝试在两个类中请求焦点,但没有帮助,因为如果我再次按空格键仍然会停止。如果我不再按空格键,它可以正常工作。以下是拍摄课程的代码:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;

public class Shooting implements ActionListener{
    private Sprite ship1;
    private Sprite ship2;
    private int win_size;
    private int bullet_size=20;//max of bullet size of 20 and min of 1
    private JFrame window;
    private Oval bullet;
    private final int stop1=0;
    private final int stop2=1;
    private final int up1=2;
    private final int up2=3;
    private final int down1=4;
    private final int down2=5;
    private int dir1=stop1;
    private int dir2=stop2;
    private int bullet_speed=5;
    private Timer timer;
    private Color bullet_color=Color.green;
    public Shooting(JFrame w,Sprite s1,Sprite s2,int w_size) {
        window=w;
        ship1=s1;
        ship2=s2;
        win_size=w_size;
    }
    public void shotmain(int firing_from) {
        timer = new Timer( 10, this );
        timer.start();
        if (firing_from==1) {//So assuming fired from ship1 or player 1 on the bottom of the screen and not the top of the screen
            int shipx_loc=ship1.getX();
            int shipy_loc=ship1.getY();
            int ship_width=ship1.getWidth();
            int ship_height=ship1.getHeight();
            bullet=new Oval(shipx_loc+(int)(ship_width/2)-(bullet_size/2)+1,shipy_loc,bullet_size,bullet_size);//Yes, I need the plus 1 or else its not quite center, I am picky I know.
            bullet.setBackground(bullet_color);
            window.add(bullet,0);
            window.repaint();
            dir1=up1;
        }
    }
    private void move_bullet() {
        boolean bob=true;
        if (dir1==up1) {
            if (bullet.getY()<=50) {//for now
                window.remove(bullet);
                window.repaint();
            }
            else {
                bullet.setLocation(bullet.getX(),bullet.getY()-bullet_speed);
            }
        }
        else if (dir2==down2) {
            if (bullet.getY()>=win_size-50) {//for now
                window.remove(bullet);
                window.repaint();
            }
            else {
                bullet.setLocation(bullet.getX(),bullet.getY()+bullet_speed);
            }
        }
    }
    public void actionPerformed(ActionEvent e) {
        if ( e.getSource() == timer ){
            move_bullet();
        }

    }
}

以下是playervsplayer类的代码:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class PlayervsPlayer implements KeyListener,ActionListener{

    private JFrame window;
    private Timer timer;
    private Shooting shot;
    private int win_size;
    private int ship_clearence=45;
    private int speed=3;
    private final int stop1=0;
    private final int stop2=1;
    private final int left1=2;
    private final int left2=3;
    private final int right1=4;
    private final int right2=5;
    private int dir1=stop1;
    private int dir2=stop2;
    private Sprite ship1=new Sprite("Spaceship.png");
    private Sprite ship2=new Sprite("Spaceship2.png");

    public PlayervsPlayer(JFrame w,int win_s) {
        window=w;
        win_size=win_s;
        window.addKeyListener(this);
    }
    public void pvpmain() {
        ship1.setSize(125,125);
        ship1.setLocation((int)((win_size/2)-(ship1.getWidth()/2)),win_size-ship1.getHeight()-ship_clearence);
        ship2.setSize(125,125);
        ship2.setLocation((int)((win_size/2)-(ship1.getWidth()/2)),0);
        window.add(ship1,0);
        window.add(ship2,0);
        window.repaint();
        shot=new Shooting(window,ship1,ship2,win_size);
        timer = new Timer( 10, this );
        timer.start();
    }
    public void keyPressed(KeyEvent e) {
        int key=e.getKeyCode();
        if ( key == KeyEvent.VK_RIGHT ){
            dir1 = right1;
        }
        else if (key==KeyEvent.VK_LEFT) {
            dir1=left1;
        }
        else if (key==KeyEvent.VK_SPACE) {
            shot.shotmain(1);
        }
        if (key==KeyEvent.VK_Q) {
            dir2=left2;
        }
        else if (key==KeyEvent.VK_W) {
            dir2=right2;
        }
        else if (key==KeyEvent.VK_E) {
            //Nothing for now as for shooting
        }
    }
    private void move() {
        if (dir1==left1) {
            if (ship1.getX()<=speed) {
                dir1=stop1;
            }
            else {
                ship1.setLocation(ship1.getX()-speed,ship1.getY());
            }
        }
        else if (dir1==right1) {
            if (ship1.getX()+ship1.getWidth()>=win_size-speed) {
                dir1=stop1;
            }
            else {
                ship1.setLocation(ship1.getX()+speed,ship1.getY());
            }
        }
        if (dir2==left2) {
            if (ship2.getX()<=speed) {
                dir2=stop2;
            }
            else {
                ship2.setLocation(ship2.getX()-speed,ship2.getY());
            }
        }
        else if (dir2==right2) {
            if (ship2.getX()+ship2.getWidth()>=win_size-speed) {
                dir2=stop2;
            }
            else {
                ship2.setLocation(ship2.getX()+speed,ship2.getY());
            }
        }
    }
    public void actionPerformed(ActionEvent e) {
        if ( e.getSource() == timer ){
            move();
        }

    }
    public void keyTyped(KeyEvent e) {
        //Nothing

    }
    public void keyReleased(KeyEvent e) {
        //Nothing

    }


}

我有很多其他类,但这些是最相关的两个类,因为我需要添加的类将在两个类之一中。没有生成错误。提前谢谢。

0 个答案:

没有答案