函数 returns 坐标为 (x, y) 的元组列表,但如果使用这些坐标绘制矩形,它们不应相交
Function that returns list of tuples with coordinates (x, y), but if you draw rectangles using these coordinates, they should not intersect
这里回答了几乎相同的问题:
但是我有坐标生成器,而不是矩形,所以我认为这个话题必须存在。
问题是这个函数还是return的非唯一坐标,我觉得是公式有误,但我不确定,可能是别的原因。
if x1 + 320 > x > x1 or y1 + 120 > y > y1
应该return唯一坐标的函数:
import random
length = 3
items_coord = []
while len(items_coord) < length:
same = False
x = random.randint(213, 752)
y = random.randint(90, 360)
if len(items_coord) == 0:
items_coord.append((x, y))
continue
for x1, y1 in items_coord:
if x1 + 320 > x > x1 or y1 + 120 > y > y1:
same = True
break
if not same:
items_coord.append((x, y))
检查它们是否相互交叉的代码:
import pygame
pygame.init()
screen = pygame.display.set_mode((int(pygame.display.Info().current_w // 1.2), int(pygame.display.Info().current_h // 1.2)))
width = int(pygame.display.Info().current_w)
height = int(pygame.display.Info().current_h)
font = pygame.font.SysFont('calibri', 15)
running = True
colors = ['green', 'white', 'yellow', 'blue']
items_coords = get_rand_coord(10)
for i in range(10):
pygame.draw.rect(screen, random.choice(colors), (items_coords[i][0], items_coords[i][1], width // 8, height // 12))
pygame.display.update()
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.time.Clock().tick(30)
您刚刚验证了点(x
、y
)是否不在矩形区域内。 (x
, y
) 只是矩形的左上角。如果新矩形的另一个角与另一个矩形相交,但不与左上角相交,则不会找到交点
你必须检查位置 (x
, y
) 和 (x1
, y1
) 的矩形是否不相交。为碰撞测试创建pygame.Rect
objects and use colliderect()
:
rect_width = width // 8
rect_height = height // 12
same = False
x = random.randint(213, 752)
y = random.randint(90, 360)
rect = pygame.Rect(x, y, rect_width, rect_height)
# [...]
for x1, y1 in items_coord:
if rect.colliderect((x1, y1, rect_width, rect_height))
same = True
break
这里回答了几乎相同的问题:
问题是这个函数还是return的非唯一坐标,我觉得是公式有误,但我不确定,可能是别的原因。
if x1 + 320 > x > x1 or y1 + 120 > y > y1
应该return唯一坐标的函数:
import random
length = 3
items_coord = []
while len(items_coord) < length:
same = False
x = random.randint(213, 752)
y = random.randint(90, 360)
if len(items_coord) == 0:
items_coord.append((x, y))
continue
for x1, y1 in items_coord:
if x1 + 320 > x > x1 or y1 + 120 > y > y1:
same = True
break
if not same:
items_coord.append((x, y))
检查它们是否相互交叉的代码:
import pygame
pygame.init()
screen = pygame.display.set_mode((int(pygame.display.Info().current_w // 1.2), int(pygame.display.Info().current_h // 1.2)))
width = int(pygame.display.Info().current_w)
height = int(pygame.display.Info().current_h)
font = pygame.font.SysFont('calibri', 15)
running = True
colors = ['green', 'white', 'yellow', 'blue']
items_coords = get_rand_coord(10)
for i in range(10):
pygame.draw.rect(screen, random.choice(colors), (items_coords[i][0], items_coords[i][1], width // 8, height // 12))
pygame.display.update()
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.time.Clock().tick(30)
您刚刚验证了点(x
、y
)是否不在矩形区域内。 (x
, y
) 只是矩形的左上角。如果新矩形的另一个角与另一个矩形相交,但不与左上角相交,则不会找到交点
你必须检查位置 (x
, y
) 和 (x1
, y1
) 的矩形是否不相交。为碰撞测试创建pygame.Rect
objects and use colliderect()
:
rect_width = width // 8
rect_height = height // 12
same = False
x = random.randint(213, 752)
y = random.randint(90, 360)
rect = pygame.Rect(x, y, rect_width, rect_height)
# [...]
for x1, y1 in items_coord:
if rect.colliderect((x1, y1, rect_width, rect_height))
same = True
break