dadi723 2017-02-27 04:47 采纳率: 0%
浏览 1544
已采纳

Java做的坦克大战游戏(未完成),方向控制遇到问题,左右键(AD)走斜线,下(s)不管用

如题,怎么解决?尽量不要粘贴代码,说明原因即可。其他错误不用管,谢谢

 package Tank;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//主程序
public class Game extends JFrame{
    public static void main(String[] args) {
        Game game=new Game();
    }
    Game(){
        MyPanel mp=new MyPanel();
        this.add(mp);
        mp.setBackground(Color.BLACK);
        this.setBounds(100,100,400,300);
        this.setVisible(true);
        this.addKeyListener(mp);
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                Game.this.setVisible(false);
                System.exit(0);
            }
        });
    }
}
//面板区域
class MyPanel extends JPanel implements KeyListener{
    Hero hero=null;
    MyPanel(){
        hero=new Hero(150,200);
    }
    public void paint(Graphics g){
        super.paint(g);
        this.drawTank(hero.getX(), hero.getY(), g, 0, 0);
    }
    public void drawTank(int x,int y,Graphics g,int direct,int type){
        switch(type){
        case 0:
            g.setColor(Color.YELLOW);
            break;
        case 1:
            g.setColor(Color.CYAN);
            break;
        }
        switch(direct){
        case 0:
            g.fillRect(x, y, 5, 30);
            g.fill3DRect(x+15, y, 5, 30,false);
            g.fill3DRect(x+5,y+5, 10, 20,false);
            g.fillOval(x+5, y+10, 10, 10);
            g.drawLine(x+10, y+17, x+10, y);
            break;
        case 1:
            /*g.fillRect(x, y, 5, 30);
            g.fill3DRect(x+15, y, 5, 30,false);
            g.fill3DRect(x+5,y+5, 10, 20,false);
            g.fillOval(x+5, y+10, 10, 10);
            g.drawLine(x+10, y+17, x+10, y);
            break;
        case 2:
            g.fillRect(x, y, 5, 30);
            g.fill3DRect(x+15, y, 5, 30,false);
            g.fill3DRect(x+5,y+5, 10, 20,false);
            g.fillOval(x+5, y+10, 10, 10);
            g.drawLine(x+10, y+17, x+10, y);
            break;
        case 3:
            g.fillRect(x, y, 5, 30);
            g.fill3DRect(x+15, y, 5, 30,false);
            g.fill3DRect(x+5,y+5, 10, 20,false);
            g.fillOval(x+5, y+10, 10, 10);
            g.drawLine(x+10, y+17, x+10, y);
            break;*/
        }
    }
    //键盘处理
    @Override
    public void keyTyped(KeyEvent e) {}
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_W);{
            hero.setDirect(0);
            hero.moveUp();
            System.out.println("按下  "+e.getKeyChar());
            System.out.println("x -"+hero.x+"y-"+hero.y);
        }
        if(e.getKeyCode()==KeyEvent.VK_D){
            hero.setDirect(1);
            hero.moveRight();
            System.out.println("按下  "+e.getKeyChar());
            System.out.println("x -"+hero.x+"y-"+hero.y);
        }
        if(e.getKeyCode()==KeyEvent.VK_S){
            hero.setDirect(2);
            hero.moveDown();;
            System.out.println("按下  "+e.getKeyChar());
            System.out.println("x -"+hero.x+"y-"+hero.y);
        }
        if(e.getKeyCode()==KeyEvent.VK_A){
            hero.setDirect(3);
            hero.moveLeft();;
            System.out.println("按下  "+e.getKeyChar());
            System.out.println("x -"+hero.x+"y-"+hero.y);
        }
        this.repaint();
    }
    @Override
    public void keyReleased(KeyEvent e) {}
}
//坦克
class Tank{
    int direct;
    int speed=1;
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getDirect() {
        return direct;
    }
    public void setDirect(int direct) {
        this.direct = direct;
    }
    int x=0;
    int y=0;
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    Tank(int x,int y){
        this.x=x;
        this.y=y;
    }
    public void moveUp(){
        y-=speed;
    }
    public void moveRight(){
        x+=speed;
    }
    public void moveDown(){
        y+=speed;
    }
    public void moveLeft(){
        x-=speed;
    }
}
//我的坦克
class Hero extends Tank{
    Hero(int x,int y){
        super(x,y);
    }
}
  • 写回答

3条回答 默认 最新

  • 奔腾的野马 2017-02-28 14:42
    关注

    public void keyPressed(KeyEvent e) {

    if(e.getKeyCode()==KeyEvent.VK_W);{

            hero.setDirect(0);
            hero.moveUp();
            System.out.println("按下  "+e.getKeyChar());
            System.out.println("x -"+hero.x+"y-"+hero.y);
        }
    

    这里多了一个分号,所以无论你按什么键,分号后面的代码都会被执行。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号