在B站上看了一个视频,就一个小时左右吧,基本是fallow他写的,有部分改动。这里把链接放上来:Java简易坦克大战1.0 编程视频
小白练手,小白练手,小白练手
下面就直接放代码了
package game.tankbattle;
import java.awt.Color;
import java.awt.Graphics;
/**
* 坦克类
* tank_x,tank_y:坦克的位置坐标
* speed:按键一次坦克移动的距离,运动速度
* game_interferce:坦克所在的游戏界面
* setTank_x(),setTank_y():设置坦克的坐标,控制坦克坐标不会超出游戏界面
* drawTank():画出坦克,根据坦克移动方向画出坦克炮筒。
* move():坦克响应键盘操作而移动
* shootBullet():发射子弹
*
*/
public class Tank {
private int tank_x;
private int tank_y;
private int tank_width; //坦克宽度
private int speed = 20;// 移动速度
public GameInterface game_interface;
Direction tank_dir = Direction.UP;
public Tank(int tank_x,int tank_y,int tank_width,GameInterface game_interface) {
this.tank_x = tank_x;
this.tank_y = tank_y;
this.tank_width = tank_width;
this.game_interface = game_interface;
}
public void setTank_x(int tank_x) {
if (tank_x+tank_width>800)
this.tank_x = 800-tank_width;
else if(tank_x <0)
this.tank_x = 0;
else
this.tank_x = tank_x;
}
public void setTank_y(int tank_y) {
if (tank_y+tank_width>600)
this.tank_y= 600-tank_width;
else if(tank_y<0)
this.tank_y = 0;
else
this.tank_y = tank_y;
}
public void drawTank(Graphics g) {//传入参数,拿到这个参数的方法
g.setColor(Color.BLUE);
//画出坦克主体
g.drawRect(tank_x, tank_y, tank_width, tank_width);
g.fillRect(tank_x, tank_y, tank_width, tank_width);
//画出坦克炮筒
switch (tank_dir) {
case DOWN:
g.drawRect(tank_x+20, tank_y+50, 10, 30);
g.fillRect(tank_x+20, tank_y+50, 10, 30);
break;
case RIGHT:
g.drawRect(tank_x+50, tank_y+20,30, 10);
g.fillRect(tank_x+50, tank_y+20,30, 10);
break;
case LEFT:
g.drawRect(tank_x-30, tank_y+20,30, 10);
g.fillRect(tank_x-30, tank_y+20,30, 10);
break;
case UP:
g.drawRect(tank_x+20, tank_y-30, 10, 30);
g.fillRect(tank_x+20, tank_y-30, 10, 30);
break;
default:
g.drawRect(tank_x+20, tank_y-30, 10, 30);
g.fillRect(tank_x+20, tank_y-30, 10, 30);
break;
}
}
public void move(Direction dir) {//传递方向参数
tank_dir = dir;
switch (dir) {
case UP:// 向上移动
this.setTank_y(tank_y - speed);
break;
case DOWN:
this.setTank_y(tank_y + speed);
break;
case RIGHT:
this.setTank_x(tank_x + speed);
break;
case LEFT:
this.setTank_x(tank_x - speed);
break;
default:
break;
}
}
public void shootBullets() {//坦克发射子弹
Bullet bullet = new Bullet(tank_x+tank_width/2-3, tank_y+tank_width/2-3);
bullet.setBulletDir(tank_dir);
this.game_interface.bullets.add(bullet);
}
}
package game.tankbattle;
import java.awt.Color;
import java.awt.Graphics;
/**
* 子弹类
* bullet_x,bullet_y:子弹坐标
* isAlive:子弹是否有效,超出边界无效
* setBulletDir():设置子弹发射方向
* fly():子弹飞行
* drawBullet():在游戏界面画出子弹
*/
public class Bullet {
private int bullet_x;
private int bullet_y;
private int bullet_r;//子弹半径
private Direction bullet_dir;//子弹的发射方向
private int bullet_speed = 1;//子弹速度
private boolean isAlive = true;
public void setBulletDir(Direction bullet_dir) {//设置子弹发射方向
this.bullet_dir = bullet_dir;
}
public Bullet(int bullet_x,int bullet_y) {
this.bullet_x = bullet_x;
this.bullet_y = bullet_y;
bullet_r = 3;//子弹半径设置为3,直径为6
bullet_dir = Direction.STOP;
}
public void drawBullet(Graphics g) {
g.setColor(Color.RED);
this.fly();
g.drawOval(bullet_x,bullet_y,bullet_r*2,bullet_r*2);
g.fillOval(bullet_x,bullet_y,bullet_r*2,bullet_r*2);
}
public void fly() {
// 子弹飞行
switch (bullet_dir) {
case UP:
bullet_y -= bullet_speed;
break;
case DOWN:
bullet_y += bullet_speed;
break;
case LEFT:
bullet_x -= bullet_speed;
break;
case RIGHT:
bullet_x += bullet_speed;
break;
default:
break;
}
}
public boolean isAlive() {
if(bullet_x>800||bullet_x<0||bullet_y>600||bullet_y<0)
isAlive = false;//超出界面,子弹无效。
return isAlive;
}
}
package game.tankbattle;
/**
* 移动方向
* 枚举类型
*/
public enum Direction {
// 四个移动方向,或者保持不动
UP,DOWN,LEFT,RIGHT,STOP
}
package game.tankbattle;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;
/**
* 游戏界面
* 显示坦克移动以及子弹轨迹
*/
public class GameInterface extends JPanel{
public JFrame game_jframe;// 创建面板
public Tank tank;
//子弹,存放于ArrayList容器中。
public ArrayList bullets = new ArrayList();
public GameInterface() {
tank = new Tank(500,400,50,this);// 新建一个坦克对象
game_jframe = new JFrame("Tank Battle");// 设置标题
game_jframe.setSize(815,640);// 设置面板大小
game_jframe.setLayout(null);
this.setSize(800,600);// 画布的大小
this.setLayout(null);
this.setBackground(Color.GRAY);// 画布背景色
game_jframe.setLayout(null);
// 监听键盘动作
Monitor listener = new Monitor(this);
game_jframe.addKeyListener(listener);//注册监听
// 禁止界面拉伸收缩
game_jframe.setResizable(false);
// 将游戏界面添加
game_jframe.add(this);
// 相对电脑屏幕居中
game_jframe.setLocationRelativeTo(null);
// 显示界面
game_jframe.setVisible(true);
// 默认关闭窗口时退出程序
game_jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);// 清除一下
tank.drawTank(g);// 画坦克
// 画子弹
if (!bullets.isEmpty()) {
for (int i = 0;i
package game.tankbattle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* 监听键盘,控制坦克移动和发射子弹
*/
public class Monitor extends KeyAdapter{
public GameInterface game_interface;
public Monitor(GameInterface game_interface) {
this.game_interface = game_interface;
}
private Direction dir;
public void keyPressed(KeyEvent e) {
//按方向键时坦克移动
if(e.getKeyCode() == KeyEvent.VK_UP) {
dir = Direction.UP;
this.game_interface.tank.move(dir);
}else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
dir = Direction.DOWN;
this.game_interface.tank.move(dir);
}else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
dir = Direction.LEFT;
this.game_interface.tank.move(dir);
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
dir = Direction.RIGHT;
this.game_interface.tank.move(dir);
}else {
dir = Direction.STOP;
}
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
// 空格发子弹
//System.out.println("shoot!!!");
this.game_interface.tank.shootBullets();
}
}
}
package game.tankbattle;
/**
* 游戏入口
* 最简单的功能实现
* 目前只实现了一个坦克
* 坦克能够发射子弹
* 上下左右键移动坦克
* 空格键发射子弹
*/
public class Main {
public static void main(String[] args) {
//新建一个游戏界面
GameInterface game_interface = new GameInterface();
}
}
Java小白一只,第一个练手的小游戏,实际上也没有完全实现游戏功能。还需要实现:
纯属练手,熟悉一下最近学的java基础。可能代码也不是很规范。后期可能会把更多功能加进来。
一起进步呀!