0

我在 Windows 上用 Turtle 创建了一个基本的 Pong 游戏,效果很好,但每次我关闭它时,它都会抛出一个错误:“检测到致命错误:无法执行脚本 pong”。在寻求帮助之前,我一直在寻找解决方案,但我找不到合适的答案。

为了创建 .exe 文件,我使用了“pyinstaller”。这是我写的确切命令:

pyinstaller --onefile --windowed --add-data "bounce.wav;." --add-data "collision.wav;." --add-data "score.wav;." 乒乓球

这是我的代码:

"""
Basic pong game
"""

import turtle
import winsound

import os
import sys

win = turtle.Screen()
win.title("Pong")
win.bgcolor("black")
win.setup(width=800, height=600)
win.tracer(0)

# Score
score_a = 0
score_b = 0

# Paddle A
paddle_a = turtle.Turtle()
name_a = "Verde"
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("green")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)

# Paddle B
paddle_b = turtle.Turtle()
name_b = "Amarillo"
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("yellow")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.penup()
paddle_b.goto(350, 0)

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.15
ball.dy = -0.15

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("{} 0 | 0 {}".format(name_a, name_b), align="center", font=("Courier", 24, "normal"))

# Functions
def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath('.'), relative_path)


def paddle_a_up():
    if paddle_a.ycor() < 240:
        y = paddle_a.ycor()
        y += 20
        paddle_a.sety(y)

def paddle_a_down():
    if paddle_a.ycor() > -240:
        y = paddle_a.ycor()
        y -= 20
        paddle_a.sety(y)

def paddle_b_up():
    if paddle_b.ycor() < 240:
        y = paddle_b.ycor()
        y += 20
        paddle_b.sety(y)

def paddle_b_down():
    if paddle_b.ycor() > -240:
        y = paddle_b.ycor()
        y -= 20
        paddle_b.sety(y)

# Keyboard binding
win.listen()
win.onkeypress(paddle_a_up, "w")
win.onkeypress(paddle_a_down, "s")
win.onkeypress(paddle_b_up, "Up")
win.onkeypress(paddle_b_down, "Down")

# Main game loop
while True:
    win.update()

    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # Border checking
    if ball.ycor() > 290:
        winsound.PlaySound(resource_path("bounce.wav"), winsound.SND_ASYNC)
        ball.sety(290)
        ball.dy *= -1

    if ball.ycor() < -290:
        winsound.PlaySound(resource_path("bounce.wav"), winsound.SND_ASYNC)
        ball.sety(-290)
        ball.dy *= -1

    if ball.xcor() > 390:
        winsound.PlaySound(resource_path("score.wav"), winsound.SND_ASYNC)
        ball.goto(0, 0)
        ball.dx *= -1
        score_a += 1
        pen.clear()
        pen.write("{} {} | {} {}".format(name_a, score_a, score_b, name_b), align="center", font=("Courier", 24, "normal"))

    if ball.xcor() < -390:
        winsound.PlaySound(resource_path("score.wav"), winsound.SND_ASYNC)
        ball.goto(0, 0)
        ball.dx *= -1
        score_b += 1
        pen.clear()
        pen.write("{} {} | {} {}".format(name_a, score_a, score_b, name_b), align="center", font=("Courier", 24, "normal"))

    # Paddle and ball collisions
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50):
        winsound.PlaySound(resource_path("collision.wav"), winsound.SND_ASYNC)
        ball.setx(340)
        ball.dx *= -1

    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50):
        winsound.PlaySound(resource_path("collision.wav"), winsound.SND_ASYNC)
        ball.setx(-340)
        ball.dx *= -1
"""
End code
"""

我不知道是什么导致了这个错误,因为程序运行良好并且所有音效都正常工作。

谢谢大家的答案!

4

1 回答 1

0

嘿,不确定您是否还需要这方面的帮助,但第 103 行似乎引起了问题:

ball.setx(ball.xcor() + ball.dx)

就像你说的,它只在关闭应用程序时发生。我不确定是否有合适的解决方案,但是可以用来避免 pyinstaller 抛出错误的创可贴解决方案是将整个 while 循环包装在 try/catch 中。

# Main game loop
try:
    while True:
        win.update()

        # Move the ball
        ball.setx(ball.xcor() + ball.dx)
        ball.sety(ball.ycor() + ball.dy)

        # Border checking
        if ball.ycor() > 290:
            winsound.PlaySound(resource_path("bounce.wav"), winsound.SND_ASYNC)
            ball.sety(290)
            ball.dy *= -1

        if ball.ycor() < -290:
            winsound.PlaySound(resource_path("bounce.wav"), winsound.SND_ASYNC)
            ball.sety(-290)
            ball.dy *= -1

        if ball.xcor() > 390:
            winsound.PlaySound(resource_path("score.wav"), winsound.SND_ASYNC)
            ball.goto(0, 0)
            ball.dx *= -1
            score_a += 1
            pen.clear()
            pen.write("{} {} | {} {}".format(name_a, score_a, score_b, name_b), align="center", font=("Courier", 24, "normal"))

        if ball.xcor() < -390:
            winsound.PlaySound(resource_path("score.wav"), winsound.SND_ASYNC)
            ball.goto(0, 0)
            ball.dx *= -1
            score_b += 1
            pen.clear()
            pen.write("{} {} | {} {}".format(name_a, score_a, score_b, name_b), align="center", font=("Courier", 24, "normal"))

        # Paddle and ball collisions
        if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50):
            winsound.PlaySound(resource_path("collision.wav"), winsound.SND_ASYNC)
            ball.setx(340)
            ball.dx *= -1

        if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50):
            winsound.PlaySound(resource_path("collision.wav"), winsound.SND_ASYNC)
            ball.setx(-340)
            ball.dx *= -1
except Exception:
    sys.exit(0)

这不是我承认的最佳解决方案,但我希望这会有所帮助!

于 2021-01-03T03:53:08.597 回答