如何在 Python turtle 中的标记游戏中添加一些其他功能而不中断主要功能?
How to add some other functions in my tag game in Python turtle without interupting the main functions?
我正在用 Python 乌龟创建一个标记游戏。我已经创建了一个成功的机制,可以在其中一名玩家被标记时停止游戏。屏幕上的海龟也可以控制成功。现在我正在添加其他功能,其中之一是在游戏开始时启动的计时器,并在标记一名玩家时停止。之后,显示对方玩家存活的时间。无论我尝试过什么样式,代码只有在我删除时间功能代码时才有效。如果我包含时间功能代码,所有代码都将无法工作,包括标记机制。 我有什么方法可以在不干扰主代码的情况下包含时间功能吗?谢谢! :)
Player1=turtle.Pen()
Player1.up()
Player1.goto(100,0)
Playground=turtle.Screen()
Playground.title("BLACK NEEDS TO TAG ORANGE ON THE BACKSIDE")
Playground.setup(800,800)
Player1.shape("turtle")
Player1.color("black")
turtle.bgcolor("lawngreen")
Player1.speed(0)
Player1.pensize(10)
Player2=turtle.Pen()
Player2.up()
Player2.goto(-100,0)
Player2.shape("turtle")
Player2.color("orange")
Player2.speed(0)
Player2.pensize(10)
Pen=turtle.Pen()
Pen.up()
Pen.goto(-300,300)
Pen.down()
Pen.hideturtle
Pen2=turtle.Pen()
Pen2.up()
Pen2.goto(-300,100)
Pen2.down()
Pen2.hideturtle
def up1():
Player1.setheading(90)
Player1.forward(20)
def down1():
Player1.setheading(270)
Player1.forward(20)
def left1():
Player1.setheading(180)
Player1.forward(20)
def right1():
Player1.setheading(0)
Player1.forward(20)
def up2():
Player2.setheading(90)
Player2.forward(20)
def down2():
Player2.setheading(270)
Player2.forward(20)
def left2():
Player2.setheading(180)
Player2.forward(20)
def right2():
Player2.setheading(0)
Player2.forward(20)
def bounce1():
Player1.right(180)
Player1.forward(10)
def bounce2():
Player2.right(180)
Player2.forward(10)
turtle.listen()
turtle.onkeypress(up1,"t")
turtle.onkeypress(down1,"g")
turtle.onkeypress(left1,"f")
turtle.onkeypress(right1,"h")
turtle.onkeypress(up2,"Up")
turtle.onkeypress(down2,"Down")
turtle.onkeypress(left2,"Left")
turtle.onkeypress(right2,"Right")
import time
Score=0
for i in range(50):
time.sleep(1)
Score=Score+1
if Player1.xcor<-395 or Player1.xcor>395 or Player1.ycor<-395 or Player1.ycor>395:
bounce1()
if Player2.xcor<-395 or Player2.xcor>395 or Player2.ycor<-395 or Player2.ycor>395:
bounce2()
if Score==50:
Pen2.write("Times up!, Orange wins",font=("Arial",30,"bold"))
while True:
Player1.forward(0)
Player2.forward(0)
if Player1.distance(Player2)<10:
Pen.write("Black has tagged Orange's backside!!!!!!!",font=("Arial",30,"bold"))
break```
您评论中的错误是因为您在 xcor
和 ycor
之后错过了 ()
。这样就变成了。
if Player1.xcor()<-395 or Player1.xcor()>395 or Player1.ycor()<-395 or Player1.ycor()>395:
bounce1()
if Player2.xcor()<-395 or Player2.xcor()>395 or Player2.ycor()<-395 or Player2.ycor()>395:
bounce2()
至于分数每秒都在增加,您需要有一个在后台运行的线程。
Score = 0
def Start_Timer():
global Score
for i in range(50):
time.sleep(1)
Score=Score+1
Timer = threading.Thread(target=Start_Timer, daemon=True)
Timer.start()
合并所有
import threading
import turtle
Score = 0
def Start_Timer():
global Score
for i in range(50):
time.sleep(1)
Score=Score+1
Timer = threading.Thread(target=Start_Timer, daemon=True)
Timer.start()
Player1=turtle.Pen()
Player1.up()
Player1.goto(100,0)
Playground=turtle.Screen()
Playground.title("BLACK NEEDS TO TAG ORANGE ON THE BACKSIDE")
Playground.setup(800,800)
Player1.shape("turtle")
Player1.color("black")
turtle.bgcolor("lawngreen")
Player1.speed(0)
Player1.pensize(10)
Player2=turtle.Pen()
Player2.up()
Player2.goto(-100,0)
Player2.shape("turtle")
Player2.color("orange")
Player2.speed(0)
Player2.pensize(10)
Pen=turtle.Pen()
Pen.up()
Pen.goto(-300,300)
Pen.down()
Pen.hideturtle
Pen2=turtle.Pen()
Pen2.up()
Pen2.goto(-300,100)
Pen2.down()
Pen2.hideturtle
def up1():
Player1.setheading(90)
Player1.forward(20)
def down1():
Player1.setheading(270)
Player1.forward(20)
def left1():
Player1.setheading(180)
Player1.forward(20)
def right1():
Player1.setheading(0)
Player1.forward(20)
def up2():
Player2.setheading(90)
Player2.forward(20)
def down2():
Player2.setheading(270)
Player2.forward(20)
def left2():
Player2.setheading(180)
Player2.forward(20)
def right2():
Player2.setheading(0)
Player2.forward(20)
def bounce1():
Player1.right(180)
Player1.forward(10)
def bounce2():
Player2.right(180)
Player2.forward(10)
turtle.listen()
turtle.onkeypress(up1,"t")
turtle.onkeypress(down1,"g")
turtle.onkeypress(left1,"f")
turtle.onkeypress(right1,"h")
turtle.onkeypress(up2,"Up")
turtle.onkeypress(down2,"Down")
turtle.onkeypress(left2,"Left")
turtle.onkeypress(right2,"Right")
if Player1.xcor()<-395 or Player1.xcor()>395 or Player1.ycor()<-395 or Player1.ycor()>395:
bounce1()
if Player2.xcor()<-395 or Player2.xcor()>395 or Player2.ycor()<-395 or Player2.ycor()>395:
bounce2()
while True:
if Score==50:
Pen2.write("Times up!, Orange wins",font=("Arial",30,"bold"))
Player1.forward(0)
Player2.forward(0)
if Player1.distance(Player2)<10:
Pen.write("Black has tagged Orange's backside!!!!!!! with score {0}".format(Score),font=("Arial",30,"bold"))
break
如果解决了问题,您可以点赞采纳这个答案。
输出
我正在用 Python 乌龟创建一个标记游戏。我已经创建了一个成功的机制,可以在其中一名玩家被标记时停止游戏。屏幕上的海龟也可以控制成功。现在我正在添加其他功能,其中之一是在游戏开始时启动的计时器,并在标记一名玩家时停止。之后,显示对方玩家存活的时间。无论我尝试过什么样式,代码只有在我删除时间功能代码时才有效。如果我包含时间功能代码,所有代码都将无法工作,包括标记机制。 我有什么方法可以在不干扰主代码的情况下包含时间功能吗?谢谢! :)
Player1=turtle.Pen()
Player1.up()
Player1.goto(100,0)
Playground=turtle.Screen()
Playground.title("BLACK NEEDS TO TAG ORANGE ON THE BACKSIDE")
Playground.setup(800,800)
Player1.shape("turtle")
Player1.color("black")
turtle.bgcolor("lawngreen")
Player1.speed(0)
Player1.pensize(10)
Player2=turtle.Pen()
Player2.up()
Player2.goto(-100,0)
Player2.shape("turtle")
Player2.color("orange")
Player2.speed(0)
Player2.pensize(10)
Pen=turtle.Pen()
Pen.up()
Pen.goto(-300,300)
Pen.down()
Pen.hideturtle
Pen2=turtle.Pen()
Pen2.up()
Pen2.goto(-300,100)
Pen2.down()
Pen2.hideturtle
def up1():
Player1.setheading(90)
Player1.forward(20)
def down1():
Player1.setheading(270)
Player1.forward(20)
def left1():
Player1.setheading(180)
Player1.forward(20)
def right1():
Player1.setheading(0)
Player1.forward(20)
def up2():
Player2.setheading(90)
Player2.forward(20)
def down2():
Player2.setheading(270)
Player2.forward(20)
def left2():
Player2.setheading(180)
Player2.forward(20)
def right2():
Player2.setheading(0)
Player2.forward(20)
def bounce1():
Player1.right(180)
Player1.forward(10)
def bounce2():
Player2.right(180)
Player2.forward(10)
turtle.listen()
turtle.onkeypress(up1,"t")
turtle.onkeypress(down1,"g")
turtle.onkeypress(left1,"f")
turtle.onkeypress(right1,"h")
turtle.onkeypress(up2,"Up")
turtle.onkeypress(down2,"Down")
turtle.onkeypress(left2,"Left")
turtle.onkeypress(right2,"Right")
import time
Score=0
for i in range(50):
time.sleep(1)
Score=Score+1
if Player1.xcor<-395 or Player1.xcor>395 or Player1.ycor<-395 or Player1.ycor>395:
bounce1()
if Player2.xcor<-395 or Player2.xcor>395 or Player2.ycor<-395 or Player2.ycor>395:
bounce2()
if Score==50:
Pen2.write("Times up!, Orange wins",font=("Arial",30,"bold"))
while True:
Player1.forward(0)
Player2.forward(0)
if Player1.distance(Player2)<10:
Pen.write("Black has tagged Orange's backside!!!!!!!",font=("Arial",30,"bold"))
break```
您评论中的错误是因为您在 xcor
和 ycor
之后错过了 ()
。这样就变成了。
if Player1.xcor()<-395 or Player1.xcor()>395 or Player1.ycor()<-395 or Player1.ycor()>395:
bounce1()
if Player2.xcor()<-395 or Player2.xcor()>395 or Player2.ycor()<-395 or Player2.ycor()>395:
bounce2()
至于分数每秒都在增加,您需要有一个在后台运行的线程。
Score = 0
def Start_Timer():
global Score
for i in range(50):
time.sleep(1)
Score=Score+1
Timer = threading.Thread(target=Start_Timer, daemon=True)
Timer.start()
合并所有
import threading
import turtle
Score = 0
def Start_Timer():
global Score
for i in range(50):
time.sleep(1)
Score=Score+1
Timer = threading.Thread(target=Start_Timer, daemon=True)
Timer.start()
Player1=turtle.Pen()
Player1.up()
Player1.goto(100,0)
Playground=turtle.Screen()
Playground.title("BLACK NEEDS TO TAG ORANGE ON THE BACKSIDE")
Playground.setup(800,800)
Player1.shape("turtle")
Player1.color("black")
turtle.bgcolor("lawngreen")
Player1.speed(0)
Player1.pensize(10)
Player2=turtle.Pen()
Player2.up()
Player2.goto(-100,0)
Player2.shape("turtle")
Player2.color("orange")
Player2.speed(0)
Player2.pensize(10)
Pen=turtle.Pen()
Pen.up()
Pen.goto(-300,300)
Pen.down()
Pen.hideturtle
Pen2=turtle.Pen()
Pen2.up()
Pen2.goto(-300,100)
Pen2.down()
Pen2.hideturtle
def up1():
Player1.setheading(90)
Player1.forward(20)
def down1():
Player1.setheading(270)
Player1.forward(20)
def left1():
Player1.setheading(180)
Player1.forward(20)
def right1():
Player1.setheading(0)
Player1.forward(20)
def up2():
Player2.setheading(90)
Player2.forward(20)
def down2():
Player2.setheading(270)
Player2.forward(20)
def left2():
Player2.setheading(180)
Player2.forward(20)
def right2():
Player2.setheading(0)
Player2.forward(20)
def bounce1():
Player1.right(180)
Player1.forward(10)
def bounce2():
Player2.right(180)
Player2.forward(10)
turtle.listen()
turtle.onkeypress(up1,"t")
turtle.onkeypress(down1,"g")
turtle.onkeypress(left1,"f")
turtle.onkeypress(right1,"h")
turtle.onkeypress(up2,"Up")
turtle.onkeypress(down2,"Down")
turtle.onkeypress(left2,"Left")
turtle.onkeypress(right2,"Right")
if Player1.xcor()<-395 or Player1.xcor()>395 or Player1.ycor()<-395 or Player1.ycor()>395:
bounce1()
if Player2.xcor()<-395 or Player2.xcor()>395 or Player2.ycor()<-395 or Player2.ycor()>395:
bounce2()
while True:
if Score==50:
Pen2.write("Times up!, Orange wins",font=("Arial",30,"bold"))
Player1.forward(0)
Player2.forward(0)
if Player1.distance(Player2)<10:
Pen.write("Black has tagged Orange's backside!!!!!!! with score {0}".format(Score),font=("Arial",30,"bold"))
break
如果解决了问题,您可以点赞采纳这个答案。
输出