3

*although all parts of that operation are tuples python seems to think that in this instance one of them is not. This is my first time trying to make a vector class in python. my intentions are to move my simple mouse image to where i click on the screen by adding increments to it of a speed * vector to its position until it reaches the target distance*

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)

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

#The simple mouse move game by Ramon Cabral

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

2 回答 2

1

当它需要一个数字元组时,看起来你正在传递Vector.from_points一个 Vector 对象。你有没有尝试过这样的事情?

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

我不建议进行Vector支持索引。这通常是为类似列表的对象保留的。目前还不清楚什么Vector()[0]Vector()[1]应该是什么。Vector().x对我来说Vector().y更清楚。

如果您发现您经常(阅读:“不止一次”)需要将向量视为点的元组,您可以创建一个实例方法来执行此操作:

class Vector(object):
    # ...
    def points(self):
        return (self.x, self.y)
    # ...
于 2013-02-28T17:43:37.617 回答
1

您必须定义getitemsetitem方法以支持 Vector 类中的索引。

于 2013-02-28T16:40:31.173 回答