射击时如何使飞船不停?

时间:2019-02-05 04:24:37

标签: python turtle-graphics

“ q”将移动,因此当您按“ q”时,将朝鼠标方向移动,而当您再次按“ q”时,将停止。当您单击鼠标时,飞船将发射子弹,但会暂停一秒钟。有没有办法使其不暂停。您还可以使三角形成为火箭图片吗?或背景图片为空格。

代码:

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()

1 个答案:

答案 0 :(得分:1)

我在下面尝试优化您的代码,以使子弹在发射时反应更快。我将空间设为正方形,而不是矩形,因此在某些方向上发射之前,没有比在其他方向上更长的延迟。我还对其进行了参数化,因此您可以选择其他大小的空间,并且它应该进行调整。我消除了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()

要设置背景,请找到尺寸大于您需要的GIF格式的空间图像(而不是动画图像)。对其进行缩小编辑,然后使用screen.bgpic("space.gif")作为背景。

使用船只图像是一个比较困难的问题。设置乌龟使用GIF图像很简单,但是图像不会随乌龟一起旋转。这需要更多图像,更多代码和更多时间。我会将您的三角形光标停留在基于图像的太空飞船上。

或者,您可以使用多个彩色多边形在海龟中绘制一艘太空船,并将其设置为光标-它应随光标旋转。参见此example involving a tank-shaped cursor