介绍
坦克大战1.0
内容:
- 熟悉java绘图方法–>画出坦克
- 了解java事件处理机制–>监听键盘事件–>按方向键让坦克动起来
2.0版本已上传
画框
package com.hspedu.draw.tankgame;
import javax.swing.*;
/**
* @ClassName
* @Description
* @Author zxk
* @DateTime 2022-01-14-18:24
* @Version
*///画框
public class ZxkTankGame01 extends JFrame {
//定义画板
Mypanel mp = null;
public static void main(String[] args) {
ZxkTankGame01 zxkTankGame01 = new ZxkTankGame01();//新建画框
}
public ZxkTankGame01(){
mp = new Mypanel();//新建画板
this.add(mp);//添加画板到画框
setSize(1000, 750);//画板大小
this.addKeyListener(mp);//监听画板的键盘
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭画框的同时关闭jvm
setVisible(true);//可视化
}
}
画板
package com.hspedu.draw.tankgame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
/**
* @ClassName
* @Description
* @Author zxk
* @DateTime 2022-01-14-18:17
* @Version
*///画板
public class Mypanel extends JPanel implements KeyListener {//方法的参数坐标都在左上角,除了画字符串在左下角
//定义自己的坦克
Hero hero = null;
Vector<EnemyTank> enemyTanks = new Vector<EnemyTank>();//新建敌人坦克集合
public Mypanel(){//画板构造器内部新建坦克
hero = new Hero(100, 100,'w',20);
for (int i = 0; i < 3; i++) {
enemyTanks.add(new EnemyTank(200 + i * 100, 0, 's', 5));
}
}
@Override
public void paint(Graphics g) {//重写父类画画方法
super.paint(g);
System.out.println("重画过");
g.fillRect(0,0,1000,750);//画板填充颜色默认黑色
//后创建的坦克可以踩在先创建的上面
for (int i = 0; i < enemyTanks.size(); i++) {
drawTank(enemyTanks.get(i).getX(),enemyTanks.get(i).getY(),g,enemyTanks.get(i).getDirection(),false);
}
drawTank(hero.getX(), hero.getY(),g,hero.getDirection(),true);
}
public void drawTank(int x,int y,Graphics g,char direct,boolean type){
//坦克类型不同,颜色不同
if (type) {
g.setColor(Color.cyan);
} else {
g.setColor(Color.yellow);
}
//四种方向
switch (direct) {
case 'w'://坦克朝上
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 's':
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 'a':
g.fill3DRect(x-10, y+10, 60, 10, false);
g.fill3DRect(x-10, y+40, 60, 10, false);
g.fill3DRect(x, y + 20, 40, 20, false);
g.fillOval(x + 10, y + 20, 20, 20);
g.drawLine(x + 20, y + 30, x-10, y+30);
break;
case 'd':
g.fill3DRect(x-10, y+10, 60, 10, false);
g.fill3DRect(x-10, y+40, 60, 10, false);
g.fill3DRect(x, y + 20, 40, 20, false);
g.fillOval(x + 10, y + 20, 20, 20);
g.drawLine(x + 20, y + 30, x+50, y+30);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {//键盘事件
if (e.getKeyCode() == KeyEvent.VK_D) {//坦克车头朝右且位置往右挪
hero.moveRight();
hero.setDirection('d');
}else if (e.getKeyCode() == KeyEvent.VK_A) {
hero.moveLeft();
hero.setDirection('a');
}else if (e.getKeyCode() == KeyEvent.VK_W) {
hero.moveUp();
hero.setDirection('w');
}else if (e.getKeyCode() == KeyEvent.VK_S) {
hero.moveDown();
hero.setDirection('s');
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
Tank类
package com.hspedu.draw.tankgame;
/**
* @ClassName
* @Description
* @Author zxk
* @DateTime 2022-01-14-18:13
* @Version
*///坦克类
public class Tank {
private int x;
private int y;
private char direction;
private int speed;
public Tank(int x, int y,char direction,int speed) {
this.x = x;
this.y = y;
this.direction = direction;
this.speed = speed;
}
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 char getDirection() {
return direction;
}
public void setDirection(char direction) {
this.direction = direction;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void moveUp(){//开坦克:改变坦克位置
y -= speed;
}
public void moveDown(){
y += speed;
}
public void moveLeft(){
x -= speed;
}
public void moveRight(){
x += speed;
}
}
Hero类
package com.hspedu.draw.tankgame;
/**
* @ClassName
* @Description
* @Author zxk
* @DateTime 2022-01-14-18:15
* @Version
*///自己的坦克
public class Hero extends Tank{
public Hero(int x, int y,char direction,int speed) {
super(x, y,direction,speed);
}
}
EnemyTank类
package com.hspedu.draw.tankgame;
/**
* @ClassName
* @Description
* @Author zxk
* @DateTime 2022-01-14-22:19
* @Version
*///敌人坦克
public class EnemyTank extends Tank{
public EnemyTank(int x, int y, char direction, int speed) {
super(x, y, direction, speed);
}
}