“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()
我试着下面一点优化你的代码,使子弹的反应更迅速发射时。我做空间方,而不是长方形,所以没有在某些方向上再烧制比别人面前较长的延迟。我还参数,所以你可以选择不同大小的空间,它应该调整。我消除了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图像是简单,但图像将不宜与甲鱼转。这需要更多的图像,更多的代码和更多的时间。我想与你的三角形光标移到基于图像的飞船坚持。
或者,您可以使用多个带颜色的多边形绘制龟飞船,并设置为你的光标 - 它应该正确使用光标转动。看到这个qazxsw POI。