package com.neutech.base;
import com.neutech.runtime.*;
import com.neutech.util.Map.DisposeLevel;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class GameFrame extends Frame {
public GameFrame() {
init();
}
private void init() {
// 设置屏幕的大小
setSize(Constant.WINDOW_OUTER_WIDTH,Constant.WINDOW_OUTER_HEIGHT);
// 设置居中
setLocationRelativeTo(null);
// 不允许改变屏幕大小
setResizable(false);
// 屏蔽输入法
enableInputMethods(false);
// 设置显示
setVisible(true);
// 启动一个线程定时刷新窗口
new Thread()
{
public void run(){
while (true)
{
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
// 添加一个窗口监听事件,用来实现窗口关闭
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// 关虚拟机
System.exit(0);
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_SPACE)
{
Constant.execute=!Constant.execute;
}
else {
plane.keyPressed(e);
}
}
public void keyReleased(KeyEvent e)
{
plane.keyReleased(e);
}
});
}
// 等级处理
DisposeLevel disposeLevel=new DisposeLevel();
// 上面的地图
Map map1=new Map(-Constant.WINDOW_OUTER_HEIGHT);
// 下面的地图
Map map2=new Map(0);
// 分数
Score score=new Score();
// 飞机
Plane plane=new Plane();
// 飞机子弹集合
public List
planeBulletList =new CopyOnWriteArrayList<>();
// 敌机集合
public List enemyList=new CopyOnWriteArrayList<>();
// 敌机子弹集合
public List enemyBulletList=new CopyOnWriteArrayList<>();
// 爆炸效果集合
public List explodeList=new CopyOnWriteArrayList<>();
// 血条
Blood blood=new Blood();
@Override
public void paint(Graphics g)
{
if(Constant.execute)
{
map1.move();
map2.move();
map1.draw(g);
map2.draw(g);
plane.move();
plane.draw(g);
plane.fire();
// 遍历飞机的子弹
for (PlaneBullet planeBullet : planeBulletList) {
planeBullet.move();
planeBullet.draw(g);
}
// 遍历敌机
for (Enemy enemy:enemyList) {
enemy.move();
enemy.draw(g);
enemy.fire();
}
// 遍历敌机的子弹
for (EnemyBullet enemyBullet:enemyBulletList) {
enemyBullet.move();
enemyBullet.draw(g);
}
// 分数
score.draw(g);
score.create();
// 我方子弹与敌机的碰撞检测
for (PlaneBullet planeBullet : planeBulletList) {
for (Enemy enemy:enemyList) {
if(planeBullet.creatRectangle().intersects(enemy.creatRectangle()))
{
// 从集合中清除子弹和敌机
planeBulletList.remove(planeBullet);
enemyList.remove(enemy);
// 分数+1
score.setNumber(score.getNumber()+1);
// 爆炸效果
explodeList.add(new Explode(enemy.getX(),enemy.getY()));
}
}
}
// 我方飞机与敌机碰撞检测
for(Enemy enemy:enemyList){
if(plane.creatRectangle().intersects(enemy.creatRectangle()))
{
// 移除敌机
enemyList.remove(enemy);
// 爆炸效果
explodeList.add(new Explode(enemy.getX(),enemy.getY()));
// 血条-1
blood.setWidth(blood.getWidth()-20);
blood.setX(blood.getX()+20);
}
}
// 遍历爆炸效果
for (Explode explode:explodeList) {
explode.draw(g);
}
// 敌方子弹与我方飞机的碰撞检测
for (EnemyBullet enemyBullet : enemyBulletList) {
if(plane.creatRectangle().intersects(enemyBullet.creatRectangle()))
{
// 移除子弹
enemyBulletList.remove(enemyBullet);
// 血条-1
blood.setWidth(blood.getWidth()-20);
blood.setX(blood.getX()+20);
}
}
g.setColor(Color.GRAY);
g.fillRoundRect(400,40,100,30, 10, 10);
blood.draw(g);
}
}
public Image offScreenImage=null;
public void update(Graphics g){
if (offScreenImage==null)
{
offScreenImage=this.createImage(Constant.WINDOW_OUTER_WIDTH,Constant.WINDOW_OUTER_HEIGHT);
}
Graphics gOff=offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,0,0,null);
}
}