事情是這樣的,昨晚隔壁老王大晚上出去喝酒,把女友一個人丟在家裡,半夜都還沒回來。 然後我就聽到隔壁來來回回,忙忙碌碌的聲音。 像我這麼心思細膩,體貼入微的Python小哥哥(還好我不姓王) 敏銳的感覺到,老王他女友肯定是失眠了… 好擔心哦,我都睡不著了呢 輾轉反側 最後爬起來擼出了我的python代 ...
事情是這樣的,昨晚隔壁老王大晚上出去喝酒,把女友一個人丟在家裡,半夜都還沒回來。
然後我就聽到隔壁來來回回,忙忙碌碌的聲音。
像我這麼心思細膩,體貼入微的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.block_size = block_size self.border_size = border_size self.maze_size = maze_size self.blocks_list = RandomMaze.createMaze(maze_size, block_size, border_size) self.font = pygame.font.SysFont('Consolas', 15) '''畫到屏幕上''' def draw(self, screen): for row in range(self.maze_size[0]): for col in range(self.maze_size[1]): self.blocks_list[row][col].draw(screen) # 起點和終點標誌 showText(screen, self.font, 'S', (255, 0, 0), (self.border_size[0]-10, self.border_size[1])) 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)) '''創建迷宮''' @staticmethod def createMaze(maze_size, block_size, border_size): def nextBlock(block_now, blocks_list): directions = ['top', 'bottom', 'left', 'right'] blocks_around = dict(zip(directions, [None]*4)) block_next = None count = 0 # 查看上邊block if block_now.coordinate[1]-1 >= 0: block_now_top = blocks_list[block_now.coordinate[1]-1][block_now.coordinate[0]] if not block_now_top.is_visited: blocks_around['top'] = block_now_top count += 1 # 查看下邊block if block_now.coordinate[1]+1 < maze_size[0]: block_now_bottom = blocks_list[block_now.coordinate[1]+1][block_now.coordinate[0]] if not block_now_bottom.is_visited: blocks_around['bottom'] = block_now_bottom count += 1 # 查看左邊block if block_now.coordinate[0]-1 >= 0: block_now_left = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]-1] if not block_now_left.is_visited: blocks_around['left'] = block_now_left count += 1 # 查看右邊block if block_now.coordinate[0]+1 < maze_size[1]: block_now_right = blocks_list[block_now.coordinate[1]][block_now.coordinate[0]+1] if not block_now_right.is_visited: blocks_around['right'] = block_now_right count += 1 if count > 0: while True: direction = random.choice(directions) if blocks_around.get(direction): block_next = blocks_around.get(direction) if direction == 'top': block_next.has_walls[1] = False block_now.has_walls[0] = False elif direction == 'bottom': block_next.has_walls[0] = False block_now.has_walls[1] = False elif direction == 'left': block_next.has_walls[3] = False block_now.has_walls[2] = False elif direction == 'right': block_next.has_walls[2] = False block_now.has_walls[3] = False break return block_next blocks_list = [[Block([col, row], block_size, border_size) for col in range(maze_size[1])] for row in range(maze_size[0])] block_now = blocks_list[0][0] records = [] while True: if block_now: if not block_now.is_visited: block_now.is_visited = True records.append(block_now) block_now = nextBlock(block_now, blocks_list) else: block_now = records.pop() if len(records) == 0: break return blocks_list
4、游標控制玩家
通過讀取鍵盤的上下左右游標來移動我們的小可愛
''' Function: 定義其他必要模塊 Author: lexsaints ''' import sys import pygame '''在屏幕指定位置顯示文字''' def showText(screen, font, text, color, position): text_render = font.render(text, True, color) rect = text_render.get_rect() rect.left, rect.top = position screen.blit(text_render, rect) return rect.right '''按鈕''' def Button(screen, position, text, font, buttoncolor=(120, 120, 120), linecolor=(20, 20, 20), textcolor=(255, 255, 255), bwidth=200, bheight=50): left, top = position pygame.draw.line(screen, linecolor, (left, top), (left+bwidth, top), 5) pygame.draw.line(screen, linecolor, (left, top-2), (left, top+bheight), 5) pygame.draw.line(screen, linecolor, (left, top+bheight), (left+bwidth, top+bheight), 5) pygame.draw.line(screen, linecolor, (left+bwidth, top+bheight), (left+bwidth, top), 5) pygame.draw.rect(screen, buttoncolor, (left, top, bwidth, bheight)) text_render = font.render(text, 1, textcolor) rect = text_render.get_rect() rect.centerx, rect.centery = left + bwidth / 2, top + bheight / 2 return screen.blit(text_render, rect) '''游戲開始/關卡切換/游戲結束界面''' def Interface(screen, config, mode='game_start'): pygame.display.set_mode(config.SCREENSIZE) font = pygame.font.SysFont('Consolas', 30) if mode == 'game_start': clock = pygame.time.Clock() while True: screen.fill((192, 192, 192)) button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'START', font) button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(-1) elif event.type == pygame.MOUSEBUTTONDOWN: if button_1.collidepoint(pygame.mouse.get_pos()): return True elif button_2.collidepoint(pygame.mouse.get_pos()): pygame.quit() sys.exit(-1) pygame.display.update() clock.tick(config.FPS) elif mode == 'game_switch': clock = pygame.time.Clock() while True: screen.fill((192, 192, 192)) button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'NEXT', font) button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(-1) elif event.type == pygame.MOUSEBUTTONDOWN: if button_1.collidepoint(pygame.mouse.get_pos()): return True elif button_2.collidepoint(pygame.mouse.get_pos()): pygame.quit() sys.exit(-1) pygame.display.update() clock.tick(config.FPS) elif mode == 'game_end': clock = pygame.time.Clock() while True: screen.fill((192, 192, 192)) button_1 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//3), 'RESTART', font) button_2 = Button(screen, ((config.SCREENSIZE[0]-200)//2, config.SCREENSIZE[1]//2), 'QUIT', font) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(-1) elif event.type == pygame.MOUSEBUTTONDOWN: if button_1.collidepoint(pygame.mouse.get_pos()): return True elif button_2.collidepoint(pygame.mouse.get_pos()): pygame.quit() sys.exit(-1) pygame.display.update() clock.tick(config.FPS) else: raise ValueError('Interface.mode unsupport %s...' % mode)
5、定義主玩家 繪製全圖
繪製完整游戲,並定義主角,就叫hero吧
''' Function: 定義游戲精靈類 Author: lexsaints ''' import pygame '''定義hero''' class Hero(pygame.sprite.Sprite): def __init__(self, imagepath, coordinate, block_size, border_size, **kwargs): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(imagepath) self.image = pygame.transform.scale(self.image, (block_size, block_size)) self.rect = self.image.get_rect() self.rect.left, self.rect.top = coordinate[0] * block_size + border_size[0], coordinate[1] * block_size + border_size[1] self.coordinate = coordinate self.block_size = block_size self.border_size = border_size '''移動''' def move(self, direction, maze): blocks_list = maze.blocks_list if direction == 'up': if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[0]: return False else: self.coordinate[1] = self.coordinate[1] - 1 return True elif direction == 'down': if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[1]: return False else: self.coordinate[1] = self.coordinate[1] + 1 return True elif direction == 'left': if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[2]: return False else: self.coordinate[0] = self.coordinate[0] - 1 return True elif direction == 'right': if blocks_list[self.coordinate[1]][self.coordinate[0]].has_walls[3]: return False else: self.coordinate[0] = self.coordinate[0] + 1 return True else: raise ValueError('Unsupport direction %s in Hero.move...' % direction) '''綁定到屏幕''' def draw(self, screen): 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] screen.blit(self.image, self.rect)
6、引入音頻、圖片
啟動游戲主程式
在主程式中,通過讀取配置文件,引入項目資源:包括圖片、音頻等,並通過定義類,載入游戲地圖
import config import sys import pygame from modules import * '''主函數''' def main(config): # 初始化 pygame.init() pygame.mixer.init() pygame.font.init() pygame.mixer.music.load(config.BGMPATH) pygame.mixer.music.play(-1, 0.0) screen = pygame.display.set_mode(config.SCREENSIZE) pygame.display.set_caption('Python學習交流Q裙708525271') font = pygame.font.SysFont('Consolas', 15) # 開始界面 Interface(screen, config, 'game_start') # 記錄關卡數 num_levels = 0 # 記錄最少用了多少步通關 best_scores = 'None' # 關卡迴圈切換 while True: num_levels += 1 clock = pygame.time.Clock() screen = pygame.display.set_mode(config.SCREENSIZE) # --隨機生成關卡地圖 maze_now = RandomMaze(config.MAZESIZE, config.BLOCKSIZE, config.BORDERSIZE) # --生成hero hero_now = Hero(config.HEROPICPATH, [0, 0], config.BLOCKSIZE, config.BORDERSIZE) # --統計步數 num_steps = 0 # --關卡內主迴圈 while True: dt = clock.tick(config.FPS) screen.fill((255, 255, 255)) is_move = False # ----↑↓←→控制hero for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(-1) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: is_move = hero_now.move('up', maze_now) elif event.key == pygame.K_DOWN: is_move = hero_now.move('down', maze_now) elif event.key == pygame.K_LEFT: is_move = hero_now.move('left', maze_now) elif event.key == pygame.K_RIGHT: is_move = hero_now.move('right', maze_now) num_steps += int(is_move) hero_now.draw(screen) maze_now.draw(screen) # ----顯示一些信息 showText(screen, font, 'LEVELDONE: %d' % num_levels, (255, 0, 0), (10, 10)) showText(screen, font, 'BESTSCORE: %s' % best_scores, (255, 0, 0), (210, 10)) showText(screen, font, 'USEDSTEPS: %s' % num_steps, (255, 0, 0), (410, 10)) showText(screen, font, 'S: your starting point D: your destination', (255, 0, 0), (10, 600)) # ----判斷游戲是否勝利 if (hero_now.coordinate[0] == config.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == config.MAZESIZE[0] - 1): break pygame.display.update() # --更新最優成績 if best_scores == 'None': best_scores = num_steps else: if best_scores > num_steps: best_scores = num_steps # --關卡切換 Interface(screen, config, mode='game_switch') '''run''' if __name__ == '__main__': main(config)
游戲啟動方法
1、開發工具啟動
如果你配置了開發工具的環境VScode、sublimeText、notepad+、pycharm什麼的,可以直接在工具中,運行游戲。
如果沒配置,可以使用命令啟動。
2、命令行啟動 gif
最後
好了,今天的分享就到這結束了,我要繼續去隔壁安慰老王女友了。
大家記得點贊收藏!