Java编写坦克大战-PlayerOne类+PlayerTwo类

PlayerOne类

package com.learn.exercise.tanke;

import java.awt.*;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

/**
 * 玩家一类
 */
public class PlayerOne extends Tank{
    public boolean left;
    public boolean right;
    public boolean up;
    public boolean down;
    //创造构造函数匹配得出以下
    public PlayerOne(String img, int x, int y, Panel panel, String upImg, String leftImg, String rightImg, String downImg) {
        super(img, x, y, panel, upImg, leftImg, rightImg, downImg);
    }
    //控制坦克移动点击键盘
    public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_A:

                left = true;
                break;
            case KeyEvent.VK_S:
                down = true;
                break;
            case KeyEvent.VK_D:
                right = true;
                break;
            case KeyEvent.VK_W:
                up = true;
                break;
            //按空格进行射击
            case KeyEvent.VK_SPACE:
                attack();
            default:
                break;
        }
    }
    //释放键盘
    public void keyReleased(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_A:
                left = false;
                break;
            case KeyEvent.VK_S:
                down = false;
                break;
            case KeyEvent.VK_D:
                right = false;
                break;
            case KeyEvent.VK_W:
                up = false;
                break;
            default:
                break;
        }
    }
    //移动执行类
    public void move(){
        if (left){
            leftward();
        }else if (right){
            rightward();
        }else if (up){
            upward();
        }else if (down){
            downward();
        }
    }


    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        move();
    }

    @Override
    public Rectangle gerRec() {
        return new Rectangle(x,y,width,height);
    }
}

PlayerTwo类

package com.learn.exercise.tanke;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * 玩家二类
 */
public class PlayerTwo extends Tank{
    public boolean left;
    public boolean right;
    public boolean up;
    public boolean down;
    //创造构造函数匹配得出以下

    public PlayerTwo(String img, int x, int y, Panel panel, String upImg, String leftImg, String rightImg, String downImg) {
        super(img, x, y, panel, upImg, leftImg, rightImg, downImg);
    }

    //控制坦克移动点击键盘
    public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_LEFT:

                left = true;
                break;
                case KeyEvent.VK_DOWN:
                down = true;
                break;
            case KeyEvent.VK_RIGHT:
                right = true;
                break;
            case KeyEvent.VK_UP:
                up = true;
                break;
            //按空格进行射击
            case KeyEvent.VK_K:
                attack();
            default:
                break;
        }
    }
    //释放键盘
    public void keyReleased(KeyEvent e){
        int key = e.getKeyCode();
        switch (key){
            case KeyEvent.VK_LEFT:
                left = false;
                break;
            case KeyEvent.VK_DOWN:
                down = false;
                break;
            case KeyEvent.VK_RIGHT:
                right = false;
                break;
            case KeyEvent.VK_UP:
                up = false;
                break;
            default:
                break;
        }
    }
    //移动执行类
    public void move(){
        if (left){
            leftward();
        }else if (right){
            rightward();
        }else if (up){
            upward();
        }else if (down){
            downward();
        }
    }


    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        move();
    }

    @Override
    public Rectangle gerRec() {
        return new Rectangle(x,y,width,height);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值