在另一个列表Python中写入和读取数组

时间:2014-12-30 19:05:40

标签: python list pygame

我想定义25个不同的"矩形"每个都有6个不同的属性。我首先创建了一个仅包含25个不同元素的列表。然后使用不同的循环我试图将每个矩形的6个属性附加到包含1-25的列表中的列表。这是我得到的错误,我感觉这是因为我没有分配什么元素" list"将新的6个元素分配给。

Traceback (most recent call last):
  File "tryingRectangle.py", line 37, in <module>
    rectanglePos.append(x, y, width, height)
TypeError: append() takes exactly one argument (4 given)


# Create the empty array to house the rectangle
rectanglePos = []
rectangleInPos = [rectanglePos]

# Loop 25 times and add rectangle in random x,y position
for i in range(25):
    rectanglePos[i] = i
    height = random.randint(20,400)
    x = i*20
    y = 200
    width = 15
   rectangleInPos.append(rectanglepos[i])

# Ignore me clock = pygame.tme.Clock()

# Loop until user closes
done = False
while done == False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop
    # Set the background WHITE
    screen.fill(WHITE)

    # Process each rectangle
    for i in range(len(rectangle)):
        # Draw the Rectangle
        pygame.draw.rect(SIZE, RED , rectanglePos[i[1]], rectanglePos[i[2]], rectanglePos[i[3]], rectangle[i[4]], 0) 

我知道我可能会犯这个错误,但我对如何创建列表列表感到困惑。

3 个答案:

答案 0 :(得分:0)

从我在这里看到的,python使用rectanglepos [i]中的值作为方法append的参数,而不是将其作为实际列表传递。我的想法是尝试

rectangleInPos.append(list(rectanglepos[i]))

但我真的不知道它会如何产生影响。

答案 1 :(得分:0)

  

首先,我们用这种方式绘制矩形;

pygame.draw.rect(screen(defined before), color(defined before), (coordinate-x,coordinate-y,size on x coord,size on y coord))

所以实际上你不能更改你的屏幕 attiribute,因为它是你定义它的大小的pygame屏幕。

  

这就是你想要的东西;

import pygame
import random
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
gameDisplay=pygame.display.set_mode((800,600)) #screen

def draw():
    x=random.randint(20,400) #x coordinate
    y=random.randint(20,400) #y coordinate
    x1=random.randint(5,50) #size of rect on x coord
    y1=random.randint(2,30) #size of rect on y coord
    pygame.draw.rect(gameDisplay, white, (x,y,x1,y1))
    pygame.display.update() #I wrote it in function so you dont have to write it again and again in main game loop
for i in range(5):
    draw() #call function

输出:

enter image description here

  

因此,我在函数中随机定义矩形坐标和大小,并随时调用该函数。

     

通过给定的颜色列表随机颜色:

import pygame
import random
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
gameDisplay=pygame.display.set_mode((800,600))

def draw():
    colorlist=[(255,0,0),(255,255,255)]
    color=random.choice(colorlist)
    x=random.randint(10,590)
    y=random.randint(10,590)
    x1=random.randint(5,50)
    y1=random.randint(2,30)
    pygame.draw.rect(gameDisplay, color, (x,y,x1,y1)) #<--- color parameter 
    pygame.display.update()
for i in range(25):
    draw()

输出:

enter image description here

或者如果您想要更多颜色只需更改此颜色;

color1=random.randint(0,255)
color2=random.randint(0,255)
color3=random.randint(0,255)
color=(color1,color2,color3)

输出:

enter image description here

这里是整个代码:

import pygame
import random

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
gameDisplay=pygame.display.set_mode((800,600))

def draw():
    color1=random.randint(0,255)
    color2=random.randint(0,255)
    color3=random.randint(0,255)
    color=(color1,color2,color3)
    x=random.randint(10,590)
    y=random.randint(10,590)
    x1=random.randint(10,100)
    y1=random.randint(2,30)
    pygame.draw.rect(gameDisplay, color, (x,y,x1,y1))
    pygame.display.update()
done=False
while done==False:
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True

    for i in range(25):
        draw()
        if i==24:
            done=True

如果i == 24 语句,请在没有的情况下尝试执行此操作! : - )

  

编辑可以保存attiributes;

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
gameDisplay=pygame.display.set_mode((800,600))
thebiglist=[] #the list that will save attiributes
def draw():
    color1=random.randint(0,255)
    color2=random.randint(0,255)
    color3=random.randint(0,255)
    color=(color1,color2,color3)
    x=random.randint(10,590)
    y=random.randint(10,590)
    x1=random.randint(10,100)
    y1=random.randint(2,30)
    pygame.draw.rect(gameDisplay, color, (x,y,x1,y1))
    thebiglist.append((x,y,x1,y1)) #appending them in a tuple
    pygame.display.update()
done=False
while done==False:
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True

    for i in range(25):
        draw()
        if i==24:
            done=True
print (thebiglist)

输出:

  
    
      
        

[(547,135,63,16),(48,325,19,6),(541,353,77,7),(488,481,18,4),(516,214,82) ,2),(11,198,94,16),(132,324,76,21),(196,484,81,4),(257,489,14,12),(412,19,99) ,26),(258,156,82,23),(488,452,31,3),(27,190,75,28),(345,257,24,30),(138,555,49) ,8),(317,33,89,11),(589,374,60,8),(459,293,96,4),(449,401,48,11),(58,373,45) ,14),(273,590,71,27),(400,483,23,30),(426,574,65,4),(67,165,51,24),(428,389,69) ,9)]

      
    
  

答案 2 :(得分:0)

我假设列表列表是你想要一个三角形列表,每个三角形由一系列属性组成?假设这就是你想要的,试试这个。

rectangleInPos = [rectanglePos]

# Loop 25 times and add rectangle in random x,y position
for i in range(25):
    RectanglePos = []
    rectanglePos.append(i)
    rectanglePos.append(height = random.randint(20,400)
    rectanglePos.append(x = i*20)
    rectanglePos.append( y = 200)
    rectanglePos.append(width = 15)
    rectangleInPos.append(rectanglepos)