0

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

这是我的代码:

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()

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

非常需要任何帮助。

4

3 回答 3

5
于 2009-04-08T17:54:20.887 回答
0

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?

于 2009-04-08T17:55:01.760 回答
0

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

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.

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.

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.

于 2009-04-08T17:55:13.040 回答