2013-07-29 24 views
3

我正在尝试制作我的第一款游戏,它类似于自行车上的tron游戏,到目前为止我已经创建了一个自行车类和几个帮助功能。然而,当它运行下面我得到一个错误,当我尝试声明自行车物体的速度,它说的是:类只需要三个参数,两个给出

class Vector takes exactly three arguments and two are given by the what_direction function.

这对我来说是一个问题,因为我已经创建了一个2×2矩阵用于改变方向和用矢量乘以矩阵的功能。我能做些什么来解决这个错误?

import random, math, pygame, sys 

    class Vector(object): 
     """ |x| = [0] 
      |y| = [1] """ 
     def __init__(self, x, y): 
      self.vec = [ x, y] 

    def what_direction(): 
     x = random.uniform(0.0, 5.0) 
     y = math.sqrt(25-(x**2)) 
     return x, y 

    class Bike(object): 
     def __init__(self): 
      self.position = [random.randint(0, 200), random.randint(0, 200)] 
      self.velocity = Vector(what_direction()) 
      self.score = 0 
      self.path_traveled = [] 
+0

你能在这里粘贴错误? – sihrc

+0

呃。我认为这是某种元组对列表的事情,我不够聪明。 – Jiminion

+2

一个问题是你需要:'Vector(* what_direction())' –

回答

5

您需要使用星号这样的:

self.velocity = Vector(*what_direction()) 

以两个组件传递给Vector构造。目前你传递的是一个包含两个成员的元组的单个参数。星号解开元组,将其成员值作为单独的参数传递给Vector()

9

您的what_direction()函数返回一个值的元组,并且您试图将该元组传递给一个带有2个参数的函数。 Python认为你传递了一个参数(即2元组)。在表达式Vector(what_direction())中使用之前,您需要解开元组。你可以这样做你自己:

a, b = what_direction() 
Vector(a, b) 

或者您可以使用元组拆包经营者*

Vector(*what_direction()) 
相关问题