python走迷宫_python-走迷宫

from turtle import Turtle

class Nezha(Turtle): def __init__(self, maze_list, start_m, start_n, end_m, end_n): # 父类初始化 Turtle.__init__(self) self.m = start_m self.n = start_n self.end_m = end_m self.end_n = end_n self.maze_list = maze_list self.hideturtle() self.speed(0) self.penup() # 移到对应的位置 self.goto(self.n * 20 - 120, 120 - self.m * 20) # 变成海龟 self.shape('turtle') self.color('#28bea0') self.setheading(270) self.showturtle() # 添加哪吒图片作为形状 screen = self.getscreen() screen.addshape('nezha.png') def reach_exit(self, m, n): if m == self.end_m and n == self.end_n: # 变成哪吒 self.shape('nezha.png') def canmove(self, m, n): return self.maze_list[m][n] == 0 def move(self, m, n): self.m = m self.n = n self.goto(self.n * 20 - 120, 120 - self.m * 20) self.reach_exit(m, n) def go_up(self): if self.canmove(self.m - 1, self.n): self.setheading(90) self.move(self.m - 1, self.n) def go_down(self): if self.canmove(self.m + 1, self.n): self.setheading(270) self.move(self.m + 1, self.n) def go_left(self): if self.canmove(self.m, self.n - 1): self.setheading(180) self.move(self.m, self.n - 1) def go_right(self): if self.canmove(self.m, self.n + 1): self.setheading(0) self.move(self.m, self.n + 1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用Python实现迷宫游戏的示例代码[^1]: ```python import pygame import random # 初始化游戏 pygame.init() # 设置窗口大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("迷宫游戏") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 定义迷宫地图 maze_map = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 1, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 1, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 1, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] # 定义迷宫单元格大小 cell_width = screen_width // len(maze_map) cell_height = screen_height // len(maze_map) # 定义玩家初始位置 player_x = 1 player_y = 1 # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 绘制迷宫 screen.fill(BLACK) for row in range(len(maze_map)): for col in range(len(maze_map)): if maze_map[row][col] == 1: pygame.draw.rect(screen, BLUE, (col * cell_width, row * cell_height, cell_width, cell_height)) else: pygame.draw.rect(screen, WHITE, (col * cell_width, row * cell_height, cell_width, cell_height)) # 绘制玩家 pygame.draw.rect(screen, RED, (player_x * cell_width, player_y * cell_height, cell_width, cell_height)) # 更新屏幕 pygame.display.flip() # 退出游戏 pygame.quit() ``` 这段代码使用pygame库实现了一个简单的迷宫游戏。玩家通过键盘控制移动,目标是从起点到达终点。迷宫地图由二维数组表示,其中1表示墙壁,0表示通道。玩家通过改变player_x和player_y的值来移动位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值