import pygame.font
import pygame
import sys
pygame.init()
pygame.font.init()
# 初始化Pygame
pygame.init()
# 在初始化Pygame之后创建一个大号红色字体对象
font_size = 200 # 指定字号
font_color = (255, 0, 0) # 红色文字
background_color = (255, 255, 255) # 白色背景
font = pygame.font.Font(None, font_size) # 使用默认字体,指定字号
# 定义屏幕宽度和高度
SCREEN_WIDTH, SCREEN_HEIGHT = 980, 680
# 定义迷宫地图(0表示通路,1表示墙)
maze = [
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1],
[1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
# 定义角色的初始位置(以迷宫的行和列来表示)
player_pos = [1, 1]
# 定义出口的位置(以迷宫的行和列来表示)
exit_pos = [len(maze) - 2, len(maze[0]) - 2]
# 定义角色的移动速度
player_speed = 1
# 加载马里奥的图片
mario_image = pygame.image.load("mario.png")
mario_image = pygame.transform.scale(mario_image, (40, 40))
# 定义计时器
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
# 定义游戏状态
game_over = False
def main():
global start_time
global game_over
# 创建屏幕对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 游戏主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 获取按键状态
keys = pygame.key.get_pressed()
# 根据游戏状态处理角色移动
if not game_over:
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
if player_pos[1] > 0 and maze[player_pos[0]][player_pos[1] - 1] == 0:
player_pos[1] -= player_speed
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
if player_pos[1] < len(maze[0]) - 1 and maze[player_pos[0]][player_pos[1] + 1] == 0:
player_pos[1] += player_speed
if keys[pygame.K_UP] or keys[pygame.K_w]:
if player_pos[0] > 0 and maze[player_pos[0] - 1][player_pos[1]] == 0:
player_pos[0] -= player_speed
if keys[pygame.K_DOWN] or keys[pygame.K_s]:
if player_pos[0] < len(maze) - 1 and maze[player_pos[0] + 1][player_pos[1]] == 0:
player_pos[0] += player_speed
# 检查玩家是否到达出口,如果是,游戏胜利
if player_pos == exit_pos:
game_over = True
end_time = pygame.time.get_ticks()
elapsed_time = (end_time - start_time) // 1000
print("You Win! Time taken: {} seconds".format(elapsed_time))
# 填充屏幕为白色
screen.fill((255, 255, 255))
# 绘制迷宫
for row in range(len(maze)):
for col in range(len(maze[0])):
if maze[row][col] == 1:
pygame.draw.rect(screen, (0, 0, 0), (col * 40, row * 40, 40, 40))
# 绘制马里奥的图片
screen.blit(mario_image, (player_pos[1] * 40, player_pos[0] * 40))
# 检查游戏状态,如果游戏结束,显示恭喜消息
if game_over:
congrats_text = font.render("You Win!", True, font_color)
screen.blit(congrats_text, ((SCREEN_WIDTH - congrats_text.get_width()) // 2,
(SCREEN_HEIGHT - congrats_text.get_height()) // 2))
# 绘制计时器
if not game_over:
current_time = pygame.time.get_ticks()
elapsed_time = (current_time - start_time) // 1000
timer_text = font.render("Time: {} seconds".format(elapsed_time), True, font_color)
screen.blit(timer_text, (10, 10))
# 刷新屏幕
pygame.display.flip()
# 控制游戏帧率
clock.tick(30)
# 在脚本末尾调用main函数
if __name__ == "__main__":
main()
Python 迷宫小游戏
于 2023-10-11 15:10:37 首次发布