如何阻止 pygame 个圆圈重叠?
How to stop pygame circles from overlapping?
我正在 pygame
中制作游戏,我正在尝试显示 7 个气球(目前为圆圈),颜色随机,x 轴随机。颜色有效,但是,当尝试在随机 x 轴上显示圆圈时,圆圈通常会重叠。我该如何解决这个问题?
这是我的代码:
import pygame as pg
import random as r
import sys
pg.init()
def draw_balloons():
global balloon_list
global colors
for i in range(7):
balloon_x = r.randint(0, 500)
balloon_color = (r.choice([0,255]), r.randint(0,255), r.choice([0,255]))
balloon_list.append(balloon_x)
colors.append(balloon_color)
pg.draw.circle(screen, colors[i], (balloon_list[i], y), radius=30)
# Vars #
balloon_list = []
colors = []
x = 0
y = 250
velocity = 5
clock = pg.time.Clock()
screen = pg.display.set_mode((688 ,387)) # Size of the screen #
screen.fill('#ffffff')
caption = pg.display.set_caption("Remember") # Title of the window #
pg.display.flip() # Updating #
running = True # Game loop bool #
while running: # Game loop #
clock.tick(60)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
pg.quit()
sys.exit()
draw_balloons()
pg.display.update()
P.S: 如何在不使用图像的情况下使圆圈更像气球形状?
下面的这种蛮力方法似乎适用于相对较少数量的气球。现在有一个名为 create_ballons()
的单独函数,它通过在游戏循环外调用来确定它们的位置和颜色而不执行任何操作。鉴于此,draw_ballons()
现在所做的就是绘制它们。
我说这是蛮力,因为它只是检查每个新的潜在气球 x
位置与所有已选择的位置,并确保它不会太靠近任何一个。
我注意到关于您的 draw_ballons()
函数的一个错误是它不断向两个列表附加更多值,但只绘制前七个 — 所以最终您会 运行 内存不足.
import pygame as pg
import random as r
import sys
MAX_ATTEMPTS = 1000
NUM_BALLOONS = 7
WIDTH, HEIGHT = 688, 387 # Screen size.
RADIUS = 30
DIAMETER = 2 * RADIUS
pg.init()
def create_balloons():
global balloon_list
global colors
x_min, x_max = 0+RADIUS, WIDTH-RADIUS # Constrain to be entirely on screen.
max_balloons = (x_max-x_min) // DIAMETER # Maximum that would fit.
num_balloons = min(NUM_BALLOONS, max_balloons) # No more than what could fit.
balloon_list = []
colors = []
for _ in range(num_balloons):
attempts = 0
while (attempts := attempts+1) <= MAX_ATTEMPTS:
candidate = r.randint(x_min, x_max)
if all(abs(candidate-x) >= DIAMETER for x in balloon_list): # No overlaps.
break
else:
raise RuntimeError(f"No valid candiate after {attempts-1} attempts.")
balloon_list.append(candidate)
balloon_color = r.choice([0,255]), r.randint(0,255), r.choice([0,255])
colors.append(balloon_color)
def draw_balloons(y):
for i, x in enumerate(balloon_list):
pg.draw.circle(screen, colors[i], (x, y), RADIUS)
# Vars #
balloon_list = []
colors = []
x = 0
y = 250
velocity = 5
clock = pg.time.Clock()
screen = pg.display.set_mode((WIDTH, HEIGHT)) # Size of the screen #
caption = pg.display.set_caption("Remember") # Title of the window #
create_balloons()
pg.display.flip() # Updating #
running = True # Game loop bool #
while running: # Game loop #
clock.tick(60)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
pg.quit()
sys.exit()
draw_balloons(y)
pg.display.update()
截图如下:
我正在 pygame
中制作游戏,我正在尝试显示 7 个气球(目前为圆圈),颜色随机,x 轴随机。颜色有效,但是,当尝试在随机 x 轴上显示圆圈时,圆圈通常会重叠。我该如何解决这个问题?
这是我的代码:
import pygame as pg
import random as r
import sys
pg.init()
def draw_balloons():
global balloon_list
global colors
for i in range(7):
balloon_x = r.randint(0, 500)
balloon_color = (r.choice([0,255]), r.randint(0,255), r.choice([0,255]))
balloon_list.append(balloon_x)
colors.append(balloon_color)
pg.draw.circle(screen, colors[i], (balloon_list[i], y), radius=30)
# Vars #
balloon_list = []
colors = []
x = 0
y = 250
velocity = 5
clock = pg.time.Clock()
screen = pg.display.set_mode((688 ,387)) # Size of the screen #
screen.fill('#ffffff')
caption = pg.display.set_caption("Remember") # Title of the window #
pg.display.flip() # Updating #
running = True # Game loop bool #
while running: # Game loop #
clock.tick(60)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
pg.quit()
sys.exit()
draw_balloons()
pg.display.update()
P.S: 如何在不使用图像的情况下使圆圈更像气球形状?
下面的这种蛮力方法似乎适用于相对较少数量的气球。现在有一个名为 create_ballons()
的单独函数,它通过在游戏循环外调用来确定它们的位置和颜色而不执行任何操作。鉴于此,draw_ballons()
现在所做的就是绘制它们。
我说这是蛮力,因为它只是检查每个新的潜在气球 x
位置与所有已选择的位置,并确保它不会太靠近任何一个。
我注意到关于您的 draw_ballons()
函数的一个错误是它不断向两个列表附加更多值,但只绘制前七个 — 所以最终您会 运行 内存不足.
import pygame as pg
import random as r
import sys
MAX_ATTEMPTS = 1000
NUM_BALLOONS = 7
WIDTH, HEIGHT = 688, 387 # Screen size.
RADIUS = 30
DIAMETER = 2 * RADIUS
pg.init()
def create_balloons():
global balloon_list
global colors
x_min, x_max = 0+RADIUS, WIDTH-RADIUS # Constrain to be entirely on screen.
max_balloons = (x_max-x_min) // DIAMETER # Maximum that would fit.
num_balloons = min(NUM_BALLOONS, max_balloons) # No more than what could fit.
balloon_list = []
colors = []
for _ in range(num_balloons):
attempts = 0
while (attempts := attempts+1) <= MAX_ATTEMPTS:
candidate = r.randint(x_min, x_max)
if all(abs(candidate-x) >= DIAMETER for x in balloon_list): # No overlaps.
break
else:
raise RuntimeError(f"No valid candiate after {attempts-1} attempts.")
balloon_list.append(candidate)
balloon_color = r.choice([0,255]), r.randint(0,255), r.choice([0,255])
colors.append(balloon_color)
def draw_balloons(y):
for i, x in enumerate(balloon_list):
pg.draw.circle(screen, colors[i], (x, y), RADIUS)
# Vars #
balloon_list = []
colors = []
x = 0
y = 250
velocity = 5
clock = pg.time.Clock()
screen = pg.display.set_mode((WIDTH, HEIGHT)) # Size of the screen #
caption = pg.display.set_caption("Remember") # Title of the window #
create_balloons()
pg.display.flip() # Updating #
running = True # Game loop bool #
while running: # Game loop #
clock.tick(60)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
pg.quit()
sys.exit()
draw_balloons(y)
pg.display.update()
截图如下: