2013-02-28 59 views
3

*儘管該操作的所有部分都是元組python似乎認爲在這種情況下其中一個元組不是。這是我第一次嘗試在python中創建一個向量類。我的意圖是通過添加遞增到它的速度*矢量到其位置的移動我的簡單的鼠標圖像,其中i在屏幕上點擊,直到它到達目標距離*position + = heading * distance_moved TypeError:只能將元組(不是「Vector」)連接到元組

進口數學

類矢量(對象):

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

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

由拉蒙·卡布拉爾#The簡單的鼠標移動遊戲

#imports 
import pygame, sys, Vector 
from pygame.locals import * 
from Vector 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(0, 0) 

#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.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() 
+0

你能舉個那會導致錯誤的例子? – BluePeppers 2013-02-28 16:39:09

+0

這是哪裏出錯:if event.type == MOUSEBUTTONDOWN: destination = pygame.mouse.get_pos() heading = Vector.from_points(position,destination) heading.normalize()time_passed = clock.tick(30 。) time_passed_seconds = time_passed/1000.0 distance_moved = time_passed_seconds *速度 位置+ =標題* distance_moved – rrcm 2013-02-28 16:54:26

+0

我想,當我試圖添加到它是 – rrcm 2013-02-28 16:56:49

回答

1

你必須定義getitemsetitem方法來支持您的Vector類中的索引。

+0

我創建了這些,但現在我在__getitem__中出現錯誤,我不確定這些是如何工作的。 DEF __getitem __(個體,索引): 返回自[指數] DEF __setitem __(個體,索引值): 自[索引] = 1.0 *值 DEF __iter __(個體): 返回ITER(個體[: ]) – rrcm 2013-02-28 16:58:28

1

它看起來像你正在通過Vector.from_points一個Vector對象,當它想要一個數組的元組。你有沒有嘗試過這樣的事情?

position_points = (position.x, position.y) 
heading = Vector.from_points(position_points, destination) 

我不會建議讓Vector支持索引。這通常保留給類似列表的對象。目前還不清楚Vector()[0]Vector()[1]應該是什麼。 Vector().xVector().y對我來說更加清晰。

如果您發現您經常(讀:「不止一次」)需要治療矢量點的元組,你可以把一個實例方法,這樣做:

class Vector(object): 
    # ... 
    def points(self): 
     return (self.x, self.y) 
    # ... 
+0

我沒有給它傳遞一個元組,我使position =(100.0,100.0)。這對我來說很困惑。 – rrcm 2013-02-28 17:51:21

+0

語句'position + = heading * distance_moved'看起來像返回一個'Vector'。 'heading'是一個'Vector',不是? – 2013-02-28 17:55:38

+0

是的,標題是從點位置和目的地創建的矢量。我希望能夠將heading.normalize()增加到位置,直到到達目的地。基本上我想將鼠標圖像移動到我點擊屏幕的位置。 – rrcm 2013-02-28 18:02:28

相關問題