为什么即使没有碰撞或没有与地板接触,我的代码也会不断打印 1?
Why is my code constantly printing 1 even if there is no collision or has not had contact with the floor?
出于某种原因,我的代码正在打印 1,即使它没有与任何其他球或地板发生碰撞。我正在使用 sys、pygame.locals
和 pygame 作为我的主要库,并且没有任何错误出现。
代码:
pygame.init()
screen = pygame.display.set_mode((1000, 500))
green = (0, 255, 0)
circles = []
gravity = 1
def mainPhysics():
for circle_pos in circles:
circle_pos[1] += gravity
if circle_pos[1] > 495:
boundaries = circle_pos[1] - 495
circle_pos[1] -= boundaries
if circle_pos[0] < 0:
boundaries2 = circle_pos[0]
circle_pos[0] += boundaries2
if circle_pos[0] > 995:
boundaries3 = circle_pos[0] - 995
circle_pos[0] - boundaries3
circle_rect = pygame.draw.circle(screen, green, circle_pos, 5)
collide = circle_rect.collidepoint(circle_pos)
print('1') if collide else print('0')
def input():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
circles.append(list(event.pos))
if event.type == pygame.MOUSEBUTTONDOWN and pygame.MOUSEMOTION:
circles.append(list(event.pos))
clock = pygame.time.Clock()
while True:
screen.fill((15, 15, 15))
input()
mouse_pos = pygame.mouse.get_pos()
mainPhysics()
pygame.display.update()
clock.tick(120)
对于碰撞检测,您需要 2 个对象。遍历嵌套循环中的圆圈。
要检测 2 个圆圈之间的碰撞,请参阅 , or How do I detect collision in pygame?:
for i, circle_pos_1 in enumerate(circles):
for circle_pos_2 in circles[i+1:]:
dx = circle_pos_2[0] - circle_pos_1[0]
dy = circle_pos_2[1] - circle_pos_1[1]
if dx*dx + dy*dy < 5*5:
# [...]
最小示例:
import pygame
pygame.init()
screen = pygame.display.set_mode((1000, 500))
green = (0, 255, 0)
circles = []
gravity = 1
def input():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
circles.append(list(event.pos))
count = 0
clock = pygame.time.Clock()
while True:
screen.fill((15, 15, 15))
input()
mouse_pos = pygame.mouse.get_pos()
for circle_pos in circles:
circle_pos[1] += gravity
if circle_pos[1] > 495:
boundaries = circle_pos[1] - 495
circle_pos[1] -= boundaries
if circle_pos[0] < 0:
boundaries2 = circle_pos[0]
circle_pos[0] += boundaries2
if circle_pos[0] > 995:
boundaries3 = circle_pos[0] - 995
circle_pos[0] - boundaries3
for i, circle_pos_1 in enumerate(circles):
for circle_pos_2 in circles[i+1:]:
dx = circle_pos_2[0] - circle_pos_1[0]
dy = circle_pos_2[1] - circle_pos_1[1]
if dx*dx + dy*dy < 5*5:
count += 1
print(f"hit: {count}")
for circle_pos in circles:
pygame.draw.circle(screen, green, circle_pos, 5)
pygame.display.update()
clock.tick(120)
出于某种原因,我的代码正在打印 1,即使它没有与任何其他球或地板发生碰撞。我正在使用 sys、pygame.locals
和 pygame 作为我的主要库,并且没有任何错误出现。
代码:
pygame.init()
screen = pygame.display.set_mode((1000, 500))
green = (0, 255, 0)
circles = []
gravity = 1
def mainPhysics():
for circle_pos in circles:
circle_pos[1] += gravity
if circle_pos[1] > 495:
boundaries = circle_pos[1] - 495
circle_pos[1] -= boundaries
if circle_pos[0] < 0:
boundaries2 = circle_pos[0]
circle_pos[0] += boundaries2
if circle_pos[0] > 995:
boundaries3 = circle_pos[0] - 995
circle_pos[0] - boundaries3
circle_rect = pygame.draw.circle(screen, green, circle_pos, 5)
collide = circle_rect.collidepoint(circle_pos)
print('1') if collide else print('0')
def input():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
circles.append(list(event.pos))
if event.type == pygame.MOUSEBUTTONDOWN and pygame.MOUSEMOTION:
circles.append(list(event.pos))
clock = pygame.time.Clock()
while True:
screen.fill((15, 15, 15))
input()
mouse_pos = pygame.mouse.get_pos()
mainPhysics()
pygame.display.update()
clock.tick(120)
对于碰撞检测,您需要 2 个对象。遍历嵌套循环中的圆圈。
要检测 2 个圆圈之间的碰撞,请参阅
for i, circle_pos_1 in enumerate(circles):
for circle_pos_2 in circles[i+1:]:
dx = circle_pos_2[0] - circle_pos_1[0]
dy = circle_pos_2[1] - circle_pos_1[1]
if dx*dx + dy*dy < 5*5:
# [...]
最小示例:
import pygame
pygame.init()
screen = pygame.display.set_mode((1000, 500))
green = (0, 255, 0)
circles = []
gravity = 1
def input():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
circles.append(list(event.pos))
count = 0
clock = pygame.time.Clock()
while True:
screen.fill((15, 15, 15))
input()
mouse_pos = pygame.mouse.get_pos()
for circle_pos in circles:
circle_pos[1] += gravity
if circle_pos[1] > 495:
boundaries = circle_pos[1] - 495
circle_pos[1] -= boundaries
if circle_pos[0] < 0:
boundaries2 = circle_pos[0]
circle_pos[0] += boundaries2
if circle_pos[0] > 995:
boundaries3 = circle_pos[0] - 995
circle_pos[0] - boundaries3
for i, circle_pos_1 in enumerate(circles):
for circle_pos_2 in circles[i+1:]:
dx = circle_pos_2[0] - circle_pos_1[0]
dy = circle_pos_2[1] - circle_pos_1[1]
if dx*dx + dy*dy < 5*5:
count += 1
print(f"hit: {count}")
for circle_pos in circles:
pygame.draw.circle(screen, green, circle_pos, 5)
pygame.display.update()
clock.tick(120)