错误信息是:
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator.MICRO-C17310A13\桌面\pygame例子\vectorfish.py", line 24, in <module>
screen.blit(sprite, position)
TypeError: invalid destination position for blit
代码是:
background_image_filename = 'sushiplate.jpg'
sprite_image_filename = 'fugu.bmp'
import pygame
from pygame.locals import *
from sys import exit
from 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.0
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()
vector2的代码是:
import math
class Vector2(object):
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def __str__(self):
return "(%s, %s)"%(self.x, self.y)
@staticmethod
def from_points(P1, P2):
return Vector2( P2[0] - P1[0], P2[1] - P1[1] )
def get_magnitude(self):
return math.sqrt( self.x**2 + self.y**2 )
def normalize(self):
magnitude = self.get_magnitude()
self.x /= magnitude
self.y /= magnitude
不仅此代码,所有需要 vector2 的代码都遇到了这个问题:invalid destination position for blit
难道我做错了什么 ?
非常需要任何帮助。
吉尔伯特陈