【问题标题】:Collision in python Turtle Snake gamepython Turtle Snake 游戏中的碰撞
【发布时间】:2021-10-11 15:19:36
【问题描述】:

我正在编写一个代码,其中有一个乌龟必须通过的迷宫。唯一的问题是我无法弄清楚如何与所有墙壁产生碰撞。我一直无法找到一种在不对墙的每个坐标进行限制的情况下创建障碍的方法。我只能访问海龟和随机模块。有没有办法做到这一点?这是我的代码:

import turtle

screen = turtle.Screen()
tr=turtle.Turtle()
screen.tracer(0)

mazeWidth=150
tr = turtle.Turtle()
tr.width(5)
tr.speed(0)
tr.penup()
tr.goto(-mazeWidth,190)
def drawMazeSection(color):
  tr.pencolor("lightblue")
  tr.penup()
  tr.goto(-20,200)
  tr.pendown()
  tr.goto(20,200)
  tr.penup()
  tr.goto(-20,-200)
  tr.pendown()
  tr.goto(20,-200)
  tr.penup()
  tr.pencolor("black")
  tr.goto(-200,200)
  tr.pendown()
  tr.goto(-200,-160)
  tr.begin_fill()
  tr.goto(-180,-160)
  tr.goto(-180,-200)
  tr.goto(-200,-200)
  tr.end_fill()
  tr.goto(-20,-200)
  tr.penup()
  tr.goto(20,-200)
  tr.pendown()
  tr.begin_fill()
  tr.goto(200,-200)
  tr.goto(200,200)
  tr.goto(20,200)
  tr.goto(20,140)
  tr.goto(80,140)
  tr.goto(80,180)
  tr.goto(180,180)
  tr.goto(180,-80)
  tr.goto(100,-80)
  tr.goto(100,-140)
  tr.goto(20,-140)
  tr.goto(20,-200)
  tr.end_fill()
  tr.penup()
  tr.goto(20,160)
  tr.pendown()
  tr.begin_fill()
  tr.goto(-80,160)
  tr.goto(-80,140)
  tr.goto(-60,140)
  tr.goto(-60,160)
  tr.end_fill()
  tr.goto(-60,120)
  tr.goto(-20,120)
  tr.goto(-20,100)
  tr.goto(40,100)
  tr.penup()
  tr.goto(120,140)
  tr.pendown()
  tr.begin_fill()
  tr.goto(140,140)
  tr.goto(140,100)
  tr.goto(120,100)
  tr.goto(120,120)
  tr.goto(120,140)
  tr.end_fill()
  tr.goto(140,100)
  tr.goto(80,100)
  tr.goto(80,80)
  tr.goto(80,60)
  tr.begin_fill()
  tr.goto(140,60)
  tr.goto(140,20)
  tr.goto(100,20)
  tr.goto(100,60)
  tr.end_fill()
  tr.goto(20,60)
  tr.goto(20,-100)
  tr.goto(20,-40)
  tr.goto(-20,-40)
  tr.goto(-20,-100)
  tr.goto(-60,-100)
  tr.penup()
  tr.goto(-60,-140)
  tr.pendown()
  tr.goto(-60,-160)
  tr.goto(-140,-160)
  tr.goto(-140,-120)
  tr.goto(-160,-120)
  tr.goto(-160,-40)
  tr.penup()
  tr.goto(-60,-60)
  tr.pendown()
  tr.goto(-60,0)
  tr.goto(-20,0)
  tr.goto(-20,40)
  tr.goto(-60,40)
  tr.goto(-60,80)
  tr.goto(-120,80)
  tr.goto(-120,200)
  tr.penup()
  tr.goto(-160,160)
  tr.pendown()
  tr.goto(-160,40)
  tr.goto(-40,40)
  tr.penup()
  tr.goto(60,20)
  tr.pendown()
  tr.goto(60,-80)
  tr.begin_fill()
  tr.goto(60,-40)
  tr.goto(140,-40)
  tr.goto(140,-20)
  tr.goto(60,-20)
  tr.end_fill()
  tr.penup()
  tr.goto(-200,0)
  tr.pendown()
  tr.goto(-100,0)
  tr.begin_fill()
  tr.goto(-100,-120)
  tr.goto(-100,-80)
  tr.goto(-120,-80)
  tr.goto(-120,0)
  tr.end_fill()
  tr.penup()
  tr.goto(-20,-140)
  tr.pendown()
  tr.goto(-20,-200)
  
for color in ["#FF0000","#0000FF","#00FF00"]:
  drawMazeSection(color)
  

screen.tracer(1)    
from turtle import Turtle, Screen

wn = Screen()
wn.bgcolor('darkgreen')

tr.color('red')
tr.penup()

tr.speed(40)
speed = 1.5

wn.mainloop()
tr.penup()
tr.goto(20,-180)
tr.pendown()
tr.shape('turtle')
tr.color("#DB148E")
tr.width(5)
tr.left(90)

tr.penup()
tr.goto(0,-200)
def travel():
    tr.forward(speed)
    wn.ontimer(travel, 10)

wn.onkey(lambda: tr.setheading(90), 'w')
wn.onkey(lambda: tr.setheading(180), 'a')
wn.onkey(lambda: tr.setheading(0), 'd')
wn.onkey(lambda: tr.setheading(270), 's')

wn.listen()
travel()

【问题讨论】:

    标签: python turtle-graphics python-turtle


    【解决方案1】:

    我认为你需要 turtle.window_width() 和 turtle.window_height 函数

    获取程序的宽度和高度

    width, height = turtle.window_width(), turtle.window_height()
    
    ## Gets the boundaries for the x coordinates 
    minX, maxX = -width/2, width/2
    
    ## Sets the boundaries for the y coordinates
    minY, maxY = -height/2, height / 2
    
    ## Checks if the turtle hits the boundaries
    def check():
        if tr.xcor() <= maxX:
           tr.setheading(0)
        if tr.xcor() >= minX:
           tr.setheading(180)
        if tr.ycor() >= minY:
           tr.setheading(270)
        if tr.ycor() <= maxY:
           tr.setheading(90)
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2020-05-17
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2017-12-28
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多