python - 我的海龟没有检测到输入,它可以越过边界
问题描述
所以基本上,我根本不能移动我的乌龟,即使它有功能。而且,当我可以的时候,当它到达边界时,它会继续前进。我试过移动函数的位置,但它不起作用。我已经设置了它的所有功能并且它们过去工作过,我不知道是什么让它们停止工作。此外,海龟和边界之间的接触也没有任何作用。
import turtle, os, math
#setting up the dimensions of the screen + color
wn = turtle.Screen()
wn.screensize(500,500)
wn.title("Mario vs the turtles")
wn.bgcolor("white")
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("black")
border_pen.penup()
border_pen.setposition(-350,-350)
border_pen.pendown()
border_pen.pensize(3)
for side in range (4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
score_mario=0
hearts_mario=3
#score
score = turtle.Turtle()
score.speed(0)
score.color("black")
score.penup()
score.hideturtle()
score.goto(50,300)
score.pendown()
score.write("Score: 0 Hearts: 3", align="center", font=("Courier", 24, "normal"))
#Main game loop
#color of the turtle, speed, two variables for the position
mario=turtle.Turtle()
mario.shape("turtle")
colors = ['red', 'blue', 'green', 'purple', 'yellow', 'orange', 'black']
mario.penup()
mario.hideturtle()
mario.goto(-320,-320)
mario.showturtle()
simon=turtle.Turtle()
simon.shape("turtle")
simon.color("black")
simon.penup()
simon.hideturtle()
simon.setposition(-270,-320)
simon.showturtle()
while hearts_mario>0:
mario.ycor==simon.ycor
x,y=mario.position()
x,y=simon.position()
while simon.xcor<500 and simon.xcor>-500:
simon.setheading(180)
mario.forward(100)
mario.speed(0)
mario.shapesize(stretch_wid=1, stretch_len=1)
def jump():
cor1=mario.ycor()
cor1+=10
mario.delay=2
mario.setposition(x,cor1)
cor1-=10
mario.setposition(x,cor1)
def left():
mario.setheading(180)
mario.forward(100)
def right():
mario.setheading(0)
mario.forward(100)
def escape():
wn.bye()
def reset():
mario.reset()
if mario.xcor==simon.xcor and mario.ycor==simon.ycor:
hearts_mario=2
#all these functions are for movement/boundaries
#boundaries
if mario.xcor<=border_pen.xcor and mario.ycor<=border_pen.ycor:
mario.setposition(250,250)
if mario.setheading(180) and mario.forward (100):
turtle.register_shape("marioleft.gif")
mario.shape("marioleft.gif")
turtle.onkey(left, "Left")
turtle.onkey(right, "Right")
turtle.onkey(escape, "Escape")
turtle.onkey(reset, "r")
turtle.onkey(jump, "space")
turtle.listen()
turtle.mainloop()
解决方案
我不知道是什么让他们停止工作
我很难相信这段代码曾经奏效。这似乎是来自其他程序的碎片,正在等待一场好的闪电风暴。例如:
mario.ycor==simon.ycor
while simon.xcor<500 and simon.xcor>-500:
if mario.xcor==simon.xcor and mario.ycor==simon.ycor:
if mario.xcor<=border_pen.xcor and mario.ycor<=border_pen.ycor:
mario.xcor()
和.ycor()
是方法,mario.xcor
什么.ycor
都不做。还:
if mario.setheading(180) and mario.forward (100):
turtle.register_shape("marioleft.gif")
mario.shape("marioleft.gif")
两者mario.setheading(180)
都mario.forward(100)
返回None
,所以这永远不会发生。有很多设置逻辑,但没有真正的运行时循环。我已尽我所能将代码改写为可玩的游戏,尽管它可能不是您想要的游戏:
from turtle import Screen, Turtle, mainloop
FONT = ("Courier", 24, "normal")
def jump():
screen.onkey(None, 'space') # disable handler inside handler
if mario.ycor() == 0: # if mario isn't already in the air
mario.forward(150)
mario.backward(150)
screen.onkey(jump, 'space')
def left():
x = mario.xcor()
if x > -250:
mario.setx(x - 10)
def right():
x = mario.xcor()
if x < 250:
mario.setx(x + 10)
def escape():
screen.bye()
def reset():
global hearts_mario
mario.goto(0, 0)
hearts_mario = 3
score.write("Score: {} Hearts: {}".format(score_mario, hearts_mario), align="center", font=FONT)
# setting up the dimensions of the screen + color
screen = Screen()
screen.screensize(650, 650)
screen.title("Mario vs the turtles")
border_pen = Turtle()
border_pen.hideturtle()
border_pen.speed('fastest')
border_pen.pensize(3)
border_pen.penup()
border_pen.setposition(-275, -275)
border_pen.pendown()
for _ in range(4):
border_pen.fd(550)
border_pen.lt(90)
# score
score_mario = 0
hearts_mario = 3
score = Turtle()
score.hideturtle()
score.speed('fastest')
score.penup()
score.sety(300)
score.pendown()
score.write("Score: {} Hearts: {}".format(score_mario, hearts_mario), align="center", font=FONT)
# color of the turtle, speed, two variables for the position
mario = Turtle()
mario.hideturtle()
mario.shape("turtle")
mario.color('dark green', 'light green')
mario.speed('slowest')
mario.penup()
mario.setheading(90)
mario.showturtle()
simon = Turtle()
simon.hideturtle()
simon.shape("turtle")
simon.color('black', 'red')
simon.penup()
simon.setx(-250)
simon.showturtle()
# Main game loop
def game_loop():
global hearts_mario
if simon.xcor() < -250 or simon.xcor() > 250:
simon.setheading(180 - simon.heading())
simon.forward(10)
if mario.distance(simon) < 5:
hearts_mario -= 1
score.undo()
score.write("Score: {} Hearts: {}".format(score_mario, hearts_mario), align="center", font=FONT)
if hearts_mario > 0:
screen.ontimer(game_loop, 100)
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.onkey(escape, "Escape")
screen.onkey(reset, "r")
screen.onkey(jump, "space")
screen.listen()
game_loop()
mainloop()
推荐阅读
- types - `reinterpret` 不适用于 Julia 中的自定义原始类型
- postgresql - vertx 3.7.1 -> 3.8.1 在 vertx-pg-client 中没有 addJson 函数
- hyperledger - Hyperledger Indy 基本概念
- datetime - 为什么在使用请求时解析我的日期成功,但在使用 reqwests 时失败?
- kotlin - 协程执行时间与回调执行时间
- python - 比较数据框字典中的列
- python - 字典中存在的键的 KeyError
- python - 如何测量在循环中创建的多线程所花费的时间?
- javascript - 如何使用正则表达式解析 svg `viewBox` 属性?
- python - django-rest-auth "此电子邮件地址的用户配置文件已存在。" 在登录