Python实现水果忍者(开源)

news2024/9/21 12:29:16

一、整体介绍:

1.1 前言:

游戏代码基于Python制作经典游戏案例-水果忍者做出一些改动,优化并增加了一些功能。作为自己Python阶段学习的结束作品,文章最后有源码链接。

1.2 Python主要知识:

(1)面向对象编程 

类的定义与实例化、封装、继承(使用 pygame.sprite.Sprite 作为基类)

(2)模块与库

导入标准库(time, math, random)、导入第三方库( pygame)

(3)事件处理

事件监听(pygame.event.get() 处理用户输入和游戏事件)、响应事件(根据不同事件,如关闭窗口、定时器事件,执行相应操作)

(4)图形绘制

图像加载( pygame.image.load() 加载图像)、图像绘制(blit() 方法将图像绘制到窗口上)、图像旋转( pygame.transform.rotate() 旋转图像)

(5)随机数生成

(6)计时与帧率控制

使用 pygame.time.Clock() 控制游戏的帧率

(7)文件操作

使用 open() 读取和写入文本文件,保存和读取最佳分数、逐行读取文件内容并解析数据

(8)碰撞检测

(9)Sprite 和 Group

使用 pygame.sprite.Sprite 创建精灵(如水果、刀光、背景)、使用 pygame.sprite.Group 管理和更新多个精灵,方便批量处理

(10)数学运算

使用三角函数,math.sin() 和 math.cos(),计算水果的抛出轨迹

(11)音频处理

使用 pygame.mixer 播放背景音乐和音效,增强游戏体验

(12)逻辑控制

(13)字体与文本渲染

使用 pygame.font.Font() 创建字体对象,并使用 render() 方法渲染文本以显示分数和信息

(14)参数传递与返回值

1.3 游戏素材

二、完善功能:

(1)优化游戏参数

例如:首页旋转圆环速度,水果上抛高度等,使游戏体验更加平滑。

(2)禅宗模式倒计时

禅宗模式在游戏右上方增加了时间倒计时的图形化界面。

(3)增加额外音效

由于pygame同时播放音乐,会有覆盖现象。即后播放音乐会覆盖之前播放音乐,导致原版游戏结束,bgm.play_over被bgm.play_menu覆盖,播放不出来。使用独立线程对代码要求较高,取巧,利用睡眠(time.sleep)。玩家切到炸弹结束游戏,暂停0.3s画面,而不是原版的突然重新开始。

(4)游戏历史最高分数

利用IO流逐行读取txt文件,和原版分数一样的window.blit函数绘制在游戏界面,不过分数的更新要在结束程序后会执行。

Bug:

游戏的局部和实例变量较多,有些资源可能会被程序占用而无法释放。目前主要bug,在游戏碰撞检测的时候,偶尔会出现分数停止更新的情况。本人才疏学浅,至今没有有效解决,希望大佬们多多包涵,最好能够帮助解决,完善游戏。

三、代码设计:

import time
import math
import random
import pygame
from pygame.constants import *

pygame.init()

""" 背景图片 """
class Background(pygame.sprite.Sprite):

    def __init__(self, window, x, y, image_path):
        pygame.sprite.Sprite.__init__(self)
        self.window = window
        self.image = pygame.image.load(image_path)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        self.window.blit(self.image, self.rect)


""" 被抛出的水果类 """
class ThrowFruit(pygame.sprite.Sprite):

    def __init__(self, window, image_path, speed, turn_angel, flag):
        pygame.sprite.Sprite.__init__(self)

        # 游戏窗口
        self.window = window

        # 导入水果图像并获取其矩形区域
        self.image = pygame.image.load(image_path)
        self.rect = self.image.get_rect()

        # 水果抛出时x坐标取随机数
        self.rect.x = random.randint(0, Manager.WIDTH - 10)

        # 水果初始y坐标
        self.rect.y = Manager.HEIGHT

        # 抛出时速度
        self.speed = speed

        # 旋转速度
        self.turn_angel = turn_angel

        # 水果抛出时与窗口下水平线的夹角弧度,因为要用到随机函数, 所以取整数, 使用时除以100
        self.throw_angel = 157

        # 水果抛出后所经历的时间, 初始化为0
        self.fruit_t = 0

        # 旋转的总角度
        self.v_angel = 0

        # 水果抛出时的初速度
        self.v0 = 6

        # 水果标记
        self.flag = flag

    def update(self):
        """ 水果运动状态更新 """

        # 在弧度制中,一个完整的圆周对应的角度是360度,对应的弧度是2π(即360度 = 2π弧度)。
        # 因此,可以通过以下公式将角度转换为弧度: 弧度 = 角度 × π / 180
        # 当角度为90度时,根据上述公式,可以计算出对应的弧度为: 90度 × π / 180 = 0.5π = 1.57(约)

        # 如果水果的初始X坐标位于窗口左边区域, 取抛出时弧度在70度至90度之间
        if self.rect.x <= Manager.WIDTH / 2:
            self.throw_angel = random.randint(140, 157)

        # 如果水果的初始X坐标位于窗口右侧区域, 取抛出时弧度在90度至110度之间
        elif self.rect.x >= Manager.WIDTH / 2:
            self.throw_angel = random.randint(157, 175)

        # 水果旋转后的新图像
        new_fruit = pygame.transform.rotate(self.image, self.v_angel)

        self.window.blit(new_fruit, (self.rect.x + self.rect.width / 2 - new_fruit.get_width() / 2,
                                     self.rect.y + self.rect.height / 2 - new_fruit.get_height() / 2))

        # 如果水果落出屏幕,没有被切,经典模式 X 加一,并销毁水果对象
        if self.rect.y >= Manager.HEIGHT + self.rect.height:
            if self.flag != 5:
                Manager.classic_miss += 1
            self.kill()

        # 水果抛出后的运动时水平匀速运动以及竖直向上的变速运动到达最高点时下落, 所以可以判断水果做的是斜上抛运动
        # 可以利用重力加速度来求出每隔一段时间水果运动后的y坐标
        # 公式: v0 * t * sin(α) - g * t^2 / 2
        self.rect.y -= self.v0 * self.fruit_t * math.sin(self.throw_angel / 100) - (Manager.G *
                                                                                    self.fruit_t ** 2 / 10) / 2

        # 计算水果在水平方向的匀速运动位移之后的X坐标
        # 公式: v0 * t * cos(α)
        self.rect.x += self.v0 * self.fruit_t * math.cos(self.throw_angel / 100)

        # 累加经过的时间
        self.fruit_t += 0.1

        # 累加旋转总角度
        self.v_angel += self.turn_angel


""" 水果切片类 """
class HalfFruit(pygame.sprite.Sprite):

    def __init__(self, window, image_path, x, y, turn_angel, v_angel, v0):
        pygame.sprite.Sprite.__init__(self)
        self.window = window
        self.image = pygame.image.load(image_path)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.turn_angel = turn_angel
        self.fruit_t = 0
        self.v_angel = v_angel
        self.v0 = v0

    def update(self):
        """ 水果运动状态更新 """

        # 水果旋转后的新图像
        new_fruit = pygame.transform.rotate(self.image, self.v_angel)

        # 将旋转后的新图像贴入游戏窗口, 注意, 旋转后的图像尺寸以及像素都不一样了(尺寸变大了), 所以坐标需要进行适当处理
        #                               在原先图片矩形的中心位置绘制
        self.window.blit(new_fruit, (self.rect.x + self.rect.width / 2 - new_fruit.get_width() / 2,
                                     self.rect.y + self.rect.height / 2 - new_fruit.get_height() / 2))

        if self.rect.y >= Manager.HEIGHT:
            self.kill()
        self.rect.y += Manager.G * self.fruit_t ** 2 / 2

        self.rect.x += self.v0 * self.fruit_t

        self.fruit_t += 0.01

        self.v_angel += self.turn_angel


""" 水果刀光类 """
class Knife(object):

    def __init__(self, window):
        self.window = window
        self.apple_flash = pygame.image.load("./images/apple_flash.png")
        self.banana_flash = pygame.image.load("./images/banana_flash.png")
        self.peach_flash = pygame.image.load("./images/peach_flash.png")
        self.watermelon_flash = pygame.image.load("./images/watermelon_flash.png")
        self.strawberry_flash = pygame.image.load("./images/strawberry_flash.png")

    def show_apple_flash(self, x, y):
        self.window.blit(self.apple_flash, (x, y))

    def show_banana_flash(self, x, y):
        self.window.blit(self.banana_flash, (x, y))

    def show_peach_flash(self, x, y):
        self.window.blit(self.peach_flash, (x, y))

    def show_watermelon_flash(self, x, y):
        self.window.blit(self.watermelon_flash, (x, y))

    def show_strawberry_flash(self, x, y):
        self.window.blit(self.strawberry_flash, (x, y))


""" 模式选项类 """
class OptionMode(pygame.sprite.Sprite):

    def __init__(self, window, x, y, image_path, turn_angel, flag):
        pygame.sprite.Sprite.__init__(self)
        self.window = window
        self.image = pygame.image.load(image_path)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.turn_angel = turn_angel
        self.v_angel = 0
        self.flag = flag

    def update(self):
        new_image = pygame.transform.rotate(self.image, -self.v_angel)
        self.window.blit(new_image, (self.rect.x + self.rect.width / 2 - new_image.get_width() / 2,
                                     self.rect.y + self.rect.height / 2 - new_image.get_height() / 2))
        self.v_angel += self.turn_angel


""" 游戏音乐类 """
class Bgm(object):

    def __init__(self):
        pygame.mixer.init()

    def play_menu(self):
        pygame.mixer.music.load("./sound/menu.mp3")
        pygame.mixer.music.play(-1, 0)

    def play_classic(self):
        pygame.mixer.music.load("./sound/start.mp3")
        pygame.mixer.music.play(1, 0)

    def play_throw(self):
        pygame.mixer.music.load("./sound/throw.mp3")
        pygame.mixer.music.play(1, 0)

    def play_splatter(self):
        pygame.mixer.music.load("./sound/splatter.mp3")
        pygame.mixer.music.play(1, 0)

    def play_over(self):
        pygame.mixer.music.load("./sound/over.mp3")
        pygame.mixer.music.play(1, 0)

    def play_boom(self):
        pygame.mixer.music.load("./sound/boom.mp3")
        pygame.mixer.music.play(1, 0)


""" 游戏逻辑类 """
class Manager(object):
    # 窗口尺寸
    WIDTH = 640
    HEIGHT = 480

    # 游戏中的定时器常量
    THROWFRUITTIME = pygame.USEREVENT
    pygame.time.set_timer(THROWFRUITTIME, 3000)

    # 根据窗口大小,选取随机整数重力加速度, 水果下落更有层次感,使用时除以10
    G = random.randint(19, 21)

    # 经典模式miss掉的水果数
    classic_miss = 0

    # 打开文本文件
    with open('best.txt', 'r') as file:
        # 逐行读取文件内容
        for line in file:
            if 'zen_mode' in line:
                zen_best = int(line.split(':')[-1].strip())
            if 'classic_mode' in line:
                classic_best = int(line.split(':')[-1].strip())

    def __init__(self):
        # 生成游戏窗口
        self.window = pygame.display.set_mode((Manager.WIDTH, Manager.HEIGHT))
        self.window_icon = pygame.image.load("./images/score.png")
        pygame.display.set_icon(self.window_icon)
        pygame.display.set_caption("FruitNinja")

        # 创建游戏中用到的的精灵组
        self.background_list = pygame.sprite.Group()
        self.circle_option = pygame.sprite.Group()
        self.option_fruit_list = pygame.sprite.Group()
        self.fruit_half_list = pygame.sprite.Group()
        self.throw_fruit_list = pygame.sprite.Group()

        # 导入背景图像并添加入背景精灵组
        self.background = Background(self.window, 0, 0, "./images/background.jpg")
        self.home_mask = Background(self.window, 0, 0, "./images/home-mask.png")
        self.logo = Background(self.window, 20, 10, "./images/logo.png")
        self.ninja = Background(self.window, Manager.WIDTH - 320, 45, "./images/ninja.png")
        self.home_desc = Background(self.window, 20, 135, "./images/home-desc.png")
        self.zen_new = Background(self.window, 175, 215, "./images/new.png")

        self.background_list.add(self.background)
        self.background_list.add(self.home_mask)
        self.background_list.add(self.logo)
        self.background_list.add(self.ninja)
        self.background_list.add(self.home_desc)
        self.background_list.add(self.zen_new)

        # 创建旋转的圈并添加进精灵组
        self.dojo = OptionMode(self.window, Manager.WIDTH - 600, Manager.HEIGHT - 250,
                               "./images/dojo.png", 1, None)
        self.new_game = OptionMode(self.window, Manager.WIDTH - 405, Manager.HEIGHT - 250,
                                   "./images/new-game.png", 1, None)
        self.game_quit = OptionMode(self.window, Manager.WIDTH - 160, Manager.HEIGHT - 150,
                                    "./images/quit.png", -1, None)
        self.circle_option.add(self.dojo)
        self.circle_option.add(self.new_game)
        self.circle_option.add(self.game_quit)

        # 创建主菜单界面旋转的水果并添加进精灵组
        self.home_peach = OptionMode(self.window, Manager.WIDTH - 600 + self.dojo.rect.width / 2 - 31,
                                     Manager.HEIGHT - 250 + self.dojo.rect.height / 2 - 59 / 2,
                                     "./images/peach.png", -1, "option_peach")
        self.home_watermelon = OptionMode(self.window, Manager.WIDTH - 405 + self.new_game.rect.width / 2 - 49,
                                          Manager.HEIGHT - 250 + self.new_game.rect.height / 2 - 85 / 2,
                                          "./images/watermelon.png", -1, "option_watermelon")
        self.home_boom = OptionMode(self.window, Manager.WIDTH - 160 + self.game_quit.rect.width / 2 - 66 / 2,
                                    Manager.HEIGHT - 150 + self.game_quit.rect.height / 2 - 68 / 2,
                                    "./images/boom.png", 1, "option_boom")
        self.option_fruit_list.add(self.home_peach)
        self.option_fruit_list.add(self.home_watermelon)
        self.option_fruit_list.add(self.home_boom)

        # 设置定时器
        self.clock = pygame.time.Clock()

        # 模式标记
        self.mode_flag = 0

        # 音效
        self.bgm = Bgm()

        # 刀光
        self.knife = Knife(self.window)

        # 游戏分数
        self.classic_score = 0
        self.zen_score = 0

    def create_fruit(self):
        """ 创建水果 """
        if self.mode_flag == 1:
            boom_prob = random.randint(4, 6)
            if boom_prob == 5:
                self.bgm.play_throw()
                boom = ThrowFruit(self.window, "./images/boom.png", None, 5, 5)
                self.throw_fruit_list.add(boom)

        fruit_image_path = ["./images/apple.png", "./images/banana.png", "./images/peach.png",
                            "./images/watermelon.png", "./images/strawberry.png"]
        fruit_number = random.randint(1, 4)
        for n in range(fruit_number):
            rand_fruit_index = random.randint(0, len(fruit_image_path) - 1)
            self.bgm.play_throw()
            fruit = ThrowFruit(self.window, fruit_image_path[rand_fruit_index], None, 5,
                               rand_fruit_index)
            self.throw_fruit_list.add(fruit)

    def create_fruit_half(self, fruit_flag, fruit_x, fruit_y, turn_angel, v_angel):
        if fruit_flag == "option_peach":
            """ 禅宗模式的桃子被切开 """
            fruit_left = HalfFruit(self.window, "./images/peach-1.png", fruit_x - 50,
                                   fruit_y, turn_angel, v_angel, -5)
            fruit_right = HalfFruit(self.window, "./images/peach-2.png", fruit_x + 50,
                                    fruit_y, -turn_angel, v_angel, 5)
            self.fruit_half_list.add(fruit_left)
            self.fruit_half_list.add(fruit_right)

        if fruit_flag == "option_watermelon":
            """ 经典模式西瓜被切开 """
            fruit_left = HalfFruit(self.window, "./images/watermelon-1.png", fruit_x - 50,
                                   fruit_y, turn_angel, v_angel, -5)
            fruit_right = HalfFruit(self.window, "./images/watermelon-2.png", fruit_x + 50,
                                    fruit_y, -turn_angel, v_angel, 5)
            self.fruit_half_list.add(fruit_left)
            self.fruit_half_list.add(fruit_right)

        if fruit_flag == 0:
            """ 苹果被切开 """
            fruit_left = HalfFruit(self.window, "./images/apple-1.png", fruit_x - 50,
                                   fruit_y, turn_angel, v_angel, -5)
            fruit_right = HalfFruit(self.window, "./images/apple-2.png", fruit_x + 50,
                                    fruit_y, -turn_angel, v_angel, 5)
            self.fruit_half_list.add(fruit_left)
            self.fruit_half_list.add(fruit_right)

        if fruit_flag == 1:
            """ 香蕉被切开 """
            fruit_left = HalfFruit(self.window, "./images/banana-1.png", fruit_x - 50,
                                   fruit_y, turn_angel, v_angel, -5)
            fruit_right = HalfFruit(self.window, "./images/banana-2.png", fruit_x + 50,
                                    fruit_y, -turn_angel, v_angel, 5)
            self.fruit_half_list.add(fruit_left)
            self.fruit_half_list.add(fruit_right)

        if fruit_flag == 2:
            """ 梨被切开 """
            fruit_left = HalfFruit(self.window, "./images/peach-1.png", fruit_x - 50,
                                   fruit_y, turn_angel, v_angel, -5)
            fruit_right = HalfFruit(self.window, "./images/peach-2.png", fruit_x + 50,
                                    fruit_y, -turn_angel, v_angel, 5)
            self.fruit_half_list.add(fruit_left)
            self.fruit_half_list.add(fruit_right)

        if fruit_flag == 3:
            """ 西瓜被切开 """
            fruit_left = HalfFruit(self.window, "./images/watermelon-1.png", fruit_x - 50,
                                   fruit_y, turn_angel, v_angel, -5)
            fruit_right = HalfFruit(self.window, "./images/watermelon-2.png", fruit_x + 50,
                                    fruit_y, -turn_angel, v_angel, 5)
            self.fruit_half_list.add(fruit_left)
            self.fruit_half_list.add(fruit_right)

        if fruit_flag == 4:
            """ 草莓被切开 """
            fruit_left = HalfFruit(self.window, "./images/strawberry-1.png", fruit_x - 50,
                                   fruit_y, turn_angel, v_angel, -5)
            fruit_right = HalfFruit(self.window, "./images/strawberry-2.png", fruit_x + 50,
                                    fruit_y, -turn_angel, v_angel, 5)
            self.fruit_half_list.add(fruit_left)
            self.fruit_half_list.add(fruit_right)

    def impact_check(self):
        """ 碰撞检测 """
        for item in self.option_fruit_list:
            """ 主页的模式选择 """
            mouse_pos = pygame.mouse.get_pos()
            if mouse_pos[0] > item.rect.left and mouse_pos[0] < item.rect.right \
                    and mouse_pos[1] > item.rect.top and mouse_pos[1] < item.rect.bottom:
                self.bgm.play_splatter()
                self.create_fruit_half(item.flag, item.rect.x, item.rect.y, item.turn_angel, item.v_angel)
                self.option_fruit_list.remove_internal(item)
                if item.flag == "option_peach":
                    self.mode_flag = 1
                    return 1
                elif item.flag == "option_watermelon":
                    self.mode_flag = 2
                    return 2
                elif item.flag == "option_boom":
                    return 0

        for item in self.throw_fruit_list:
            """ 游戏开始后判断水果是否被切到 """
            mouse_pos = pygame.mouse.get_pos()
            if mouse_pos[0] > item.rect.left and mouse_pos[0] < item.rect.right \
                    and mouse_pos[1] > item.rect.top and mouse_pos[1] < item.rect.bottom:
                if item.flag == 0:
                    self.knife.show_apple_flash(item.rect.x, item.rect.y)
                if item.flag == 1:
                    self.knife.show_banana_flash(item.rect.x, item.rect.y)
                if item.flag == 2:
                    self.knife.show_peach_flash(item.rect.x, item.rect.y)
                if item.flag == 3:
                    self.knife.show_watermelon_flash(item.rect.x, item.rect.y)
                if item.flag == 4:
                    self.knife.show_strawberry_flash(item.rect.x, item.rect.y)

                if self.mode_flag == 1:
                    self.zen_score += 2
                if self.mode_flag == 2:
                    self.classic_score += 2
                if item.flag != 5:
                    self.bgm.play_splatter()

                self.create_fruit_half(item.flag, item.rect.x, item.rect.y, item.turn_angel, item.v_angel)
                self.throw_fruit_list.remove_internal(item)
                if item.flag == 5:
                    self.bgm.play_boom()
                    time.sleep(0.4)
                    return 3

    def check_key(self):
        """ 监听事件 """
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                exit()
            elif event.type == Manager.THROWFRUITTIME and self.mode_flag == 1:
                self.create_fruit()
            elif event.type == Manager.THROWFRUITTIME and self.mode_flag == 2:
                self.create_fruit()

    def zen_mode(self):
        """ 禅宗模式 """
        self.bgm.play_classic()
        score_image = Background(self.window, 10, 5, "./images/score.png")
        text = pygame.font.Font("./images/simhei.ttf", 30)  # 设置分数显示的字体
        best = pygame.font.Font("./images/simhei.ttf", 20)  # 设置历史最好分数显示的字体

        # 禅宗模式游戏时间
        record_time = 3600

        while True:
            # 设置游戏帧率
            self.clock.tick(60)
            self.check_key()
            self.background_list.sprites()[0].update()
            score_image.update()

            text_score = text.render("%d" % self.zen_score, 1, (255, 213, 156))
            self.window.blit(text_score, (50, 8))
            best_score = best.render("BEST %d" % self.zen_best, 1, (255, 179, 78))
            self.window.blit(best_score, (10, 40))

            game_time = text.render("Time:%d" % (record_time / 60), 1, (178, 34, 34))
            self.window.blit(game_time, (510, 12))

            temp_flag = self.impact_check()

            self.throw_fruit_list.update()
            self.fruit_half_list.update()
            pygame.display.update()

            record_time -= 1

            # 禅宗模式更新最高历史记录
            if record_time == 0 or temp_flag == 3:
                if self.zen_score > self.zen_best:
                    file_path = 'best.txt'
                    with open(file_path, 'r') as file:
                        content = file.read()
                        content = content.replace(str(self.zen_best), str(self.zen_score))
                        with open(file_path, 'w') as file:
                            file.write(content)
                return

    def classic_mode(self):
        """ 经典模式 """
        pygame.font.init()
        self.bgm.play_classic()
        score_image = Background(self.window, 10, 5, "./images/score.png")
        text = pygame.font.Font("./images/simhei.ttf", 30)  # 设置分数显示的字体
        best = pygame.font.Font("./images/simhei.ttf", 20)  # 设置历史最好分数显示的字体

        x_nums = pygame.sprite.Group()
        miss_times = pygame.sprite.Group()
        xxx = Background(self.window, Manager.WIDTH - 30, 5, "./images/xxx.png")
        xx = Background(self.window, Manager.WIDTH - 60, 5, "./images/xx.png")
        x = Background(self.window, Manager.WIDTH - 90, 5, "./images/x.png")
        x_nums.add(xxx)
        x_nums.add(xx)
        x_nums.add(x)

        while True:
            # 设置游戏帧率
            self.clock.tick(60)
            pygame.display.update()
            self.check_key()
            self.background_list.sprites()[0].update()
            score_image.update()

            text_score = text.render("%d" % self.classic_score, 1, (255, 213, 156))
            self.window.blit(text_score, (50, 8))
            best_score = best.render("BEST %d" % self.classic_best, 1, (255, 179, 78))
            self.window.blit(best_score, (10, 40))

            x_nums.update()
            miss_times.update()
            temp_flag = self.impact_check()
            if temp_flag == 3:
                # 经典模式炸弹结束游戏更新历史最高记录
                if self.classic_score > self.classic_best:
                    file_path = 'best.txt'
                    with open(file_path, 'r') as file:
                        content = file.read()
                        content = content.replace(str(self.classic_best), str(self.classic_score))
                        with open(file_path, 'w') as file:
                            file.write(content)
                self.bgm.play_boom()
                time.sleep(0.4)
                return

            self.throw_fruit_list.update()
            self.fruit_half_list.update()
            if Manager.classic_miss == 1:
                xf = Background(self.window, Manager.WIDTH - 90, 5, "./images/xf.png")
                miss_times.add(xf)
            elif Manager.classic_miss == 2:
                xf = Background(self.window, Manager.WIDTH - 90, 5, "./images/xf.png")
                miss_times.add(xf)
                xxf = Background(self.window, Manager.WIDTH - 60, 5, "./images/xxf.png")
                miss_times.add(xxf)
            elif Manager.classic_miss >= 3:
                # 经典模式正常结束更新历史最高记录
                if self.classic_score > self.classic_best:
                    file_path = 'best.txt'
                    with open(file_path, 'r') as file:
                        content = file.read()
                        content = content.replace(str(self.classic_best), str(self.classic_score))
                        with open(file_path, 'w') as file:
                            file.write(content)
                self.bgm.play_over()
                time.sleep(0.4)
                Manager.classic_miss = 0
                return

    def main(self):
        """ 主页 """
        self.bgm.play_menu()
        while True:
            # 设置游戏帧率
            self.clock.tick(60)
            self.background_list.update()
            self.circle_option.update()
            self.option_fruit_list.update()
            self.fruit_half_list.update()
            temp_flag = self.impact_check()
            pygame.display.update()

            if temp_flag == 1:
                self.zen_mode()
                self.__init__()
                self.bgm.play_over()
                self.bgm.play_menu()

            if temp_flag == 2:
                self.classic_mode()
                self.__init__()
                self.bgm.play_over()
                self.bgm.play_menu()

            elif temp_flag == 0:
                self.bgm.play_over()
                time.sleep(0.3)
                pygame.quit()
                exit()
            elif temp_flag == 3:
                self.__init__()
                self.bgm.play_menu()
            self.check_key()


if __name__ == '__main__':
    manager = Manager()
    manager.main()

源码:

链接:https://pan.baidu.com/s/11YM7GzqzFz1QkcGbJHnDCQ 
提取码:daz5

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2047586.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

FPGA开发——UART回环实现之发送模块的设计和数据回环整体实现

一、简介 在上一篇文章当中我们实现了UART接收模块的相关设计和功能实现&#xff0c;在今天的文章中我们继续实现剩下的发送模块的相关设计和完成完整的串口数据回环的实验。 在文章的最后我会给出完整的工程&#xff0c;给小伙伴们参考。 二、接收模块的基本设计 在接收模块…

如何下载老版本 的mysql

方案1&#xff1a;打开地址&#xff0c;即可 MySQL :: Download MySQL Community Server (Archived Versions) 输入地址 https://downloads.mysql.com/archives/community/ 方案2&#xff1a; MySQL :: Download MySQL Installer https://dev.mysql.com/downloads/windows…

推荐使用阿贝云免费云服务器、免费虚拟主机

官网地址&#xff1a;https://www.abeiyun.com 阿贝云免费云服务器&#xff0c;性价比之选&#xff01; 不得不说&#xff0c;阿贝云的免费云服务器真的太棒了&#xff01;不仅免费&#xff0c;还能提供如此优质的服务。服务器的配置虽然不算高端&#xff0c;但对于一般的应用…

【MySQL】数据库约束

系列文章目录 第一章 数据库基础 第二章 数据库基本操作 文章目录 系列文章目录前言约束关键字一览NOT NULLUNIQUEDEFAULTPRIMARY KEY自增主键 FOREIGN KEY总结 前言 在学习了数据库的增删改查操作之后&#xff0c;接下来就需要进阶的学习关键字来完善SQL语句的条件。学习数据…

宝塔部署Django项目(华为云)

1、登录华为云: 2、点击远程登录: 3、打开宝塔网址(华为云选的是centos) 4、在华为终端复制指令点击运行: 会显示安装完成,出现一个页面记录一下,方便以后登录: 5、复制外网面板地址到浏览器地址栏,输入账号,密码登录,在这里进行配置: 一、Django项目的设置以及压…

Gafgyt僵尸网络针对云原生环境,SSH弱密码成GPU挖矿新目标

近日&#xff0c;网络安全研究人员发现了 Gafgyt 僵尸网络的一个新变种&#xff0c;它以 SSH 密码较弱的机器为目标&#xff0c;最终利用其 GPU 计算能力在被攻击的实例上挖掘加密货币。 Aqua Security 研究员 Assaf Morag 在周三的一份分析报告中说&#xff1a;"这表明&…

虎牙的商业化畅想

2024年8月13日&#xff0c;虎牙公司公布了2024年第二季度财报。财报显示&#xff0c;2024年第二季度&#xff0c;虎牙公司总收入达15.4亿元。其中&#xff0c;来自游戏相关服务、广告和其他业务的收入同比增长152.7%至3.1亿元。在非美国通用会计准则下&#xff0c;该季度归属于…

KDP数据平台:以实战案例验证技术领先力

本文由智领云 LeetTools 工具自动生成 申请试用&#xff1a; https://www.leettools.com/feedback/ 在当今快速发展的技术环境中&#xff0c;数据平台的选择对企业的数字化转型和业务发展至关重要。智领云开源KDP&#xff08;Kubernetes Data Platform&#xff09;在数据处理和…

状态dp或滑动窗口

前言&#xff1a;这个题目可以用状态dp来做&#xff0c;其实还有一个思路&#xff0c;类似滑动窗口&#xff0c;如果有遇到第二个0&#xff0c;左指针加一 class Solution { public:int longestSubarray(vector<int>& nums) {int n nums.size();vector<vector<…

Mac 自定义键盘快捷键打开系统应用

在 macOS 中&#xff0c;可以通过自定义键盘快捷键来打开系统应用程序。这可以更快捷地访问常用的应用程序&#xff0c;提高工作效率。下面以创建活动监视器快捷键为例介绍“Mac 自定义键盘快捷键打开系统应用”的一般步骤&#xff1a; 创建活动监视器快捷键 键入command空格打…

DVWA | Brute Force通关指南

目录 Low Medium High Impossible Brute force主要是通过爆破达到渗透目的&#xff1a; Low 查看源代码&#xff1a; <?phpif( isset( $_GET[ Login ] ) ) {// Get username$user $_GET[ username ];// Get password$pass $_GET[ password ];$pass md5( $pass );//…

软件工程概述(上)

1、软件的概念、特点和分类 要了解软件工程&#xff0c;首先让我们重新认识一下软件。如今可以说是一个软件定义一切的时代&#xff0c;虽然人工智能发展的如火如荼&#xff0c;但究其本质&#xff0c;核心还是软件。那么&#xff0c;如何给软件下一个定义呢&#xff1f;软件又…

再造行业旗舰爆款,书客L2 Pro重塑医护级”护眼更养眼”,被誉为最强旗舰级标杆!

近日&#xff0c;作为照明领域黑马的SUKER书客正式发布护眼台灯L2 Pro&#xff0c;在各项标准都远超国AA的基础上&#xff0c;推出了RRT2.0红光养眼技术、第三代SDIT自适应调光技术以及全新一代紫光激发全光谱技术等一系列首创护眼黑科技&#xff0c;从强悍的护眼效果到惊艳的光…

《python语言程序设计》2018版第7章第04题Fan类,设计一个名为Fan的类表示一个风扇

8点下课到家也9点多。眼睛抬不起来 明天到周二要为好几十个兄弟姐妹们&#xff0c;完成日志和反馈表&#xff0c;还要保证低错误。明天还要完成单位的一些工作。 哦对了&#xff0c;还有这些兄弟姐妹们的视频。 好先看一下Fan类的代码&#xff0c;它继续存在exCode07 class Fa…

【uni-app】使用天气API做一个天气APP(全过程)- 实况、逐小时、40日

头一次使用uni-app写代码, 现学现卖, 写的不好的地方见谅, 申请个appid就可以运行 切换城市界面比较简单, 城市名称需要符合天气api参数规则, 录入的城市不要带市区县; 格式如: 青岛、铁西、海淀、沛县 APP效果 功能说明 实况天气逐小时预报未来7日天气未来40日天气空气质量详…

【Qt】QWidget的toolTip属性

QWidget的toolTip属性 如果一个GUI程序&#xff0c;界面比较复杂&#xff0c;按钮比较多&#xff0c;使用toolTip可以设置当鼠标悬停在控件上的时候&#xff0c;可以弹出一个提示。 API说明 setToolTip 设置 toolTip. ⿏标悬停在该 widget 上时会有提⽰说明. setToolTipDur…

10 ARM 体系

10 ARM 体系 ARM体系1、基本概念1.1 常见的处理器1.2 ARM7三级指令流水线1.3 初识PC寄存器 2、 ARM核的七种工作模式3、ARM核七种异常 ARM体系 1、基本概念 1.1 常见的处理器 PowerPC处理器&#xff1a;飞思卡尔MPC系列 DSP:TI达芬奇系列 FPGA&#xff1a;Xilinx赛灵思的ZYN…

python小游戏之摇骰子猜大小

最近学习Python的随机数&#xff0c;逻辑判断&#xff0c;循环的用法&#xff0c;就想找一些练习题&#xff0c;比如小游戏猜大小&#xff0c;程序思路如下&#xff1a; 附上源代码如下&#xff1a; 摇骰子的函数&#xff0c;这个函数其实并不需要传任何参数&#xff0c;调用后…

理性看待、正确理解 AI 中的 Scaling “laws”

编者按&#xff1a;LLMs 规模和性能的不断提升&#xff0c;让人们不禁产生疑问&#xff1a;这种趋势是否能一直持续下去&#xff1f;我们是否能通过不断扩大模型规模最终实现通用人工智能&#xff08;AGI&#xff09;&#xff1f;回答这些问题对于理解 AI 的未来发展轨迹至关重…

独立开发者,技术只是其一

刚开始做独立开发者时&#xff0c;总是想着追究技术的深度。我的软件用了特别牛的技术&#xff0c;我的软件这个功能技术花了很多个日日夜夜才实现&#xff01;真的好有成就感啊&#xff01; “额&#xff0c;请问一下&#xff0c;你技术这么牛&#xff0c;卖出去多少钱啦&…