简体   繁体   English

pygame:无效的目标位置

[英]pygame:invalid destination position for blit

The wrong info is: 错误的信息是:

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

The code is: 代码是:

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

The code of vector2 is: 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

Not only this code,but all the codes that needs vector2 met this question: invalid destination position for blit 不仅此代码,而且所有需要vector2的代码都满足以下问题:blit的目标位置无效

am I doing something wrong ? 难道我做错了什么 ?

Any help is much needed. 任何帮助都是非常需要的。

Gilbert chan 陈嘉宝

Surface.blit expects a tuple as dest parameter. Surface.blit期望将tuple作为dest参数。 if you want to work with your own vector class, change it to this: 如果要使用自己的向量类,请将其更改为:

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

Now, it's subclassed from tuple and you can pass it to the blit function. 现在,它是从tuple ,您可以将其传递给blit函数。 (Also note that the tuple must contains int s). (还要注意,元组必须包含int )。

I also added __add__ and __mul__ to have it support addition and multiplication. 我还添加了__add____mul__使其支持加法和乘法。

This way, no further modifications to your code are required, and you can use your vector class as intended. 这样,不需要对代码进行进一步的修改,并且可以按预期使用向量类。

Try the following: 请尝试以下操作:

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

The problem is that your Vector2 does not have an overload for __iter__ which is an iterator so you can call tuple on your object. 问题是您的Vector2没有作为迭代器的__iter__的重载,因此您可以在对象上调用tuple This means that it cannot be converted to a tuple by the blit function call and thus the parameter is invalid. 这意味着不能通过blit函数调用将其转换为元组,因此参数无效。

Your Vector2 would then contain: 您的Vector2将包含:

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

And your blit would be: 而您的痛点是:

screen.blit(sprite, tuple(position))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM