package tankfire1;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.*;
public class Paint_Tank_Class extends JPanel implements KeyListener,Runnable{
//以坦克的中心为参考点画坦克
int x =500;
int y = 500;//坦克中心点坐标(x,y)
int count = 0;
//坦克初始化方向向上
protected char direction = KeyEvent.VK_UP;
//我方坦克移动速度
private final int tank_speed = 3;
//创建我方子弹类容器
Vector
private char key ;
//创建敌人坦克类容器
Vector
Thread[] enemy_tank11 = new Thread[4];
public Paint_Tank_Class(int x,int y) {
this.x = x;
this.y = y;
}
public Paint_Tank_Class() {
//创建四辆敌方坦克
for(int i = 0; i <= 1;i ++) {
Enemy_Tank_Class tank = new Enemy_Tank_Class(200+i*200,200);
enemy_tank_vector.add(tank);
}
for(int i = 0; i <= 1;i ++) {
Enemy_Tank_Class tank = new Enemy_Tank_Class(200+i*200,600);
enemy_tank_vector.add(tank);
}
for(int i = 0; i < 4;i++) {
enemy_tank11[i] = new Thread(enemy_tank_vector.get(i));
enemy_tank11[i].start();
}
}
public Paint_Tank_Class(int n,int[] a,int b,int c) {
for(int i = 0; i < n ;i ++,count+=2) {
Enemy_Tank_Class tank = new Enemy_Tank_Class(a[count],a[count+1]);
enemy_tank_vector.add(tank);
}
for(int i = 0; i < n;i++) {
enemy_tank11[i] = new Thread(enemy_tank_vector.get(i));
enemy_tank11[i].start();
}
this.x = b;
this.y = c;
}
@Override
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
switch((char)e.getKeyCode()) {
case KeyEvent.VK_UP:
if(y<=30)
y=30;
y-=tank_speed;
direction = KeyEvent.VK_UP;
break;
case KeyEvent.VK_DOWN:
if(y>=780)
y=780;
y+=tank_speed;
direction = KeyEvent.VK_DOWN;
direction = KeyEvent.VK_DOWN;
break;
case KeyEvent.VK_LEFT:
if(x<=25)
x=25;
x-=tank_speed;
direction = KeyEvent.VK_LEFT;
break;
case KeyEvent.VK_RIGHT:
if(x>=965)
x=965;
x+=tank_speed;
direction = KeyEvent.VK_RIGHT;
break;
//每按下一次空格键就增加一颗子弹
case KeyEvent.VK_SPACE:
key = KeyEvent.VK_SPACE;
break;
}
if(key==KeyEvent.VK_SPACE) {
//每按下一次空格键就增加一颗子弹
Bullet_Class bullet1 = new Bullet_Class();
//用坦克坐标初始化新增加的子弹坐标
bullet1.setXY(x, y);
bullet1.setDirection(direction);
bullet_vector.add(bullet1);
key ='1';
}
}
public void keyReleased(KeyEvent e) {
}
//画坦克
@Override
public void paint(Graphics g) {
g.fillRect(0,0,1000,1000);
//显示坦克血量
enemyBlood( g);
//画我方坦克
this.paintMyTank( g);
//画我方坦克子弹
this.paintBullet(g);
//画敌方坦克子弹
paintEnemyBullet( g);
//画敌方坦克
this.paintEnemyTank( g);
//判断是否命中敌方坦克
hitTank();
}
public void run() {
while(true) {
try {
Thread.sleep(200);
//判断我方坦克的每颗子弹是否可以消失
for(int i = 0;i
{
bullet_vector.remove(i);
}
bullet_vector.get(i).chang();
}
//判断敌方坦克的每颗子弹是否可以消失
for(int i = 0;i < enemy_tank_vector.size();i ++) {
for(int n = 0;n
{
enemy_tank_vector.get(i).getEnemyBullet().remove(n);
}
enemy_tank_vector.get(i).getEnemyBullet().get(n).chang();
}
enemy_tank_vector.get(i).tankRemove();
}
}
catch(Exception e) {
}
//重画
repaint();
}
}
public void paintBullet(Graphics g) {
for(int i = 0;i < bullet_vector.size();i++) {
g.fillRect(bullet_vector.get(i).getX()-1,bullet_vector.get(i).getY()-1, 4, 4);
}
}
public void paintMyTank(Graphics g) {
g.setColor(Color.yellow);
if(direction==KeyEvent.VK_UP) {
g.fillRect(x-10, y-10, 5,20);//画左边矩形
g.fillRect(x-5, y-6, 10,12);//画中间矩形
g.fillRect(x+5, y-10, 5,20);//画左边矩形
g.drawLine(x,y,x,y-15);//炮筒
}
else if(direction==KeyEvent.VK_DOWN) {
g.fillRect(x-10, y-10, 5,20);//画左边矩形
g.fillRect(x-5, y-6, 10,12);//画中间矩形
g.fillRect(x+5, y-10, 5,20);//画左边矩形
g.drawLine(x,y,x,y+15);//炮筒
}
else if(direction==KeyEvent.VK_LEFT) {
g.fillRect(x-10, y-10, 20,5);//画上边矩形
g.fillRect(x-6, y-5, 12,10);//画中间矩形
g.fillRect(x-10, y+5, 20,5);//画下边矩形
g.drawLine(x,y,x-15,y);//炮筒
}
else if(direction==KeyEvent.VK_RIGHT) {
g.fillRect(x-10, y-10, 20,5);//画上边矩形
g.fillRect(x-6, y-5, 12,10);//画中间矩形
g.fillRect(x-10, y+5, 20,5);//画下边矩形
g.drawLine(x,y,x+15,y);//炮筒
}
}
public void paintEnemyTank(Graphics g) {
//画敌方坦克
g.setColor(Color.red);
for(int i = 0;i < enemy_tank_vector.size();i ++) {
if(enemy_tank_vector.get(i).getEnemyDirection()==KeyEvent.VK_UP) {
g.fillRect(enemy_tank_vector.get(i).getX()-10, enemy_tank_vector.get(i).getY()-10, 5,20);//画左边矩形
g.fillRect(enemy_tank_vector.get(i).getX()-5, enemy_tank_vector.get(i).getY()-6, 10,12);//画中间矩形
g.fillRect(enemy_tank_vector.get(i).getX()+5, enemy_tank_vector.get(i).getY()-10, 5,20);//画左边矩形
g.drawLine(enemy_tank_vector.get(i).getX(),enemy_tank_vector.get(i).getY(),enemy_tank_vector.get(i).getX(),enemy_tank_vector.get(i).getY()-15);//炮筒
}
else if(enemy_tank_vector.get(i).getEnemyDirection()==KeyEvent.VK_DOWN) {
g.fillRect(enemy_tank_vector.get(i).getX()-10, enemy_tank_vector.get(i).getY()-10, 5,20);//画左边矩形
g.fillRect(enemy_tank_vector.get(i).getX()-5, enemy_tank_vector.get(i).getY()-6, 10,12);//画中间矩形
g.fillRect(enemy_tank_vector.get(i).getX()+5, enemy_tank_vector.get(i).getY()-10, 5,20);//画左边矩形
g.drawLine(enemy_tank_vector.get(i).getX(),enemy_tank_vector.get(i).getY(),enemy_tank_vector.get(i).getX(),enemy_tank_vector.get(i).getY()+15);//炮筒
}
else if(enemy_tank_vector.get(i).getEnemyDirection()==KeyEvent.VK_LEFT) {
g.fillRect(enemy_tank_vector.get(i).getX()-10, enemy_tank_vector.get(i).getY()-10, 20,5);//画左边矩形
g.fillRect(enemy_tank_vector.get(i).getX()-6, enemy_tank_vector.get(i).getY()-5, 12,10);//画中间矩形
g.fillRect(enemy_tank_vector.get(i).getX()-10, enemy_tank_vector.get(i).getY()+5, 20,5);//画左边矩形
g.drawLine(enemy_tank_vector.get(i).getX(),enemy_tank_vector.get(i).getY(),enemy_tank_vector.get(i).getX()-15,enemy_tank_vector.get(i).getY());//炮筒
}
else if(enemy_tank_vector.get(i).getEnemyDirection()==KeyEvent.VK_RIGHT) {
g.fillRect(enemy_tank_vector.get(i).getX()-10, enemy_tank_vector.get(i).getY()-10, 20,5);//画左边矩形
g.fillRect(enemy_tank_vector.get(i).getX()-6, enemy_tank_vector.get(i).getY()-5, 12,10);//画中间矩形
g.fillRect(enemy_tank_vector.get(i).getX()-10, enemy_tank_vector.get(i).getY()+5, 20,5);//画左边矩形
g.drawLine(enemy_tank_vector.get(i).getX(),enemy_tank_vector.get(i).getY(),enemy_tank_vector.get(i).getX()+15,enemy_tank_vector.get(i).getY());//炮筒
}
}
repaint();
// System.out.println(1);
}
public void paintEnemyBullet(Graphics g) {
for(int n = 0;n < enemy_tank_vector.size();n ++) {
for(int i = 0;i < enemy_tank_vector.get(n).getEnemyBullet().size(); i ++) {
g.fillRect(enemy_tank_vector.get(n).getEnemyBullet().get(i).getX()-1,enemy_tank_vector.get(n).getEnemyBullet().get(i).getY()-1, 4, 4);
}
}
}
public void hitTank() {
for(int i = 0;i < bullet_vector.size(); i ++) {
for(int n = 0;n < enemy_tank_vector.size();n ++) {
if(bullet_vector.get(i).getX()>=enemy_tank_vector.get(n).getX()-10&& bullet_vector.get(i).getX() <= enemy_tank_vector.get(n).getX()+10 ) {
if(bullet_vector.get(i).getY()>=enemy_tank_vector.get(n).getY()-10&& bullet_vector.get(i).getY() <= enemy_tank_vector.get(n).getY()+10 ) {
bullet_vector.remove(i);
// enemy_life = false;
// enemy_tank_vector.get(n).setX(0);
enemy_tank_vector.remove(n);
}
}
}
}
}
public void enemyBlood(Graphics g) {
//敌方坦克
g.setColor(Color.yellow);
g.drawLine(0, 830, 1000, 830);
g.drawRect(60, 870, 100,15);
g.setColor(Color.red);
g.fill3DRect(60, 870, enemy_tank_vector.size()*25,15,true);
g.fillRect(45-10, 878-10, 5,20);//画左边矩形
g.fillRect(45-5, 878-6, 10,12);//画中间矩形
g.fillRect(45+5, 878-10, 5,20);//画左边矩形
g.drawLine(45,878,45,878-15);//炮筒
//我方坦克
g.setColor(Color.yellow);
g.fillRect(745-10, 878-10, 5,20);//画左边矩形
g.fillRect(745-5, 878-6, 10,12);//画中间矩形
g.fillRect(745+5, 878-10, 5,20);//画左边矩形
g.drawLine(745,878,745,878-15);//炮筒
g.drawRect(760, 870, 110,15);
g.setColor(Color.red);
g.fill3DRect(760, 870, 110,15,true);
}
public Vector
return enemy_tank_vector;
}
}
package tankfire1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class Main_Class extends JFrame implements ActionListener{
Menu_Class menu = new Menu_Class();
Customs_Pass_Class pass = new Customs_Pass_Class();
Paint_Tank_Class tank = new Paint_Tank_Class();
Thread tank1 = new Thread(tank);
Thread pass1 = new Thread(pass);
//创建敌方坦克
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main_Class();
}
public Main_Class(){
setSize(1000,1000);
setTitle("坦克");
setLocation(0,0);
setJMenuBar(menu);
menu.item_new_game.addActionListener(this);
menu.item_exit_game.addActionListener(this);
menu. item_save_game.addActionListener(this);
menu.item_continue_game.addActionListener(this);
add(pass);
pass1.start();
//remove(pass);
/*add(tank);
addKeyListener(tank);
tank1.start();*/
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("退出游戏")) {
System.exit(0);
}
else if(e.getActionCommand().equals("开始游戏")) {
//终止线程
pass1.stop();
//移除关卡面板
remove(pass);
//添加游戏面板
add(tank);
addKeyListener(tank);
tank1.start();
this.setVisible(true);
}
else if(e.getActionCommand().equals("保存游戏")) {
try {
FileWriter out = new FileWriter ("e:/123.text");
int size = tank.getEnemyVector().size();
String ss = String.valueOf(size);
out.write(ss+"\r\n");
for(int i = 0; i < size; i++) {
String s = String.valueOf(tank.getEnemyVector().get(i).getX());
String s1 = String.valueOf(tank.getEnemyVector().get(i).getY());
out.write(s+"\r\n");
out.write(s1+"\r\n");
}
String a = String.valueOf(tank.x);
String b = String.valueOf(tank.y);
out.write(a+"\r\n");
out.write(b+"\r\n");
out.close();
System.out.println(size);
}
catch(Exception E) {
}
}
else if(e.getActionCommand().equals("继续游戏")) {
try {
FileReader input = new FileReader ("E:/123.text");
BufferedReader read = new BufferedReader(input);
int a;
int b;
int size = Integer.parseInt(read.readLine());
int[] array = new int[size*2];
for(int i = 0; i < size*2 ;i ++) {
array[i] = Integer.parseInt(read.readLine());
}
a = Integer.parseInt(read.readLine());
b = Integer.parseInt(read.readLine());
Paint_Tank_Class tank1 = new Paint_Tank_Class(size,array,a,b);
pass1.stop();
//移除关卡面板
remove(pass);
//添加游戏面板
add(tank1);
addKeyListener(tank1);
Thread tank11 = new Thread(tank1);
tank11.start();
this.setVisible(true);
}
catch(Exception e1) {
}
}
}
}
package tankfire1;
import java.awt.*;
import javax.swing.*;
public class Menu_Class extends JMenuBar{
JMenuItem item_new_game ;
JMenuItem item_exit_game;
JMenuItem item_save_game;
JMenuItem item_continue_game;
public Menu_Class() {
JMenu menu_game = new JMenu("游戏(G)");
//设置快捷键
menu_game.setMnemonic('G');
item_new_game = new JMenuItem("开始游戏(N)");
item_new_game.setActionCommand("开始游戏");
item_new_game.setMnemonic('N');
item_exit_game = new JMenuItem("退出游戏(E)");
item_exit_game.setActionCommand("退出游戏");
item_exit_game.setMnemonic('E');
item_save_game = new JMenuItem("保存游戏(C)");
item_save_game.setActionCommand("保存游戏");
item_save_game.setMnemonic('C');
item_continue_game = new JMenuItem("继续游戏(S)");
item_continue_game.setActionCommand("继续游戏");
item_continue_game.setMnemonic('S');
menu_game.add(item_new_game);
menu_game.add(item_exit_game);
menu_game.add(item_save_game);
menu_game.add(item_continue_game);
add(menu_game);
}
}
package tankfire1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
public class Bullet_Class {
private int bullet_speed = 20;
private boolean fire = true;
//子弹坐标
private int bullet_x ;
private int bullet_y = 900;
private char bullet_direction;
//创建子弹类容器
//Vector
public Bullet_Class() {
}
public Bullet_Class(int x,int y,char direction) {
bullet_x = x;
bullet_y = y;
bullet_direction = direction;
}
//设置子弹运动路径
public void chang() {
if(bullet_direction==KeyEvent.VK_UP&&bullet_y>=5) {
bullet_y-=bullet_speed;
}
else if(bullet_y<=5+bullet_speed) {
fire = false;
}
if(bullet_direction==KeyEvent.VK_DOWN&&bullet_y<=810) {
bullet_y+=bullet_speed;
}
else if(bullet_y>=810-bullet_speed) {
fire = false;
}
if(bullet_direction==KeyEvent.VK_LEFT&&bullet_x>=0) {
bullet_x-=bullet_speed;
}
else if(bullet_x<=bullet_speed) {
fire = false;
}
if(bullet_direction==KeyEvent.VK_RIGHT&&bullet_x<=1000) {
bullet_x+=bullet_speed;
}
else if(bullet_x>=1000-bullet_speed) {
fire = false;
}
}
public int getBulletSpeed() {
return bullet_speed;
}
public boolean getFire() {
return fire;
}
public void setFire(boolean fire) {
this.fire = fire;
}
public int getX() {
return bullet_x;
}
public int getY() {
return bullet_y;
}
public void setY(int y) {
bullet_y = y;
}
public void setX(int x) {
bullet_x = x;
}
public int getSpeed() {
return bullet_speed;
}
public void setDirection(char direction) {
bullet_direction = direction;
}
public void setXY(int x,int y) {
bullet_x = x;
bullet_y = y;
}
}
package tankfire1;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Vector;
public class Enemy_Tank_Class implements Runnable{
private int enemy_direction ;
private final int enemy_tank_speed = 5;
private int x = 200;
private int y = 200;
//定义敌方子弹类容器
Vector
//Bullet_Class《 enemy_bullet = new Bullet_Class();
public Enemy_Tank_Class() {
}
public Enemy_Tank_Class(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(3000);
enemy_direction = (int)(Math.random()*4);
if(enemy_direction==0) {
enemy_direction = KeyEvent.VK_UP;
Bullet_Class b = new Bullet_Class();
b.setXY(x,y);
b.setDirection((char)KeyEvent.VK_UP);
enemy_bullet.add(b);
}
else if(enemy_direction==1) {
enemy_direction = KeyEvent.VK_DOWN;
Bullet_Class b = new Bullet_Class();
b.setXY(x,y);
b.setDirection((char)KeyEvent.VK_DOWN);
enemy_bullet.add(b);
}
else if(enemy_direction==2) {
enemy_direction = KeyEvent.VK_LEFT;
Bullet_Class b = new Bullet_Class();
b.setXY(x,y);
b.setDirection((char)KeyEvent.VK_LEFT);
enemy_bullet.add(b);
}
else {
enemy_direction = KeyEvent.VK_RIGHT;
Bullet_Class b = new Bullet_Class();
b.setXY(x,y);
b.setDirection((char)KeyEvent.VK_RIGHT);
enemy_bullet.add(b);
}
//System.out.println(enemy_direction);
}
catch(Exception e) {
}
}
}
public int getEnemyDirection() {
return enemy_direction;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Vector
return enemy_bullet;
}
public void setX(int x) {
this.x = x;
}
public void tankRemove() {
if(x!=0) {
if(enemy_direction == KeyEvent.VK_UP) {
if(y<=30)
y=30;
else
y-=enemy_tank_speed;
}
if(enemy_direction == KeyEvent.VK_DOWN) {
if(y>=780)
y=780;
else
y+=enemy_tank_speed;
}
if(enemy_direction == KeyEvent.VK_LEFT) {
if(x<=25)
x=25;
else
x-=enemy_tank_speed;
}
if(enemy_direction == KeyEvent.VK_RIGHT) {
if(x>=965)
x=965;
else
x+=enemy_tank_speed;
}
//System.out.println(x+" "+y);
}
}
}
package tankfire1;
import java.awt.*;
import javax.swing.*;
public class Customs_Pass_Class extends JPanel implements Runnable{
private int size = 38;
public Customs_Pass_Class() {
}
@Override
public void paint(Graphics g) {
//setSize(1000,1000);
g.fillRect(0, 0,1000,1000);
g.setColor(Color.yellow);
g.setFont(new Font("华文行楷",Font.PLAIN,size));
g.drawString("第一关", 400, 500);
}
public void run() {
while(true) {
repaint();
try {
if(size==38) {
size = 2;
}
else {
size = 38;
}
Thread.sleep(200);
}
catch(Exception e) {
}
}
}
}