展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 编程开发 > 怎么使用Python+Pygame实现简单的单词小游戏

怎么使用Python+Pygame实现简单的单词小游戏

一、环境准备1)运行环境环境安装:python3.8:解释器、pycharm:代码编辑器、pygame、numpy、部分自带的模块直接安装Python就可以使用了。2)模块安装第三方库的安装方式如下:一般安装:pipinstall+模块名镜像源安装:pipinstall-ipypi.douban.com/simple/+模块名(还有很多国内镜像源,这里是豆瓣的用习惯了)3)图片文字素材等二、代码展示主程序——importpygameimportsysimporttracebacki

本文目录

    一、环境准备

    1)运行环境 

    环境安装:python 3.8: 解释器、pycharm: 代码编辑器、pygame、numpy、部分自带的模块直接安装Python就可以使用了。

     2)模块安装

     第三方库的安装方式如下:

     一般安装:pip install +模块名 镜像源安装:pip install -i 

    pypi.douban.com/simple/+模块名 (还有很多国内镜像源,这里是豆瓣的用习惯了) 

    3)图片文字素材等

    怎么使用Python+Pygame实现简单的单词小游戏

    二、代码展示

    主程序——

    import pygame
    import sys
    import traceback
    import os
    from pygame.locals import *
    from random import *
    import numpy as np
    import linecache
    
    pygame.init()  # 游戏初始化
    pygame.mixer.init()  # 音效初始化
    
    bg_size = width, height = 480, 700  # 屏幕大小
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("英语单词挑战")  # 标题
    
    # 背景图片
    background = pygame.image.load("source/背景.png")  # .convert()
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)
    
    # 游戏音乐
    pygame.mixer.music.load("source/背景音乐.mp3")
    pygame.mixer.music.set_volume(0.2)
    success_sound = pygame.mixer.Sound("source/正确.wav")
    success_sound.set_volume(0.2)
    lost_sound = pygame.mixer.Sound("source/失败.wav")
    lost_sound.set_volume(0.2)
    win_sound = pygame.mixer.Sound("source/胜利.wav")
    win_sound.set_volume(0.2)
    
    class Word(pygame.sprite.Sprite):
        def __init__(self, bg_size, showword):
            pygame.sprite.Sprite.__init__(self)
    
            self.word = showword  # 获取单词
            self.length = len(self.word)  # 单词长度
            self.wordfont = pygame.font.SysFont("arial", 36)  # 使用系统字体
            self.wordtext = self.wordfont.render(self.word, True, WHITE, BLACK)  # 单词
            self.promptword = "*"*self.length
            self.showtext = self.wordfont.render(self.promptword, True, WHITE, BLACK)  # 隐藏单词
            self.succtext = self.wordfont.render("", True, WHITE)
            self.rect = self.wordtext.get_rect()  # 单词坐标
            self.width, self.height = bg_size[0], bg_size[1]
            self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, 20  # 定义坐标
            self.speed = 1  # 下移速度
            # self.destroy_images = []
            # self.destroy_images.extend([pygame.image.load("爆炸小.png").convert_alpha()])
            self.active = True  # 活动标志
            self.success = False  # 正确标志
    
        # 判断输入字母是否正确,并显示
        def show(self, a):
            for i in range(self.length):
                if self.promptword[i] == "*":
                    if self.word[i] == a:
                        self.promptword =self.promptword[:i] + a + self.promptword[i+1:]
                        self.showtext = self.wordfont.render(self.promptword, True, WHITE, BLACK)  # 隐藏单词
                    if self.promptword == self.word:
                        self.success = True
                    break
                else:
                    continue
    
        # 单词移动
        def move(self):
            if self.rect.top < self.height - 50:
                self.rect.top += self.speed
            else:
                self.reset()
    
        # 单词重置
        def reset(self):
            self.active = True
            self.success = False
            self.rect.left, self.rect.top = (self.width - self.rect.width) // 2, 20
    
        # 中文提示
        def describe(self, prop):
            myprop = prop
            self.propfont = pygame.font.Font("source/楷体_GB2312.ttf", 20)  # 使用楷体字体
            # print(myprop)
            self.describetext = self.propfont.render(myprop, True, BLACK)  # 中文提示
            self.proprect = self.describetext.get_rect()  # 提示坐标
            self.proprect.left, self.proprect.top = (self.width - self.proprect.width) // 2, (self.height - 30 - self.proprect.height / 2)
            screen.blit(self.describetext, self.proprect)
    
    # 获取单词,读取字典文件
    def Getletters(filename):
        words = []  # 保存单词
        prompts = []  # 保存中文提示
        worddict = {}  # 单词字典
        f = open(filename, encoding='utf-8')  # 打开文本,定义格式,能够读取中文
        for line in f.readlines():  # 读取行
            line = line.strip()  # 去掉/n
            word = line.split(":")[0]  # 截取单词
            prompt = line.split(":")[1]  # .split(";")[0]  # 截取中文提示
            words.append(word)
            prompts.append(prompt)
            worddict.update({word : prompt})  # 字典添加元素
        f.close()
        return worddict
    
    # 保存字典文件
    def SaveDict(dict1, filename):
        # 打开字典文件
        with open(filename, mode='w', encoding='utf-8') as f:
            for k, v in dict1.items():
                str = f"{k}:{v}\n"
                f.write(str)
            f.close()
    
    
    # 随机抽取字典的数据
    def ChoseWord(dict1):
        n = len(dict1)
        random.choice(list(dict1.keys()))
        words = dict1.keys()
        prompts = dict1.values()
        i = randint(0, n)
        key = words[i]
        value = prompts[i]
        return key, value
    
    
    # 主函数
    def main():
        pygame.mixer.music.play(-1)  # 播放背景音乐
        running = True  # 判断运行状态
        clock = pygame.time.Clock()  # 时钟
        delay = 100
        olingefile = "source/words.txt"  # 原始单词文件
        myfile = "source/newword.txt"  # 使用单词文件
        historyfile = "source/record.txt"  # 最高记录文件
        olindict = Getletters(olingefile)  # 获取原始单词
        num = len(olindict)  # 总单词数量
        # getnum = 0
        # record_score = 0  # 最高得分记录
        # record_rate = 0.00  # 最高进度
        myfont_big = pygame.font.SysFont("arial", 36)  # 使用系统大字体
        myfont_small = pygame.font.SysFont("arial", 24)  # 使用系统小字体
        # 标志是否暂停游戏
        paused = False
        paused_image = pygame.image.load("source/暂停.png").convert_alpha()
        resume_image = pygame.image.load("source/播放.png").convert_alpha()
        paused_rect = paused_image.get_rect()
        paused_rect.left, paused_rect.top = width - paused_rect.width - 10, 10
        paused_show_image = paused_image
        # 主页
        mained = False  # 主页标志
        main_image = pygame.image.load("source/主页.png").convert_alpha()
        main_rect = main_image.get_rect()
        main_rect.left, main_rect.top = width - paused_rect.width - 70, 10
        # 成功页面
        success_image = pygame.image.load("source/成功.png").convert_alpha()
        # 底部页面
        bottom_image = pygame.image.load("source/底部.png").convert_alpha()
        # 统计得分
        # score = 0  # 当前得分
        # rate = 0.00  # 当前进度
        # 主页面
        goon_image = pygame.image.load("source/继续游戏.png").convert_alpha()
        goon_rect = goon_image.get_rect()
        restart_image = pygame.image.load("source/重新开始.png").convert_alpha()
        restart_rect = restart_image.get_rect()
        gameover_image = pygame.image.load("source/结束游戏.png").convert_alpha()
        gameover_rect = gameover_image.get_rect()
        flag = False  # 新单词标记
        promptflag = False  # 空格提示单词标记
        nextflag = False  # 回车下一个单词标记
        winflag = False  # 胜利标志
        keyvalue = ""  # 获取按键
        if os.path.exists(myfile) and os.path.exists(historyfile):  # 如果有记录
            mydict = Getletters(myfile)
            getnum = num - len(mydict)  # 完成数量
            mained = True
            with open(historyfile, mode='r', encoding='utf-8') as f:
                record_score = int(linecache.getline(historyfile, 1))  # 读取最高记录
                record_rate = float(linecache.getline(historyfile, 2))  # 读取最高进度
                score = int(linecache.getline(historyfile, 3))  # 读取上一次记录
                f.close()
            # print(record_score, record_rate)
        else:
            mydict = Getletters(olingefile)
            getnum = 0
            score = 0
            rate = 0.00
            record_score = score
            record_rate = rate
            mained = False
    
        while running:
            for event in pygame.event.get():
                if event.type == QUIT:  # 退出
                    # 写入记录文件
                    with open(historyfile, mode='w', encoding='utf-8') as f:
                        f.write(str(record_score))
                        f.write("\n")
                        f.write(str(record_rate))
                        f.write("\n")
                        f.write(str(score))
                        f.close()
                    # 保存剩余单词
                    SaveDict(mydict, myfile)
                    pygame.quit()
                    sys.exit()
                elif event.type == MOUSEBUTTONDOWN:  # 鼠标按下
                    # 按下暂停键
                    if event.button == 1 and paused_rect.collidepoint(event.pos):  # 检测鼠标是否在范围内
                        paused = not paused
                        if paused:
                            pygame.mixer.music.pause()  # 背景音乐暂停
                            pygame.mixer.pause()  # 音效暂停
                            paused_show_image = resume_image
                        else:
                            pygame.mixer.music.unpause()  # 背景音乐暂停
                            pygame.mixer.unpause()  # 音效暂停
                            paused_show_image = paused_image
                    # 按下主页键
                    if event.button == 1 and main_rect.collidepoint(event.pos):  # 检测鼠标是否在范围内
                        mained = True
                        if mained:
                            pygame.mixer.music.pause()  # 背景音乐暂停
                            pygame.mixer.pause()  # 音效暂停
    
                elif event.type == KEYDOWN:  # 按键
                    if event.key == K_TAB:  # tab键
                        promptflag = True
                    elif event.key == K_RETURN:  # 回车键
                        nextflag = True
                    else:
                        keyvalue = chr(event.key)  # 获取ASCII码转字符串
            screen.blit(background, (0, 0))  # 载入背景图片
            screen.blit(bottom_image, (0, height - 60))  # 载入底部图片
            # 绘制得分
            score_text = myfont_big.render(f"score:{str(score)}", True, WHITE)
            screen.blit(score_text, (10, 5))
            # 暂停/播放
            screen.blit(paused_show_image, paused_rect)  # 暂停图片
            # 绘制主页
            screen.blit(main_image, main_rect)  # 主页图片
            # 绘制进度
            pygame.draw.rect(screen, WHITE, ((10, 60), (200, 20)), 2)  # 画矩形,坐标(10,60),长宽(200,20),线宽2
    
            # 当进度大于80%显示绿色,否则显示红色
            rate = getnum / num
            if rate > 0.8:
                rate_color = GREEN
            else:
                rate_color = RED
            pygame.draw.rect(screen, rate_color, ((10, 60), (200 * rate, 20)), 0)  # 填充
            remaintext = myfont_small.render(f"{rate*100:.2f}%", True, WHITE)
            screen.blit(remaintext, (220, 55))
            if not paused and not mained:
                if not flag:
                    # 生成单词
                    showword = np.random.choice(list(mydict.keys()))  # 随机选择单词
                    showprompt = mydict[showword]  # 单词中文提示
                    # print(showword, showprompt)
                    myword = Word(bg_size, showword)  # 生成单词
                    flag = True  # 新单词
                else:
                    myword.move()  # 单词向下移动
                    myword.describe(showprompt)
                    myword.show(keyvalue)  # 获取键盘按键
                    if promptflag:
                        screen.blit(myword.wordtext, myword.rect)
                    else:
                        screen.blit(myword.showtext, myword.rect)
                        # 成功
                        if myword.success:
                            screen.blit(myword.succtext, myword.rect)  # 清空
                            screen.blit(success_image, myword.rect)  # 成功图片
                            success_sound.play()
                            if not (delay % 10):  # 延时
                                myword.reset()
                                flag = False
                                score += 5
                                getnum += 1
                                del mydict[showword]
                                if getnum == num:
                                    winflag = True
                                    mained = True
                    if nextflag:
                        myword.reset()
                        flag = False
                        nextflag = False
                    if myword.rect.top > height - 118:
                        lost_sound.play()
                        flag = False
                        score -= 2
            # 暂停时
            elif paused and not mained:
                myword.active = False
                screen.blit(myword.showtext, myword.rect)
                myword.describe(showprompt)
            # 显示主页
            elif mained and not winflag:
                # myword.active = False
                screen.blit(background, (0, 0))  # 载入背景图片
                # 绘制结束界面
                # 更新最高分
                if score > record_score:
                    record_score = score
                # 更新进度
                if rate > record_rate:
                    record_rate = rate
                # 最高分
                record_score_text = myfont_big.render(f"Highest Score:{record_score}", True, WHITE)
                screen.blit(record_score_text, (50, 50))
                # 最高进度
                record_rate_text = myfont_big.render(f"Highest Rate:{record_rate*100:.2f}%", True, WHITE)
                screen.blit(record_rate_text, (50, 100))
                # 当前得分
                nowscore_text1 = myfont_big.render("Your Score:", True, WHITE)
                nowscore_text1_rect = nowscore_text1.get_rect()
                nowscore_text1_rect.left, nowscore_text1_rect.top = 50, 150
                screen.blit(nowscore_text1, nowscore_text1_rect)
                nowscore_text2 = myfont_big.render(str(score), True, RED)
                nowscore_text2_rect = nowscore_text2.get_rect()
                nowscore_text2_rect.left, nowscore_text2_rect.top = 50 + nowscore_text1_rect.width, nowscore_text1_rect.top
                screen.blit(nowscore_text2, nowscore_text2_rect)
                # 当前进度
                nowrate_text1 = myfont_big.render("Your Rate:", True, WHITE)
                nowrate_text1_rect = nowrate_text1.get_rect()
                nowrate_text1_rect.left, nowrate_text1_rect.top = 50, 200
                screen.blit(nowrate_text1, nowrate_text1_rect)
                nowrate_text2 = myfont_big.render(f"{rate*100:.2f}%", True, RED)
                nowrate_text2_rect = nowrate_text2.get_rect()
                nowrate_text2_rect.left, nowrate_text2_rect.top = 50 + nowrate_text1_rect.width, nowrate_text1_rect.top
                screen.blit(nowrate_text2, nowrate_text2_rect)
    
                # 继续游戏
                goon_rect.left, goon_rect.top = (width - goon_rect.width) // 2, 300
                screen.blit(goon_image, goon_rect)
                # 重新开始
                restart_rect.left, restart_rect.top = (width - restart_rect.width) // 2, goon_rect.bottom + 20
                screen.blit(restart_image, restart_rect)
                # 结束游戏
                gameover_rect.left, gameover_rect.top = (width - gameover_rect.width) // 2, restart_rect.bottom + 20
                screen.blit(gameover_image, gameover_rect)
    
                # 检测用户鼠标操作
                # 如果用户按下鼠标左键
                if pygame.mouse.get_pressed()[0]:
                    # 获取鼠标位置
                    pos = pygame.mouse.get_pos()
                    # 如果用户点击继续游戏
                    if goon_rect.left < pos[0] < goon_rect.right and goon_rect.top < pos[1] < goon_rect.bottom:
                        # 跳出主页面
                        mained = False
                    # 重新开始
                    elif restart_rect.left < pos[0] < restart_rect.right and restart_rect.top < pos[1] < restart_rect.bottom:
                        # 判断最高记录是否更新,保存记录
                        if score > record_score:
                            record_score = score
                        # 写入记录文件
                        with open(historyfile, mode='w', encoding='utf-8') as f:
                            f.write(str(record_score))
                            f.write("\n")
                            f.write(str(record_rate))
                            f.close()
                        # 保存剩余单词
                        SaveDict(mydict, myfile)
                        # 退出主页
                        mained = False
                        score = 0
                        mydict = Getletters(olingefile)  # 获取原始单词
                        getnum = 0
    
                    # 如果用户点击结束游戏
                    elif gameover_rect.left < pos[0] < gameover_rect.right and gameover_rect.top < pos[1] < gameover_rect.bottom:
                        # 写入记录文件
                        with open(historyfile, mode='w', encoding='utf-8') as f:
                            f.write(str(record_score))
                            f.write("\n")
                            f.write(str(record_rate))
                            f.write("\n")
                            f.write(str(score))
                            f.close()
                        # 保存剩余单词
                        SaveDict(mydict, myfile)
                        # 退出游戏
                        pygame.quit()
                        sys.exit()
            else:
                # screen.blit(background, (0, 0))  # 载入背景图片
                pygame.mixer.music.pause()  # 背景音乐暂停
                win_sound.play()
                win_text = myfont_big.render("Congratulations! You WIN!!!", True, WHITE)
                screen.blit(win_text, (50, 300))
    
    
            # 时间间隔
            delay -= 1
            if not delay:
                delay = 50
                promptflag = False
            pygame.display.flip()  # 页面刷新
    
            clock.tick(60)
    
    
    if __name__ == "__main__":
        try:
            main()
        except SystemExit:
            pass
        except:
            traceback.print_exc()
            pygame.quit()
            input()
    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    编程开发 Python Pygame
    相关文章 更多
    精品专题 更多
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    IOS软件

    正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

    AI

    正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

    Mac软件 更多
    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    Aperty macOS版
    Aperty macOS版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw macOS版
    AionClaw macOS版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy macOS版
    Loomy macOS版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirBuddy 3 macOS版
    AirBuddy 3 macOS版

    AirBuddy 3 是面向 macOS 的蓝牙外设增强工具,可集中查看 AirPods、Beats 与苹果设备电量,提供快速连接、播放控制、电量提醒、桌面小组件和多台 Mac 外设接力等功能。

    AirServer macOS版
    AirServer macOS版

    AirServer macOS版可把 Mac 转换为无线投屏接收端,通过 AirPlay 或 Google Cast 接收手机、平板和电脑画面,并支持多设备同时连接、高清镜像及会话录制,适合家庭娱乐、课堂教学、会议演示和应用展示。

    AnyDesk macOS版
    AnyDesk macOS版

    AnyDesk macOS版是一款适用于远程办公、设备维护和客户支持的远程桌面工具,可连接并操作异地计算机,支持文件传输、无人值守访问、会话权限控制及跨平台协作。

    WINDOWS 更多
    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    PixTV 网页版
    PixTV 网页版

    PixTV 是一款在浏览器中使用的 AI 视觉内容创作平台,提供图片、视频、音频、文字生成、AI 分镜、局部重绘、提示词反推和无限画布等工具,适合短剧、漫剧、广告及社交媒体内容制作。

    讯飞绘文 网页版
    讯飞绘文 网页版

    讯飞绘文网页版是科大讯飞推出的AI图文创作与内容运营工具,覆盖热点选题、文章生成、稿件校对、智能配图、排版发布和数据复盘等环节,适合自媒体创作者及内容团队在线使用。

    Maya(Autodesk Maya) Windows版
    Maya(Autodesk Maya) Windows版

    Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

    Aperty Windows版
    Aperty Windows版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw Windows版
    AionClaw Windows版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy Windows版
    Loomy Windows版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。