-3
파이 게임에서 게임을 만들었으며 충돌 감지 기능을 추가하기 시작했습니다. 두 마리의 스프 라이트 (오리와 록)의 경우, 나는 그것들에 rect 변수를주었습니다. 충돌이 있는지 확인하려면 무엇을해야합니까? 이 코드입니다 : 그 변수 대신에 사각형의 원을 만들기 위해 조언을 것파이썬 충돌 감지
import pygame, sys
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
#screen constants
screen = pygame.display.set_mode((1050,350),0,32)
x = 0
screenWidth = 1050
speed = 2
#background constants
b1 = "bg1.jpg"
b2 = "bg2.jpg"
back = pygame.image.load(b1).convert()
back2 = pygame.image.load(b2).convert()
#duck constants
duck = pygame.image.load("duck.png")
rect_duck = duck.get_rect()
duckY = 246
duckMoveY=0
flying = False
falling = False
#rock constants
rock = pygame.image.load("rock.png")
rect_rock = rock.get_rect()
rockX = 0
#init game loop
while True:
#check if they want to quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#see if they fly
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
flying = True
#see if they fall
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
falling = True
flying = False
#flying
if flying == True:
duckMoveY = -5
#falling
if falling == True:
duckMoveY = 5
#display the backgrounds
screen.blit(back, (x,0))
screen.blit(back2,(x-screenWidth,0))
#display the duck
screen.blit(duck, (500,duckY))
#display the rock
screen.blit(rock, (rockX, 275))
#move background
x += speed
if x == screenWidth:
x = 0
#move the duck
duckY += duckMoveY
#move the rock
rockX += speed
#stop them falling off the screen
if duckY > 246:
falling = False
duckY = 246
#stop them flying off the top
if duckY < 0:
flying = False
duckY = 0
#update the screen and set fps
pygame.display.update()
pygame.display.flip()
msElapsed = clock.tick(100)
['pygame.Rect.colliderect()'] (http://www.pygame.org/docs/ref/rect.html)를 사용할 수 있습니다. 그것을 사용하는 [예제] (http://pygame.org/project-Rect+Collision+Response-1061-.html)가 있습니다. – martineau