【问题标题】:TypeError: can only concatenate tuple (not "vector") to tupleTypeError:只能将元组(不是“向量”)连接到元组
【发布时间】:2013-02-28 04:28:35
【问题描述】:

我是 python 和 pygame 的新手,我正在努力学习向量和类的基础知识,在这个过程中我设法搞砸了,我正在努力理解和修复标题中的错误消息。

这是我的 Vector 类的代码:

import math

class Vector(object):

    #defaults are set at 0.0 for x and y
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

    #allows us to return a string for print
    def __str__(self):
        return "(%s, %s)"%(self.x, self.y)

    # from_points generates a vector between 2 pairs of (x,y) coordinates
    @classmethod
    def from_points(cls, P1, P2):
        return cls(P2[0] - P1[0], P2[1] - P1[1])

    #calculate magnitude(distance of the line from points a to points b
    def get_magnitude(self):
        return math.sqrt(self.x**2+self.y**2)

    #normalizes the vector (divides it by a magnitude and finds the direction)
    def normalize(self):
        magnitude = self.get_magnitude()
        self.x/= magnitude
        self.y/= magnitude

    #adds two vectors and returns the results(a new line from start of line ab to end of line bc)
    def __add__(self, rhs):
        return Vector(self.x +rhs.x, self.y+rhs.y)

    #subtracts two vectors
    def __sub__(self, rhs):
        return Vector(self.x - rhs.x, self.y-rhs.y)

    #negates or returns a vector back in the opposite direction
    def __neg__(self):
        return vector(-self.x, -self.y)

    #multiply the vector (scales its size) multiplying by negative reverses the direction
    def __mul__(self, scalar):
        return Vector(self.x*scalar, self.y*scalar)

    #divides the vector (scales its size down)
    def __div__(self, scalar):
        return Vector(self.x/scalar, self.y/scalar)

这是我的程序的代码:

#The simple mouse move game by Ramon Cabral


#imports
import pygame, sys, Vector
from pygame.locals import *

#game init
pygame.init()

#screen
screen = pygame.display.set_mode((800,600),0,32)

#images
mouse_file = 'mouse.png'
MOUSE = pygame.image.load(mouse_file).convert_alpha()


#variables
bgcolor = (255,255,255)
position = (100.0,100.0)
heading = Vector.vector()

#clock and speed
clock = pygame.time.Clock()
speed = 250.0


#main game function
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()

        if event.type == MOUSEBUTTONDOWN:
            destination = pygame.mouse.get_pos()
            heading = Vector.vector.from_points(position, destination)
            heading.normalize()

    screen.fill(bgcolor)
    screen.blit(MOUSE, position)

    time_passed = clock.tick(30.)
    time_passed_seconds = time_passed/1000.0

    distance_moved = time_passed_seconds*speed
    position += heading * distance_moved

    pygame.display.update()

【问题讨论】:

  • 能否提供回溯

标签: python vector concatenation tuples pygame


【解决方案1】:

给你的班级一个tuple 方法:

class Vector(object):
    ...

    def tuple(self):
        return (self.x, self.y)

变化:

position = (100.0,100.0)

收件人:

position = Vector.vector(100.0,100.0)

还有:

screen.blit(MOUSE, position)

收件人:

screen.blit(MOUSE, position.tuple())

您正在添加不兼容的类型。

【讨论】:

  • 感谢您的帮助。你所说的很有意义,但由于某种原因,现在我得到了不同的信息。 position = Vector(100.0,100.0) TypeError: 'module' object is not callable
  • @rrcm:把我的Vector改成Vector.vector
  • 我想我刚刚做了一个可怕的课程。这是我现在得到的错误。 position = Vector.vector(100.0,100.0) AttributeError: 'module' 对象没有属性 'vector'
  • @rrcm:你之前是如何运行这段代码的? Vector.vector() 应该被定义。还是Vector.Vector(大写)?
  • position = (100.0,100.0) 是我之前运行它的方式。并且 heading = Vector.vector() 有效,但我想它由于某种原因不适用于位置
猜你喜欢
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多