课设作品分享
1.游戏主界面
图一
2.游戏简介
我们用pygame做成了一个初级的游戏系统,里面有扫雷和躲避方块两个游戏,其中游戏音乐可以自己设置,扫雷游戏还实现了难度选择和最佳游戏记录。这个游戏系统极大的满足了玩家的不同需求实现了个性化选择游戏。
3.游戏介绍
3.1扫雷游戏
基本规则:
游戏区包括雷区、地雷计数器(位于左上角,记录剩余地雷数)和计时器(位于右上角,记录游戏时间),确定大小的矩形雷区中随机布置一定数量的地雷(初级为9*9个方块10个雷,中级为16*16个方块40个雷,高级为16*30个方块99个雷,自定义级别可以自己设定雷区大小和雷数,但是雷区大小不能超过24*30),玩家需要尽快找出雷区中的所有不是地雷的方块,而不许踩到地雷。
游戏的基本操作包括左键单击(Left Click)、右键单击(Right Click)、双击(Chording)三种。其中左键用于打开安全的格子,推进游戏进度;右键用于标记地雷,以辅助判断,或为接下来的双击做准备;双击在一个数字周围的地雷标记完时,相当于对数字周围未打开的方块均进行一次左键单击操作:
左键单击:在判断出不是雷的方块上按下左键,可以打开该方块。如果方块上出现数字,则该数字表示其周围3×3区域中的地雷数(一般为8个格子,对于边块为5个格子,对于角块为3个格子。所以扫雷中最大的数字为8);如果方块上为空(相当于0),则可以递归地打开与空相邻的方块;如果不幸触雷,则游戏结束。
右键单击:在判断为地雷的方块上按下右键,可以标记地雷(显示为小红旗)。重复一次或两次操作可取消标记(如果在游戏菜单中勾选了“标记(?)”,则需要两次操作来取消标雷)。
双击:同时按下左键和右键完成双击。当双击位置周围已标记雷数等于该位置数字时操作有效,相当于对该数字周围未打开的方块均进行一次左键单击操作。地雷未标记完全时使用双击无效。若数字周围有标错的地雷,则游戏结束,标错的地雷上会显示一个“ ×”。
Clear_mines.py(扫雷游戏主程序)简介
我们自制的扫雷游戏还只是初级,只实现了其中最基本的功能,难度选择(不同难度棋盘大小不同),布雷和找雷(左键单击),并且点击一次只能揭示一个方块,递归和标雷,逻辑上有bug暂时无法实现。
后面加上了时间的计时功能,然后也可以记录最佳战绩。
但是我的这个自制扫雷也有亮点,就是有游戏配乐且游戏胜利后有烟花效果
图二 扫雷简单模式
3.2躲避方块游戏
这是一个使用Pygame库开发的躲避胡桃的小游戏。游戏中有一个玩家角色,通过键盘控制其在屏幕上移动,并要躲避随机移动的胡桃障碍物。玩家需要尽量避免与胡桃碰撞,否则生命值减少,当生命值用尽时游戏结束。同时,玩家可以通过收集胡桃来得分,每秒钟得一分。游戏还设置了一个彩蛋,当按下特定的按键顺序(上上下下左右左右)时,玩家将获得无敌状态。游戏界面使用了图片作为背景,并在屏幕上显示玩家的生命值和得分。当玩家生命值用尽或者成功躲避所有胡桃时,游戏结束并显示相应的背景图片。游戏还记录了最高分,如果玩家的得分超过最高分,则保存新的最高分。 (注:上为w,下为s,左为a,右为d)
图三 躲避方块游戏
设计方案介绍
总体方案
(1)问题定义:实现游戏系统包括:游戏选择及实现,菜单选择,音乐选择,烟花效果实现。
(2)问题分析:通过对“游戏系统”这一题材的分析,我需要四个包来实现其“扫雷游戏”和“躲避方块游戏”、“菜单页面”、“音乐读取”、“烟花效果”这四大功能。通过主菜单函数main_menu(),创建菜单主窗口,游戏初始化,游戏循环, 目录切换,游戏切换等功能,具体功能实现通过导入和调用我自定义的包的具体函数来实现。
(3)研究意义:通过本次课程设计,我们掌握了Python语言的基本运用,提高了理论结合实际的能力,和面向对象编程思想的能力。
(4)设计特色:这个程序全部采用模块化,专业化,对象化的编程思想,具体的功能全都是通过函数或者是类对象的实现,最终运行一个主菜单函数接口即可实现全部功能。躲避方块游戏为原创作品,风格独特。
实现功能
- 游戏选择及实现
- 菜单选择
- 音乐选择
- 烟花效果
项目逻辑框图
图四
涉及的知识点
通过这次课程设计,我们得以掌握Python语言的基本运用,提高了理论结合实际的能力和面向对象模块化编程思想的能力。设计分工合理,利用函数和类对象实现了一个复杂的游戏系统。设计方案的模块化、专业化、对象化思想使得程序的功能更加清晰有效。
主程序代码分享
main.py
from meau_modle import button_new
from music_mode import music
from game import clear_mines
from pythonProject7 import main
import pygame
DISPLAY_WIDTH = DISPLAY_HEIGHT = 800
window = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
window_rect = window.get_rect()
# 定义菜单变量
menu_state = "main"
game_loop = False
game_music = 0
pos = 150
# 定义字体
font = pygame.font.SysFont("华文楷体", 30)
font_1 = pygame.font.SysFont("华文楷体", 20)
font_2 = pygame.font.SysFont("华文楷体", 40)
# 定义颜色
TEXT_COL = (0, 0, 0)
TEXT_COL_1 = (137, 207, 240)
TEXT_COL_2 = '#00BFFF'
# 加载图片
resume_img = font.render("返回", True, TEXT_COL)
options_img = font.render("选择模式", True, TEXT_COL)
quit_img = font.render("退出程序", True, TEXT_COL)
audio_img = font.render("音乐设置", True, TEXT_COL)
easy_img = font.render('简单', True, TEXT_COL)
difficult_img = font.render('困难', True, TEXT_COL)
hell_img = font.render('地狱', True, TEXT_COL)
clear_mines_main_image = pygame.image.load('游戏主菜单.jpg')
run_img = font_2.render('躲避方块游戏', True, TEXT_COL)
mines_img = font_2.render('扫雷游戏', True, TEXT_COL)
start_img = font.render('开始游戏', True, TEXT_COL)
music1_img = font_1.render('Breath and Life (No Choir)', True, TEXT_COL_1)
music2_img = font_1.render('2.白昼之夜', True, TEXT_COL_1)
music3_img = font_1.render('3.幻昼', True, TEXT_COL_1)
music4_img = font_1.render('4.勿念他归', True, TEXT_COL_1)
music5_img = font_1.render('5.Fade', True, TEXT_COL_1)
music6_img = font_1.render('6.A Little Story', True, TEXT_COL_1)
music7_img = font_1.render('7.卡农钢琴曲', True, TEXT_COL_1)
music8_img = font_1.render('8.皎洁的笑颜', True, TEXT_COL_1)
music9_img = font_1.render('9.Windy Hill', True, TEXT_COL_1)
music10_img = font_1.render('10.森林', True, TEXT_COL_1)
# 创建实例
resume_button = button_new.Button(336, 125, resume_img, 1)
options_button = button_new.Button(350, 350, options_img, 1)
quit_button = button_new.Button(436, 475, quit_img, 1)
audio_button = button_new.Button(336, 200, audio_img, 1)
easy_button = button_new.Button(300, 200, easy_img, 1)
difficult_button = button_new.Button(300, 250, difficult_img, 1)
hell_button = button_new.Button(300, 300, hell_img, 1)
running_button = button_new.Button(250,250, run_img, 1)
mines_button = button_new.Button(300, 350, mines_img, 1)
start_button = button_new.Button(350, 300, start_img, 1)
# 音乐实例
music1_img_button = button_new.Button(100, pos, music1_img, 1)
music2_img_button = button_new.Button(100, pos+30, music2_img, 1)
music3_img_button = button_new.Button(100, pos+60, music3_img, 1)
music4_img_button = button_new.Button(100, pos+90, music4_img, 1)
music5_img_button = button_new.Button(100, pos+120, music5_img, 1)
music6_img_button = button_new.Button(100, pos+150, music6_img, 1)
music7_img_button = button_new.Button(100, pos+180, music7_img, 1)
music8_img_button = button_new.Button(100, pos+210, music8_img, 1)
music9_img_button = button_new.Button(100, pos+240, music9_img, 1)
music10_img_button = button_new.Button(100, pos+270, music10_img, 1)
#pygame写字函数(把字写在pygame的窗口上)
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)#所写的字的图片
window.blit(img, (x, y))#执行写操作
def start_menu():
# 外部全局变量声明
global game_loop
global menu_state
if menu_state == 'main':#开始界面主菜单
draw_text("请选择游戏:", font_2, TEXT_COL_2, 250, 500)
music.play_music(game_music)#播放游戏音乐
if running_button.draw(window):
game_loop = True#进入游戏循环
menu_state = "running"#切换躲避方块游戏
pygame.display.set_caption("躲避方块游戏")
print('*'*20)
print('躲避方块游戏')
print('*'*20)
elif mines_button.draw(window):
game_loop = True # 进入游戏循环
menu_state = "clear_mines_main" # 切换扫雷游戏
pygame.display.set_caption("扫雷游戏")
print('*'*20)
print('扫雷游戏')
print('*'*20)
def main_menu():
pygame.init() #游戏初始化
pygame.display.set_caption("小游戏")#游戏窗口改名
#外部全局变量声明
global game_loop
global menu_state
run = True
#游戏菜单循环
while run:
#菜单主页面图片
image_rect = clear_mines_main_image.get_rect()
image_rect.center = window_rect.center
window.blit(clear_mines_main_image, image_rect)
start_menu()#开始界面函数
# 判断游戏是否继续
if game_loop:
#判断菜单状态
if menu_state == "running":
if resume_button.draw(window):
music.stop_music()#暂停播放音乐
menu_state = 'main'#切换至主菜单
game_loop = False#执行切换
elif audio_button.draw(window):
menu_state = "music"#切换至音乐菜单
elif quit_button.draw(window):
run = False#退出程序
elif start_button.draw(window):
main.hutao()#运行躲避方块游戏
music.stop_music()#游戏结束音乐暂停
break
# 判断菜单状态
elif menu_state == "clear_mines_main":
# 在屏幕画按钮
if resume_button.draw(window):
menu_state = 'main'
music.stop_music()
game_loop = False#同上
elif options_button.draw(window):
draw_difficulty_menu()#难度选择函数
elif audio_button.draw(window):
menu_state = "music" # 切换至音乐菜单
elif quit_button.draw(window):
run = False # 退出程序
elif menu_state == 'music':
pygame.display.set_caption("音乐切换")
music.stop_music()#音乐暂停
music_menu()#音乐菜单函数
menu_state = "main"
# music.stop_music()#音乐暂停
elif menu_state == 'easy':
clear_mines.easy()#扫雷简单模式
break
elif menu_state == 'difficulty':
clear_mines.difficulty() # 扫雷困难模式
break
elif menu_state == 'hell':
clear_mines.hell() # 扫雷地狱模式
break
# 事件处理器
for event in pygame.event.get():#获取事件
if event.type == pygame.QUIT:#如果点击了退出
run = False # 退出程序
pygame.display.update()#刷新页面
def music_menu():
tmp = True
# 外部全局变量声明
global menu_state
global game_music
# 音乐菜单循环
while tmp:
# 事件处理器
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()#退出程序,同上
image_rect = clear_mines_main_image.get_rect()
image_rect.center = window_rect.center
window.blit(clear_mines_main_image, image_rect)
if music1_img_button.draw(window):
game_music=10
print('-'*20)
print('切换音乐:Monody (纯钢琴版)')
print('-'*20)
tmp = False
elif music2_img_button.draw(window):
game_music = 1
print('-'*20)
print('切换音乐:白昼之夜')
print('-'*20)
tmp = False
elif music3_img_button.draw(window):
game_music = 2
print('-'*20)
print('切换音乐:幻昼')
print('-'*20)
tmp = False
elif music4_img_button.draw(window):
game_music = 3
print('-'*20)
print('切换音乐:勿念他归')
print('-'*20)
tmp = False
elif music5_img_button.draw(window):
game_music = 4
print('-'*20)
print('切换音乐:Fade')
print('-'*20)
tmp = False
elif music6_img_button.draw(window):
game_music = 5
print('-'*20)
print('切换音乐:A Little Story')
print('-'*20)
tmp = False
elif music7_img_button.draw(window):
game_music = 6
print('-'*20)
print('切换音乐:卡农钢琴曲')
print('-'*20)
tmp = False
elif music8_img_button.draw(window):
game_music = 7
print('-'*20)
print('切换音乐:皎洁的笑颜')
print('-'*20)
tmp = False
elif music9_img_button.draw(window):
game_music = 8
print('-'*20)
print('切换音乐:Windy Hill')
print('-'*20)
tmp = False
elif music10_img_button.draw(window):
game_music = 9
print('-'*20)
print('切换音乐:森林')
print('-'*20)
tmp = False
draw_text("请点击音乐(1~10)", font, (255, 255, 255), 100, pos+350)
if resume_button.draw(window):
menu_state == "main"
tmp = False#退出循环
pygame.display.update()
def draw_difficulty_menu():
loop = True
global menu_state
while loop:
# 事件处理器
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
image_rect = clear_mines_main_image.get_rect()
image_rect.center = window_rect.center
window.blit(clear_mines_main_image, image_rect)
# 显示简单、困难和地狱的按钮
if easy_button.draw(window):
menu_state = "easy"#切换菜单状态
loop = False # 退出循环
elif difficult_button.draw(window):
menu_state = "difficulty"
loop = False
elif hell_button.draw(window):
menu_state = "hell"
loop = False
# 显示返回主菜单的按钮
elif resume_button.draw(window):
menu_state = "clear_mines_main"
loop = False
pygame.display.update() # 刷新页面
main_menu()
完整项目源码
链接:https://pan.baidu.com/s/1aKToTraRPET6aJG0GGoLCQ?pwd=9vd0 提取码:9vd0
这是本up的第一篇文章有收获的话点个关注,谢谢,之后我还会更新其他系列的项目哦!