Pygame无效语法我无法弄清楚

时间:2023-01-27 14:51:16

I've been following a tutorial "McGugan - Beginning Game Development with Python and Pygame (Apress, 2007)" and in the code at around chapter five involving object movement I keep getting invalid syntax alerts on '-' being used in the code. It isn't up to date but I would've thought a subtract wouldn't be changed in any updates due to its simplicity and necessity.

我一直在关注一个教程“McGugan - 用Python和Pygame开始游戏开发(Apress,2007)”,并且在第五章涉及对象移动的代码中,我一直在代码中使用' - '上获得无效语法警报。它不是最新的,但我认为由于其简单性和必要性,任何更新都不会更改减法。

This is the code I have:

这是我的代码:

background_image_filename = 'sushiplate.jpg'
sprite_image_filename = 'fugu.png'
import pygame
from pygame.locals import *
from sys import exit
from gameobjects.vector2 import Vector2
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
background = pygame.image.load(background_image_filename).convert()
sprite = pygame.image.load(sprite_image_filename).convert_alpha()
clock = pygame.time.Clock()
position = Vector2(100.0, 100.0)
speed = 250.
heading = Vector2()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    if event.type == MOUSEBUTTONDOWN:
        destination = Vector2(*event.pos) – Vector2(*sprite.get_size())/2.
        heading = Vector2.from_points(position, destination)
        heading.normalize()
    screen.blit(background, (0,0))
    screen.blit(sprite, position)
    time_passed = clock.tick()
    time_passed_seconds = time_passed / 1000.0
    distance_moved = time_passed_seconds * speed
    position += heading * distance_moved
    pygame.display.update()

am I doing something wrong or is it just simply outdated?

我做错了什么还是只是过时了?

Any help is much needed.

非常需要任何帮助。

3 个解决方案

#1


In this line:

在这一行:

destination = Vector2(*event.pos) – Vector2(*sprite.get_size())/2.

You somehow typed the character "" (EN DASH) instead of "-" (HYPHEN-MINUS). Use "-" (HYPHEN-MINUS) instead, like this:

你以某种方式输入了字符“ - ”(EN DASH)而不是“ - ”(HYPHEN-MINUS)。改为使用“ - ”(HYPHEN-MINUS),如下所示:

destination = Vector2(*event.pos) - Vector2(*sprite.get_size())/2.

#2


I can't be sure without a stack trace, but I have a hunch that it's the wrong - symbol. What editor are you using? Is it possible that your editor is taking the - symbol and turning it into a fancier dash, like an ndash or an mdash?

没有堆栈跟踪我无法确定,但我有一种预感,这是错误的 - 符号。你在用什么编辑器?您的编辑是否可能使用 - 符号并将其转换为更高级的短划线,如ndash或mdash?

#3


Maybe try changing speed to "speed = 250.0". I don't know if that dangling dot would throw python off.

也许尝试将速度改为“速度= 250.0”。我不知道那个悬空点是否会抛出python。

What is going on here, with your error message at least, is the Python parser is stumbling over something before your '-', which screws up its interpretation of '-'. So I recommend looking before the '-' for typos.

这里发生的事情,至少是你的错误消息,是Python解析器在你的' - '之前绊倒了什么,这搞砸了它对' - '的解释。所以我建议在' - '之前查看拼写错误。

Also, make sure you turn on visible white space in your editor when debugging Python code. This could be a white space error, which would be invisible to us at Stack Overflow.

此外,在调试Python代码时,请确保在编辑器中打开可见空白区域。这可能是一个空白错误,在Stack Overflow中我们看不到它。

EDIT:
So I was completely wrong about that '-' error being a red herring. But keep that parser behavior in mind/white space thing in mind, could help in the future.

编辑:所以我完全错了' - '错误是红鲱鱼。但请记住解析器行为在脑海/白色空间的事情,可能在将来有所帮助。

Apologies if this is obvious to you, I don't know what level you are at with Python.

抱歉,如果这对您来说很明显,我不知道您使用Python的级别。

#1


In this line:

在这一行:

destination = Vector2(*event.pos) – Vector2(*sprite.get_size())/2.

You somehow typed the character "" (EN DASH) instead of "-" (HYPHEN-MINUS). Use "-" (HYPHEN-MINUS) instead, like this:

你以某种方式输入了字符“ - ”(EN DASH)而不是“ - ”(HYPHEN-MINUS)。改为使用“ - ”(HYPHEN-MINUS),如下所示:

destination = Vector2(*event.pos) - Vector2(*sprite.get_size())/2.

#2


I can't be sure without a stack trace, but I have a hunch that it's the wrong - symbol. What editor are you using? Is it possible that your editor is taking the - symbol and turning it into a fancier dash, like an ndash or an mdash?

没有堆栈跟踪我无法确定,但我有一种预感,这是错误的 - 符号。你在用什么编辑器?您的编辑是否可能使用 - 符号并将其转换为更高级的短划线,如ndash或mdash?

#3


Maybe try changing speed to "speed = 250.0". I don't know if that dangling dot would throw python off.

也许尝试将速度改为“速度= 250.0”。我不知道那个悬空点是否会抛出python。

What is going on here, with your error message at least, is the Python parser is stumbling over something before your '-', which screws up its interpretation of '-'. So I recommend looking before the '-' for typos.

这里发生的事情,至少是你的错误消息,是Python解析器在你的' - '之前绊倒了什么,这搞砸了它对' - '的解释。所以我建议在' - '之前查看拼写错误。

Also, make sure you turn on visible white space in your editor when debugging Python code. This could be a white space error, which would be invisible to us at Stack Overflow.

此外,在调试Python代码时,请确保在编辑器中打开可见空白区域。这可能是一个空白错误,在Stack Overflow中我们看不到它。

EDIT:
So I was completely wrong about that '-' error being a red herring. But keep that parser behavior in mind/white space thing in mind, could help in the future.

编辑:所以我完全错了' - '错误是红鲱鱼。但请记住解析器行为在脑海/白色空间的事情,可能在将来有所帮助。

Apologies if this is obvious to you, I don't know what level you are at with Python.

抱歉,如果这对您来说很明显,我不知道您使用Python的级别。