1

我正在为 Python 使用 turtle 模块,并且正在尝试制作游戏。我试图让一只乌龟射出另一只乌龟作为子弹。我无法弄清楚如何跟踪射手龟的xcor()ycor()然后将子弹龟运送到那里。这是我的代码:


# Projectiles

bullet = turtle.Turtle()
bullet.hideturtle()
bullet.color('purple')
bullet.shape('circle')
bullet.penup()
bullet.shapesize(0.5, 0.5)

bulletspeed = 0


# The player  

player = turtle.Turtle()
player.speed(0)
player.color('blue')
player.shape('triangle')
player.penup()
player.setposition(0, -250)
player.setheading(90)

x = player.xcor()
y = player.ycor()
ph = player.heading()

def right():
    player.right(30) 

def left():
    player.left(30)

def increasespeed():
    global speed
    speed += 1

def decreasespeed():
    global speed
    speed -= 1

def bulletmove():
    global bulletspeed
    bullet.pos() == player.pos()
    bullet.showturtle()
    bulletspeed += 10


# Keybindings

turtle.listen()
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
turtle.onkey(increasespeed, 'Up')
turtle.onkey(decreasespeed, 'Down')
turtle.onkey(bulletmove, 'space')
4

2 回答 2

0

在这里,我完全重写了您的代码。它仍然有点问题,但那是因为turtle模块本身。我不得不转动播放器,但它有效。

import turtle

# Projectiles

bullet = turtle.Turtle()

bullet.color('purple')
bullet.penup()
bullet.speed("fastest")
bullet.hideturtle()
bullet.shape("circle")

bullet.setposition(0, -250)



bulletspeed = 0

s = turtle.Screen()
# The player  


player = turtle.Turtle()
player.speed("fastest")
player.color('blue')
player.shape('triangle')
player.penup()
player.setposition(0, 0)
player.setheading(90)
#player.hideturtle()
x = player.xcor()
y = player.ycor()
ph = player.heading()
player.right(180)

speed = 5

def right():
    player.right(30) 

def left():
    player.left(30)

def increasespeed():
    global speed
    speed += 1

def decreasespeed():
    global speed
    speed -= 1


def bulletmove():
    global bulletspeed
    bullet.hideturtle()
    turtlex = 0
    turtley = -250
    bullet.setposition(turtlex, turtley)

    bullet.reset()
    bullet.penup()

    bullet.goto(0, -250)




# Keybindings

turtle.listen()
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
turtle.onkey(increasespeed, 'Up')
turtle.onkey(decreasespeed, 'Down')
turtle.onkey(bulletmove, 'space')

#s.tracer(0, 0)
s.exitonclick()

另外,我建议用移动子弹制作游戏,不建议使用乌龟。我会建议pygame

希望这可以帮助!

于 2020-05-19T04:50:59.177 回答
0

不要相信那些试图在不同图书馆向你推销你的乌龟的例子。我并不是说 pygame 不适合这种程序,只是说 turtle 的能力经常被低估。

下面是对您的代码的重写,它允许您旋转和射击——一次多发子弹!我扔掉了你的增加和减少速度功能,以保持示例简单:

from turtle import Screen, Turtle

def right():
    player.right(30)
    screen.update()

def left():
    player.left(30)
    screen.update()

def move_bullet(bullet):
    screen.onkey(None, 'space')  # disable handler inside handler

    if bullet.distance(player) < 300:
        bullet.forward(10)
        screen.ontimer(lambda: move_bullet(bullet), 100)
    else:
        bullet.hideturtle()
        bullets.append(bullet)  # reduce, reuse & recycle

    screen.update()
    screen.onkey(fire_bullet, 'space')  # reenable handler

def fire_bullet():
    bullet = bullets.pop() if bullets else bullet_prototype.clone()

    bullet.setposition(player.position())
    bullet.setheading(player.heading())
    bullet.showturtle()

    move_bullet(bullet)

screen = Screen()
screen.tracer(False)

bullet_prototype = Turtle()
bullet_prototype.hideturtle()
bullet_prototype.shape('circle')
bullet_prototype.shapesize(0.5)
bullet_prototype.color('purple')
bullet_prototype.penup()

bullets = []

player = Turtle()
player.shape('triangle')
player.color('blue')
player.penup()
player.setheading(90)

screen.onkey(left, 'Left')
screen.onkey(right, 'Right')
screen.onkey(fire_bullet, 'space')
screen.listen()

screen.update()
screen.mainloop()

在此处输入图像描述

于 2020-05-19T05:53:36.707 回答