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
没有合适的资源?快使用搜索试试~ 我知道了~
天天酷跑Java源代码-原创
共96个文件
png:62个
class:13个
java:9个
需积分: 50 73 下载量 147 浏览量
2018-08-06
22:18:48
上传
评论 12
收藏 1.59MB ZIP 举报
温馨提示
纯java代码编写:敌人、背景动态,英雄奔跑三连跳,滑翔,障碍物,不同形状的金币生成方式。代码简单易懂,复用性高,
资源详情
资源评论
资源推荐
收起资源包目录
TTKP.zip (96个子文件)
TTKP
.project 383B
bin
tedu
cn
a.jpg 198KB
Hero.class 4KB
start.jpg 56KB
loading1_12.png 19KB
loading1_5.png 22KB
Coin.class 1KB
loading_1.png 15KB
loading1_7.png 19KB
loading1_4.png 21KB
loading_9.png 13KB
loading1_6.png 21KB
loading_8.png 14KB
loading_7.png 14KB
loading_3.png 13KB
SuperClass.class 2KB
World$2$1.class 864B
zhangaiwu.gif 4KB
loading1_2.png 21KB
loading_12.png 14KB
loading1_down.png 23KB
loading_11.png 14KB
GoldAward.class 1KB
loading1_3.png 19KB
Ground.class 1KB
z.gif 4KB
pause.png 15KB
World$1.class 2KB
World$2.class 821B
loading_6.png 14KB
loading1_10.png 21KB
loading1_1.png 25KB
loading_down.png 14KB
jin.png 47KB
World$2$2.class 606B
loading_2.png 15KB
gameover.png 20KB
loading1_8.png 21KB
loading_4.png 13KB
World.class 9KB
loading_5.png 13KB
loading1_9.png 20KB
Enemies.class 1KB
coin.png 27KB
loading_10.png 14KB
ground.png 18KB
Background.class 1KB
loading1_11.png 22KB
Award.class 128B
.settings
org.eclipse.core.resources.prefs 134B
org.eclipse.jdt.core.prefs 598B
src
tedu
cn
a.jpg 198KB
start.jpg 56KB
loading1_12.png 19KB
loading1_5.png 22KB
Hero.java 3KB
loading_1.png 15KB
loading1_7.png 19KB
loading1_4.png 21KB
loading_9.png 13KB
loading1_6.png 21KB
loading_8.png 14KB
Enemies.java 583B
loading_7.png 14KB
loading_3.png 13KB
zhangaiwu.gif 4KB
loading1_2.png 21KB
loading_12.png 14KB
loading1_down.png 23KB
loading_11.png 14KB
loading1_3.png 19KB
Coin.java 645B
SuperClass.java 1KB
z.gif 4KB
World.java 12KB
pause.png 15KB
Background.java 656B
loading_6.png 14KB
loading1_10.png 21KB
loading1_1.png 25KB
loading_down.png 14KB
jin.png 47KB
GoldAward.java 619B
loading_2.png 15KB
gameover.png 20KB
loading1_8.png 21KB
loading_4.png 13KB
loading_5.png 13KB
Ground.java 631B
loading1_9.png 20KB
coin.png 27KB
loading_10.png 14KB
ground.png 18KB
Award.java 68B
loading1_11.png 22KB
.classpath 301B
共 96 条
- 1
qq_33352340
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 202304910142原道明(1).pbix
- 文本.txt
- 基于Lua的聊天过滤修改版设计源码
- A1_SSE_123090177.py
- Uibot6.0 (RPA财务机器人师资培训第5天 ) 报销汇总机器人案例实战
- 基于Vue的西安美食攻略应用程序设计源码
- tensorflow-2.6.2-cp38-cp38-win-amd64.whl
- 2023-04-06-项目笔记 - 第八十六阶段 - 4.4.2.84全局变量的作用域-84 -2024.03.28
- 基于C语言解决九宫重排问题(源码+剖析)
- 考研分数计算神器(通过考研分数计算规则制作出来的计算工具,结果精准,操作简单,并且还可以与第二个人进行比较)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0