<blockquote>
<p>First of all, we drawing rectangle with this way;</p>
</blockquote>
<pre><code>pygame.draw.rect(screen(defined before), color(defined before), (coordinate-x,coordinate-y,size on x coord,size on y coord))
</code></pre>
<p>所以实际上,你不能改变你的屏幕属性,因为你定义了游戏屏幕的大小。在</p>
<blockquote>
<p>This is what you want excatly;</p>
</blockquote>
^{pr2}$
<p>输出:</p>
<p><img src="https://i.stack.imgur.com/fuuCO.jpg" alt="enter image description here"/></p>
<blockquote>
<p>So I define rect coords and size randomly in a function, and call that function whatever times you want.</p>
<p>With random color by given list of colors:</p>
</blockquote>
<pre><code>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()
</code></pre>
<p>输出:</p>
<p><img src="https://i.stack.imgur.com/1deA5.jpg" alt="enter image description here"/></p>
<p><strong>或者如果您想要更多颜色</strong>只需更改这一个</p>
<pre><code>color1=random.randint(0,255)
color2=random.randint(0,255)
color3=random.randint(0,255)
color=(color1,color2,color3)
</code></pre>
<p>输出:</p>
<p><img src="https://i.stack.imgur.com/uEXuj.jpg" alt="enter image description here"/></p>
<p><strong>这里是整个代码:</strong></p>
<pre><code>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
</code></pre>
<p>尝试在不使用<em>if i==24</em>语句的情况下执行此操作!:-)</p>
<blockquote>
<p><strong>Edit</strong> for can save the attiributes;</p>
</blockquote>
<pre><code>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)
</code></pre>
<p>输出:</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>[(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)]</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>