繁体   English   中英

空闲蟒蛇龟赛跑游戏。 结束游戏功能似乎不起作用

[英]Idle python Turtle race game. Ending game function does not seem to work

我在如何结束乌龟空闲 python 游戏时遇到了问题。 如果我将海龟放在代码开头的起点/终点,代码似乎可以运行,但是当海龟在游戏中到达终点时它不会注册。 据我所知,我认为我对 end 函数的数学是正确的。 我是新手,感谢您的帮助。 不过我目前处于离线状态。

代码:

import time
import turtle
from turtle import *
    
wn = turtle.Screen()
    
name=textinput("Question", "what is your name")
#display
pencolor("white")
penup()
goto(0,170)
write("hello " +name,align='center',font=('Comic Sans', 20))

#wn = turtle.screen() if the code doesn't work
#diffrent turtles here
    
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t3 = turtle.Turtle()

#starting psoition
turtle.penup()
t1.penup()
turtle.goto(-1, -230)
t1.goto(-1, -170)
#starting line postion
    

def f():
    fd(10)


def b():
    bk(10)


def l():
    left(10)

    
def r():
    right(10)


#testing
def fo():
    t1.fd(10)


def ba():
    t1.bk(10)
    

def le():
    t1.left(10)
 

def ri():
    t1.right(10)
    

#turtle coordinates
first=turtle.ycor()
second=turtle.xcor()
third=t1.ycor()
fourth=t1.xcor()
      
#when to end the game
if (turtle.ycor()>= (-160)) and (turtle.ycor()<= (-240)):
    if (turtle.xcor()>= (0)) and (turtle.xcor()<= (11)):    
        print("Finally working")
        #replaced with write who the winner is later
   
if (t1.ycor()>= (-160)) and (t1.ycor()<= (-240)):
    if (t1.xcor()>= (0)) and (t1.xcor()<= (11)):    
        print("Finally")

# onkey creates the key board = turtle.onkey("function, key")  You have to keep pressing keys for it to move
turtle.onkey(f, "w")
turtle.onkey(b, "s")
turtle.onkey(l, "a")
turtle.onkey(r, "d")
    
wn.onkey(fo, "Up")
wn.onkey(ba, "Down")
wn.onkey(le, "Left")
wn.onkey(ri, "Right")
listen()
    
#WINDOW SETUP
window = Screen()    

window.setup(800, 800)
window.title("Turtle game")
turtle.bgcolor("forestgreen")
t3.color("black")
t3.speed(0) 
t3.penup()
t3.setpos(-140, 250)
t3.write("THE TURTLE RACE", font=("Comic Sans", 30, "bold"))
t3.penup()

#turtle ask name

#add images here

#turtle controls

# def creates a function. : means opperation f means move turtle foward. fd push turtle forward

# onkey creates the key board = turtle.onkey("function, key")  You have to keep pressing keys for it to move
    
t2.speed(0)
t2.color("grey")
t2.pensize(100)
t2.penup()
t2.goto(-200, -200)
t2.left(90)
t2.pendown()
t2.forward(300)
t2.right(90)
t2.forward(500)
t2.right(90)
t2.forward(300)
t2.right(90)
t2.forward(500)
  
turtle.penup()

首先,你的数学不太正确 - 坐标永远不能同时是 <= -240 和 >= -160。 它应该是t.ycor() >= -240 and t.ycor() <= -160 ,或者更简单地说, -240 <= t.ycor() <= -160

其次,当代码第一次运行时,条件只检查一次。 相反,您需要让程序定期检查它。 您可以通过添加一个通用的onkeypress事件处理程序来做到这一点,每次按下任何键时都会检查该处理程序。

def check_status():
    for player, t in enumerate([turtle, t1]):
        if  0 <= t.xcor() <= 11 and -240 <= t.ycor() <= -160:
            print(f"Player {player} is at the endpoint")

...
wn.onkeypress(check_status)
listen()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM