2010-04-15 56 views
2
# INTIALISATION 
import pygame, math, sys 
from pygame.locals import * 
screen = pygame.display.set_mode((1024, 768)) 
car = pygame.image.load('car.png') 
clock = pygame.time.Clock() 
k_up = k_down = k_left = k_right = 0 
speed = direction = 0 
position = (100, 100) 
TURN_SPEED = 5 
ACCELERATION = 2 
MAX_FORWARD_SPEED = 10 
MAX_REVERSE_SPEED = ­5 
BLACK = (0,0,0) 
while 1: 
    # USER INPUT 
    clock.tick(30) 
    for event in pygame.event.get(): 
     if not hasattr(event, 'key'): continue 
     down = event.type == KEYDOWN  # key down or up? 
     if event.key == K_RIGHT: k_right = down * ­5 
     elif event.key == K_LEFT: k_left = down * 5 
     elif event.key == K_UP: k_up = down * 2 
     elif event.key == K_DOWN: k_down = down * ­2 
     elif event.key == K_ESCAPE: sys.exit(0)  # quit the game 
    screen.fill(BLACK) 
    # SIMULATION 
    # .. new speed and direction based on acceleration and turn 
    speed += (k_up + k_down) 
    if speed > MAX_FORWARD_SPEED: speed = MAX_FORWARD_SPEED 
    if speed < MAX_REVERSE_SPEED: speed = MAX_REVERSE_SPEED 
    direction += (k_right + k_left) 
    # .. new position based on current position, speed and direction 
    x, y = position 
    rad = direction * math.pi/180 
    x += ­speed*math.sin(rad) 
    y += ­speed*math.cos(rad) 
    position = (x, y) 
    # RENDERING 
    # .. rotate the car image for direction 
    rotated = pygame.transform.rotate(car, direction) 
    # .. position the car on screen 
    rect = rotated.get_rect() 
    rect.center = position 
    # .. render the car to screen 
    screen.blit(rotated, rect) 
    pygame.display.flip() 
    enter code here 

我得到的錯誤是第13行文件race1.py中的非ASCII字符'\ xc2',但沒有聲明編碼;詳情請見http://www.python.org/peps/pep-0263.htmlpygame代碼中的錯誤

不能明白錯誤是什麼以及如何擺脫它?

回答

2

至於格雷格說,你的代碼中有一個非ascii字符 - 在第13行的5前面看起來像一個負號。它被稱爲「軟連字符」。此字符出現在代碼中的幾個位置,而不是減號。刪除這些字符並用負號替換。

上面的代碼不顯示字符。不知道爲什麼。當我複製並粘貼到文本編輯器中時,我可以看到該字符。

如果你把在你代碼的頂部,編碼註釋例如:

# -*- coding: utf-8 -*- 

您將獲得與「軟連字符」語法錯誤。所以你需要用減號來代替它們。 (那麼你不需要代碼頂部的編碼註釋。)

4

你對線,除非你把一個特殊的註釋在您的文件的頂部13 Python沒有在源文件中接受UTF-8非ASCII字符:

# encoding: UTF-8 
+1

你在說什麼字符?我甚至不能看到它.. – Hick 2010-04-15 21:04:57