以下是部分代码预览:
"""pygame基本多关卡迷宫游戏核心代码.py,运行本程序可以操作一个小方块从一个房间移到另一个房间,碰到了“墙壁”就不能前进。"""
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
class Wall(pygame.sprite.Sprite):
"""墙类,代表方块障碍物"""
def __init__(self, x, y, width, height, color):
# 调用父类的初始方法
super().__init__()
# 根据指定的宽高参数新建图层
self.image = pygame.Surface([width, height])
self.image.fill(color)
# 新建矩形对象,以左上角为它的坐标
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Player(pygame.sprite.Sprite):
""" 玩家通过方向箭头控制的角色 """
def __init__(self, x, y):
super().__init__()
# 玩家控制的角色是个白色的小方块
self.image = pygame.Surface([15, 15])
self.image.fill(WHITE)
pass
def changespeed(self, x, y):
""" 通过按键改变速度的值 """
self.change_x += x
self.change_y += y
def move(self, walls):
""" 改变角色的x,y坐标 """
# 左右移动
self.rect.x += self.change_x
# 碰到墙的检测
pass
class Room(object):
""" 所有房间的基类"""
def __init__(self):
""" 所有房间共同的属性 """
self.wall_list = pygame.sprite.Group()
self.enemy_sprites = pygame.sprite.Group()
class Room1(Room):
"""房间1的类"""
def __init__(self):
super().__init__()
# 生成一些墙,参数为x坐标,y坐标,宽,高
walls = [[0, 0, 20, 250, WHITE],
[0, 350, 20, 250, WHITE],
[780, 0, 20, 250, WHITE],
[780, 350, 20, 250, WHITE],
[20, 0, 760, 20, WHITE],
[20, 580, 760, 20, WHITE],
[390, 50, 20, 500, BLUE]
]
# 遍历列表生成这些墙,加到墙表中。
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
class Room2(Room):
"""房间2的类"""
def __init__(self):
super().__init__()
walls = [[0, 0, 20, 250, RED],
[0, 350, 20, 250, RED],
[780, 0, 20, 250, RED],
[780, 350, 20, 250, RED],
[20, 0, 760, 20, RED],
[20, 580, 760, 20, RED],
[190, 50, 20, 500, GREEN],
[590, 50, 20, 500, GREEN]
]
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
class Room3(Room):
"""房间3的类"""
def __init__(self):
super().__init__()
walls = [[0, 0, 20, 250, PURPLE],
[0, 350, 20, 250, PURPLE],
[780, 0, 20, 250, PURPLE],
[780, 350, 20, 250, PURPLE],
[20, 0, 760, 20, PURPLE],
[20, 580, 760, 20, PURPLE] ]
for item in walls:
wall = Wall(item[0], item[1], item[2], item[3], item[4])
self.wall_list.add(wall)
for x in range(100, 800, 100):
for y in range(50, 451, 300):
wall = Wall(x, y, 20, 200, RED)
self.wall_list.add(wall)
for x in range(150, 700, 100):
wall = Wall(x, 200, 20, 200, WHITE)
self.wall_list.add(wall)
def main():
""" 主要函数代码"""
# 初始化pygame
pygame.init()
# 创建800X600的屏幕对象,它也是一个surface
screen = pygame.display.set_mode([800, 600])
# 设置窗口标题
pygame.display.set_caption('基本多关卡迷宫游戏核心代码')
# 创建玩家控制的小方块
player = Player(50, 50)
movingsprites = pygame.sprite.Group() # 玩家角色组
movingsprites.add(player)
rooms = [Room1(),Room2(),Room3()] # 房间列表
current_room_index = 0
current_room = rooms[current_room_index]
clock = pygame.time.Clock()
done = False
while not done:
# 事件处理
pass
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
main()
如需要下载完整源代码及素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

