如何实现Python Turtle迷宫游戏中玩家碰撞迷宫后重置至起始位置?
Python Turtle迷宫:碰撞墙体重置玩家位置的实现方案
问题
我正在开发一个Python Turtle迷宫游戏,需要实现**玩家碰撞绿色墙体时自动重置到起始位置(-400, 20)**的功能。尝试用distance方法实现但未成功,求可行的解决办法。原代码如下:
import turtle player = input('Pick square or arrow: ') if player == "arrow": turtle.shape("arrow") turtle.pu() turtle.goto(-400, 20) elif player == "square": turtle.shape("square") turtle.pu() turtle.goto(-400, 20) else: print ('You did not type square or arrow, so you have been given default.') turtle.pu() turtle.goto(-400, 20) def up(): turtle.forward(45) def right(): turtle.right(90) def left(): turtle.left(90) def down(): turtle.backward(45) sc=turtle.Screen() sc.setup(600, 600) turtle.onkey(up,'w') turtle.onkey(right,'d') turtle.onkey(left,'a') turtle.onkey(down,'s') turtle.listen() turtle.penup() instructions = turtle.Turtle() instructions.ht() instructions.pu() instructions.goto(-600, 200) instructions.write("Maze Game", font=('Arial', 32, 'normal')) instructions.goto(-600, 125) instructions.write("Touch the Green", font=('Arial', 26, 'normal')) instructions.goto(-580, 100) instructions.write("And DIE!!!", font=('Arial', 26, 'normal')) instructions.goto(-620, 50) instructions.write("Make it Out to Win", font=('Arial', 26, 'normal')) instructions.goto(-620, -75) instructions.write("Use w/a/s/d to move", font=('Arial', 26, 'normal')) #drawing the maze maze = turtle.Turtle() maze.ht() maze.speed(0) maze.penup() maze.goto(-310, 300) maze.pendown() maze.pensize(40) maze.color('green') maze.forward(700) maze.ht() maze.speed(0) maze.penup() maze.goto(390, 300) maze.pendown() maze.right(90) maze.pensize(40) maze.color('green') maze.forward(300) maze.ht() maze.speed(0) maze.penup() maze.goto(390, 0) maze.pendown() maze.left(90) maze.pensize(40) maze.color('green') maze.forward(125) maze.ht() maze.speed(0) maze.penup() maze.goto(515, 0) maze.pendown() maze.right(90) maze.pensize(40) maze.color('green') maze.forward(100)
解决思路
turtle.distance()只能计算玩家到maze海龟坐标的直线距离,无法检测与宽线条墙体的碰撞。正确的做法是根据每段墙体的位置、长度和笔宽,定义墙体的坐标范围,检测玩家是否进入该范围。
修改后的完整代码
import turtle # 定义起始位置常量,方便后续修改 START_POS = (-400, 20) player = input('Pick square or arrow: ') if player == "arrow": turtle.shape("arrow") turtle.pu() turtle.goto(START_POS) elif player == "square": turtle.shape("square") turtle.pu() turtle.goto(START_POS) else: print ('You did not type square or arrow, so you have been given default.') turtle.pu() turtle.goto(START_POS) # 碰撞检测函数:判断玩家是否进入任意一段墙体范围 def check_collision(): x, y = turtle.xcor(), turtle.ycor() pen_half_width = 20 # 因为pensize是40,半宽为20 # 第一段墙体:顶部水平墙(y≈300,x从-310到390) if 300 - pen_half_width <= y <= 300 + pen_half_width and -310 <= x <= 390: return True # 第二段墙体:右侧垂直墙(x≈390,y从0到300) if 390 - pen_half_width <= x <= 390 + pen_half_width and 0 <= y <= 300: return True # 第三段墙体:右侧下方水平墙(y≈0,x从390到515) if 0 - pen_half_width <= y <= 0 + pen_half_width and 390 <= x <= 515: return True # 第四段墙体:最右侧垂直墙(x≈515,y从-100到0) if 515 - pen_half_width <= x <= 515 + pen_half_width and -100 <= y <= 0: return True return False def up(): turtle.forward(45) if check_collision(): turtle.goto(START_POS) def right(): turtle.right(90) # 转向后也可能触发碰撞(比如贴墙转向) if check_collision(): turtle.goto(START_POS) def left(): turtle.left(90) if check_collision(): turtle.goto(START_POS) def down(): turtle.backward(45) if check_collision(): turtle.goto(START_POS) sc=turtle.Screen() sc.setup(600, 600) turtle.onkey(up,'w') turtle.onkey(right,'d') turtle.onkey(left,'a') turtle.onkey(down,'s') turtle.listen() turtle.penup() instructions = turtle.Turtle() instructions.ht() instructions.pu() instructions.goto(-600, 200) instructions.write("Maze Game", font=('Arial', 32, 'normal')) instructions.goto(-600, 125) instructions.write("Touch the Green", font=('Arial', 26, 'normal')) instructions.goto(-580, 100) instructions.write("And DIE!!!", font=('Arial', 26, 'normal')) instructions.goto(-620, 50) instructions.write("Make it Out to Win", font=('Arial', 26, 'normal')) instructions.goto(-620, -75) instructions.write("Use w/a/s/d to move", font=('Arial', 26, 'normal')) #drawing the maze maze = turtle.Turtle() maze.ht() maze.speed(0) maze.penup() maze.goto(-310, 300) maze.pendown() maze.pensize(40) maze.color('green') maze.forward(700) maze.ht() maze.speed(0) maze.penup() maze.goto(390, 300) maze.pendown() maze.right(90) maze.pensize(40) maze.color('green') maze.forward(300) maze.ht() maze.speed(0) maze.penup() maze.goto(390, 0) maze.pendown() maze.left(90) maze.pensize(40) maze.color('green') maze.forward(125) maze.ht() maze.speed(0) maze.penup() maze.goto(515, 0) maze.pendown() maze.right(90) maze.pensize(40) maze.color('green') maze.forward(100) turtle.done()
说明
- 定义了
START_POS常量,统一管理起始位置,后续修改更方便。 check_collision函数根据每段墙体的笔宽(半宽20)和绘制范围,判断玩家坐标是否进入墙体区域。- 在所有移动和转向函数中加入碰撞检测,确保玩家无论移动还是贴墙转向时触发碰撞都能重置位置。
内容的提问来源于stack exchange,提问作者Aar
相关产品推荐
相关产品推荐

