package com.iti8.control;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import org.omg.CORBA.PRIVATE_MEMBER;
public class WindowTest extends JPanel {
public static int game_width;
public static int game_height;
public Bullet[] bullets= {}; //子弹数
public Enemy[] enemys = {}; //敌人数
tank tankerTank = new tank(); //用户
public int state = 0; //游戏状态 0开,1暂停
int START = 0;
public int PAUSE = 1;
public boolean key_w = false;
public boolean key_d = false;
public boolean key_s = false;
public boolean key_a = false;
public boolean key_p = false;
public static final int enterEnemyNum = 3;
public int x = 0;
public int y = 0;
public static BufferedImage background;
public static int getGame_width() {
return game_width;
}
public static void setGame_width(int game_width) {
WindowTest.game_width = game_width;
}
public static int getGame_height() {
return game_height;
}
public static void setGame_height(int game_height) {
WindowTest.game_height = game_height;
}
public String toString(){
return "坐标为:(" + x +"," + y +")";
}
static {
try {
background = ImageIO.read(WindowTest.class.getResource("background.png"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//重写JPanel中的paint函数
@Override
public void paint(Graphics g){
//g.drawImage(background,0,0,null); //画背景
// g.drawImage(null, 11, 11, 80, 30,Color.blue,null);
//g.drawRect(0, 0, 200, 60);
if(state == START){
super.paint(g);
painScore(g); //画坐标
painBullets(g); //画子弹
painTanker(g); //画坦克
paintEnemy(g); //画敌人
}
else {
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 50);
g.setColor(Color.red);
g.setFont(font);
g.clearRect(0, 0, game_width, game_height);
g.drawString("GAME OVER", game_width/4, game_height/2);
}
}
//画鼠标坐标
public void painScore(Graphics g){
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 16);
g.setColor(Color.red);
//g.fillOval(100, 100, 300, 200); //圆形:距离x轴,距离y轴,X轴长度,Y轴长度
// g.fillRect(0, 0, 100, 100); //方形:距离X轴,距离Y轴,X轴长度,Y轴长度
g.setFont(font);
g.drawString(this.toString(), this.x, this.y);
}
//画坦克
public void painTanker(Graphics g){
g.fillRect(tankerTank.getX(), tankerTank.getY(), tankerTank.getWidth(), tankerTank.getHeight()); //画坦克
}
//画子弹
public void painBullets(Graphics g){
for(int i=0;i<bullets.length;i++){
g.fillOval(bullets[i].getX(), bullets[i].getY(), bullets[i].getWidth(), bullets[i].getHeight());
}
}
//画敌人
public void paintEnemy(Graphics g){
for(int i=0;i<enemys.length;i++){
g.fillOval(enemys[i].getX(), enemys[i].getY(), enemys[i].getWidth(), enemys[i].getHeight());
}
}
//敌人出现 一次3个
public void enterEnemy(){
Random random = new Random();
for(int i=0;i<enterEnemyNum;i++){
int x = random.nextInt(game_width);
int y = random.nextInt(game_height);
Enemy enemy = new Enemy(x, y);
if(!checkEnemyShoot(enemy) && !checkBulletShoot(enemy) && !checkTankShoot(enemy) && !enemyOfBorder(enemy)){
enemys = Arrays.copyOf(enemys,enemys.length+1);//扩容
enemys[enemys.length-1] = enemy; //成功增加一个
}
else {
i--;
continue;
}
}
}
//判断与敌人是否重叠
public boolean checkEnemyShoot(MovingObj movingObj){
for(int j=1;j<enemys.length;j++){//判断是否与敌人重叠
if(movingObj.shootBy(enemys[j]))
return true; //发生重叠
}
return false;
}
//判断与子弹是否重叠
public boolean checkBulletShoot(MovingObj movingObj){
for(int j=1;j<bullets.length;j++){//判断是否与敌人重叠
if(movingObj.shootBy(bullets[j]))
return true; //发生重叠
}
return false;
}
//判断与人物是否重叠
public boolean checkTankShoot(MovingObj movingObj){
if(movingObj.shootBy(tankerTank))
return true; //发生重叠
return false;
}
//是否碰撞边界
public boolean enemyOfBorder(MovingObj x){
if(x.x+x.width>=this.game_width||x.x<=0 || x.y<=0 ||x.y+x.height>this.game_height){
//转向
return true;
}
return false;
}
//子弹移动
public void bulletsMove(){
for(int i=0;i<bullets.length;i++){
switch (bullets[i].direction) {
case 0:
bullets[i].setY(bullets[i].getY()-bullets[i].speed);
break;
case 1:
bullets[i].setX(bullets[i].getX()+bullets[i].speed);
break;
case 2:
bullets[i].setY(bullets[i].getY()+bullets[i].speed);
break;
case 3:
bullets[i].setX(bullets[i].getX()-bullets[i].speed);
break;
default:
break;
}
}
}
//敌人移动
public void enemyMove(){
for(int i=0;i<enemys.length;i++){
enemys[i].move(game_width,game_height);
}
}
//敌人转向
public void changeMove(){
Random random = new Random();
for(int i=0;i<enemys.length;i++){
enemys[i].direction = random.nextInt(4);
}
}
//删除越界的子弹
public void outOfBorder(){
for(int i=0;i<bullets.length;i++){
if(bullets[i].x>=this.game_width||bullets[i].x<=0 || bullets[i].y<=0 ||bullets[i].y>this.game_height){
// Bullet bullet = bullets[i];
bullets[i] = bullets[bullets.length-1];
bullets = Arrays.copyOf(bullets, bullets.length-1); //缩容
}
}
// System.out.println("子弹数量:"+ bullets.length);
}
//判断击中目标
public void shootEnemyto(){
//需要判断击中的最近目标
//有个BUG,当子弹速度大于(目标速度+目标宽度时),子弹会穿过目标,造成没有击中的现象
int x,y;
x= bullets.length;
y = enemys.length;
int k = 0;
int[] bulletNum = new int[x];
int[] enemyNum = new int[y];
for(int i=0;i<bullets.length;i++){
for(int j=0;j<enemys.length;j++)
if(bullets[i].shootBy(enemys[j])){
//击中目标,记录子弹下标和目标下标
bulletNum[k] = i;
enemyNum[k] = j;
k++;
System.out.println("子弹"+i+"击中目标"+j);
}
}
for(int i=0;i<k;i++){
//删除子弹和目标
x = bullets.length;
y = enemys.length;
bullets[bulletNum[i]] = bullets[x-1];
bullets = Arrays.copyOf(bullets, bullets.length-1);//缩容
enemys[enemyNum[i]] = enemys[y-1];
enemys = Arrays.copyOf(enemys,enemys.length-1); //缩容
// System.out.println("子弹数:"+(x-1)+"敌人数:"+(y-1));
System.out.println("敌人数:"+(y-1));
}
}
//判断用户是否被撞击
public void overGame(){
for(int i=0;i<enemys.length;i++){
if(tankerTank.shootBy(enemys[i]))
{
//被击中
state = PAUSE; //游戏停止
bullets = new Bullet[0];
enemys = new Enemy[0];
}
}
}
public void action1(){
//创建线程1,监听用户移动
RunnableTest tt = new RunnableTest(this);
Thread thread = new Thread(tt);
thread.start();
//创建线程2,监听用户射击,添加敌人
RunnableTest1 tt2 = new RunnableTest1(this);
Thread thread2 = new Thread(tt2);
thread2.start();
//鼠标监听
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
//mouseAdapter类是一个抽象类(适配器)用于接收鼠标事件。
public void mouseMoved(MouseEvent e){
x= e.getX(