坦克的绘制与移动
public class Hero extends Tank {
public Hero(int x, int y) {
super(x, y);
}
}
public class Tank {
private int x;//坦克的横坐标
private int y;//坦克的纵坐标
private int direct;//坦克方向 0:上,1:右,2:下,3:左
private int speed;//坦克的速度
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
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 getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
//上右下左移动方法
public void moveUp(){
y -= speed;
}
public void moveRight(){
x += speed;
}
public void moveDown(){
y += speed;
}
public void moveLeft(){
x -= speed;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* @ClassName MyPanel
* @Description 坦克大战绘图区
* @Author 小黄debug
* @Date 2022/3/18 6:35
* @Version 1.0
**/
//为了监听键盘事件
public class MyPanel extends JPanel implements KeyListener {
//定义我的坦克
Hero hero = null;
public MyPanel(){
hero = new Hero(100,100); //初始化自己坦克
hero.setSpeed(50);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0,0,1000,750);//填充矩形,默认黑色
//画出坦克-封装方法
drawTank(hero.getX(), hero.getY(),g,hero.getDirect(),1);
}
/**
*
* @param x 坦克的左上角x坐标
* @param y 坦克的在左上角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;
}
//根据坦克方向,来绘制坦克
//(direct) 表示方向(0:向上 1:向右 2:向下 3:向左
switch(direct){
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,y+20,x+30,y+20);
break;
default:
System.out.println("暂时没有处理");
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
//改变坦克的方向
hero.setDirect(0);
//修改坦克的坐标 y -= 1;
hero.moveUp();
}else if(e.getKeyCode() == KeyEvent.VK_D){
//改变坦克的方向
hero.setDirect(1);
hero.moveRight();
}else if(e.getKeyCode() == KeyEvent.VK_S){
//改变坦克的方向
hero.setDirect(2);
hero.moveDown();
}else if(e.getKeyCode() == KeyEvent.VK_A){
//改变坦克的方向
hero.setDirect(3);
hero.moveLeft();
}
//将面板重绘
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
public class HsyTankGame02 extends JFrame {
MyPanel mp = null;
public static void main(String[] args) {
HsyTankGame02 hsyTankGanme01 = new HsyTankGame02();
}
public HsyTankGame02(){
mp = new MyPanel();
this.add(mp); //把面板(就是游戏的绘图区域)
this.setSize(1000,750); //设置绘制面板大小(相当于相框)
this.addKeyListener(mp); //让JFrame监听mp的键盘事件
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}