有没有办法让海龟图形中的两个闪烁对象在不同情况下工作?
Is there a way to make two flashing objects in turtle graphic work in different situations?
我正在制作一款射击游戏。我现在正在研究一种机制,当子弹准备好射击时,两盏灯会闪烁,当子弹在其他地方而无法射击时,另一盏灯会闪烁。我使用笔作为灯,使用 hideturtle() 和 showturtle() 让笔闪烁。
#Shooting game
import turtle
import time
#Setting up thingies and background
Arena=turtle.Screen()
Arena.bgcolor("lawngreen")
Arena.setup(1000,1000)
Arena.title("BLACK HAS TO BANG WHITE WITH BOMBS")
#Setting up the cannon
Banger=turtle.Pen()
Banger.up()
Banger.shape("square")
Banger.shapesize(2,8)
Banger.goto(-440,-300)
Banger.color("black")
Banger.setheading(180)
#Setting the ready "light"
Ready=turtle.Pen()
Ready.hideturtle()
Ready.up()
Ready.shape("square")
Ready.color("green")
Ready.shapesize(2,2)
Ready.goto(290,300)
#setting the not ready "light"
Notready=turtle.Pen()
Notready.hideturtle()
Notready.up()
Notready.shape("square")
Notready.color("red")
Notready.shapesize(2,2)
Notready.goto(300,300)
#Setting the bullet
Bullet=turtle.Pen()
Bullet.up()
Bullet.shape("circle")
Bullet.shapesize(2)
Bullet.color("darkblue")
Bullet.goto(-440,-300)
Bullet.speed(5)
#Setting up moving and interacting functions
def moveup():
Banger.left(10)
Bullet.left(10)
def movedown():
Banger.right(10)
Bullet.right(10)
def shoot():
Bullet.showturtle()
Bullet.forward(600)
time.sleep(1)
Bullet.hideturtle()
Bullet.goto(-440,-300)
#Setting the signal for the "lights" to react
if Bullet.xcor()==-440 and Bullet.ycor==-300:
Ready.showturtle()
Notready.hideturtle()
else:
Notready.showturtle()
Ready.hideturtle()
#Setting up the keybinds
turtle.listen()
turtle.onkeypress(moveup,"t")
turtle.onkeypress(movedown,"g")
turtle.onkeypress(shoot,"1")
turtle.mainloop()
我尝试了很多方法,比如 if 和 else 语句,甚至线程。但不知何故对我来说,“灯”甚至没有闪烁一次。我已经投入了很多时间,非常感谢您的帮助,非常感谢! :D
Ps。此代码中的其他所有内容均有效,但如果您发现任何其他问题,我也将接受您的帮助
问题是你在顶层有这个逻辑,它只能执行一次,所以没有好处:
#Setting the signal for the "lights" to react
if Bullet.xcor()==-440 and Bullet.ycor==-300:
Ready.showturtle()
Notready.hideturtle()
else:
Notready.showturtle()
Ready.hideturtle()
我们需要将其整合到子弹逻辑中:
from turtle import Screen, Turtle
# Moving and interacting functions
def moveup():
banger.left(10)
def movedown():
banger.right(10)
def shoot():
screen.onkeypress(None, '1')
if bullet.isvisible():
if bullet.distance(banger) > 600:
bullet.hideturtle()
ready.color('green')
screen.onkeypress(shoot, '1')
return
bullet.forward(5)
else:
ready.color('red')
bullet.goto(banger.position())
bullet.setheading(banger.heading())
bullet.forward(80)
bullet.showturtle()
screen.ontimer(shoot, 10)
# Set up screen
screen = Screen()
screen.setup(1000, 1000)
screen.bgcolor('lawngreen')
screen.title("BLACK HAS TO BANG WHITE WITH BOMBS")
# Set up the cannon
banger = Turtle()
banger.shape('square')
banger.shapesize(2, 8)
banger.penup()
banger.goto(-440, -300)
# Set up the ready 'light'
ready = Turtle()
ready.shape('square')
ready.shapesize(2)
ready.color('green')
ready.penup()
ready.goto(290, 300)
# Set up the bullet
bullet = Turtle()
bullet.hideturtle()
bullet.shape('circle')
bullet.shapesize(2)
bullet.color('darkblue')
bullet.penup()
bullet.speed('fastest')
# Set up the keybindings
screen.onkeypress(moveup, 't')
screen.onkeypress(movedown, 'g')
screen.onkeypress(shoot, '1')
screen.listen()
screen.mainloop()
你会注意到我通过添加一个定时器使程序变得有点复杂,但这允许你在你的子弹穿过空气时瞄准你的大炮,为你的下一次射击做准备。
我正在制作一款射击游戏。我现在正在研究一种机制,当子弹准备好射击时,两盏灯会闪烁,当子弹在其他地方而无法射击时,另一盏灯会闪烁。我使用笔作为灯,使用 hideturtle() 和 showturtle() 让笔闪烁。
#Shooting game
import turtle
import time
#Setting up thingies and background
Arena=turtle.Screen()
Arena.bgcolor("lawngreen")
Arena.setup(1000,1000)
Arena.title("BLACK HAS TO BANG WHITE WITH BOMBS")
#Setting up the cannon
Banger=turtle.Pen()
Banger.up()
Banger.shape("square")
Banger.shapesize(2,8)
Banger.goto(-440,-300)
Banger.color("black")
Banger.setheading(180)
#Setting the ready "light"
Ready=turtle.Pen()
Ready.hideturtle()
Ready.up()
Ready.shape("square")
Ready.color("green")
Ready.shapesize(2,2)
Ready.goto(290,300)
#setting the not ready "light"
Notready=turtle.Pen()
Notready.hideturtle()
Notready.up()
Notready.shape("square")
Notready.color("red")
Notready.shapesize(2,2)
Notready.goto(300,300)
#Setting the bullet
Bullet=turtle.Pen()
Bullet.up()
Bullet.shape("circle")
Bullet.shapesize(2)
Bullet.color("darkblue")
Bullet.goto(-440,-300)
Bullet.speed(5)
#Setting up moving and interacting functions
def moveup():
Banger.left(10)
Bullet.left(10)
def movedown():
Banger.right(10)
Bullet.right(10)
def shoot():
Bullet.showturtle()
Bullet.forward(600)
time.sleep(1)
Bullet.hideturtle()
Bullet.goto(-440,-300)
#Setting the signal for the "lights" to react
if Bullet.xcor()==-440 and Bullet.ycor==-300:
Ready.showturtle()
Notready.hideturtle()
else:
Notready.showturtle()
Ready.hideturtle()
#Setting up the keybinds
turtle.listen()
turtle.onkeypress(moveup,"t")
turtle.onkeypress(movedown,"g")
turtle.onkeypress(shoot,"1")
turtle.mainloop()
我尝试了很多方法,比如 if 和 else 语句,甚至线程。但不知何故对我来说,“灯”甚至没有闪烁一次。我已经投入了很多时间,非常感谢您的帮助,非常感谢! :D
Ps。此代码中的其他所有内容均有效,但如果您发现任何其他问题,我也将接受您的帮助
问题是你在顶层有这个逻辑,它只能执行一次,所以没有好处:
#Setting the signal for the "lights" to react
if Bullet.xcor()==-440 and Bullet.ycor==-300:
Ready.showturtle()
Notready.hideturtle()
else:
Notready.showturtle()
Ready.hideturtle()
我们需要将其整合到子弹逻辑中:
from turtle import Screen, Turtle
# Moving and interacting functions
def moveup():
banger.left(10)
def movedown():
banger.right(10)
def shoot():
screen.onkeypress(None, '1')
if bullet.isvisible():
if bullet.distance(banger) > 600:
bullet.hideturtle()
ready.color('green')
screen.onkeypress(shoot, '1')
return
bullet.forward(5)
else:
ready.color('red')
bullet.goto(banger.position())
bullet.setheading(banger.heading())
bullet.forward(80)
bullet.showturtle()
screen.ontimer(shoot, 10)
# Set up screen
screen = Screen()
screen.setup(1000, 1000)
screen.bgcolor('lawngreen')
screen.title("BLACK HAS TO BANG WHITE WITH BOMBS")
# Set up the cannon
banger = Turtle()
banger.shape('square')
banger.shapesize(2, 8)
banger.penup()
banger.goto(-440, -300)
# Set up the ready 'light'
ready = Turtle()
ready.shape('square')
ready.shapesize(2)
ready.color('green')
ready.penup()
ready.goto(290, 300)
# Set up the bullet
bullet = Turtle()
bullet.hideturtle()
bullet.shape('circle')
bullet.shapesize(2)
bullet.color('darkblue')
bullet.penup()
bullet.speed('fastest')
# Set up the keybindings
screen.onkeypress(moveup, 't')
screen.onkeypress(movedown, 'g')
screen.onkeypress(shoot, '1')
screen.listen()
screen.mainloop()
你会注意到我通过添加一个定时器使程序变得有点复杂,但这允许你在你的子弹穿过空气时瞄准你的大炮,为你的下一次射击做准备。