射击时如何让船不停?
How to make ship not stop when shooting?
"q" 是移动,所以当您按下 "q" 时,您会朝鼠标的方向移动,当您再次按下 "q" 时,您会停止。当您单击鼠标时,飞船会发射一颗子弹,但会暂停一秒钟。有没有办法让它不暂停。你也可以把三角形做成火箭图片吗?或者背景图片一张space.
代码:
from turtle import Screen, Turtle, Vec2D, Turtle
import turtle
import time
screen = turtle.Screen()
screen.title("Test")
screen.setup(width=1000, height=650)
ship = turtle.Turtle()
ship.shape("triangle")
ship.turtlesize(3)
ship.speed('fast')
ship.penup()
bullet = turtle.Turtle()
bullet.shape("circle")
bullet.speed('fast')
bullet.color("green")
bullet.penup()
bullet.hideturtle()
bullet.goto(-525, -350)
bullet.hideturtle()
bullet.turtlesize(0.5)
shot=0
coin=-1
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(Vec2D(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale))
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(position):
global target
onmove(screen, None)
target = position
onmove(screen, goto_handler)
def move():
global shot
if -350<bullet.ycor() and bullet.ycor()<350 and -525<bullet.xcor() and bullet.xcor()<525:
bullet.forward(15)
ship.setheading(ship.towards(target))
if coin==1:
ship.forward(5)
if shot==1:
bullet.hideturtle()
bullet.goto(ship.xcor(), ship.ycor())
bullet.setheading(ship.heading())
bullet.forward(15)
bullet.showturtle()
shot=0
if -275>ship.ycor() or ship.ycor()>275 or -450>ship.xcor() or ship.xcor()>450:
ship.backward(5)
screen.ontimer(move, 50)
def shoot(x, y):
global shot
if not(-350<bullet.ycor() and bullet.ycor()<350 and -525<bullet.xcor() and bullet.xcor()<525):
shot=1
def forward():
global coin
coin=-coin
target = (0, 0)
onmove(screen, goto_handler)
move()
screen.onscreenclick(shoot)
screen.onkeypress(forward, "q")
screen.listen()
screen.mainloop()
我已经尝试在下面稍微优化一下您的代码,使子弹在发射时反应更快。我制作了 space 正方形,而不是长方形,因此在某些方向重新开火之前没有比其他方向更长的延迟。我还对其进行了参数化,因此您可以选择不同的大小 space,它应该会进行调整。我删除了 shot
变量并改用海龟的可见性状态:
from turtle import Screen, Turtle, Vec2D
SPACE = 825
CURSOR_SIZE = 20
BULLET_SIZE = 10
BULLET_BOUNDS = SPACE/2 + BULLET_SIZE/2
SHIP_SIZE = 60
SHIP_BOUNDS = SPACE/2 - SHIP_SIZE/2
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(Vec2D(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale))
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(position):
global target
onmove(screen, None)
target = position
onmove(screen, goto_handler)
def move():
x, y = bullet.position()
if -BULLET_BOUNDS < x < BULLET_BOUNDS and -BULLET_BOUNDS < y < BULLET_BOUNDS:
bullet.forward(15)
else:
bullet.hideturtle()
ship.setheading(ship.towards(target))
if coin:
x, y = ship.position()
if -SHIP_BOUNDS < x < SHIP_BOUNDS and -SHIP_BOUNDS < y < SHIP_BOUNDS:
pass
else:
ship.setheading(ship.towards((0, 0)))
ship.forward(5)
screen.ontimer(move, 50)
def shoot(x, y):
screen.onscreenclick(None)
if not bullet.isvisible():
bullet.goto(ship.position())
bullet.setheading(ship.heading())
bullet.showturtle()
bullet.forward(30)
screen.onscreenclick(shoot)
def forward():
global coin
coin = not coin
screen = Screen()
screen.setup(width=SPACE, height=SPACE)
ship = Turtle("triangle")
ship.turtlesize(SHIP_SIZE / CURSOR_SIZE)
ship.speed('fastest')
ship.penup()
bullet = Turtle("circle", visible=False)
bullet.turtlesize(BULLET_SIZE / CURSOR_SIZE)
bullet.speed('fastest')
bullet.color("green")
bullet.penup()
coin = False
target = (0, 0)
onmove(screen, goto_handler)
move()
screen.onscreenclick(shoot)
screen.onkeypress(forward, "q")
screen.listen()
screen.mainloop()
要设置背景,请找到比您需要的尺寸大的 space GIF 格式图片(但不是动画图片)。将其缩小到一定大小并使用 screen.bgpic("space.gif")
将其设为背景。
使用船舶图像是一个更难的问题。将海龟设置为使用 GIF 图像很简单,但图像不会随海龟一起转动。这需要更多图像、更多代码和更多时间。我会将你的三角形光标放在基于图像的 spaceship 上。
或者,您可以使用多个彩色多边形在海龟中绘制一艘 space飞船,并将其设置为您的光标——它应该随光标正确转动。看到这个 .
"q" 是移动,所以当您按下 "q" 时,您会朝鼠标的方向移动,当您再次按下 "q" 时,您会停止。当您单击鼠标时,飞船会发射一颗子弹,但会暂停一秒钟。有没有办法让它不暂停。你也可以把三角形做成火箭图片吗?或者背景图片一张space.
代码:
from turtle import Screen, Turtle, Vec2D, Turtle
import turtle
import time
screen = turtle.Screen()
screen.title("Test")
screen.setup(width=1000, height=650)
ship = turtle.Turtle()
ship.shape("triangle")
ship.turtlesize(3)
ship.speed('fast')
ship.penup()
bullet = turtle.Turtle()
bullet.shape("circle")
bullet.speed('fast')
bullet.color("green")
bullet.penup()
bullet.hideturtle()
bullet.goto(-525, -350)
bullet.hideturtle()
bullet.turtlesize(0.5)
shot=0
coin=-1
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(Vec2D(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale))
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(position):
global target
onmove(screen, None)
target = position
onmove(screen, goto_handler)
def move():
global shot
if -350<bullet.ycor() and bullet.ycor()<350 and -525<bullet.xcor() and bullet.xcor()<525:
bullet.forward(15)
ship.setheading(ship.towards(target))
if coin==1:
ship.forward(5)
if shot==1:
bullet.hideturtle()
bullet.goto(ship.xcor(), ship.ycor())
bullet.setheading(ship.heading())
bullet.forward(15)
bullet.showturtle()
shot=0
if -275>ship.ycor() or ship.ycor()>275 or -450>ship.xcor() or ship.xcor()>450:
ship.backward(5)
screen.ontimer(move, 50)
def shoot(x, y):
global shot
if not(-350<bullet.ycor() and bullet.ycor()<350 and -525<bullet.xcor() and bullet.xcor()<525):
shot=1
def forward():
global coin
coin=-coin
target = (0, 0)
onmove(screen, goto_handler)
move()
screen.onscreenclick(shoot)
screen.onkeypress(forward, "q")
screen.listen()
screen.mainloop()
我已经尝试在下面稍微优化一下您的代码,使子弹在发射时反应更快。我制作了 space 正方形,而不是长方形,因此在某些方向重新开火之前没有比其他方向更长的延迟。我还对其进行了参数化,因此您可以选择不同的大小 space,它应该会进行调整。我删除了 shot
变量并改用海龟的可见性状态:
from turtle import Screen, Turtle, Vec2D
SPACE = 825
CURSOR_SIZE = 20
BULLET_SIZE = 10
BULLET_BOUNDS = SPACE/2 + BULLET_SIZE/2
SHIP_SIZE = 60
SHIP_BOUNDS = SPACE/2 - SHIP_SIZE/2
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(Vec2D(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale))
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(position):
global target
onmove(screen, None)
target = position
onmove(screen, goto_handler)
def move():
x, y = bullet.position()
if -BULLET_BOUNDS < x < BULLET_BOUNDS and -BULLET_BOUNDS < y < BULLET_BOUNDS:
bullet.forward(15)
else:
bullet.hideturtle()
ship.setheading(ship.towards(target))
if coin:
x, y = ship.position()
if -SHIP_BOUNDS < x < SHIP_BOUNDS and -SHIP_BOUNDS < y < SHIP_BOUNDS:
pass
else:
ship.setheading(ship.towards((0, 0)))
ship.forward(5)
screen.ontimer(move, 50)
def shoot(x, y):
screen.onscreenclick(None)
if not bullet.isvisible():
bullet.goto(ship.position())
bullet.setheading(ship.heading())
bullet.showturtle()
bullet.forward(30)
screen.onscreenclick(shoot)
def forward():
global coin
coin = not coin
screen = Screen()
screen.setup(width=SPACE, height=SPACE)
ship = Turtle("triangle")
ship.turtlesize(SHIP_SIZE / CURSOR_SIZE)
ship.speed('fastest')
ship.penup()
bullet = Turtle("circle", visible=False)
bullet.turtlesize(BULLET_SIZE / CURSOR_SIZE)
bullet.speed('fastest')
bullet.color("green")
bullet.penup()
coin = False
target = (0, 0)
onmove(screen, goto_handler)
move()
screen.onscreenclick(shoot)
screen.onkeypress(forward, "q")
screen.listen()
screen.mainloop()
要设置背景,请找到比您需要的尺寸大的 space GIF 格式图片(但不是动画图片)。将其缩小到一定大小并使用 screen.bgpic("space.gif")
将其设为背景。
使用船舶图像是一个更难的问题。将海龟设置为使用 GIF 图像很简单,但图像不会随海龟一起转动。这需要更多图像、更多代码和更多时间。我会将你的三角形光标放在基于图像的 spaceship 上。
或者,您可以使用多个彩色多边形在海龟中绘制一艘 space飞船,并将其设置为您的光标——它应该随光标正确转动。看到这个