简体   繁体   English

TypeError:“ int”对象没有属性“ __getitem__”

[英]TypeError: 'int' object has no attribute '__getitem__'

I'm trying to make a play surface out of rects in pygame. 我正在尝试使用pygame中的rects来制作游戏表面。 I'm not sure what the problem is exactly but I believe it has something to do with the actual iteration of the list when creating the rects. 我不确定问题到底出在哪里,但我相信这与创建rect时列表的实际迭代有关。 Sorry, noob here. 抱歉,这是菜鸟。 :) :)

import pygame

w = 800
h = 600
board_pos = 0, 0
tile = 27
playfield = 0

class Board(object):
    def __init__(self, surface, pos, tile_size):
        self.surface = surface
        self.x, self.y = pos
        self.tsize = tile_size
        self.color = 50, 50, 50

        playfield = [list(None for i in xrange(22)) for i in xrange(10)]  

    def draw(self):
        for i in xrange(10):
            for j in xrange(22):
                playfield[i][j] = pygame.draw.rect(self.surface, self.color,
                                                  (self.x + (i * self.tsize),
                                                   self.y + (j * self.tsize),
                                                   self.tsize, self.tsize))

pygame.display.init()
screen = pygame.display.set_mode((w, h))

board = Board(screen, board_pos, tile)
board.draw()

while __name__ == '__main__':
    pygame.display.flip()

I keep getting this error: 我不断收到此错误:

Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\My
Documents\Dropbox\Programming\DeathTris
\test2.py", line 30, in <module>
    board.draw()
  File "C:\Documents and Settings\Administrator\My
Documents\Dropbox\Programming\DeathTris
\test2.py", line 24, in draw
    self.tsize, self.tsize))
TypeError: 'int' object has no attribute '__getitem__'

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

Your line playfield = [list(None for i in xrange(22)) for i in xrange(10)] creates a local variable inside the __init__ function. 您的行playfield = [list(None for i in xrange(22)) for i in xrange(10)]__init__函数内创建局部变量。 That variable disappears after the __init__ function returns. __init__函数返回后,该变量消失。 Later in draw when you do playfield[i][j] , you are accessing the global value of playfield , which is still 0 (since you initialized it to 0 at the beginning). draw稍后部分,当您执行playfield[i][j] ,您将访问playfield的全局值,该值仍为0(因为在开始时将其初始化为0)。

If you want to overwrite the global playfield from inside your __init__ , you need to do global playfield before you assign to it. 如果要从__init__内部覆盖全局运动场,则需要先执行global playfield然后再分配给它。 (But. . . why are you using a global variable for this anyway?) (但是……为什么您仍然为此使用全局变量?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError&#39;int&#39;对象没有属性&#39;__getitem__&#39; - TypeError 'int' object has no attribute '__getitem__' 初始化对象错误TypeError:“ int”对象没有属性“ __getitem__” - Init object error TypeError: 'int' object has no attribute '__getitem__' TypeError:&#39;int对象没有属性&#39;__getitem__&#39;……但是在哪里? - TypeError: 'int object has no attribute '__getitem__' … but where? AI Python-TypeError:“ int”对象没有属性“ __getitem__” - AI Python - TypeError: 'int' object has no attribute '__getitem__' 如何修复typeError&#39;int&#39;对象没有属性&#39;__getitem__&#39;? - How to fix typeError 'int' object has no attribute '__getitem__'? 如何解决TypeError:“ int”对象没有属性“ __getitem__”? - How to solve TypeError: 'int' object has no attribute '__getitem__'? TypeError:&#39;int&#39;对象在Python枚举中没有属性&#39;__getitem__&#39; - TypeError: 'int' object has no attribute '__getitem__' in Python enumerate TypeError:“ int”对象没有属性“ __getitem __”(值为0) - TypeError: 'int' object has no attribute '__getitem__' (the value is 0) TypeError:“ int”对象没有属性“ __getitem __”(简单问题) - TypeError: 'int' object has no attribute '__getitem__' (simple issue) scrapy exceptions.TypeError:&#39;int&#39;对象没有属性&#39;__getitem__&#39; - scrapy exceptions.TypeError: 'int' object has no attribute '__getitem__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM