事情是这样的,昨晚隔壁老王大晚上出去喝酒,把女友一个人丢在家里,半夜都还没回来。
然后我就听到隔壁来来回回,忙忙碌碌的声音。
像我这么心思细腻,体贴入微的Python小哥哥(还好我不姓王)
敏锐的感觉到,老王他女友肯定是失眠了…
好担心哦,我都睡不着了呢
辗转反侧
最后爬起来撸出了我的python代码
windows系统,python3.6+ 安装游戏依赖模块 pip install pyqt5 pip install pygame
1、游戏目标
随机生成一张迷宫地图,将玩家设置在迷宫内部,通过光标 上 下 左 右,来移动玩家,按照迷宫地图的道路来走出迷宫。
2、先上游戏效果图
1、项目主结构
首先,先整理一下项目的主结构,其实看一下主结构,基本就清晰了
modules:存放自己写的python类
——mazes.py
——misc.py
——sprites.py
resources:存放引用到的图片、音频等等
——audios:音频资源
——images:图片资源
config.py:为主配置文件
maze.py:主程序文件
requirements.txt:需要引入的python依赖包
2、详细配置
配置文件中,需要引入os模块,并且配置打开游戏的屏幕大小,并将资源中引用到的图片、音频插入到合适的位置。
因为我们的迷宫游戏,需要划开模块。
'''配置文件''' import os '''屏幕大小''' SCREENSIZE = (800, 625) '''游戏素材''' # 完整源码+Q裙:708525271 BGMPATH = os.path.join(os.getcwd(), 'resources/audios/bgm.mp3') HEROPICPATH = os.path.join(os.getcwd(), 'resources/images/hero.png') '''FPS''' FPS = 20 '''块大小''' BLOCKSIZE = 15 MAZESIZE = (35, 50) # num_rows * num_cols BORDERSIZE = (25, 50) # 25 * 2 + 50 * 15 = 800, 50 * 2 + 35 * 15 = 625
3、随机生成迷宫地图
迷宫虽然是个小游戏,但是我们每次打开,进入 地图需要随机生成一个新地图。
定义randommaze 随机生成地图,并将地图投在主游戏屏幕上
import pygame import random from .misc import * '''一个游戏地图块''' # Python学习交流裙 708525271 class Block(): def __init__(self, coordinate, block_size, border_size, **kwargs): # (col, row) self.coordinate = coordinate self.block_size = block_size self.border_size = border_size self.is_visited = False # 上下左右有没有墙 self.has_walls = [True, True, True, True] self.color = (0, 0, 0) '''画到屏幕上''' def draw(self, screen): directions = ['top', 'bottom', 'left', 'right'] for idx, direction in enumerate(directions): if self.has_walls[idx]: if direction == 'top': x1 = self.coordinate[0] * self.block_size + self.border_size[0] y1 = self.coordinate[1] * self.block_size + self.border_size[1] x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0] y2 = self.coordinate[1] * self.block_size + self.border_size[1] pygame.draw.line(screen, self.color, (x1, y1), (x2, y2)) elif direction == 'bottom': x1 = self.coordinate[0] * self.block_size + self.border_size[0] y1 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1] x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0] y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1] pygame.draw.line(screen, self.color, (x1, y1), (x2, y2)) elif direction == 'left': x1 = self.coordinate[0] * self.block_size + self.border_size[0] y1 = self.coordinate[1] * self.block_size + self.border_size[1] x2 = self.coordinate[0] * self.block_size + self.border_size[0] y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1] pygame.draw.line(screen, self.color, (x1, y1), (x2, y2)) elif direction == 'right': x1 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0] y1 = self.coordinate[1] * self.block_size + self.border_size[1] x2 = (self.coordinate[0] + 1) * self.block_size + self.border_size[0] y2 = (self.coordinate[1] + 1) * self.block_size + self.border_size[1] pygame.draw.line(screen, self.color, (x1, y1), (x2, y2)) return True '''随机生成迷宫类''' class RandomMaze(): def __init__(self, maze_size, block_size, border_size, **kwargs): self.bl