2012-09-11 35 views
0

的錯誤信息無效的目標定位是:pygame的:對位塊

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的代碼都符合此問題: blit的目標位置無效

我做錯了什麼?

任何幫助是非常需要的。

吉爾伯特陳

回答

3

Surface.blit需要一個tupledest參數。如果你想用自己的向量類的工作,將其更改爲:現在

class Vector2(tuple): 

    def __new__(typ, x=1.0, y=1.0): 
     n = tuple.__new__(typ, (int(x), int(y))) 
     n.x = x 
     n.y = y 
     return n 

    def __mul__(self, other): 
     return self.__new__(type(self), self.x*other, self.y*other) 

    def __add__(self, other): 
     return self.__new__(type(self), self.x+other.x, self.y+other.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 

,它是從tuple子類,你可以將它傳遞給blit功能。 (還要注意元組必須包含int s)。

我還添加了__add____mul__以支持添加和乘法。

通過這種方式,不需要對代碼進行進一步的修改,您可以按照預期使用您的矢量類。

+0

太好了!非常感謝。所有代碼都有效。 – user1662213

+0

@ user1662213很高興能幫到你。你可以通過使用'x'和'y'的屬性使它不可改變來進一步改進這個類,但是這會超出這個問題/答案的範圍:-) – sloth

1

嘗試以下操作:

screen.blit(sprite, (position.x, position.y)) 

的問題是,你的Vector2沒有爲__iter__過載是一個迭代器,因此您可以在對象上調用tuple。這意味着它不能通過blit函數調用轉換爲元組,因此參數無效。

你Vector2隨後將包含:

def __iter__(self): 
     return [self.x, self.y].__iter__() 

而且你的塊傳輸將是:

screen.blit(sprite, tuple(position)) 
+0

我已經將類Vector2(object)更改爲Vector(元組),它工作。謝謝! – user1662213

+0

是的,因爲你消除了通過迭代器將對象轉換爲元組的需求。實際上,當本教程出來時,如果我沒有記錯的話,仍然可以重載Python中的__tuple__類型。 –