毕业设计 接金币小游戏设计与实现 (源码)


1 项目简介

🔥 Hi,各位同学好呀,这里是L学长!

🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品

python小游戏毕设 接金币小游戏设计与实现 (源码)

🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分)


1 游戏介绍

基于python实现的接金币小游戏。

游戏规则:

方向键控制小人左右移动接金币。

2 实现效果

在这里插入图片描述
在这里插入图片描述

3 开发工具

3.1 环境配置

  • Python版本:3.6.4

  • 相关模块:

  • pygame模块;

  • 以及一些Python自带的模块。

3.2 Pygame介绍

简介

Pygame是一系列专门为编写电子游戏而设计的Python模块(modules)。Pygame在已经非常优秀的SDL库的基础上增加了许多功能。这让你能够用Python语言编写出丰富多彩的游戏程序。

Pygame可移植性高,几乎能在任何平台和操作系统上运行。

Pygame已经被下载过数百万次。

Pygame免费开源。它在LGPL许可证(Lesser General Public License,GNU宽通用公共许可证)下发行。使用Pygame,你可以创造出免费开源,可共享,或者商业化的游戏。详情请见LGPL许可证。

优点

  • 能够轻松使用多核CPU(multi core CPUs) :如今双核CPU很常用,8核CPU在桌面系统中也很便宜,而利用好多核系统,能让你在你的游戏中实现更多东西。特定的pygame函数能够释放令人生畏的python GIL(全局解释器锁),这几乎是你用C语言才能做的事。

  • 核心函数用最优化的C语言或汇编语言编写:C语言代码通常比Python代码运行速度快10-20倍。而汇编语言编写的代码(assembly code)比Python甚至快到100多倍。

  • 安装便捷:一般仅需包管理程序或二进制系统程序便能安装。

  • 真正地可移植:支持Linux (主要发行版), Windows (95, 98, ME, 2000, XP, Vista, 64-bit Windows,), Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX等操作系统.也能支持AmigaOS, Dreamcast, Atari, AIX, OSF/Tru64, RISC OS, SymbianOS and OS/2,但是还没有受到官方认可。你也可以在手持设备,游戏控制台, One Laptop Per Child (OLPC) computer项目的电脑等设备中使用pygame.

  • 用法简单:无论是小孩子还是大人都能学会用pygame来制作射击类游戏。

  • 很多Pygame游戏已发行:其中包括很多游戏大赛入围作品、非常受欢迎的开源可分享的游戏。

  • 由你来控制主循环:由你来调用pygame的函数,pygame的函数并不需要调用你的函数。当你同时还在使用其他库来编写各种各种的程序时,这能够为你提供极大的掌控权。

  • 不需要GUI就能使用所有函数:仅在命令行中,你就可以使用pygame的某些函数来处理图片,获取游戏杆输入,播放音乐……

  • 对bug反应迅速:很多bug在被上报的1小时内就能被我们修复。虽然有时候我们确实会卡在某一个bug上很久,但大多数时候我们都是很不错的bug修复者。如今bug的上报已经很少了,因为许多bug早已被我们修复。

  • 代码量少:pygame并没有数以万计的也许你永远用不到的冗杂代码。pygame的核心代码一直保持着简洁特点,其他附加物诸如GUI库等,都是在核心代码之外单独设计研发的。

  • 模块化:你可以单独使用pygame的某个模块。想要换着使用一个别的声音处理库?没问题。pygame的很多核心模块支持独立初始化与使用。

最小开发框架

import pygame,sys #sys是python的标准库,提供Python运行时环境变量的操控

pygame.init()  #内部各功能模块进行初始化创建及变量设置,默认调用
size = width,height = 800,600  #设置游戏窗口大小,分别是宽度和高度
screen = pygame.display.set_mode(size)  #初始化显示窗口
pygame.display.set_caption("小游戏程序")  #设置显示窗口的标题内容,是一个字符串类型
while True:  #无限循环,直到Python运行时退出结束
    for event in pygame.event.get():  #从Pygame的事件队列中取出事件,并从队列中删除该事件
        if event.type == pygame.QUIT:  #获得事件类型,并逐类响应
            sys.exit()   #用于退出结束游戏并退出          
    pygame.display.update()  #对显示窗口进行更新,默认窗口全部重绘

代码执行流程

在这里插入图片描述

4 具体实现

ok,言归正传,首先,我们来进行一下游戏的初始化操作(比如初始化游戏窗口,加载一些必要的游戏素材等等),代码实现如下:

'''游戏初始化'''
def initGame():
    # 初始化pygame, 设置展示窗口
    pygame.init()
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('catch coins —— 微信公众号: Charles的皮卡丘')
    # 加载必要的游戏素材
    game_images = {}
    for key, value in cfg.IMAGE_PATHS.items():
        if isinstance(value, list):
            images = []
            for item in value: images.append(pygame.image.load(item))
            game_images[key] = images
        else:
            game_images[key] = pygame.image.load(value)
    game_sounds = {}
    for key, value in cfg.AUDIO_PATHS.items():
        if key == 'bgm': continue
        game_sounds[key] = pygame.mixer.Sound(value)
    # 返回初始化数据
    return screen, game_images, game_sounds

接着,为了让我们在写小游戏的过程中保持一个良好的心情,就先让自己喜欢的游戏背景音乐响起来吧:

# 播放背景音乐
pygame.mixer.music.load(cfg.AUDIO_PATHS['bgm'])
pygame.mixer.music.play(-1, 0.0)

然后选一张好看的背景图片,这样才可以在它的基础上逐渐加入接金币这个小游戏涉及到的游戏元素嘛:
在这里插入图片描述

源代码如下:

# 游戏主循环
clock = pygame.time.Clock()
while True:
    # --填充背景
    screen.fill(0)
    screen.blit(game_images['background'], (0, 0))
    # --按键检测
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # --更新屏幕
    pygame.display.flip()
    clock.tick(cfg.FPS)

接着,我们把用来接金币的小人放进去呗,就像这样:
在这里插入图片描述
代码实现如下:

hero = Hero(game_images['hero'], position=(375, 520))
while True:
    ...
    key_pressed = pygame.key.get_pressed()
    if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
        hero.move(cfg.SCREENSIZE, 'left')
    if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
        hero.move(cfg.SCREENSIZE, 'right')
    ...

小人的移动主要是通过我们自己定义的Hero类里的move函数实现的,具体而言代码实现如下:

'''定义hero类'''
class Hero(pygame.sprite.Sprite):
    def __init__(self, images, position=(375, 520), **kwargs):
        pygame.sprite.Sprite.__init__(self)
        self.images_right = images[:5]
        self.images_left = images[5:]
        self.images = self.images_right.copy()
        self.image = self.images[0]
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = position
        self.diretion = 'right'
        self.speed = 8
        self.switch_frame_count = 0
        self.switch_frame_freq = 1
        self.frame_index = 0
    '''左右移动hero'''
    def move(self, screensize, direction):
        assert direction in ['left', 'right']
        if direction != self.diretion:
            self.images = self.images_left.copy() if direction == 'left' else self.images_right.copy()
            self.image = self.images[0]
            self.diretion = direction
            self.switch_frame_count = 0
        self.switch_frame_count += 1
        if self.switch_frame_count % self.switch_frame_freq == 0:
            self.switch_frame_count = 0
            self.frame_index = (self.frame_index + 1) % len(self.images)
            self.image = self.images[self.frame_index]
        if direction == 'left':
            self.rect.left = max(self.rect.left-self.speed, 0)
        else:
            self.rect.left = min(self.rect.left+self.speed, screensize[0])
    '''画到屏幕上'''
    def draw(self, screen):
        screen.blit(self.image, self.rect)

实现小人左右走动这个效果的核心思路其实就是根据函数输入的方向来改变小人可以使用的图片组合(images_right/images_left)。然后对于正在往某个方向行动中的小人,每隔switch_frame_freq帧游戏画面就切换一次小人的图片,游戏中的小人图片一共十张:

在这里插入图片描述
前5张是向右走的图片组,后5张是向左走的图片组。

现在,我们开始实现掉金币的效果啦,就像这样:

在这里插入图片描述

核心代码实现如下:

# 定义食物组
food_sprites_group = pygame.sprite.Group()
generate_food_freq = random.randint(10, 20)
generate_food_count = 0
while True:
    ...
    # --随机生成食物
    generate_food_count += 1
    if generate_food_count > generate_food_freq:
        generate_food_freq = random.randint(10, 20)
        generate_food_count = 0
        food = Food(game_images, random.choice(['gold',] * 10 + ['apple']), cfg.SCREENSIZE)
        food_sprites_group.add(food)
    # --更新食物
    for food in food_sprites_group:
        if food.update(): food_sprites_group.remove(food)
    ...

其中食物类的定义如下:

'''定义食物类'''
class Food(pygame.sprite.Sprite):
    def __init__(self, images_dict, selected_key, screensize, **kwargs):
        pygame.sprite.Sprite.__init__(self)
        self.screensize = screensize
        self.image = images_dict[selected_key]
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.bottom = random.randint(20, screensize[0]-20), -10
        self.speed = random.randrange(5, 10)
        self.score = 1 if selected_key == 'gold' else 5
    '''更新食物位置'''
    def update(self):
        self.rect.bottom += self.speed
        if self.rect.top > self.screensize[1]:
            return True
        return False

整个定义其实很简单,苹果5分,金币1分,update函数用于实时更新食物的位置以实现食物下降的效果。

问题来了,我们该怎么实现接金币的效果呢,这金币一直往下掉接不住可咋办啊。别担心,只需要调用到pygame库里的碰撞检测功能就可以啦,具体而言代码实现如下:

while True:
    ...
    # --碰撞检测
    for food in food_sprites_group:
        if pygame.sprite.collide_mask(food, hero):
            game_sounds['get'].play()
            food_sprites_group.remove(food)
            score += food.score
    ...

代码原理其实很简单,就是实时地检测小人有没有接触到金币,接触到的话就发出接到金币的声音,并移除对应的金币和获得一定的奖励得分。效果是这样子的:
在这里插入图片描述

最后,我们来完善一下我们的小游戏,让它看起来更加"规范"一些:

(1) 加入倒计时(在规定的时间内获得尽量高的得分)

while True:
    ...
    # --倒计时信息
    countdown_text = 'Count down: ' + str((90000 - pygame.time.get_ticks()) // 60000) + ":" + str((90000 - pygame.time.get_ticks()) // 1000 % 60).zfill(2)
    countdown_text = font.render(countdown_text, True, (0, 0, 0))
    countdown_rect = countdown_text.get_rect()
    countdown_rect.topright = [cfg.SCREENSIZE[0]-30, 5]
    screen.blit(countdown_text, countdown_rect)
    ...

(2) 显示得分(显示当前的得分和历史最高分)

while True:
    ...
    # --显示得分
    score_text = f'Score: {score}, Highest: {highest_score}'
    score_text = font.render(score_text, True, (0, 0, 0))
    score_rect = score_text.get_rect()
    score_rect.topleft = [5, 5]
    screen.blit(score_text, score_rect)
    ...

(3) 添加游戏结束界面(玩家想结束游戏还是想重新开始游戏呢?)

'''游戏结束画面'''
def showEndGameInterface(screen, cfg, score, highest_score):
    # 显示的文本信息设置
    font_big = pygame.font.Font(cfg.FONT_PATH, 60)
    font_small = pygame.font.Font(cfg.FONT_PATH, 40)
    text_title = font_big.render(f"Time is up!", True, (255, 0, 0))
    text_title_rect = text_title.get_rect()
    text_title_rect.centerx = screen.get_rect().centerx
    text_title_rect.centery = screen.get_rect().centery - 100
    text_score = font_small.render(f"Score: {score}, Highest Score: {highest_score}", True, (255, 0, 0))
    text_score_rect = text_score.get_rect()
    text_score_rect.centerx = screen.get_rect().centerx
    text_score_rect.centery = screen.get_rect().centery - 10
    text_tip = font_small.render(f"Enter Q to quit game or Enter R to restart game", True, (255, 0, 0))
    text_tip_rect = text_tip.get_rect()
    text_tip_rect.centerx = screen.get_rect().centerx
    text_tip_rect.centery = screen.get_rect().centery + 60
    text_tip_count = 0
    text_tip_freq = 10
    text_tip_show_flag = True
    # 界面主循环
    clock = pygame.time.Clock()
    while True:
        screen.fill(0)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    return False
                elif event.key == pygame.K_r:
                    return True
        screen.blit(text_title, text_title_rect)
        screen.blit(text_score, text_score_rect)
        if text_tip_show_flag:
            screen.blit(text_tip, text_tip_rect)
        text_tip_count += 1
        if text_tip_count % text_tip_freq == 0:
            text_tip_count = 0
            text_tip_show_flag = not text_tip_show_flag
        pygame.display.flip()
        clock.tick(cfg.FPS)

5 最后

项目获取:https://gitee.com/sinonfin/system-sharing

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>