package javawj.view;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.Color;
import javawj.view.tankeview.Tanke;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javawj.direction.Direction;
import javawj.view.tankeview.TankeBall;
import javawj.view.tankeview.FoeTanke;
import javawj.tanke.map.FoeMap;
import javax.swing.JLabel;
import java.awt.Font;
/**
* 程序运行界面
* 王军制作
* @version 1.0
*/
public class MainView extends JFrame {
JPanel pan = new JPanel(); // 游戏面板
Tanke tanke = new Tanke(); //坦克
int tankespeed = 7; //坦克每次的位移量
int tankedirection = Direction.up; //坦克的移动方向
public MainView() {
try {
jbInit();
this.setBounds(100, 100, 520, 500);
this.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
pan.setBackground(new Color(236, 243, 243));
pan.setBorder(BorderFactory.createLineBorder(Color.black));
pan.setBounds(new Rectangle(12, 48, 489, 402));
pan.setLayout(null);
//调用函数初始化坦克
TankeInit();
jLabel1.setText("上下左右控制 空格子弹 B键添加敌人 当前分数:");
jLabel1.setBounds(new Rectangle(11, 12, 292, 25));
lab_fenshu.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
lab_fenshu.setForeground(Color.red);
lab_fenshu.setText("0");
lab_fenshu.setBounds(new Rectangle(306, 10, 47, 26));
this.getContentPane().add(pan);
this.getContentPane().add(jLabel1);
this.getContentPane().add(lab_fenshu);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(key_Tanke_Listener);
}
//初始化坦克
private void TankeInit() {
tanke.setLocation((pan.getWidth() - tanke.getWidth()) / 2,
(pan.getHeight() - tanke.getHeight()) / 2);
pan.add(tanke);
}
//坦克移动的方法
public void tankeMove() {
int x = tanke.getX();
int y = tanke.getY();
if (tankedirection == Direction.up) {
tanke.setLocation(x, y - this.tankespeed);
} else if (tankedirection == Direction.down) {
tanke.setLocation(x, y + this.tankespeed);
} else if (tankedirection == Direction.right) {
tanke.setLocation(x + this.tankespeed, y);
} else if (tankedirection == Direction.left) {
tanke.setLocation(x - this.tankespeed, y);
}
x = tanke.getX();
y = tanke.getY();
if (x < 0) {
x = 1;
tanke.setLocation(x, tanke.getY());
}
if (x > pan.getWidth() - tanke.getWidth()) {
x = pan.getWidth() - tanke.getWidth() - 1;
tanke.setLocation(x, tanke.getY());
}
if (y < 0) {
tanke.setLocation(tanke.getX(), 1);
}
if (y > pan.getHeight() - tanke.getHeight()) {
tanke.setLocation(tanke.getX(),
pan.getHeight() - tanke.getHeight() - 1);
}
pan.repaint();
}
//发射子弹的方法
public void sendTankeBall() {
TankeBall ball = new TankeBall(pan,false);
//得到坦克的方向
ball.setDirection(this.tankedirection);
int x = tanke.getX();
int y = tanke.getY();
int tankew = tanke.getWidth();
int tankeh = tanke.getHeight();
if (tankedirection == Direction.up) {
ball.setLocation(x + (tankew - ball.getWidth()) / 2,
y - ball.getHeight());
} else if (tankedirection == Direction.down) {
ball.setLocation(x + (tankew - ball.getWidth()) / 2, y + tankeh);
} else if (tankedirection == Direction.right) {
ball.setLocation(x + tankew, y + (tankeh - ball.getHeight()) / 2);
} else if (tankedirection == Direction.left) {
ball.setLocation(x - ball.getWidth(),
y + (tankeh - ball.getHeight()) / 2);
}
pan.add(ball);
pan.repaint();
ball.setJLabel(this.lab_fenshu);
Thread th = new Thread(ball);
th.start();
}
//键盘监听器
int foecount = 0; //敌人数量
int foeballcount=0;
int myball = 0;
KeyListener key_Tanke_Listener = new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
// System.out.println(e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_UP) {
tankedirection = Direction.up;
tanke.setText("↑");
tankeMove();
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
tankedirection = Direction.down;
tanke.setText("↓");
tankeMove();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
tankedirection = Direction.right;
tanke.setText("→");
tankeMove();
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
tankedirection = Direction.left;
tanke.setText("←");
tankeMove();
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
//发射子弹
sendTankeBall();
} else if (e.getKeyCode() == KeyEvent.VK_B) {
FoeTanke f = new FoeTanke(pan);
f.setLocation(f.r.nextInt(pan.getWidth() - f.getWidth()), 0);
pan.add(f);
pan.repaint();
//
FoeMap.foeMap.add(f);
foecount++;
Thread th = new Thread(f);
th.start();
}
}
public void keyReleased(KeyEvent e) {
}
};
JLabel jLabel1 = new JLabel();
JLabel lab_fenshu = new JLabel();
}
java swing thread 坦克大战
需积分: 9 13 浏览量
2009-03-13
09:35:23
上传
评论
收藏 7KB RAR 举报
zhengdian
- 粉丝: 1
- 资源: 35
最新资源
- Fortran语言语法快速入门.pdf
- 明日方舟 年 鼠标指针.rar
- 全国银行经济监管可视化系统
- e商小二-供应链管理物流交仓创业商业计划书.rar
- Edge浏览器下载文件提示 “无法安全下载” 的解决方法
- 基于springboot+layui的医院日常耗材管理系统.zip
- Emkex亿迈克思新型互联网磁材供应链商业计划书.rar
- 计算机毕业设计-ASP.NET某店POS积分管理系统-销售情况,会员卡再发行数据生成(源代码+)-毕设源码实例.zip
- 计算机毕业设计-asp.net某店POS积分管理系统-清除履历表、日志表、月购买额(源代码+)-毕设源码实例.zip
- 计算机毕业设计-ASP.NET某店POS积分管理系统-积分实绩更新及销售状况统计(源代码+)-毕设源码实例.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈