愤怒的小鸟 Day 4(完结)

目录

1.处理关卡界面星星个数的显示

2.关卡之间的切换

3.地图切换,制作多个关卡

4.snap settings使用介绍

5.地图面板信息显示

6.异步加载场景

7.发布游戏


1.处理关卡界面星星个数的显示

在LevelSelect中添加下面的代码

    public GameObject[] stars;
    private void Start()
    {
        if(transform.parent.GetChild(0).name == gameObject.name)
        {
            isSelect = true;
        }

        if (isSelect)
        {
            image.overrideSprite = levelBG;
            transform.Find("Num").gameObject.SetActive(true);

            //获取现在关卡对应的名字,然后获得对应的星星个数
            int count = PlayerPrefs.GetInt("level" + gameObject.name);
            if(count > 0)
            {
                for(int i = 0; i < count; i++)
                {
                    stars[i].SetActive(true);
                }
            }
        }
    }

运行程序,结果如下

但是存在BUG,它只会显示上次的结果。

2.关卡之间的切换

在LevelSelect的Start方法中添加下面的代码

        else//判断当前关卡是否可以选择
        {
            int beforeNum = int.Parse(gameObject.name) - 1;
            if (PlayerPrefs.GetInt("level" + beforeNum.ToString()) > 0)
            {
                isSelect = true;
            }
        }

运行程序,结果如下:

3.地图切换,制作多个关卡

在GameManager中添加如下代码

    private int totalNum = 10;//关卡总数
    public void SaveData()
    {
        if (starsNum > PlayerPrefs.GetInt(PlayerPrefs.GetString("nowLevel")))
        {
            PlayerPrefs.SetInt(PlayerPrefs.GetString("nowLevel"), starsNum);
        }
        int sum = 0;
        for(int i = 1; i <= totalNum; i++)
        {
            sum += PlayerPrefs.GetInt("level" + i.ToString());
        }
        PlayerPrefs.SetInt("totalNum", sum);
        
    }

运行程序,结果如下:

4.snap settings使用介绍

5.地图面板信息显示

在MapSelect中添加下面的代码来统计星星个数

    public Text starsText;

    public int startnum = 1;
    public int endnum = 3;
    private void Start()
    {
        if (PlayerPrefs.GetInt("totalNum", 0) >= starsNum)
        {
            isSelect = true;
        }

        if (isSelect)
        {
            locks.SetActive(false);
            stars.SetActive(true);

            //TODO:text显示
            int count = 0;
            for(int i = startnum; i < endnum;i++)
            {
                count += PlayerPrefs.GetInt("level" + i.ToString(), 0);
            }
            starsText.text = count.ToString() + "/9";
        }
    }

运行程序,结果如下

6.异步加载场景

在相机添加新的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class LoadLevelAsync : MonoBehaviour {

    private void Start()
    {
        Invoke("Load", 2f);
    }

    void Load()
    {
        SceneManager.LoadSceneAsync(1);
    }

}

7.发布游戏

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个基于 Pygame愤怒小鸟游戏的 Python 代码: ```python import pygame import random pygame.init() # 游戏窗口大小 WIDTH = 288 HEIGHT = 512 SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Flappy Bird") # 颜色定义 BLACK = (0, 0, 0) WHITE = (255, 255, 255) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 加载图片 BACKGROUND_IMAGE = pygame.image.load("assets/background-day.png").convert() GROUND_IMAGE = pygame.image.load("assets/base.png").convert() BIRD_IMAGES = [pygame.image.load("assets/yellowbird-downflap.png").convert_alpha(), pygame.image.load("assets/yellowbird-midflap.png").convert_alpha(), pygame.image.load("assets/yellowbird-upflap.png").convert_alpha()] PIPE_IMAGE = pygame.image.load("assets/pipe-green.png").convert() # 设置帧率 CLOCK = pygame.time.Clock() FPS = 60 # 鸟类 class Bird(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = BIRD_IMAGES[0] self.rect = self.image.get_rect() self.rect.centerx = WIDTH // 2 self.rect.centery = HEIGHT // 2 self.speed = 0 self.gravity = 0.5 def update(self): self.speed += self.gravity self.rect.centery += self.speed if self.rect.bottom >= 400: self.rect.bottom = 400 def jump(self): self.speed = -10 # 管道类 class Pipe(pygame.sprite.Sprite): def __init__(self, x, y, position): super().__init__() self.image = PIPE_IMAGE self.rect = self.image.get_rect() self.rect.centerx = x if position == "top": self.image = pygame.transform.flip(self.image, False, True) self.rect.bottom = y - 100 else: self.rect.top = y + 100 self.rect.centerx = x self.speed = -2 def update(self): self.rect.centerx += self.speed if self.rect.right < 0: self.kill() # 地面类 class Ground(pygame.sprite.Sprite): def __init__(self, x): super().__init__() self.image = GROUND_IMAGE self.rect = self.image.get_rect() self.rect.x = x self.rect.y = 400 self.speed = -2 def update(self): self.rect.x += self.speed if self.rect.right < 0: self.rect.x = WIDTH # 创建精灵组 bird_group = pygame.sprite.Group() pipe_group = pygame.sprite.Group() ground_group = pygame.sprite.Group() # 创建鸟 bird = Bird() bird_group.add(bird) # 创建地面 for i in range(2): ground = Ground(i * WIDTH) ground_group.add(ground) # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bird.jump() # 添加管道 if len(pipe_group) < 3: pipe_x = WIDTH + 100 offset = random.randint(-100, 100) pipe_y = HEIGHT / 2 + offset top_pipe = Pipe(pipe_x, pipe_y, "top") bottom_pipe = Pipe(pipe_x, pipe_y, "bottom") pipe_group.add(top_pipe) pipe_group.add(bottom_pipe) # 更新精灵组 bird_group.update() pipe_group.update() ground_group.update() # 绘制背景 SCREEN.blit(BACKGROUND_IMAGE, (0, 0)) # 绘制精灵组 bird_group.draw(SCREEN) pipe_group.draw(SCREEN) ground_group.draw(SCREEN) # 显示分数 pygame.display.set_caption("Flappy Bird - Score: {}".format(pygame.time.get_ticks() // 1000)) # 刷新屏幕 pygame.display.flip() # 设置帧率 CLOCK.tick(FPS) ``` 这个代码实现了一个基本的愤怒小鸟游戏,其包括了鸟、管道、地面等基本元素的定义和更新,以及游戏主循环、事件处理、精灵组的创建和更新等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值