【发布时间】:2020-09-17 00:46:47
【问题描述】:
在这个程序中,我想要一个更大的可移动圆圈和许多彩色的小圆圈。然而,当我运行程序时,所有的小圆圈都是相同的颜色,我无法弄清楚如何随机给它们每个不同的颜色。我该如何解决这个问题?
import pygame as pg
import random as rd
pg.init()
screen = pg.display.set_mode((800, 600))
p_1_x = 200
p_1_y = 200
p_1_change_x = 0
def p_1(x, y):
player_1 = pg.draw.circle(screen, (2, 2, 0), (x, y), 15)
locations = []
small_color = []
for i in range(50):
red = rd.randint(0, 220)
blue = rd.randint(0, 220)
green = rd.randint(0, 220)
x = rd.randint(100, 700)
y = rd.randint(100, 500)
locations.append((x, y))
small_color.append((red, blue, green))
while True:
screen.fill((255, 255, 255))
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_RIGHT:
p_1_change_x = 1
if event.key == pg.K_LEFT:
p_1_change_x = -1
if event.type == pg.KEYUP:
if event.key == pg.K_RIGHT or pg.K_LEFT:
p_1_change_x = 0
p_1_x += p_1_change_x
p_1(p_1_x, p_1_y)
for locate in locations:
pg.draw.circle(screen, (small_color[i]), locate, 5)
pg.display.update()
【问题讨论】:
-
每个答案旁边都有一个✔勾号,如果它适合您,您可以接受答案并通过✅检查勾号来批准它,提前致谢