2011-12-11 66 views
1

这是我第一次问Stack Overflow的问题,所以如果我的问题太模糊或者没有提供足够的信息,我会提前道歉。TypeError:预期的整数参数,得到浮点数

基本上我遇到的问题是我的代码不会由于TypeError而运行。

这里是确切的消息:

Traceback (most recent call last): 
    File "C:\Program Files (x86)\Wing IDE 101 4.0\src\debug\tserver\_sandbox.py", line 61, in <module> 
    File "C:\pygamehelper.py", line 62, in mainLoop 
    File "C:\Program Files (x86)\Wing IDE 101 4.0\src\debug\tserver\_sandbox.py", line 54, in draw 
TypeError: integer argument expected, got float 

所以我认为该错误发生由于与正使用vec2d方法被转换为一个矢量,然后作为参数被传递相关联的元组(作为浮点数)传递给第54行中的draw.circle方法,其中int是预期的。然而,这是我在教程中完成的工作,而且他的代码完全相同,并且执行时没有问题。

难道这可能是由于他使用了不同版本的Python或PyGame,或者我的代码有问题吗?

在此先感谢您的帮助。

from pygamehelper import * 
from pygame import * 
from pygame.locals import * 
from vec2d import * 
from math import e, pi, cos, sin, sqrt 
from random import uniform 

class Agents: 
    def __init__(self): 
     self.pos = vec2d(0, 0) 
     self.target = vec2d(0, 0) 

class Starter(PygameHelper): 
    def __init__(self): 
     self.w, self.h = 800, 600 
     PygameHelper.__init__(self, size=(self.w, self.h), fill=((255,255,255))) 

     self.agents = [] 

     for i in range(10): 
      a = Agents() 
      a.pos = vec2d(uniform(0, self.w), uniform(0, self.h)) 
      a.target = vec2d(uniform(0, self.w), uniform(0, self.h)) 
      self.agents.append(a) 

    def update(self): 

     for a in self.agents: 

      dir = a.target - a.pos 
      dir.length = 5 
      a.pos = a.pos + dir 

    def keyUp(self, key): 
     pass 

    def mouseUp(self, button, pos): 

     for a in self.agents: 
      a.target = vec2d(pos) 

    def mouseMotion(self, buttons, pos, rel): 
     pass 

    def draw(self): 

     # clear the screen 40 times/sec so that only 1 char and target 
     # present on screen at a time 
     self.screen.fill((255,255,255)) 


     for a in self.agents: 
      # character 
      pygame.draw.circle(self.screen, (200, 200, 255), a.pos, 10) 
      # black character outline 
      pygame.draw.circle(self.screen, (0, 0, 0), a.pos, 11, 1) 
      # target 
      pygame.draw.circle(self.screen, (200, 0, 0), a.target, 20, 1) 

s = Starter() 
s.mainLoop(40) 

回答

1

random.uniform(a,b)返回一个浮点数。使用int(random.uniform(a,b))作为整数。

1

看起来92行上的tuiototouch.py​​传递了一个float参数(self.x_mouse)。 Uinput只需要整数。

我认为int转换可以在suinput引擎下完成。所以也许suinput在把它传递给uinput-system之前应该确保ev_value是整数。

所以有两种可能的解决方案。我既可以

  • 文档uinput.Device.emit()

  • 允许任何数值类型和它类型强制转换为整数suinput整数要求

我不确定哪个更好,我会考虑一段时间。

同时,我认为你可以通过在tuiototouch.py​​中int类型self.x_mouse来解决这个问题:int(self.x_mouse)。

相关问题