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



相关文章
|
1天前
|
缓存 开发者 Python
Python中的装饰器:提升代码灵活性与可复用性
众所周知,Python作为一门流行的编程语言,其装饰器(Decorator)机制为代码的优化和重用提供了强大支持。本文将深入探讨Python中装饰器的概念、用法和实际应用,帮助读者更好地理解并应用这一技术,从而提升代码的灵活性和可复用性。
|
1天前
|
缓存 开发者 Python
Python中的装饰器:提升代码灵活性和可维护性
Python中的装饰器是一种强大的工具,它可以帮助开发者提升代码的可维护性和灵活性。本文将深入探讨Python装饰器的原理、用法以及实际应用场景,帮助读者更好地理解并运用装饰器来优化自己的代码。
|
2天前
|
Python
10个Python绘画表白代码【内附源码,再不收藏你只能单身了】_有趣的python代码表白
10个Python绘画表白代码【内附源码,再不收藏你只能单身了】_有趣的python代码表白
|
2天前
|
Python
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
10个python入门小游戏,零基础打通关,就能掌握编程基础_python编写的入门简单小游戏
|
2天前
|
存储 程序员 C#
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码
|
2天前
|
程序员 C# Python
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
100行python代码,轻松完成贪吃蛇小游戏_c#游戏100行代码(2)
|
4天前
|
算法 编译器 开发者
如何提高Python代码的性能:优化技巧与实践
本文探讨了如何提高Python代码的性能,重点介绍了一些优化技巧与实践方法。通过使用适当的数据结构、算法和编程范式,以及利用Python内置的性能优化工具,可以有效地提升Python程序的执行效率,从而提升整体应用性能。本文将针对不同场景和需求,分享一些实用的优化技巧,并通过示例代码和性能测试结果加以说明。
|
4天前
|
人工智能 Python
Python中的反对称矩阵:理论、应用与代码实践
Python中的反对称矩阵:理论、应用与代码实践
27 1
|
4天前
|
存储 算法 搜索推荐
如何提升Python代码的性能:优化技巧与实践
本文将介绍如何通过优化技巧和实践方法来提升Python代码的性能。从避免不必要的循环和函数调用,到利用内置函数和库,再到使用适当的数据结构和算法,我们将深入探讨各种提升Python代码性能的方法,帮助开发者写出更高效的程序。
|
4天前
|
设计模式 缓存 数据安全/隐私保护
使用装饰器优化 Python 代码的技巧与实践
使用装饰器优化 Python 代码的技巧与实践
66 0