package com.wx.tankgame02;
import javax.swing.*;
import java.awt.event.KeyListener;
@SuppressWarnings({"all"})
public class WxTankGame02 extends JFrame {
//定义一个myPanel
private MyPanel myPanel = null;
public static void main(String[] args) {
WxTankGame02 wxTankGame02 = new WxTankGame02();
}
public WxTankGame02(){
myPanel = new MyPanel();
this.add(myPanel);//把游戏的绘图区域加入到窗口
this.setLocation(300,150);
this.setSize(1000,750);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addKeyListener(myPanel);//增加一个监听事件,让JFrame监听事件
}
}
public class Tank {
private int x; //坦克的横坐标
private int y; //坦克的纵坐标
private int direct;//坦克的方向0上 1右 2下 3左
private int speed = 1;//控制坦克速度
//向上右下左移动的方法
public void moveUp() {
y -= speed;
}
public void moveRight() {
x += speed;
}
public void moveDown() {
y += speed;
}
public void moveLeft() {
x -= speed;
}
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
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;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
public class Hero extends Tank{
public Hero(int x, int y) {
super(x, y);
}
}
//为了实现键盘监听事件,引入KeyListener
public class MyPanel extends JPanel implements KeyListener {
//定义坦克
Hero hero = null;
public MyPanel() {
hero = new Hero(100, 100);//初始化自己的坦克
hero.setSpeed(2);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);//填充矩形,默认黑色
drawTank(hero.getX(), hero.getX(), g, hero.getDirect(), 1);
}
/**
* @param x 坦克左上角的横坐标
* @param y 坦克左上角的纵坐标
* @param g 传进来一个画笔
* @param direct 坦克的方向
* @param type 坦克的类型,敌人/自己
*/
public void drawTank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
//根据不同坦克,设置不同颜色
case 0://敌人的坦克
g.setColor(Color.cyan);
break;
case 1://我们的坦克
g.setColor(Color.yellow);
break;
}
//根据坦克的方向来绘制对应坦克
switch (direct) {
//0表示向上 1表示向右,2表示向下,3表示向左
case 0: //向上
g.fill3DRect(x, y, 10, 60, false);//坦克的左边轮子
g.fill3DRect(x + 30, y, 10, 60, false);//坦克的右边轮子
g.fill3DRect(x + 10, y + 10, 20, 40, false);//坦克中间的身子
g.fillOval(x + 10, y + 20, 20, 20);//画出坦克的圆盖
g.drawLine(x + 20, y + 30, x + 20, y);//画出坦克的炮筒
break;
case 1://向右
g.fill3DRect(x, y, 60, 10, false);//坦克的上边轮子
g.fill3DRect(x, y + 30, 60, 10, false);//坦克的下边轮子
g.fill3DRect(x + 10, y + 10, 40, 20, false);//坦克中间的身子
g.fillOval(x + 20, y + 10, 20, 20);//画出坦克的圆盖
g.drawLine(x + 30, y + 20, x + 60, y + 20);//画出坦克的炮筒
break;
case 2://向下
g.fill3DRect(x, y, 10, 60, false);//坦克的左边轮子
g.fill3DRect(x + 30, y, 10, 60, false);//坦克的右边轮子
g.fill3DRect(x + 10, y + 10, 20, 40, false);//坦克中间的身子
g.fillOval(x + 10, y + 20, 20, 20);//画出坦克的圆盖
g.drawLine(x + 20, y + 30, x + 20, y + 60);//画出坦克的炮筒
break;
case 3://向左
g.fill3DRect(x, y, 60, 10, false);//坦克的上边轮子
g.fill3DRect(x, y + 30, 60, 10, false);//坦克的下边轮子
g.fill3DRect(x + 10, y + 10, 40, 20, false);//坦克中间的身子
g.fillOval(x + 20, y + 10, 20, 20);//画出坦克的圆盖
g.drawLine(x + 30, y + 20, x, y + 20);//画出坦克的炮筒
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
//处理wasd按下的情况
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) {
hero.setDirect(0); //按下W,坦克向上
hero.moveUp();
} else if (e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT) {
hero.setDirect(1);//按下D,坦克向右
hero.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN) {
hero.setDirect(2);//按下S,坦克向下
hero.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT) {
hero.setDirect(3);//按下A,坦克向左
hero.moveLeft();
}
//重绘一下
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
drawTank(hero.getX(), hero.getX(), g, hero.getDirect(), 1);
你这句话写错了,改成如下即可:
drawTank(hero.getX(),hero.getY(), g, hero.getDirect(), 1);
左右移动只能改变x的值,上下移动只能改变y的值。