【问题标题】:Invalid rect assignment [duplicate]无效的矩形分配[重复]
【发布时间】:2021-07-19 18:06:31
【问题描述】:

我有一个类,我试图在其中创建一个新方法 move_piece() 。这是我当前的代码:

def move_piece(self):
      propinfo = open('D:\pythonProject\monop\propertyinfo.txt')
      propinfolines = propinfo.readlines()

      temp = (propinfolines[self.squareID + 1].split(',')) # ^ all code above this is simply reading a specific line from a text file , and splitting it into an array by a delimited character (,)
      print('x = ' + temp[12])
      print('y = ' + temp[13]) # these two lines here are simply to print the new x and y value to the console to make sure I have the correct values.
      x = temp[12]
      y = temp[13]
      self.rect.y = y
      self.rect.x = x

当我运行代码时,我收到以下错误:

File "D:\monop\car.py", line 38, in move_piece
    self.rect.x = x
TypeError: invalid rect assignment

我不确定为什么,从我读到的内容中,您不能直接从数组为 rect 赋值,因此我首先将它传递给变量。但它仍然无法正常工作。感谢您的帮助。

【问题讨论】:

    标签: python class pygame


    【解决方案1】:

    temp 是一个字符串,您必须将字符串转换为整数值:

    x = int(temp[12])
    y = int(temp[13])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多