package tedu.cn;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class World extends JPanel {
private static final long serialVersionUID = 1L;
public static final int WIDTH=1100;
public static final int HEIGHT=620;
static JFrame frame;
public static final int START=0; //启动状态
public static final int RUN=1; //运行状态
public static final int PAUSE=2; //暂停状态
public static final int GAME_OVER=3;//游戏结束状态
private static int state=START; //游戏状态
public static final int COMPOSING_WIDTH=2400; //排版宽度
private static BufferedImage start;
private static BufferedImage pause;
private static BufferedImage gameover;
static {//初始化静图片
start =SuperClass.loadImage("start.jpg");
pause=SuperClass.loadImage("pause.png");
gameover=SuperClass.loadImage("gameover.png");
}
Hero hero = new Hero();
Background back = new Background();
SuperClass[] award = {};
Ground[] grounds = {};
Enemies[] enemies = {};
/*生成金币银币奖励数组*/
public SuperClass[] nextOneBigAward() {
Random rand = new Random();
int type = rand.nextInt(25);
int goldWidths=40;//金币之间的距离
if(type<7) {
GoldAward[] golds = new GoldAward[26] ;
for (int i = 0; i < 13; i++) {
if(i<=6){
golds[i]= new GoldAward(World.WIDTH+goldWidths*i,200+goldWidths*i);
}else{
golds[i]= new GoldAward(World.WIDTH+goldWidths*i,200+goldWidths*(13 -i-1));
}
}
golds[13] = new GoldAward(World.WIDTH+goldWidths*0, 200-40);
golds[14] = new GoldAward(World.WIDTH+goldWidths*1, 200-70);
golds[15] = new GoldAward(World.WIDTH+goldWidths*2, 200-80);
golds[16] = new GoldAward(World.WIDTH+goldWidths*3, 200-60);
golds[17] = new GoldAward(World.WIDTH+goldWidths*4, 200-50);
golds[18] = new GoldAward(World.WIDTH+goldWidths*5, 200-40);
golds[19] = new GoldAward(World.WIDTH+goldWidths*6, 200-20);
golds[20] = new GoldAward(World.WIDTH+goldWidths*7, 200-40);
golds[21] = new GoldAward(World.WIDTH+goldWidths*8, 200-50);
golds[22] = new GoldAward(World.WIDTH+goldWidths*9, 200-60);
golds[23] = new GoldAward(World.WIDTH+goldWidths*10, 200-80);
golds[24] = new GoldAward(World.WIDTH+goldWidths*11, 200-70);
golds[25] = new GoldAward(World.WIDTH+goldWidths*12, 200-40);
return golds;
}else if(type<15){
GoldAward[] g = new GoldAward[33] ;
int index=0;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if(i==0||i==3||i==6||j==0||j==3||j==6){
g[index++]= new GoldAward(World.WIDTH+goldWidths*i,100+goldWidths*j);
}
}
}
return g;
}else{
Coin[] coins = new Coin[49] ;
int index=0;
for (int i = 1; i < 8; i++) {
for (int j = 1; j < 8; j++) {
coins[index++]= new Coin(World.WIDTH+goldWidths*i,100+goldWidths*j);
}
}
return coins;
}
}
/*生成金币银币奖励数组*/
public SuperClass[] nextOneSmallAward() {
Random rand = new Random();
int type = rand.nextInt(25);
int goldWidths=40;
if(type<15) {
GoldAward[] golds = new GoldAward[7] ;
for (int i = 0; i < golds.length; i++) {
if(i<3){
golds[i]= new GoldAward(World.WIDTH+goldWidths*i,350-goldWidths*i);
}else{
golds[i]= new GoldAward(World.WIDTH+goldWidths*i,350-goldWidths*(7-i-1));
}
}
return golds;
}else {
Coin[] coins = new Coin[14];
for (int i = 0; i < 7; i++) {
if (i<3) {
coins[i]= new Coin(World.WIDTH+goldWidths*i,350-goldWidths*i);
}else{
coins[i]= new Coin(World.WIDTH+goldWidths*i,350-goldWidths*(7-i-1));
}
}
for (int i = 7; i <14 ; i++) {
if (i<10) {
coins[i]= new Coin(World.WIDTH+goldWidths*(i-7),350-goldWidths*(i-7+1));
}else{
coins[i]= new Coin(World.WIDTH+goldWidths*(i-7),350-goldWidths*(15-i-1));
}
}
return coins;
}
}
/*生成障碍物*/
public Ground[] nextOneGround(){
Ground[] grounds = new Ground[2];
grounds[0] = new Ground(World.WIDTH+COMPOSING_WIDTH/6);
grounds[1] = new Ground(World.WIDTH+COMPOSING_WIDTH*5/6);
return grounds;
}
/*生成敌人*/
public Enemies[] nextOneEnemies() {
Enemies[] enemies = new Enemies[3];
for (int i = 0; i < 3; i++) {
enemies[i]=new Enemies(World.WIDTH+COMPOSING_WIDTH/3*i+100,420);
}
return enemies;
}
public Enemies[] nextAnotherEnemies() {
Enemies[] enemies = new Enemies[3];
for (int i = 0; i < 3; i++) {
enemies[i]=new Enemies(World.WIDTH+COMPOSING_WIDTH/3*i+100,420+300);
}
return enemies;
}
int enterIndex=0;
int enterIndex1=enterIndex-COMPOSING_WIDTH/12;
int enterIndex2=enterIndex-COMPOSING_WIDTH/6;
int index=0;
/*敌人入场*/
public void enterAction() {
if(enterIndex++%600==0) {
SuperClass[] obj = nextOneBigAward();
award = Arrays.copyOf(award, award.length+obj.length);
System.arraycopy(obj, 0, award, award.length-obj.length , obj.length);
}
if (enterIndex1++%600==0) {
SuperClass[] obj = nextOneSmallAward();
award = Arrays.copyOf(award, award.length+obj.length);
System.arraycopy(obj, 0, award, award.length-obj.length , obj.length);
Enemies[] enemy = nextOneEnemies();
enemies = Arrays.copyOf(enemies, enemies.length+enemy.length);
System.arraycopy(enemy, 0, enemies, enemies.length-enemy.length , enemy.length);
if(enterIndex1>0){
Enemies[] enemy1 = nextAnotherEnemies();
enemies = Arrays.copyOf(enemies, enemies.length+enemy1.length);
System.arraycopy(enemy1, 0, enemies, enemies.length-enemy1.length , enemy1.length);
}
}
if (enterIndex2++%600==0) {
Ground[] obj = nextOneGround();
/*越过障碍物分数*/
for(int i=0;i<obj.length;i++) {
index+=obj[i].getAwardType();
}
grounds = Arrays.copyOf(grounds, grounds.length+obj.length);
System.arraycopy(obj, 0, grounds, grounds.length-obj.length , obj.length);
}
}
int score=0;
/*检测碰撞*/
public void hitAction(){
for (int i = 0; i < award.length; i++) {
SuperClass a = award[i];
if(a.hit(hero)&&a.isLife()) {
a.goDead();
if(a instanceof Award) {
Award e = (Award)a;
score+=e.getAwardType();/*生成金币和银币的分数*/
}
}
}
for (int i = 0; i < grounds.length; i++) {
Ground g = grounds[i];
if (g.hit(hero)) {
if (hero.x+hero.width<g.x+15||hero.y>hero.groundY-10) {
hero.hitStep();
}
if (hero.y+hero.height<g.y+20) {
hero.fly();
hero.myState = Hero.JUMP_TWO;
}
}
}
}
/*对象越界后删除*/
public void OutOfBoundAction(){
int index = 0;
SuperClass[] awardLives = new SuperClass[award.length];
for(int i=0;i<award.length;i++){
SuperClass s = award[i];
if(!s.outOfBounds() && s.isLife()){
awardLives[index++] = s;
}
}
award = Arrays.copyOf(awardLives,index);
index = 0;
Ground[] groundLives = new Ground[grounds.length];
for(int i=0;i<grounds.length;i++){
Ground g = grounds[i];
if(!g.outOfBounds() && g.isLife()){
groundLives[index++] = g;
}
}
grounds = Arrays.copyOf(groundLives,index);
index = 0;
Enemies[] enemiesLives = new Enemies[enemies.length];
for (int i = 0; i < enemies.length; i++) {
Enemies e = enemies[i];
if(!e.outOfBounds() && e.isLife()){
enemiesLives[index++] = e;
}
}
enemies = Arrays.copyOf(enemiesLives, index);
}
int distance;
/**获取距离*/
public void getDistance(){
distance = (int)enterIndex/10;
}
/*障碍物、金币、敌人移动*/
public void stepAction() {
for(int i=0;i<award.length;i++) {
award[i].step();
}
for (int i = 0; i < grounds.length; i++) {
grounds[i].step();
}
for (int i = 0; i < enemies.length; i++) {
enemies[i].step();
}
back.step();
hero.step();
}
/*画对象*/
public void paint(Graphics g) {
if(state==RUN||state==PAUSE||state==GAME_OVER) {
back.pain
评论0