连续上了七天学,终于熬到放假!正好赶上中秋节,就编个游戏庆祝一下吧!
在这个数字化的时代,用编程创造出有趣的游戏来庆祝传统节日,无疑是一种独特而富有创意的方式。今天,我要为大家介绍一个用 Python 的 Pygame 库制作的中秋节小游戏。这个游戏不仅充满了欢乐和挑战,还能让玩家在游戏中学习到关于中秋节的知识。
所有用到的资源在顶部显示!!!!免费下载!!!!
一、游戏背景与目标
中秋节是一个团圆的节日,月饼和天狗的传说更是为这个节日增添了神秘的色彩。这个小游戏以中秋节为背景,将吃月饼游戏和中秋节知识问答相结合,让玩家在享受游戏乐趣的同时,也能了解中秋节的文化内涵。
游戏的目标很简单:在吃月饼游戏中,玩家要控制天狗接住不断下落的月饼,尽可能地获得高分;在知识问答部分,玩家需要正确回答与中秋节相关的问题,展示自己对中秋节的了解程度。
二、游戏功能与特点
1. 吃月饼游戏
- 游戏玩法:玩家通过控制天狗的左右移动来接住下落的月饼。月饼从屏幕上方随机位置落下,天狗必须在月饼落到屏幕底部之前接住它。如果天狗成功接住月饼,玩家将获得分数,月饼的下落速度也会随着得分的增加而逐渐加快,增加游戏的难度和挑战性。
- 得分系统:游戏设有多个得分阶段,当玩家达到一定分数时,月饼的下落速度会加快,给玩家带来更大的挑战。同时,游戏还会在屏幕上显示玩家的当前得分,让玩家随时了解自己的游戏进度。
- 游戏失败与重新开始:如果月饼落到屏幕底部或者天狗没有接住月饼,游戏不会立即结束,而是继续进行,让玩家有更多的机会来提高分数。当玩家觉得游戏难度太大或者想要重新开始时,可以选择退出游戏并重新开始。
2. 中秋节知识问答
- 问题与答案:游戏从 “中秋节知识问答.txt” 文件中读取问题和答案,每个问题都有四个选项,玩家需要选择正确的答案。问题涵盖了中秋节的历史、传统习俗、神话传说等方面的知识,让玩家在游戏中学习到更多关于中秋节的文化内涵。
- 答案判断与反馈:当玩家选择答案后,游戏会立即判断答案是否正确,并给出相应的反馈。如果答案正确,答案会显示为绿色;如果答案错误,答案会显示为红色。同时,游戏还会在屏幕上显示正确答案,让玩家了解自己的错误之处。
- 下一题与游戏结束:玩家可以通过按下回车键切换到下一题。当玩家回答完所有问题或者想要退出知识问答部分时,可以选择返回游戏主界面,继续进行吃月饼游戏或者重新开始知识问答。
三、代码结构与实现
1. 导入模块与初始化
游戏首先导入了必要的模块和常量,包括 Pygame 库、随机数模块和系统模块。然后,初始化 Pygame 库,并打开 “中秋节知识问答.txt” 文件读取问题和答案。
import pygame,random,sys
from pygame.locals import *
pygame.init()
f = open('中秋节知识问答.txt',"rt",encoding="UTF-8", errors='ignore')
file=f.readlines()
这里,pygame.init()
函数用于初始化 Pygame 库,确保所有的子系统都被正确初始化。打开文件的操作使得游戏能够读取中秋节知识问答的内容,为知识问答部分提供数据支持。
2. 定义函数与变量
- 定义打印文本函数:
remind='请选择与你的答案相对应的数字:1A,2B,3C,4D。填写完答案后按回车键切换下一题'
color=[(255,255,255),(255,255,255),(255,255,255),(255,255,255)]
j=0
right=False
def print_text(text,size,x,y,color):
font=pygame.font.Font("C:\\WINDOWS\\FONTS\\SIMHEI.TTF",size)
text_surface=font.render(text,True,color)
screen.blit(text_surface,(x,y))
这个函数print_text
接受文本内容、字体大小、坐标和颜色作为参数,使用指定的字体将文本渲染为图像,并将其绘制在屏幕上指定的位置。remind
变量存储了知识问答部分的提示信息,color
列表用于存储四个选项的颜色,初始状态下都为白色。j
变量用于记录当前问题在文件中的索引,right
变量用于标记答案是否正确。
- 设置屏幕与窗口属性:
# 绘制屏幕 x_mode=650 y_mode=975 screen=pygame.display.set_mode((650,975)) # 给窗口命名 pygame.display.set_caption("中秋节小游戏")
这里设置了屏幕的大小为
(650, 975)
,并给窗口命名为 “中秋节小游戏”。这样玩家在打开游戏时就能看到一个特定大小的窗口和有明确标识的标题。 - 设置菜单页面状态与加载图片资源:
# 设置菜单页面状态 windows=True Trivia=False #加载图片 background_window=pygame.image.load("中秋游戏背景图.jpg").convert() start_button=pygame.image.load("吃月饼游戏开始按钮.png").convert_alpha() mooncake=pygame.image.load("月饼.png").convert_alpha() eat_mooncake_background=pygame.image.load("吃月饼游戏背景图.jpg").convert() sky_dog=pygame.image.load("天狗.png").convert_alpha() gameover=pygame.image.load("游戏结束图片.jpg").convert() backspace=pygame.image.load("返回.png").convert_alpha() Trivia_button=pygame.image.load("中秋知识问答.png").convert_alpha()
定义了两个标志变量
windows
和Trivia
来表示游戏的不同状态。初始状态下,windows
为True
,表示处于游戏主菜单页面;Trivia
为False
,表示知识问答部分未开启。接着,加载了各种图片资源,包括游戏背景图、按钮、月饼、天狗等,为游戏的可视化提供了素材。 - 调整图片尺寸与定义游戏数据:
# 量取图片 width,height=start_button.get_size() width1,height1=Trivia_button.get_size() # 改图片尺寸 width=width//4 height=height//4 start_button=pygame.transform.smoothscale(start_button,(width,height)) start_button_rect=start_button.get_rect(center=(500-width/2,975-275)) width1=width1//4 height1=height1//4 Trivia_button=pygame.transform.smoothscale(Trivia_button,(width1,height1)) Trivia_button_rect=Trivia_button.get_rect(center=(500-width/2,975-375)) # 量取月饼尺寸 mooncake_width,mooncake_height=mooncake.get_size() # 改月饼尺寸 mooncake_width=mooncake_width//6 mooncake_height=mooncake_height//6 mooncake=pygame.transform.smoothscale(mooncake,(mooncake_width,mooncake_height)) # 定义吃月饼游戏数据 eat_mooncake=False score=0 mooncake1=True # 量返回键尺寸 backspace_x,backspace_y=backspace.get_size() # 改返回键尺寸 backspace_width=backspace_x//10 backspace_height=backspace_y//10 backspace=pygame.transform.smoothscale(backspace,(backspace_width,backspace_height)) backspace_rect=backspace.get_rect(center=(x_mode-backspace_width,25)) # 月饼属性 speed=0.1 location_1=random.randint(0,x_mode-mooncake_width) mooncake_y=-70 # 量天狗尺寸 sky_dog_width,sky_dog_height=sky_dog.get_size() # 修改天狗尺寸 sky_dog_width=sky_dog_width//4 sky_dog_height=sky_dog_height//4 sky_dog=pygame.transform.smoothscale(sky_dog,(sky_dog_width,sky_dog_height)) # 绘制结束文字 font=pygame.font.Font("C:\\WINDOWS\\FONTS\\SIMHEI.TTF",80) font2=pygame.font.Font("C:\\WINDOWS\\FONTS\\SIMHEI.TTF",30) text=font2.render("游戏结束",True,(255,255,255)) text2=font.render("中秋节快乐!",True,(255,255,30))
这段代码对加载的图片进行了尺寸调整,使得它们适应游戏窗口的大小。同时,定义了吃月饼游戏的相关数据,如游戏状态标志
eat_mooncake
、玩家得分score
、月饼生成状态标志mooncake1
等。还定义了月饼的属性,如下落速度speed
、初始位置location_1
和mooncake_y
。对天狗的图片也进行了尺寸调整,并绘制了游戏结束时的文字。3. 游戏主循环
游戏的主循环负责处理各种事件,更新游戏状态,并绘制游戏画面。
- 事件处理:
while True: for event in pygame.event.get(): if event.type == pygame.MOUSEMOTION: x,y=event.pos if x<=0 : x=0 if x>=x_mode-sky_dog_width: x=x_mode-sky_dog_width buttons=pygame.mouse.get_pressed() if windows==True: if start_button_rect.collidepoint(event.pos) and buttons[0]: eat_mooncake=True windows=False pygame.display.update() if Trivia_button_rect.collidepoint(event.pos) and buttons[0]: Trivia=True windows=False j=0 right=False color=[(255,255,255),(255,255,255),(255,255,255),(255,255,255)] pygame.display.update() else: if backspace_rect.collidepoint(event.pos) and buttons[0]: eat_mooncake=False windows=True pygame.display.update() if event.type==pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key==K_ESCAPE: sys.exit() elif event.key == pygame.K_1: if file[j+5].strip()=='A': color[0]=(0,255,0) else : color[0]=(255,0,0) right=True elif event.key == pygame.K_2: if file[j+5].strip()=='B': color[1]=(0,255,0) else : color[1]=(255,0,0) right=True elif event.key == pygame.K_3: if file[j+5].strip()=='C': color[2]=(0,255,0) else : color[2]=(255,0,0) right=True elif event.key == pygame.K_4: if file[j+5].strip()=='D': color[3]=(0,255,0) else : color[3]=(255,0,0) right=True elif event.key == pygame.K_RETURN: j=j+6 color=[(255,255,255),(255,255,255),(255,255,255),(255,255,255)] right=False
在主循环中,首先遍历 Pygame 事件队列。当鼠标移动时,获取鼠标的位置并进行边界限制,确保天狗不会超出屏幕范围。如果处于游戏主菜单页面,当鼠标点击 “开始游戏” 按钮或 “知识问答” 按钮时,相应地设置游戏状态标志,并更新界面。如果在游戏过程中点击 “返回” 按钮,则切换回主菜单页面。当检测到退出事件或按下 Esc 键时,游戏退出。当按下数字键 1、2、3、4 时,根据对应的答案选项判断答案是否正确,并设置选项的颜色。按下回车键时,切换到下一题,并重置选项颜色。
- 绘制不同游戏状态的画面:
if windows: screen.blit(background_window,(0,0)) screen.blit(start_button,(start_button_rect)) screen.blit(Trivia_button,(Trivia_button_rect)) score=0 speed=0.1 location_1=random.randint(0,x_mode-mooncake_width) mooncake_y=-70 over=False Trivia=False if eat_mooncake: screen.blit(eat_mooncake_background,(0,0)) screen.blit(sky_dog,(x,y_mode-sky_dog_height)) screen.blit(mooncake,(location_1,mooncake_y)) text3=font2.render("你的分数为:"+str(score),True,(255,255,255)) screen.blit(text3,(0,0)) if score==15: speed=0.2 if score==65: speed=0.3 if score==100: speed=0.4 if score==200: speed=0.5 if mooncake1 : location_1=random.randint(0,x_mode-mooncake_width) mooncake1=False mooncake_y=mooncake_y+speed if mooncake_y>=y_mode-sky_dog_height and x+sky_dog_width-60>=location_1>=x-mooncake_width/2: mooncake_y=-70 mooncake1=True score=score+1 if mooncake_y>=x_mode+mooncake_height: over=True if over: eat_mooncake=False screen.blit(gameover,(0,0)) screen.blit(text,(250,500)) screen.blit(text2,(130,300)) screen.blit(text3,(200,600)) score=0 speed=0.1 if windows==False: screen.blit(backspace,(backspace_rect))
如果处于主菜单页面,绘制游戏背景图、“开始游戏” 按钮和 “知识问答” 按钮。如果处于吃月饼游戏状态,绘制吃月饼游戏的背景图、天狗、月饼和玩家的得分。根据玩家的得分调整月饼的下落速度。当月饼被天狗接住时,重置月饼的位置并增加玩家得分。当月饼落到屏幕底部时,游戏结束,绘制游戏结束的画面。如果不在主菜单页面且不是吃月饼游戏状态,绘制 “返回” 按钮。
- 知识问答部分的绘制与处理:
if Trivia: screen.fill((46,158,214)) print_text( remind,15,0,y_mode-20,(0,0,0)) print_text('中秋节知识问答',60,100,0,(255,253,85)) print_text(file[j].strip(),30,0,250,(255,255,255)) print_text(file[j+1].strip(),25,0,400,color[0]) print_text(file[j+2].strip(),25,0,500,color[1]) print_text(file[j+3].strip(),25,0,600,color[2]) print_text(file[j+4].strip(),25,0,700,color[3]) if j==84: Trivia=False screen.blit(gameover,(0,0)) screen.blit(text,(250,500)) screen.blit(text2,(130,300)) if right: print_text('正确答案:'+str(file[j+5].strip()),30,0,800,(0,255,0))
如果处于知识问答状态,绘制蓝色背景,并显示知识问答的标题、提示信息、问题和四个选项。根据答案的正确性设置选项的颜色。当回答完所有问题(
j==84
)时,结束知识问答部分,绘制游戏结束的画面。如果答案错误,显示正确答案。最后,更新屏幕显示,让玩家看到最新的游戏画面。
以下是完整代码:
import pygame,random,sys
from pygame.locals import *
pygame.init()
f = open('中秋节知识问答.txt',"rt",encoding="UTF-8", errors='ignore')
file=f.readlines()
remind='请选择与你的答案相对应的数字:1A,2B,3C,4D。填写完答案后按回车键切换下一题'
color=[(255,255,255),(255,255,255),(255,255,255),(255,255,255)]
j=0
right=False
def print_text(text,size,x,y,color):
font=pygame.font.Font("C:\\WINDOWS\\FONTS\\SIMHEI.TTF",size)
text_surface=font.render(text,True,color)
screen.blit(text_surface,(x,y))
# 绘制屏幕
x_mode=650
y_mode=975
screen=pygame.display.set_mode((650,975))
# 给窗口命名
pygame.display.set_caption("中秋节小游戏")
# 设置菜单页面状态
windows=True
Trivia=False
#加载图片
background_window=pygame.image.load("中秋游戏背景图.jpg").convert()
start_button=pygame.image.load("吃月饼游戏开始按钮.png").convert_alpha()
mooncake=pygame.image.load("月饼.png").convert_alpha()
eat_mooncake_background=pygame.image.load("吃月饼游戏背景图.jpg").convert()
sky_dog=pygame.image.load("天狗.png").convert_alpha()
gameover=pygame.image.load("游戏结束图片.jpg").convert()
backspace=pygame.image.load("返回.png").convert_alpha()
Trivia_button=pygame.image.load("中秋知识问答.png").convert_alpha()
# 量取图片
width,height=start_button.get_size()
width1,height1=Trivia_button.get_size()
# 改图片尺寸
width=width//4
height=height//4
start_button=pygame.transform.smoothscale(start_button,(width,height))
start_button_rect=start_button.get_rect(center=(500-width/2,975-275))
width1=width1//4
height1=height1//4
Trivia_button=pygame.transform.smoothscale(Trivia_button,(width1,height1))
Trivia_button_rect=Trivia_button.get_rect(center=(500-width/2,975-375))
# 量取月饼尺寸
mooncake_width,mooncake_height=mooncake.get_size()
# 改月饼尺寸
mooncake_width=mooncake_width//6
mooncake_height=mooncake_height//6
mooncake=pygame.transform.smoothscale(mooncake,(mooncake_width,mooncake_height))
# 定义吃月饼游戏数据
eat_mooncake=False
score=0
mooncake1=True
# 量返回键尺寸
backspace_x,backspace_y=backspace.get_size()
# 改返回键尺寸
backspace_width=backspace_x//10
backspace_height=backspace_y//10
backspace=pygame.transform.smoothscale(backspace,(backspace_width,backspace_height))
backspace_rect=backspace.get_rect(center=(x_mode-backspace_width,25))
# 月饼属性
speed=0.1
location_1=random.randint(0,x_mode-mooncake_width)
mooncake_y=-70
# 量天狗尺寸
sky_dog_width,sky_dog_height=sky_dog.get_size()
# 修改天狗尺寸
sky_dog_width=sky_dog_width//4
sky_dog_height=sky_dog_height//4
sky_dog=pygame.transform.smoothscale(sky_dog,(sky_dog_width,sky_dog_height))
# 绘制结束文字
font=pygame.font.Font("C:\\WINDOWS\\FONTS\\SIMHEI.TTF",80)
font2=pygame.font.Font("C:\\WINDOWS\\FONTS\\SIMHEI.TTF",30)
text=font2.render("游戏结束",True,(255,255,255))
text2=font.render("中秋节快乐!",True,(255,255,30))
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
x,y=event.pos
if x<=0 :
x=0
if x>=x_mode-sky_dog_width:
x=x_mode-sky_dog_width
buttons=pygame.mouse.get_pressed()
if windows==True:
if start_button_rect.collidepoint(event.pos) and buttons[0]:
eat_mooncake=True
windows=False
pygame.display.update()
if Trivia_button_rect.collidepoint(event.pos) and buttons[0]:
Trivia=True
windows=False
j=0
right=False
color=[(255,255,255),(255,255,255),(255,255,255),(255,255,255)]
pygame.display.update()
else:
if backspace_rect.collidepoint(event.pos) and buttons[0]:
eat_mooncake=False
windows=True
pygame.display.update()
if event.type==pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key==K_ESCAPE:
sys.exit()
elif event.key == pygame.K_1:
if file[j+5].strip()=='A':
color[0]=(0,255,0)
else :
color[0]=(255,0,0)
right=True
elif event.key == pygame.K_2:
if file[j+5].strip()=='B':
color[1]=(0,255,0)
else :
color[1]=(255,0,0)
right=True
elif event.key == pygame.K_3:
if file[j+5].strip()=='C':
color[2]=(0,255,0)
else :
color[2]=(255,0,0)
right=True
elif event.key == pygame.K_4:
if file[j+5].strip()=='D':
color[3]=(0,255,0)
else :
color[3]=(255,0,0)
right=True
elif event.key == pygame.K_RETURN:
j=j+6
color=[(255,255,255),(255,255,255),(255,255,255),(255,255,255)]
right=False
if windows:
screen.blit(background_window,(0,0))
screen.blit(start_button,(start_button_rect))
screen.blit(Trivia_button,(Trivia_button_rect))
score=0
speed=0.1
location_1=random.randint(0,x_mode-mooncake_width)
mooncake_y=-70
over=False
Trivia=False
if eat_mooncake:
screen.blit(eat_mooncake_background,(0,0))
screen.blit(sky_dog,(x,y_mode-sky_dog_height))
screen.blit(mooncake,(location_1,mooncake_y))
text3=font2.render("你的分数为:"+str(score),True,(255,255,255))
screen.blit(text3,(0,0))
if score==15:
speed=0.2
if score==65:
speed=0.3
if score==100:
speed=0.4
if score==200:
speed=0.5
if mooncake1 :
location_1=random.randint(0,x_mode-mooncake_width)
mooncake1=False
mooncake_y=mooncake_y+speed
if mooncake_y>=y_mode-sky_dog_height and x+sky_dog_width-60>=location_1>=x-mooncake_width/2:
mooncake_y=-70
mooncake1=True
score=score+1
if mooncake_y>=x_mode+mooncake_height:
over=True
if over:
eat_mooncake=False
screen.blit(gameover,(0,0))
screen.blit(text,(250,500))
screen.blit(text2,(130,300))
screen.blit(text3,(200,600))
score=0
speed=0.1
if windows==False:
screen.blit(backspace,(backspace_rect))
pygame.display.update()
if Trivia:
screen.fill((46,158,214))
print_text( remind,15,0,y_mode-20,(0,0,0))
print_text('中秋节知识问答',60,100,0,(255,253,85))
print_text(file[j].strip(),30,0,250,(255,255,255))
print_text(file[j+1].strip(),25,0,400,color[0])
print_text(file[j+2].strip(),25,0,500,color[1])
print_text(file[j+3].strip(),25,0,600,color[2])
print_text(file[j+4].strip(),25,0,700,color[3])
if j==84:
Trivia=False
screen.blit(gameover,(0,0))
screen.blit(text,(250,500))
screen.blit(text2,(130,300))
if right:
print_text('正确答案:'+str(file[j+5].strip()),30,0,800,(0,255,0))
我们的游戏到此就完工啦!祝大家中秋节快乐!