내 목표는 볼 스프라이트가 'g_ball'스프라이트 중 하나에 도달 할 때 내 프로그램이 충돌을 감지 할 수있게하는 것입니다. 코드는 apperently 충돌을 감지하고 그것을 테스트하는 "print"문을 가지고 있지만 ... 스프라이트 중 아무 것도 만지지 않고도 계속 "진행"을 인쇄합니다. 여기에 코드입니다 : 더이 충돌 설정 오프셋이 없기 때문에sprite가 글자 그대로 충돌하지 않아도 프로그램이 충돌을 감지합니다 'pygame.sprite.collide_rect'
while 1:
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_a:
m_x = -4
s+=1
elif event.key == K_d:
m_x = +4
s2+=1
if event.type == KEYUP:
if event.key == K_a:
m_x = 0
elif event.key == K_d:
m_x = 0
x+= m_x
y+= m_y
ball = pygame.sprite.Sprite()
ball.image = pygame.image.load('red_ball.png').convert()
ball.rect = ball.image.get_rect()
ball.image.set_colorkey((white))
screen.blit(ball.image,(x,y))
if x > 640:
x = 0
if x < 0:
x = 640
g_ball = pygame.sprite.Sprite()
g_ball.image = pygame.image.load('green_ball.png').convert()
g_ball.rect = g_ball.image.get_rect()
g_ball.image.set_colorkey(white)
screen.blit(g_ball.image,(50,t))
t+=5
if t > 521:
t = 0
collision = pygame.sprite.collide_rect(ball, g_ball)
if collision == True:
print ('PROGRESS!!!')