android 水果忍者源码,Fruit Ninja(水果忍者)游戏源代码下载、分析(下)---可运行Android,Ios,Window,Mac,Html5平台...

背景:

这一篇是结尾篇,主要分析×××检测,游戏结束和保存最高分;

ps:

1 CocosEditor已发布新版本,现在提供6个实战demo学习,包括flappy ,popstar ,fruitninja,moonwarroris,fruitattack,testjavascript;

2 代码是基于javascript语言,cocos2d-x游戏引擎,CocosEditor手游开发工具完成的;

3 运行demo需要配置好CocosEditor,暂不支持其他工具。demo是跨平台的,可移植运行android,ios,html5网页等。

源代码下载:

效果图:

621bdfaa79565f61caa2b1dc612ab704.png

代码分析:

1 进入主场景从本地数据库中获取最高分,并显示最高分文字this.bestScoreLabel;

//bestScore

this.bestScore = sys.localStorage.getItem("bestScore");

if(this.bestScore !=null&&this.bestScore != undefined) {

this.bestScore = Number(this.bestScore);

}

else{

this.bestScore = 0;

}

cc.log("bestScore=="+this.bestScore);

this.bestScoreLabel.setString(FRUIT_STRINGS.bestScore +this.bestScore);

this.overLayer.setVisible(false);

2  在触摸移动的过程中,会切到水果也会切刀×××,如果是×××,游戏直接结束 ;

#我们已经建立过水果数组,×××的编号num是5,所以很简单,只要判断水果的编号就可以轻易的确定×××;

#如果是×××,游戏状态over,播放音效boom;

#创建×××光芒light精灵;

#播放一个序列动画,放大选择 然后清除,最后回调到结束函数this.gameOver();

//if bomb

if(fruit.num == 5) {

this.gameStatus = OVER;

cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.boom, false);

varlight = cc.MySprite.create(this.rootNode, FRUIT_DATA[5].cutImage, loc, 1100);

light.runAction(cc.Sequence.create(

cc.Spawn.create(cc.ScaleTo.create(2, 10), cc.RotateBy.create(1, 360)),

cc.CleanUp.create(light),

cc.CallFunc.create(function()

{

this.gameOver();

}, this)

));

return;

}

3 游戏结束有两种情况,一种是时间到,另一种是切到×××;

#游戏结束后,显示游戏层overLayer;

#提示你得到的分数gameScoreTip;

#如果本次玩的最高分大于历史最佳分数,本地数据库存储当前分数

#延时6s回到开始界面;

MainLayer.prototype.gameOver =function()

{

cc.AudioEngine.getInstance().playEffect(FRUIT_SOUNDS.over, false);

this.overLayer.setZOrder(1000);

this.overLayer.setVisible(true);

vargameScoreTip = FRUIT_STRINGS.youGet +this.totalScore + FRUIT_STRINGS.score;

cc.log("this.totalScore="+this.totalScore);

cc.log("this.bestScore="+this.bestScore);

if(this.totalScore >this.bestScore) {

cc.log("this.bestScore="+this.bestScore);

sys.localStorage.setItem("bestScore",this.totalScore +"");

gameScoreTip = gameScoreTip + FRUIT_STRINGS.record;

}

this.overScoreLabel.setString(gameScoreTip);

this.overLayer.scheduleOnce(function()

{

cc.AudioEngine.getInstance().stopAllEffects();

cc.BuilderReader.runScene("","StartLayer");

}, 6);

};

水果忍者系列结束

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,这是一个简单的示例代码,实现了一个基本的水果忍者游戏。你可以在Pygame中运行它。 ```python import pygame import random # 初始化Pygame pygame.init() # 设置窗口大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Fruit Ninja") # 加载背景图像 background_img = pygame.image.load("background.jpg") background_img = pygame.transform.scale(background_img, (screen_width, screen_height)) # 加载水果图像 fruit_images = [] fruit_images.append(pygame.image.load("apple.png")) fruit_images.append(pygame.image.load("banana.png")) fruit_images.append(pygame.image.load("orange.png")) fruit_images.append(pygame.image.load("watermelon.png")) fruit_images.append(pygame.image.load("pineapple.png")) # 设置水果大小 fruit_width = 100 fruit_height = 100 # 创建水果类 class Fruit: def __init__(self): self.image = random.choice(fruit_images) self.x = random.randint(0, screen_width - fruit_width) self.y = screen_height self.speed = random.randint(1, 5) def update(self): self.y -= self.speed def draw(self): screen.blit(self.image, (self.x, self.y)) # 创建水果列表 fruits = [] # 游戏主循环 running = True while running: screen.fill((255, 255, 255)) screen.blit(background_img, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 添加新水果 if len(fruits) < 10: fruits.append(Fruit()) # 更新水果位置 for fruit in fruits: fruit.update() fruit.draw() # 移除超出屏幕的水果 for fruit in fruits[:]: if fruit.y < -fruit_height: fruits.remove(fruit) pygame.display.update() # 退出游戏 pygame.quit() ``` 这是一个简单的示例代码,你可以根据自己的需求来修改和扩展它。希望能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值