package JSD1806HYQ;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/** 世界 */
public class World extends JPanel implements KeyListener{
public static final int WIDTH = 800;
public static final int HEIGHT = 1000;
public static final int updistance=30;//java窗口中上边距为30个像素;
public static final int downdistance=8;//java窗口中下边距左右编剧为8个像素;
private static final long serialVersionUID = 1L;
private Boss zzlz = new Boss();
private Sky sky = new Sky();
private Hero hero = new Hero();
private FlyingObject[] enemies = {}; //敌人数组
private Bullet[] bullets = {}; //子弹数组
private DrBullet[] drbullets = {};
Date startTime;//界面开始的时间
Date startTime1;//游戏开始的时间
Date endTime;//飞机死亡的时间
Date nowTime;//现在的时间
Date collisionTime=new Date();
Date collisionTimeNow;
long timefirst=24625;
long now_startTime;//开始到现在的时间
int enterNum=0;
boolean bigbullet=true;
boolean boss=false;//判断是否是boss关
boolean flag = false;//表示游戏是否开始
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
static{
start = FlyingObject.loadImage("start.png");
pause = FlyingObject.loadImage("pause.png");
gameover = FlyingObject.loadImage("gameover.png");
}
public FlyingObject nextOne(){
Random rand = new Random();
int type = rand.nextInt(20);
if(type < 4){
return new Bee();
}else if(type < 12){
return new Airplane();
}else {
return new BigAirplane();
}
}
int flyEnterIndex = 0;
public void enterAction(){
flyEnterIndex++;
if(flyEnterIndex%40==0){
FlyingObject obj = nextOne();
enemies = Arrays.copyOf(enemies,enemies.length+1);
enemies[enemies.length-1] = obj;
}
}
public void stepAction(){
zzlz.step();
sky.step();
for(int i=0;i<enemies.length;i++){
enemies[i].step();
}
for(int i=0;i<bullets.length;i++){
bullets[i].step();
}
}
int shootIndex = 0;
public void shootAction(){
shootIndex++;
if(shootIndex%30==0){
Bullet[] bs = hero.shoot();
bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
}
}
private int score = 0;
public void bangAction(){
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
for(int j=0;j<enemies.length;j++){
FlyingObject f = enemies[j];
if(b.isLife() && f.isLife() && b.hit(f)){
b.goDead();
f.goDead();
if(f instanceof Enemy){
Enemy e = (Enemy)f;
score += e.getScore();
}
if(f instanceof Award){
Award a = (Award)f;
int type = a.getAwardType();
switch(type){
case Award.DOUBLE_FIRE:
hero.addDoubleFire();
break;
case Award.LIFE:
hero.addLife();
break;
}
}
}
}
}
}
public void outOfBoundsAction(){
int index = 0;
FlyingObject[] enemyLives = new FlyingObject[enemies.length];
for(int i=0;i<enemies.length;i++){
FlyingObject e = enemies[i];
if(!e.outOfBounds() && !e.isRemove()){
enemyLives[index++] = e;
}
}
enemies = Arrays.copyOf(enemyLives,index);
index = 0;
Bullet[] bulletLives = new Bullet[bullets.length];
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
if(!b.outOfBounds() && !b.isRemove()){
bulletLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletLives,index);
}
public void hitAction(){
for(int i=0;i<enemies.length;i++){
FlyingObject f = enemies[i];
if(f.isLife() && hero.hit(f)){
f.goDead();
hero.subtractLife();
hero.clearDoubleFire();
break;
}
}
}
public void checkGameOverAction(){
if(hero.getLife()<=0){
flag = false;
}
}
int num = 1;
public void action(){ //启动执行
Timer timer = new Timer();
int intervel = 10;
timer.schedule(new TimerTask(){
public void run(){
if(flag==true){
enterAction();
stepAction();
shootAction();
outOfBoundsAction();
bangAction();
hitAction();
checkGameOverAction();
}
repaint();
}
},intervel,intervel);
}
public void paint(Graphics g){
if(now_startTime>1000){
System.out.println(now_startTime);
boss=true;
zzlz.paintObject(g);
}
sky.paintObject(g);
hero.paintObject(g);
for(int i=0;i<enemies.length;i++){
enemies[i].paintObject(g);
}
for(int i=0;i<bullets.length;i++){
bullets[i].paintObject(g);
}
for(int i=0;i<drbullets.length;i++){
drbullets[i].paintObject(g);
}
g.drawString("SCORE: "+score,10,25);
g.drawString("LIFE: "+hero.getLife(),10,45);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
World world = new World();
frame.add(world);
frame.addKeyListener(world);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
world.action();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
hero.addDirection(e);
if(e.getKeyCode()==KeyEvent.VK_ENTER){
flag = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
hero.minusDirection(e);
if(e.getKeyCode()==KeyEvent.VK_ENTER){
enterNum++;
if(enterNum==1){
startTime=new Date();//游戏开始的时间
System.out.println(startTime);
}
}
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/** 世界 */
public class World extends JPanel implements KeyListener{
public static final int WIDTH = 800;
public static final int HEIGHT = 1000;
public static final int updistance=30;//java窗口中上边距为30个像素;
public static final int downdistance=8;//java窗口中下边距左右编剧为8个像素;
private static final long serialVersionUID = 1L;
private Boss zzlz = new Boss();
private Sky sky = new Sky();
private Hero hero = new Hero();
private FlyingObject[] enemies = {}; //敌人数组
private Bullet[] bullets = {}; //子弹数组
private DrBullet[] drbullets = {};
Date startTime;//界面开始的时间
Date startTime1;//游戏开始的时间
Date endTime;//飞机死亡的时间
Date nowTime;//现在的时间
Date collisionTime=new Date();
Date collisionTimeNow;
long timefirst=24625;
long now_startTime;//开始到现在的时间
int enterNum=0;
boolean bigbullet=true;
boolean boss=false;//判断是否是boss关
boolean flag = false;//表示游戏是否开始
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
static{
start = FlyingObject.loadImage("start.png");
pause = FlyingObject.loadImage("pause.png");
gameover = FlyingObject.loadImage("gameover.png");
}
public FlyingObject nextOne(){
Random rand = new Random();
int type = rand.nextInt(20);
if(type < 4){
return new Bee();
}else if(type < 12){
return new Airplane();
}else {
return new BigAirplane();
}
}
int flyEnterIndex = 0;
public void enterAction(){
flyEnterIndex++;
if(flyEnterIndex%40==0){
FlyingObject obj = nextOne();
enemies = Arrays.copyOf(enemies,enemies.length+1);
enemies[enemies.length-1] = obj;
}
}
public void stepAction(){
zzlz.step();
sky.step();
for(int i=0;i<enemies.length;i++){
enemies[i].step();
}
for(int i=0;i<bullets.length;i++){
bullets[i].step();
}
}
int shootIndex = 0;
public void shootAction(){
shootIndex++;
if(shootIndex%30==0){
Bullet[] bs = hero.shoot();
bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
}
}
private int score = 0;
public void bangAction(){
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
for(int j=0;j<enemies.length;j++){
FlyingObject f = enemies[j];
if(b.isLife() && f.isLife() && b.hit(f)){
b.goDead();
f.goDead();
if(f instanceof Enemy){
Enemy e = (Enemy)f;
score += e.getScore();
}
if(f instanceof Award){
Award a = (Award)f;
int type = a.getAwardType();
switch(type){
case Award.DOUBLE_FIRE:
hero.addDoubleFire();
break;
case Award.LIFE:
hero.addLife();
break;
}
}
}
}
}
}
public void outOfBoundsAction(){
int index = 0;
FlyingObject[] enemyLives = new FlyingObject[enemies.length];
for(int i=0;i<enemies.length;i++){
FlyingObject e = enemies[i];
if(!e.outOfBounds() && !e.isRemove()){
enemyLives[index++] = e;
}
}
enemies = Arrays.copyOf(enemyLives,index);
index = 0;
Bullet[] bulletLives = new Bullet[bullets.length];
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
if(!b.outOfBounds() && !b.isRemove()){
bulletLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletLives,index);
}
public void hitAction(){
for(int i=0;i<enemies.length;i++){
FlyingObject f = enemies[i];
if(f.isLife() && hero.hit(f)){
f.goDead();
hero.subtractLife();
hero.clearDoubleFire();
break;
}
}
}
public void checkGameOverAction(){
if(hero.getLife()<=0){
flag = false;
}
}
int num = 1;
public void action(){ //启动执行
Timer timer = new Timer();
int intervel = 10;
timer.schedule(new TimerTask(){
public void run(){
if(flag==true){
enterAction();
stepAction();
shootAction();
outOfBoundsAction();
bangAction();
hitAction();
checkGameOverAction();
}
repaint();
}
},intervel,intervel);
}
public void paint(Graphics g){
if(now_startTime>1000){
System.out.println(now_startTime);
boss=true;
zzlz.paintObject(g);
}
sky.paintObject(g);
hero.paintObject(g);
for(int i=0;i<enemies.length;i++){
enemies[i].paintObject(g);
}
for(int i=0;i<bullets.length;i++){
bullets[i].paintObject(g);
}
for(int i=0;i<drbullets.length;i++){
drbullets[i].paintObject(g);
}
g.drawString("SCORE: "+score,10,25);
g.drawString("LIFE: "+hero.getLife(),10,45);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
World world = new World();
frame.add(world);
frame.addKeyListener(world);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
world.action();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
hero.addDirection(e);
if(e.getKeyCode()==KeyEvent.VK_ENTER){
flag = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
hero.minusDirection(e);
if(e.getKeyCode()==KeyEvent.VK_ENTER){
enterNum++;
if(enterNum==1){
startTime=new Date();//游戏开始的时间
System.out.println(startTime);
}
}
}
}