pygame:blit 的目标位置无效

use*_*213 1 python iterator pygame blit

错误的信息是:

\n\n
Traceback (most recent call last):\n  File "C:\\Documents and Settings\\Administrator.MICRO-C17310A13\\\xe6\xa1\x8c\xe9\x9d\xa2\\pygame\xe4\xbe\x8b\xe5\xad\x90\\vectorfish.py", line 24, in <module>\n    screen.blit(sprite, position)\nTypeError: invalid destination position for blit\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码是:

\n\n
background_image_filename = \'sushiplate.jpg\'    \nsprite_image_filename = \'fugu.bmp\'    \nimport pygame    \nfrom pygame.locals import *    \nfrom sys import exit    \nfrom vector2 import Vector2    \npygame.init()    \nscreen = pygame.display.set_mode((640, 480), 0, 32)    \nbackground = pygame.image.load(background_image_filename).convert()    \nsprite = pygame.image.load(sprite_image_filename).convert_alpha()    \nclock = pygame.time.Clock()\n\nposition = Vector2(100.0, 100.0)\n\nspeed = 250.0\n\nheading = Vector2()\n\nwhile True:\n\n    for event in pygame.event.get():\n        if event.type == QUIT:\n            exit()\n    if event.type == MOUSEBUTTONDOWN:\n        destination = Vector2(*event.pos) - Vector2(*sprite.get_size())/2.\n        heading = Vector2.from_points(position, destination)\n        heading.normalize()\n    screen.blit(background, (0,0))\n    screen.blit(sprite, position)\n    time_passed = clock.tick()\n    time_passed_seconds = time_passed / 1000.0\n    distance_moved = time_passed_seconds * speed\n    position += heading * distance_moved\n    pygame.display.update()\n
Run Code Online (Sandbox Code Playgroud)\n\n

Vector2的代码为:

\n\n
import math\n\nclass Vector2(object):\n\n    def __init__(self, x=0.0, y=0.0):\n        self.x = x\n        self.y = y\n    def __str__(self):\n        return "(%s, %s)"%(self.x, self.y)\n    @staticmethod\n    def from_points(P1, P2):\n        return Vector2( P2[0] - P1[0], P2[1] - P1[1] )\n    def get_magnitude(self):\n        return math.sqrt( self.x**2 + self.y**2 )\n    def normalize(self):\n        magnitude = self.get_magnitude()\n        self.x /= magnitude\n        self.y /= magnitude\n
Run Code Online (Sandbox Code Playgroud)\n\n

不仅是这段代码,所有需要vector2的代码都遇到了这个问题:\ninvalid destination location for blit

\n\n

难道我做错了什么 ?

\n\n

非常需要任何帮助。

\n\n

陈吉尔伯特

\n

slo*_*oth 7

Surface.blit需要一个tuple作为dest参数。如果您想使用自己的向量类,请将其更改为:

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
Run Code Online (Sandbox Code Playgroud)

现在,它是它的子类tuple,您可以将它传递给blit函数。(另请注意,元组必须包含ints)。

我还添加了__add____mul__让它支持加法和乘法。

这样,不需要对代码进行进一步修改,并且您可以按预期使用向量类。