翼度科技»论坛 编程开发 python 查看内容

隔壁老王出喝酒去了,留下女友半夜一个人在家,我用python给她写了一个...

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
事情是这样的,昨晚隔壁老王大晚上出去喝酒,把女友一个人丢在家里,半夜都还没回来。
然后我就听到隔壁来来回回,忙忙碌碌的声音。
像我这么心思细腻,体贴入微的Python小哥哥(还好我不姓王)
 

 
 

敏锐的感觉到,老王他女友肯定是失眠了…

 
 

好担心哦,我都睡不着了呢
辗转反侧
最后爬起来撸出了我的python代码

 
 
环境要求
  1. windows系统,python3.6+
  2. 安装游戏依赖模块
  3. pip install pyqt5
  4. pip install pygame
复制代码
 
游戏介绍

1、游戏目标
随机生成一张迷宫地图,将玩家设置在迷宫内部,通过光标 上 下 左 右,来移动玩家,按照迷宫地图的道路来走出迷宫。
2、先上游戏效果图

完整开发流程

1、项目主结构
首先,先整理一下项目的主结构,其实看一下主结构,基本就清晰了
  1. modules:存放自己写的python类
  2. ——mazes.py
  3. ——misc.py
  4. ——sprites.py
  5. resources:存放引用到的图片、音频等等
  6. ——audios:音频资源
  7. ——images:图片资源
  8. config.py:为主配置文件
  9. maze.py:主程序文件
  10. requirements.txt:需要引入的python依赖包
复制代码
 

2、详细配置
配置文件中,需要引入os模块,并且配置打开游戏的屏幕大小,并将资源中引用到的图片、音频插入到合适的位置。
因为我们的迷宫游戏,需要划开模块。
  1. '''配置文件'''
  2. import os
  3. '''屏幕大小'''
  4. SCREENSIZE = (800, 625)
  5. '''游戏素材'''
  6. # 完整源码+Q裙:708525271
  7. BGMPATH = os.path.join(os.getcwd(), 'resources/audios/bgm.mp3')
  8. HEROPICPATH = os.path.join(os.getcwd(), 'resources/images/hero.png')
  9. '''FPS'''
  10. FPS = 20
  11. '''块大小'''
  12. BLOCKSIZE = 15
  13. MAZESIZE = (35, 50) # num_rows * num_cols
  14. BORDERSIZE = (25, 50) # 25 * 2 + 50 * 15 = 800, 50 * 2 + 35 * 15 = 625
复制代码
 
3、随机生成迷宫地图
迷宫虽然是个小游戏,但是我们每次打开,进入 地图需要随机生成一个新地图。
定义randommaze 随机生成地图,并将地图投在主游戏屏幕上
  1. import pygame
  2. import random
  3. from .misc import *
  4. '''一个游戏地图块'''
  5. # Python学习交流裙 708525271
  6. class Block():
  7.     def __init__(self, coordinate, block_size, border_size, **kwargs):
  8.         # (col, row)
  9.         self.coordinate = coordinate
  10.         self.block_size = block_size
  11.         self.border_size = border_size
  12.         self.is_visited = False
  13.         # 上下左右有没有墙
  14.         self.has_walls = [True, True, True, True]
  15.         self.color = (0, 0, 0)
  16.     '''画到屏幕上'''
  17.     def draw(self, screen):
  18.         directions = ['top', 'bottom', 'left', 'right']
  19.         for idx, direction in enumerate(directions):
  20.             if self.has_walls[idx]:
  21.                 if direction == 'top':
  22.                     x1 = self.coordinate[0] * self.block_size + self.border_size[0]
  23.                     y1 = self.coordinate[1] * self.block_size + self.border_size[1]
  24.                     x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
  25.                     y2 = self.coordinate[1] * self.block_size + self.border_size[1]
  26.                     pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  27.                 elif direction == 'bottom':
  28.                     x1 = self.coordinate[0] * self.block_size + self.border_size[0]
  29.                     y1 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
  30.                     x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
  31.                     y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
  32.                     pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  33.                 elif direction == 'left':
  34.                     x1 = self.coordinate[0] * self.block_size + self.border_size[0]
  35.                     y1 = self.coordinate[1] * self.block_size + self.border_size[1]
  36.                     x2 = self.coordinate[0] * self.block_size + self.border_size[0]
  37.                     y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
  38.                     pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  39.                 elif direction == 'right':
  40.                     x1 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
  41.                     y1 = self.coordinate[1] * self.block_size + self.border_size[1]
  42.                     x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0]
  43.                     y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1]
  44.                     pygame.draw.line(screen, self.color, (x1, y1), (x2, y2))
  45.         return True
  46. '''随机生成迷宫类'''
  47. class RandomMaze():
  48.     def __init__(self, maze_size, block_size, border_size, **kwargs):
  49.         self.block_size = block_size
  50.         self.border_size = border_size
  51.         self.maze_size = maze_size
  52.         self.blocks_list = RandomMaze.createMaze(maze_size, block_size, border_size)
  53.         self.font = pygame.font.SysFont('Consolas', 15)
  54.     '''画到屏幕上'''
  55.     def draw(self, screen):
  56.         for row in range(self.maze_size[0]):
  57.             for col in range(self.maze_size[1]):
  58.                 self.blocks_list[row][col].draw(screen)
  59.         # 起点和终点标志
  60.         showText(screen, self.font, 'S', (255, 0, 0), (self.border_size[0]-10, self.border_size[1]))
  61.         showText(screen, self.font, 'D', (255, 0, 0), (self.border_size[0]+(self.maze_size[1]-1)*self.block_size, self.border_size[1]+self.maze_size[0]*self.block_size+5))
  62.     '''创建迷宫'''
  63.     @staticmethod
  64.     def createMaze(maze_size, block_size, border_size):
  65.         def nextBlock(block_now, blocks_list):
  66.             directions = ['top', 'bottom', 'left', 'right']
  67.             blocks_around = dict(zip(directions, [None]*4))
  68.             block_next = None
  69.             count = 0
  70.             # 查看上边block
  71.             if block_now.coordinate[1]-1 >= 0:
  72.                 block_now_top = blocks_list[block_now.coordinate[1]-1][block_now.coordinate[0]]
  73.                 if not block_now_top.is_visited:
  74.                     blocks_around['top'] = block_now_top
  75.                     count += 1
  76.             # 查看下边block
  77.             if block_now.coordinate[1]+1 < maze_size[0]:
  78.                 block_now_bottom = blocks_list[block_now.coordinate[1]+1][block_now.coordinate[0]]
  79.                 if not block_now_bottom.is_visited:
  80.                     blocks_around['bottom'] = block_now_bottom
  81.                     count += 1
  82.             # 查看左边block
  83.             if block_now.coordinate[0]-1 >= 0:
  84.                 block_now_left = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]-1]
  85.                 if not block_now_left.is_visited:
  86.                     blocks_around['left'] = block_now_left
  87.                     count += 1
  88.             # 查看右边block
  89.             if block_now.coordinate[0]+1 < maze_size[1]:
  90.                 block_now_right = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]+1]
  91.                 if not block_now_right.is_visited:
  92.                     blocks_around['right'] = block_now_right
  93.                     count += 1
  94.             if count > 0:
  95.                 while True:
  96.                     direction = random.choice(directions)
  97.                     if blocks_around.get(direction):
  98.                         block_next = blocks_around.get(direction)
  99.                         if direction == 'top':
  100.                             block_next.has_walls[1] = False
  101.                             block_now.has_walls[0] = False
  102.                         elif direction == 'bottom':
  103.                             block_next.has_walls[0] = False
  104.                             block_now.has_walls[1] = False
  105.                         elif direction == 'left':
  106.                             block_next.has_walls[3] = False
  107.                             block_now.has_walls[2] = False
  108.                         elif direction == 'right':
  109.                             block_next.has_walls[2] = False
  110.                             block_now.has_walls[3] = False
  111.                         break
  112.             return block_next
  113.         blocks_list = [[Block([col, row], block_size, border_size) for col in range(maze_size[1])] for row in range(maze_size[0])]
  114.         block_now = blocks_list[0][0]
  115.         records = []
  116.         while True:
  117.             if block_now:
  118.                 if not block_now.is_visited:
  119.                     block_now.is_visited = True
  120.                     records.append(block_now)
  121.                 block_now = nextBlock(block_now, blocks_list)
  122.             else:
  123.                 block_now = records.pop()
  124.                 if len(records) == 0:
  125.                     break
  126.         return blocks_list
复制代码
 
4、光标控制玩家
通过读取键盘的上下左右光标来移动我们的小可爱
  1. '''
  2. Function:
  3.     定义其他必要模块
  4. Author:
  5.     lexsaints
  6. '''
  7. import sys
  8. import pygame
  9. '''在屏幕指定位置显示文字'''
  10. def showText(screen, font, text, color, position):
  11.     text_render = font.render(text, True, color)
  12.     rect = text_render.get_rect()
  13.     rect.left, rect.top = position
  14.     screen.blit(text_render, rect)
  15.     return rect.right
  16. '''按钮'''
  17. def Button(screen, position, text, font, buttoncolor=(120, 120, 120), linecolor=(20, 20, 20), textcolor=(255, 255, 255), bwidth=200, bheight=50):
  18.     left, top = position
  19.     pygame.draw.line(screen, linecolor, (left, top), (left+bwidth, top), 5)
  20.     pygame.draw.line(screen, linecolor, (left, top-2), (left, top+bheight), 5)
  21.     pygame.draw.line(screen, linecolor, (left, top+bheight), (left+bwidth, top+bheight), 5)
  22.     pygame.draw.line(screen, linecolor, (left+bwidth, top+bheight), (left+bwidth, top), 5)
  23.     pygame.draw.rect(screen, buttoncolor, (left, top, bwidth, bheight))
  24.     text_render = font.render(text, 1, textcolor)
  25.     rect = text_render.get_rect()
  26.     rect.centerx, rect.centery = left + bwidth / 2, top + bheight / 2
  27.     return screen.blit(text_render, rect)
  28. '''游戏开始/关卡切换/游戏结束界面'''
  29. def Interface(screen, config, mode='game_start'):
  30.     pygame.display.set_mode(config.SCREENSIZE)
  31.     font = pygame.font.SysFont('Consolas', 30)
  32.     if mode == 'game_start':
  33.         clock = pygame.time.Clock()
  34.         while True:
  35.             screen.fill((192, 192, 192))
  36.             button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'START', font)
  37.             button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font)
  38.             for event in pygame.event.get():
  39.                 if event.type == pygame.QUIT:
  40.                     pygame.quit()
  41.                     sys.exit(-1)
  42.                 elif event.type == pygame.MOUSEBUTTONDOWN:
  43.                     if button_1.collidepoint(pygame.mouse.get_pos()):
  44.                         return True
  45.                     elif button_2.collidepoint(pygame.mouse.get_pos()):
  46.                         pygame.quit()
  47.                         sys.exit(-1)
  48.             pygame.display.update()
  49.             clock.tick(config.FPS)
  50.     elif mode == 'game_switch':
  51.         clock = pygame.time.Clock()
  52.         while True:
  53.             screen.fill((192, 192, 192))
  54.             button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'NEXT', font)
  55.             button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font)
  56.             for event in pygame.event.get():
  57.                 if event.type == pygame.QUIT:
  58.                     pygame.quit()
  59.                     sys.exit(-1)
  60.                 elif event.type == pygame.MOUSEBUTTONDOWN:
  61.                     if button_1.collidepoint(pygame.mouse.get_pos()):
  62.                         return True
  63.                     elif button_2.collidepoint(pygame.mouse.get_pos()):
  64.                         pygame.quit()
  65.                         sys.exit(-1)
  66.             pygame.display.update()
  67.             clock.tick(config.FPS)
  68.     elif mode == 'game_end':
  69.         clock = pygame.time.Clock()
  70.         while True:
  71.             screen.fill((192, 192, 192))
  72.             button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'RESTART', font)
  73.             button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font)
  74.             for event in pygame.event.get():
  75.                 if event.type == pygame.QUIT:
  76.                     pygame.quit()
  77.                     sys.exit(-1)
  78.                 elif event.type == pygame.MOUSEBUTTONDOWN:
  79.                     if button_1.collidepoint(pygame.mouse.get_pos()):
  80.                         return True
  81.                     elif button_2.collidepoint(pygame.mouse.get_pos()):
  82.                         pygame.quit()
  83.                         sys.exit(-1)
  84.             pygame.display.update()
  85.             clock.tick(config.FPS)
  86.     else:
  87.         raise ValueError('Interface.mode unsupport %s...' % mode)
复制代码
 
5、定义主玩家 绘制全图
绘制完整游戏,并定义主角,就叫hero吧
  1. '''
  2. Function:
  3.     定义游戏精灵类
  4. Author:
  5.     lexsaints
  6. '''
  7. import pygame
  8. '''定义hero'''
  9. class Hero(pygame.sprite.Sprite):
  10.     def __init__(self, imagepath, coordinate, block_size, border_size, **kwargs):
  11.         pygame.sprite.Sprite.__init__(self)
  12.         self.image = pygame.image.load(imagepath)
  13.         self.image = pygame.transform.scale(self.image, (block_size, block_size))
  14.         self.rect = self.image.get_rect()
  15.         self.rect.left, self.rect.top = coordinate[0] * block_size + border_size[0], coordinate[1] * block_size + border_size[1]
  16.         self.coordinate = coordinate
  17.         self.block_size = block_size
  18.         self.border_size = border_size
  19.     '''移动'''
  20.     def move(self, direction, maze):
  21.         blocks_list = maze.blocks_list
  22.         if direction == 'up':
  23.             if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[0]:
  24.                 return False
  25.             else:
  26.                 self.coordinate[1] = self.coordinate[1] - 1
  27.                 return True
  28.         elif direction == 'down':
  29.             if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[1]:
  30.                 return False
  31.             else:
  32.                 self.coordinate[1] = self.coordinate[1] + 1
  33.                 return True
  34.         elif direction == 'left':
  35.             if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[2]:
  36.                 return False
  37.             else:
  38.                 self.coordinate[0] = self.coordinate[0] - 1
  39.                 return True
  40.         elif direction == 'right':
  41.             if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[3]:
  42.                 return False
  43.             else:
  44.                 self.coordinate[0] = self.coordinate[0] + 1
  45.                 return True
  46.         else:
  47.             raise ValueError('Unsupport direction %s in Hero.move...' % direction)
  48.     '''绑定到屏幕'''
  49.     def draw(self, screen):
  50.         self.rect.left, self.rect.top = self.coordinate[0] * self.block_size + self.border_size[0], self.coordinate[1] * self.block_size + self.border_size[1]
  51.         screen.blit(self.image, self.rect)
复制代码
 
6、引入音频、图片
启动游戏主程序
在主程序中,通过读取配置文件,引入项目资源:包括图片、音频等,并通过定义类,加载游戏地图
  1. import config
  2. import sys
  3. import pygame
  4. from modules import *
  5. '''主函数'''
  6. def main(config):
  7.     # 初始化
  8.     pygame.init()
  9.     pygame.mixer.init()
  10.     pygame.font.init()
  11.     pygame.mixer.music.load(config.BGMPATH)
  12.     pygame.mixer.music.play(-1, 0.0)
  13.     screen = pygame.display.set_mode(config.SCREENSIZE)
  14.     pygame.display.set_caption('Python学习交流Q裙708525271')
  15.     font = pygame.font.SysFont('Consolas', 15)
  16.     # 开始界面
  17.     Interface(screen, config, 'game_start')
  18.     # 记录关卡数
  19.     num_levels = 0
  20.     # 记录最少用了多少步通关
  21.     best_scores = 'None'
  22.     # 关卡循环切换
  23.     while True:
  24.         num_levels += 1
  25.         clock = pygame.time.Clock()
  26.         screen = pygame.display.set_mode(config.SCREENSIZE)
  27.         # --随机生成关卡地图
  28.         maze_now = RandomMaze(config.MAZESIZE, config.BLOCKSIZE, config.BORDERSIZE)
  29.         # --生成hero
  30.         hero_now = Hero(config.HEROPICPATH, [0, 0], config.BLOCKSIZE, config.BORDERSIZE)
  31.         # --统计步数
  32.         num_steps = 0
  33.         # --关卡内主循环
  34.         while True:
  35.             dt = clock.tick(config.FPS)
  36.             screen.fill((255, 255, 255))
  37.             is_move = False
  38.             # ----↑↓←→控制hero
  39.             for event in pygame.event.get():
  40.                 if event.type == pygame.QUIT:
  41.                     pygame.quit()
  42.                     sys.exit(-1)
  43.                 elif event.type == pygame.KEYDOWN:
  44.                     if event.key == pygame.K_UP:
  45.                         is_move = hero_now.move('up', maze_now)
  46.                     elif event.key == pygame.K_DOWN:
  47.                         is_move = hero_now.move('down', maze_now)
  48.                     elif event.key == pygame.K_LEFT:
  49.                         is_move = hero_now.move('left', maze_now)
  50.                     elif event.key == pygame.K_RIGHT:
  51.                         is_move = hero_now.move('right', maze_now)
  52.             num_steps += int(is_move)
  53.             hero_now.draw(screen)
  54.             maze_now.draw(screen)
  55.             # ----显示一些信息
  56.             showText(screen, font, 'LEVELDONE: %d' % num_levels, (255, 0, 0), (10, 10))
  57.             showText(screen, font, 'BESTSCORE: %s' % best_scores, (255, 0, 0), (210, 10))
  58.             showText(screen, font, 'USEDSTEPS: %s' % num_steps, (255, 0, 0), (410, 10))
  59.             showText(screen, font, 'S: your starting point    D: your destination', (255, 0, 0), (10, 600))
  60.             # ----判断游戏是否胜利
  61.             if (hero_now.coordinate[0] == config.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == config.MAZESIZE[0] - 1):
  62.                 break
  63.             pygame.display.update()
  64.         # --更新最优成绩
  65.         if best_scores == 'None':
  66.             best_scores = num_steps
  67.         else:
  68.             if best_scores > num_steps:
  69.                 best_scores = num_steps
  70.         # --关卡切换
  71.         Interface(screen, config, mode='game_switch')
  72. '''run'''
  73. if __name__ == '__main__':
  74.     main(config)
复制代码
 
游戏启动方法

1、开发工具启动
如果你配置了开发工具的环境VScode、sublimeText、notepad+、pycharm什么的,可以直接在工具中,运行游戏。
如果没配置,可以使用命令启动。
2、命令行启动 gif

最后

好了,今天的分享就到这结束了,我要继续去隔壁安慰老王女友了。
大家记得点赞收藏!

来源:https://www.cnblogs.com/hahaa/p/17212623.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具