Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t了解如何基于变量(示例对象[variable[variable2]])修改列表_Python_Arrays_List_Pygame - Fatal编程技术网

Python Can';t了解如何基于变量(示例对象[variable[variable2]])修改列表

Python Can';t了解如何基于变量(示例对象[variable[variable2]])修改列表,python,arrays,list,pygame,Python,Arrays,List,Pygame,我正在尝试为一个项目制作一个简单的游戏,我需要这个游戏能够处理多个矩形对象(大约10-15个,但显然我不希望每个对象都有一个函数) 我遇到的问题是,例如,我希望Python能够访问 对象是一个包含所有矩形(当前为Rct0和Rct1)的列表,因此我希望函数能够修改/访问对象[variable[variable2]],所以假设我想更改对象中矩形1的颜色,我该怎么做?当我尝试“嵌套”它时,它总是抛出一个错误。 我在下面包含了我的代码 import pygame, sys, time, random f

我正在尝试为一个项目制作一个简单的游戏,我需要这个游戏能够处理多个矩形对象(大约10-15个,但显然我不希望每个对象都有一个函数)

我遇到的问题是,例如,我希望Python能够访问 对象是一个包含所有矩形(当前为Rct0和Rct1)的列表,因此我希望函数能够修改/访问对象[variable[variable2]],所以假设我想更改对象中矩形1的颜色,我该怎么做?当我尝试“嵌套”它时,它总是抛出一个错误。 我在下面包含了我的代码

import pygame, sys, time, random
from pygame.locals import *

pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()

cWhite = pygame.Color(255,255,255)
cGray = pygame.Color (70,70,70)
cRed = pygame.Color (255,0,0)
cBlue = pygame.Color (0,0,255)
cCyan = pygame.Color (0,255,255)
cGreen = pygame.Color (0,255,0)


global cycleCount, longCycle, pause
cycleCount = 0
longCycle = 0
pause = False
global x, y, cID, rID, objCount
global Objects, Rct0, Rct1
Rct0 = [0,0,0,0,0]
Rct1 = [0,0,0,0,0]
Objects = [[Rct0],[Rct1]]


def spawnRandomizer():
    global x, y, cID, rID
    x = 440
    y = random.randrange(60,440)
    cID = random.choice(colours)
    rID = 0
    Objects[rID] = [x, y, cID, 1, 1]
    print(Objects[rID])


def drawRectangle():
    global x, y, cID, rID
    Objects[rID] = [x, y, cID, 1, 1]
    pygame.draw.rect(Window, cGreen, (Rct0[0], Rct0[1], 50, 50))


#FRAME LOOP

while True:
    Window=pygame.display.set_mode((500,500), 0, 32)
    pygame.display.set_caption('Rectangle generator')
    Window.fill(cWhite)

    if cycleCount == 0:
        spawnRandomizer();
    drawRectangle();






    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()


    if cycleCount == 30:
        cycleCount = 0

    if longCycle == 300:
        longCycle = 0

    cycleCount = cycleCount + 1
    longCycle = longCycle + 1
    pygame.display.update()
    fpsClock.tick(30)

我发现您可以执行对象[0][1]或对象[variable1][variable2],因此我解决了我的问题。因为某种原因,我以前在任何地方都找不到它。