나는 Pygame (분명히)으로 Pygame의 Pong 게임을 만들고 Pygame에 익숙하지 않으므로 공이 패들에 닿았을 때의 물리학 작업에 도움이되고 싶을 때 속도를 뒤집고 반대 방향으로 갈 것입니다 . 지금까지는 모든 것이 작동하지만 공이 패들로 이동하면 오른쪽으로 지나가고 방향이 바뀌지 않습니다. 나는 외륜이 화면을 벗어나지 않고 볼이 벽을 만날 때 방향을 바꾸지 만, 볼이 외륜을 만날 때가 아니라 방향을 바꾸기 때문에 운동을했습니다. 도움이나 조언을 주시면 감사하겠습니다. Pygame Rect Collision
내 패들 클래스 : 는class Paddle:
def __init__(self, x, y):
self.x = x
self.y = y
self.height = 40
self.width = 10
def draw(self, canvas):
pygame.draw.rect(canvas, pygame.Color(0,0,255),(self.x,self.y,self.width,self.height))
def contains(self, ptX, ptY):
return self.x < ptX < self.x + self.width & self.y < ptY < self.y + self.height
def overlaps(self, otherRectangle):
return otherRectangle.colliderect(Rect(self.x,self.y,self.height, self.width))
내 볼 클래스는
class Ball:
def __init__(self, x, y):
#position of ball
self.x = x
self.y = y
#speed of ball
self.dx = 5
self.dy = 5
self.height = 10
self.width = 10
def draw(self, canvas):
pygame.draw.rect(canvas, pygame.Color(0,255,0), (self.x,self.y,self.width,self.height))
def reset(self):
self.x = 320
self.y = 240
self.dx = -self.dx
self.dy = 5
내 목표는 패들을 접촉 또는에 반사 공 역 (음 속도)의 속도를하는 것입니다 (중복 전철기).
당신의 메인 루프 코드? –
https://docs.google.com/document/d/1joSOmt1qN5kBVXUnRuDvO97wt7e2Em4VgI-pXEsCTWg/edit?usp=sharing – ttmcl98
Protip : 코드 공유를위한 Google 문서보다 Github Gists (https://gist.github.com/)가 훨씬 좋음 . –