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

2024-05-21 04:34:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想定义25个不同的“矩形”,每个矩形有6个不同的属性。我首先创建了一个只有25个不同元素的列表。然后使用不同的循环,我试图将每个矩形的6个属性附加到包含1-25的列表中。这是我得到的错误,我有一种感觉,这是由于我没有分配什么元素“列表”分配新的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) 

我知道我可能在这方面做得不对,但我对如何创建列表感到困惑。在


Tags: theinevent元素列表for属性width
3条回答

从这里我可以看到,python使用rectanglepos[I]处的值作为append方法的参数,而不是将其作为实际列表传递。我唯一的想法就是尝试

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

但我真的不明白这会有什么不同。在

First of all, we drawing rectangle with this way;

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

所以实际上,你不能改变你的屏幕属性,因为你定义了游戏屏幕的大小。在

This is what you want excatly;

^{pr2}$

输出:

enter image description here

So I define rect coords and size randomly in a function, and call that function whatever times you want.

With random color by given list of colors:

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

尝试在不使用if i==24语句的情况下执行此操作!:-)

Edit for can save the 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)]

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

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)

相关问题 更多 >