python小游戏——走出迷宫代码开源

简介: python小游戏——走出迷宫代码开源

一,游戏代码效果呈现

二,游戏主代码

'''
Function:
    走迷宫小游戏
Author:
    Charles
'''
import cfg
import sys
import pygame
from modules import *
'''主函数'''
def main(cfg):
    # 初始化
    pygame.init()
    pygame.mixer.init()
    pygame.font.init()
    pygame.mixer.music.load(cfg.BGMPATH)
    pygame.mixer.music.play(-1, 0.0)
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('Maze —— Charles的皮卡丘')
    font = pygame.font.SysFont('Consolas', 15)
    # 开始界面
    Interface(screen, cfg, 'game_start')
    # 记录关卡数
    num_levels = 0
    # 记录最少用了多少步通关
    best_scores = 'None'
    # 关卡循环切换
    while True:
        num_levels += 1
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode(cfg.SCREENSIZE)
        # --随机生成关卡地图
        maze_now = RandomMaze(cfg.MAZESIZE, cfg.BLOCKSIZE, cfg.BORDERSIZE)
        # --生成hero
        hero_now = Hero(cfg.HEROPICPATH, [0, 0], cfg.BLOCKSIZE, cfg.BORDERSIZE)
        # --统计步数
        num_steps = 0
        # --关卡内主循环
        while True:
            dt = clock.tick(cfg.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] == cfg.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == cfg.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, cfg, mode='game_switch')
'''run'''
if __name__ == '__main__':
    main(cfg)

1.主代码

'''

Function:

   走迷宫小游戏

Author:

   Charles


'''

import cfg

import sys

import pygame

from modules import *



'''主函数'''

def main(cfg):

   # 初始化

   pygame.init()

   pygame.mixer.init()

   pygame.font.init()

   pygame.mixer.music.load(cfg.BGMPATH)

   pygame.mixer.music.play(-1, 0.0)

   screen = pygame.display.set_mode(cfg.SCREENSIZE)

   pygame.display.set_caption('Maze —— Charles的皮卡丘')

   font = pygame.font.SysFont('Consolas', 15)

   # 开始界面

   Interface(screen, cfg, 'game_start')

   # 记录关卡数

   num_levels = 0

   # 记录最少用了多少步通关

   best_scores = 'None'

   # 关卡循环切换

   while True:

       num_levels += 1

       clock = pygame.time.Clock()

       screen = pygame.display.set_mode(cfg.SCREENSIZE)

       # --随机生成关卡地图

       maze_now = RandomMaze(cfg.MAZESIZE, cfg.BLOCKSIZE, cfg.BORDERSIZE)

       # --生成hero

       hero_now = Hero(cfg.HEROPICPATH, [0, 0], cfg.BLOCKSIZE, cfg.BORDERSIZE)

       # --统计步数

       num_steps = 0

       # --关卡内主循环

       while True:

           dt = clock.tick(cfg.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] == cfg.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == cfg.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, cfg, mode='game_switch')



'''run'''

if __name__ == '__main__':

   main(cfg)

三.cfg


'''配置文件'''

import os



'''屏幕大小'''

SCREENSIZE = (800, 625)

'''游戏素材'''

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

四.README



相关文章
|
2天前
|
数据安全/隐私保护 Python
探索Python中的装饰器:简化代码,提升效率
【9月更文挑战第32天】在Python编程世界中,装饰器是一个强大的工具,它允许我们在不改变函数源代码的情况下增加函数的功能。本文将通过直观的例子和代码片段,引导你理解装饰器的概念、使用方法及其背后的魔法,旨在帮助你写出更加优雅且高效的代码。
|
1天前
|
大数据 Python
Python 高级编程:深入探索高级代码实践
本文深入探讨了Python的四大高级特性:装饰器、生成器、上下文管理器及并发与并行编程。通过装饰器,我们能够在不改动原函数的基础上增添功能;生成器允许按需生成值,优化处理大数据;上下文管理器确保资源被妥善管理和释放;多线程等技术则助力高效完成并发任务。本文通过具体代码实例详细解析这些特性的应用方法,帮助读者提升Python编程水平。
18 5
|
6天前
|
Python
? Python 装饰器入门:让代码更灵活和可维护
? Python 装饰器入门:让代码更灵活和可维护
12 4
|
6天前
|
缓存 测试技术 Python
探索Python中的装饰器:简化代码,提高可读性
【9月更文挑战第28天】在Python编程中,装饰器是一个强大的工具,它允许我们在不修改原有函数代码的情况下增加额外的功能。本文将深入探讨装饰器的概念、使用方法及其在实际项目中的应用,帮助读者理解并运用装饰器来优化和提升代码的效率与可读性。通过具体示例,我们将展示如何创建自定义装饰器以及如何利用它们简化日常的编程任务。
11 3
|
5天前
|
机器学习/深度学习 数据格式 Python
将特征向量转化为Python代码
将特征向量转化为Python代码
12 1
|
5月前
|
设计模式 前端开发 数据库
Django是一个用Python编写的开源Web应用框架
Django是一个用Python编写的开源Web应用框架
44 1
|
1天前
|
机器学习/深度学习 人工智能 数据可视化
Python比较适合哪些场景的编程?
Python比较适合哪些场景的编程?
14 7
|
6天前
|
数据挖掘 索引 Python
Python数据挖掘编程基础3
字典在数学上是一个映射,类似列表但使用自定义键而非数字索引,键在整个字典中必须唯一。可以通过直接赋值、`dict`函数或`dict.fromkeys`创建字典,并通过键访问元素。集合是一种不重复且无序的数据结构,可通过花括号或`set`函数创建,支持并集、交集、差集和对称差集等运算。
15 9
下一篇
无影云桌面