에서 외륜을 치는 경우에만 공을 튀게 만드는 방법이 누구든 도와 줄 수 있습니까? 공이 패들에 부딪쳤을 때이를 감지하고 rect_change_y 변수를 변경하여 바운스를 감지하는 간단한 방법을 만드는 데 문제가 있습니다. 여기는 작동하는 코드이지만 공이 패들과 상호 작용하지 않거나 게임의 바닥에 닿았을 때 죽지 않습니다.공이 파이썬, 패기 (브레이크 아웃 레크 리 에이션)
import pygame
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
rect_x = 10
rect_y = 250
pad_x = 350
pad_y = 480
ball = 3
rect_change_x = 1
rect_change_y = 1
pad_x_c = 0
pygame.init()
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.mouse.set_visible(0)
pygame.display.set_caption("Breakout Recreation WIP")
done=False
clock=pygame.time.Clock()
# -------- Main Program Loop -----------
while ball != 0:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
keys = pygame.key.get_pressed() #checking pressed keys
if keys[pygame.K_LEFT]:
pad_x_c -= 2
elif keys[pygame.K_RIGHT]:
pad_x_c += 2
else :
pad_x_c = 0
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(black)
# Draw the rectangle
pygame.draw.rect(screen,white,[rect_x,rect_y,15,15])
pygame.draw.rect(screen,white,[pad_x,pad_y,50,10])
# Move the rectangle starting point
rect_x += rect_change_x
rect_y += rect_change_y
pad_x += pad_x_c
# Bounce the ball if needed
if rect_y > 480 or rect_y < 10:
rect_change_y = rect_change_y * -1
if rect_x > 680 or rect_x < 5:
rect_change_x = rect_change_x * -1
if pad_x <= 5 :
pad_x_c = 0
if pad_x >= 645 :
pad_x_c = 0
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
pygame.display.flip()
clock.tick(100)
pygame.quit()
다음은 (나에게) 효과가 있지만 작동하지 않는 코드입니다.
import pygame
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
rect_x = 10
rect_y = 250
pad_x = 350
pad_y = 480
ball = 3
rect_change_x = 1
rect_change_y = 1
pad_x_c = 0
pygame.init()
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.mouse.set_visible(0)
pygame.display.set_caption("Breakout Recreation WIP")
done=False
clock=pygame.time.Clock()
# -------- Main Program Loop -----------
while ball != 0:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
keys = pygame.key.get_pressed() #checking pressed keys
if keys[pygame.K_LEFT]:
pad_x_c -= 2
elif keys[pygame.K_RIGHT]:
pad_x_c += 2
else :
pad_x_c = 0
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(black)
# Draw the rectangle
pygame.draw.rect(screen,white,[rect_x,rect_y,15,15])
pygame.draw.rect(screen,white,[pad_x,pad_y,50,10])
# Move the rectangle starting point
rect_x += rect_change_x
rect_y += rect_change_y
pad_x += pad_x_c
# Bounce the ball if needed
if rect_y == pad_y:
if rect_x == pad_x:
rect_change_y = rect_change_y * -1
if rect_y > 490:
ball -= 1
rect_y = 250
rect_x = 10
rect_change_x = 1
rect_change_y = 1
if rect_y < 10:
rect_change_y = rect_change_y * -1
if rect_x > 680 or rect_x < 5:
rect_change_x = rect_change_x * -1
if pad_x <= 5 :
pad_x_c = 0
if pad_x >= 645 :
pad_x_c = 0
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
pygame.display.flip()
clock.tick(100)
pygame.quit()
마침내, 블록을 만드는 방법과 공을 대신 파괴하는 것을 제외하고는 똑같은 방법을 제안합니다.
감사합니다. 모두들!
감사합니다! 새로운 명령이 어떻게 작동하는지 설명 할 수 있습니까? 나는 그 중 어떤 명령도 사용하지 않았습니다. 나는 당신이 그것의 대부분을 어떻게 지키는 지 좋아하지 만, 나는 move_ip이 어떻게 작동하는지, 클램프가 작동하는지 모르겠다 ... 고마워! 또한 공이 패들 아래쪽에 닿았을 때 쉽게 감지 할 수 있습니까? – Pip
@PythonInProgress 각 메소드를 설명하기 위해 코드의 작은 샘플을 추가했으며 튀는 공 이동 방향도 수정했습니다 (작은 오류가 있음). –
감사합니다 Rodas! 가장 도움이되는 : P! – Pip