package test;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.*;
import javax.swing.*;
public class GameFrame extends JFrame{
Random rnd = new Random();
final int speed = 5;
public static boolean isGameOver = false;//为true表示游戏结束
JPanel panel = new JPanel();//面板
ArrayList<JLabel> enemies = new ArrayList<JLabel>();//敌人集合
ArrayList<JLabel> walls = new ArrayList<JLabel>();//墙体集合
JLabel myTank = new JLabel(new ImageIcon("img/4.png"));//玩家坦克对象
int lastDirection = KeyEvent.VK_UP;//记录上次的方向
long lastShotTime = 0;//记录上次发射子弹时间
ImageIcon[] myTankIcons = new ImageIcon[]{
new ImageIcon("img/2.png"),
new ImageIcon("img/4.png"),
new ImageIcon("img/6.png"),
new ImageIcon("img/0.png")
};
public GameFrame(){
//设置窗体标题
this.setTitle("坦克大战");
//设置窗体左上角小图标
this.setIconImage(Toolkit.getDefaultToolkit().createImage("img/4.png"));
this.setBounds(50,50,800,600);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(panel);
// panel.setBackground(Color.black);
this.panel.setLayout(null);
myTank.setBounds(400,500,48,48);
panel.add(myTank);
//随机产生墙对象
for (int i = 0; i < 30; i++) {
JLabel wall = new JLabel(new ImageIcon("img/wall.png"));
int x = rnd.nextInt(600);
int y = rnd.nextInt(400)+50;
wall.setBounds(x,y,48,48);
walls.add(wall);
for (int j = 0; j < walls.size()-1; j++) {
if(wall.getBounds().intersects(walls.get(j).getBounds())){
walls.remove(i);
i--;
break;
}
}
}
//添加墙对象
for (int i = 0; i < walls.size(); i++) {
panel.add(walls.get(i));
}
// 添加敌人
for (int i = 0; i < 10; i++) {
//创建敌人
JLabel enemy = new JLabel(new ImageIcon("img/enemy0.png"));
enemy.setBounds((i+1)*48,0,48,48);
enemies.add(enemy);
panel.add(enemy);
EnemyThread et = new EnemyThread(this,enemy);
et.start();
}
ImageIcon img = new ImageIcon("img/desert_800X600.jpg");
JLabel back = new JLabel(img);
this.getLayeredPane().add(back, new Integer(Integer.MIN_VALUE));
back.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
panel.add(new JLabel("Hello Background"));
((JPanel) getContentPane()).setOpaque(false);
this.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
int xSpeed = 0;//定义横纵偏移量
int ySpeed = 0;
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
//将最后一次移动的方向保存起来
lastDirection = e.getKeyCode();
if(myTank.getX()>speed){
xSpeed = -speed;
}
myTank.setIcon(new ImageIcon("img/2.png"));
break;
case KeyEvent.VK_RIGHT:
//将最后一次移动的方向保存起来
lastDirection = e.getKeyCode();
if(myTank.getX()<panel.getWidth()-myTank.getWidth()-speed){
xSpeed = speed;
}
myTank.setIcon(new ImageIcon("img/6.png"));
break;
case KeyEvent.VK_UP:
//将最后一次移动的方向保存起来
lastDirection = e.getKeyCode();
if(myTank.getY()>speed){
ySpeed = -speed;
}
myTank.setIcon(new ImageIcon("img/4.png"));
break;
case KeyEvent.VK_DOWN:
//将最后一次移动的方向保存起来
lastDirection = e.getKeyCode();
if(myTank.getY()<panel.getWidth()-myTank.getHeight()-speed){
ySpeed = speed;
}
myTank.setIcon(new ImageIcon("img/0.png"));
break;
case KeyEvent.VK_SPACE:
//如果离上次发射子弹时间不到一秒,则不允许发射
if(System.currentTimeMillis()-lastShotTime<1000)
return;
JLabel bullet = new JLabel();
panel.add(bullet);
//根据上次的方向来决定子弹显示在什么位置
switch(lastDirection){
case KeyEvent.VK_LEFT:
bullet.setIcon(new ImageIcon("img/bulletLeft.png"));
bullet.setBounds(myTank.getX()-16,myTank.getY()+16,16,16);
break;
case KeyEvent.VK_RIGHT:
bullet.setIcon(new ImageIcon("img/bulletRight.png"));
bullet.setBounds(myTank.getX()+ myTank.getWidth(),myTank.getY()+16,16,16);
break;
case KeyEvent.VK_UP:
bullet.setIcon(new ImageIcon("img/bulletUp.png"));
bullet.setBounds(myTank.getX()+ 16,myTank.getY()- 16,16,16);
break;
case KeyEvent.VK_DOWN:
bullet.setIcon(new ImageIcon("img/bulletDown.png"));
bullet.setBounds(myTank.getX()+ 16,myTank.getY()+myTank.getHeight(),16,16);
}
BulletThread bt = new BulletThread(bullet,GameFrame.this,lastDirection);
bt.start();
lastShotTime = System.currentTimeMillis();
break;
}
//添加玩家的坦克
myTank.setLocation(myTank.getX()+xSpeed,myTank.getY()+ySpeed);
//判断玩家是否撞墙
for (int i = 0; i < walls.size(); i++) {
// 如果墙已经不显示,则不阻拦玩家坦克
if(walls.get(i).isVisible() == false)
continue;
if(myTank.getBounds().intersects(walls.get(i).getBounds())){
myTank.setLocation(myTank.getX()-xSpeed, myTank.getY()-ySpeed);
}
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new GameFrame();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
自己用java Swing 开发的坦克大战小游戏
共141个文件
png:111个
class:14个
java:12个
4星 · 超过85%的资源 需积分: 10 28 下载量 44 浏览量
2011-08-06
20:01:46
上传
评论
收藏 1.55MB RAR 举报
温馨提示
自己用java Swing开发的坦克大战小游戏,提供一些自己开发的思路
资源推荐
资源详情
资源评论
收起资源包目录
自己用java Swing 开发的坦克大战小游戏 (141个子文件)
GameFrame.class 4KB
EnemyThread.class 4KB
GameFrame.class 3KB
GameFrame$1.class 3KB
BulletThread.class 2KB
EnemyBulletThread.class 2KB
EnemyBulletThread.class 2KB
Boom1Thread.class 1KB
Boom2Thread.class 1KB
Boom1Thread.class 1KB
Boom2Thread.class 1KB
GameFrame$1.class 1KB
EnemyThread.class 962B
BulletThread.class 798B
.classpath 232B
Thumbs.db 233KB
GameFrame.java 5KB
EnemyThread.java 5KB
GameFrame.java 3KB
BulletThread.java 3KB
EnemyBulletThread.java 2KB
EnemyBulletThread.java 2KB
EnemyThread.java 2KB
BulletThread.java 1KB
Boom2Thread.java 667B
Boom2Thread.java 663B
Boom1Thread.java 637B
Boom1Thread.java 603B
desert_800X600.jpg 377KB
explosion2-63.png 32KB
explosion2-62.png 32KB
explosion2-61.png 31KB
explosion2-64.png 31KB
explosion2-60.png 30KB
explosion2-59.png 30KB
explosion2-58.png 29KB
explosion2-57.png 28KB
explosion1-14.png 27KB
explosion1-13.png 27KB
explosion2-56.png 27KB
explosion2-55.png 26KB
explosion2-65.png 26KB
explosion2-54.png 25KB
explosion1-12.png 25KB
explosion2-53.png 24KB
explosion2-52.png 23KB
explosion1-11.png 22KB
explosion2-51.png 22KB
explosion2-50.png 21KB
explosion2-49.png 20KB
explosion2-48.png 20KB
explosion1-10.png 19KB
explosion2-47.png 19KB
explosion2-46.png 18KB
explosion2-45.png 17KB
explosion1-9.png 17KB
explosion2-44.png 16KB
explosion1-15.png 16KB
explosion2-43.png 15KB
explosion2-42.png 15KB
explosion2-41.png 14KB
explosion1-8.png 14KB
explosion2-40.png 13KB
explosion2-39.png 13KB
explosion2-38.png 12KB
explosion2-37.png 11KB
explosion1-7.png 11KB
explosion2-36.png 11KB
explosion2-66.png 11KB
explosion2-35.png 10KB
explosion2-34.png 10KB
explosion2-33.png 9KB
explosion2-32.png 9KB
explosion1-6.png 9KB
explosion2-31.png 8KB
explosion2-30.png 8KB
explosion2-29.png 8KB
explosion2-28.png 7KB
explosion2-27.png 7KB
explosion2-67.png 6KB
explosion2-26.png 6KB
explosion1-5.png 6KB
explosion2-25.png 6KB
explosion2-24.png 6KB
explosion2-23.png 5KB
explosion2-22.png 5KB
wall.png 5KB
explosion2-21.png 5KB
explosion1-4.png 5KB
explosion2-20.png 4KB
explosion2-19.png 4KB
explosion2-18.png 4KB
explosion2-17.png 4KB
explosion1-3.png 3KB
explosion2-16.png 3KB
explosion2-15.png 3KB
explosion2-14.png 3KB
explosion2-13.png 3KB
explosion2-12.png 2KB
explosion1-2.png 2KB
共 141 条
- 1
- 2
资源评论
- 小傅哥2013-08-07这个怎么运行呀
- siwalet2014-03-17有错误 不太值啊 呵呵
- Kiss_TF2014-08-26没法直接运行!!!!
- lsattralias2013-09-20还不错吧!
- In-Memory2013-01-18没有样例效果!
dcyydc
- 粉丝: 47
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功