小游戏中的记分牌似乎一直在覆盖它自己,我将如何解决这个问题才能清楚地看到记分牌



[https://gist.github.com/Ralph94/125934b8aaaf28764c30d0fbe2b18419]

import turtle  # Our module's
import random

score = 0
lives = 3


# Screen
ms = turtle.Screen()
ms.title("Falling Piece's mini_game by Rafa94")
ms.bgcolor("purple")
ms.setup(width=800, height=600)
ms.tracer(0.0)

# player
player = turtle.Turtle()
player.shape("turtle")
player.color("blue")
player.penup()
player.goto(0, -250)  # y is postive in the up direction y is negative in the down direction
player.speed(0)
player.direction = "stop"

# create a list of good players
goods = []  # Empty list

# Addgood players
for _ in range(20):  # we are making a set of 20 players
    good = turtle.Turtle()  # we want the other player basically across from each other thats we copyed the code one on -y and one on +y (first player in the bottom, second player on top of Screen)
    good.speed(0)
    good.shape("circle")
    good.color("red")
    good.penup()
    good.goto(-100, 250)  # y is postive in the up direction y is negative in the down direction
    good.speed = random.randint(1, 4)
    goods.append(good)

# create a list of bad players
bads = []  # Empty list

# Addbad players
for _ in range(20):  # we are making a set of 20 players
    bad = turtle.Turtle()  # we want the other player basically across from each other thats we copyed the code one on -y and one on +y (first player in the bottom, second player on top of Screen)
    bad.speed(0)
    bad.shape("circle")
    bad.color("yellow")
    bad.penup()
    bad.goto(100, 250)  # y is postive in the up direction y is negative in the down direction
    bad.speed = random.randint(1, 4)
    bads.append(bad)

    # make the pen
    pen = turtle.Turtle()
    pen.hideturtle()
    pen.speed(0)
    pen.shape("turtle")
    pen.color("blue")
    pen.penup()
    pen.goto(0, 260)  # y is postive in the up direction y is negative in the down direction
    font = ("Courier", 24, "normal")
    pen.write("Score: {} Lives: {}".format(score, lives), align="center", font=font)





# Functions
def go_left():
    player.direction = "left"


def go_right():
    player.direction = "right"


# keyboard Binding
ms.listen()  # it is bascally saying listen for keyboard input < ^ >
ms.onkeypress(go_left, "Left")
ms.onkeypress(go_right, "Right")

# Main game loop # while something is true it will repeat
while True:
    # update screen
    ms.update()

    # Move player
    if player.direction == "left":
        x = player.xcor()
        x -= + 3
        player.setx(x)

    if player.direction == "right":
        x = player.xcor()
        x += + 3
        player.setx(x)

    # Move Good Player
    for good in goods:
        y = good.ycor()
        y -= good.speed  # We want the ball to be falling at a smooth speed
        good.sety(y)
        # Check it off the Screen
        if y < -300:
            x = random.randint(-380, 380)
            y = random.randint(300, 400)
            good.goto(x, y)

        # check for collision with player
        if good.distance(player) < 20:
            x = random.randint(-380, 380)
            y = random.randint(300, 400)
            good.goto(x, y)
            score += 10
            pen.clear()
            pen.write("Score: {} Lives: {}".format(score, lives), align="center", font=font)



    # Move bad Player
    for bad in bads:
        y = bad.ycor()
        y -= bad.speed  # We want the ball to be falling at a slow speed
        bad.sety(y)
        # Check it off the Screen
        if y < -300:
            x = random.randint(-380, 380)
            y = random.randint(300, 400)
            bad.goto(x, y)

        # check for collision with player
        if bad.distance(player) < 20:
            x = random.randint(-380, 380)
            y = random.randint(300, 400)
            bad.goto(x, y)
            score -= 10
            lives -= 1
            pen.clear()
            pen.write("Score: {} Lives: {}".format(score, lives), align="center", font=font)

ms.mainloop()

你有错误的缩进,你在 -loop 中for pen,所以你创建了 20 支笔,每支笔都写字符串。稍后pen.clear()只能删除最后一个字符串,但仍有其他 19 支笔创建的其他字符串。

for循环后创建pen,您将只有一支笔,它将正常工作。

相关内容

最新更新