저는 현재 탁구 게임을 만들려고 노력하고 있습니다. 그러나 저는 현재 그 부분에서 고심하고 있습니다. 나는 화면의 가장자리가 튀어 오르면 공을 튀게 할 필요가 있지만, 어떤 이유로해서 앞으로 계속해서 충돌을 감지하지 못합니다. 여기 내 코드는 다음과 같습니다.원이 창 가장자리에 있는지 확인하십시오.
import sys, pygame, random
pygame.init()
width, height = 1200, 675
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
clock = pygame.time.Clock()
FPS = 120
xmb1 = False
xmf1 = False
ymb1 = False
ymf1 = False
xmb2 = False
xmf2 = False
ymb2 = False
ymf2 = False
squareh = 275
squarew = 35
squares = 3
x1 = 75
y1 = (height/2) - (squareh/2)
x2 = width - 75 - squarew
y2 = (height/2) - (squareh/2)
BLACK = (0,0,0)
WHITE = (255,255,255)
font = pygame.font.SysFont("arial", 30)
text = font.render("Press Space to start", True, WHITE)
text3 = font.render("3", True, WHITE)
text2 = font.render("2", True, WHITE)
text1 = font.render("1", True, WHITE)
startt = font.render("Start!", True, WHITE)
text3b = False
text2b = False
text1b = False
starttb = False
start = False
startballmove = False
bx = width/2
by = height/2
br = 40
bxm = random.randint(-6, 6)
bym = random.randint(-6, 6)
btc = by
blc = bx
bbc = by + br + br
brc = bx + br + br
circle=pygame.Surface((br * 2, br * 2))
circle.fill((0, 0, 0))
pygame.draw.circle(circle, WHITE , (br, br), br, 0)
circle.set_colorkey((0, 0, 0))
while 1:
if start and not text1b and not text2b and not text3b and not starttb and not startballmove:
text3b = True
pygame.time.set_timer(pygame.USEREVENT, 1000)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
# pad 2 check if key is down
if event.key == pygame.K_UP:
ymb2 = True
if event.key == pygame.K_DOWN:
ymf2 = True
# pad 1 check if key is down
if event.key == pygame.K_w:
ymb1 = True
if event.key == pygame.K_s:
ymf1 = True
if event.key == pygame.K_SPACE:
start = True
elif event.type == pygame.KEYUP:
#pad 2 check if key goes up
if event.key == pygame.K_UP:
ymb2 = False
if event.key == pygame.K_DOWN:
ymf2 = False
# pad 1 check if key goes up
if event.key == pygame.K_w:
ymb1 = False
if event.key == pygame.K_s:
ymf1 = False
# check if window has been resized
if event.type == pygame.VIDEORESIZE:
width = event.dict['size'][0]
height = event.dict['size'][1]
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
if event.type == pygame.USEREVENT:
# check if start should be hidden
if starttb:
starttb = False
startballmove = True
# check if start should be showed
if text1b and not text2b and not text3b:
text1b = False
text2b = False
text3b = False
starttb = True
# check if 1 should be showed
if text2b and not text3b and not text1b:
text3b = False
text2b = False
text1b = True
# check if 2 should be showed
if text3b and not text2b and not text1b:
text3b = False
text2b = True
text1b = False
# check if pad 1 is out of bounds and move it
if ymb1 and not (y1 <= 0): y1 -= squares
if ymf1 and not (y1 + squareh >= height): y1 += squares
if y1 > (height - squareh) + 1: y1 -= squares
# check if pad 2 is out of bounds and move it
if ymb2 and not (y2 <= 0): y2 -= squares
if ymf2 and not (y2 + squareh >= height): y2 += squares
if y2 > (height - squareh) + 1: y2 -= squares
# put pads in center if game has not started
if not start:
# pad 1
x1 = 75
y1 = (height/2) - (squareh/2)
# pad 2
x2 = width - 75 - squarew
y2 = (height/2) - (squareh/2)
#ball
bx = width/2 - br
by = height/2 - br
# put pads in center in x if game has started
else:
# pad 1
x1 = 75
# pad 2
x2 = width - 75 - squarew
# if ball has not started moving center it
if not startballmove:
bx = width/2 - br
by = height/2 - br
# check if movement variables are 0
while bxm == 0 or bym == 0:
if bxm == 0:
bxm = random.randint(-6, 6)
if bym == 0:
bym = random.randint(-6, 6)
screen.fill(BLACK)
# draw starting text if game has not started
if not start:
screen.blit(text,((width/2) - text.get_width() // 2, (height/4) - text.get_height() // 2))
# put 3 on screen
if start and text3b:
screen.blit(text3,((width/2) - 15, (height/4) - (text.get_height()/2)))
# put 2 on screen
if start and text2b:
screen.blit(text2,((width/2) - 15, (height/4) - (text.get_height()/2)))
# put 1 on screen
if start and text1b:
screen.blit(text1,((width/2) - 15, (height/4) - (text.get_height()/2)))
# put start on screen
if start and starttb:
screen.blit(startt,((width/2) - (text.get_width()/8), (height/4) - (text.get_height()/2)))
# check if ball is out of bounds
if start and startballmove:
if btc <= 0:
by = by * -1
print("top side")
if bbc >= height:
by = by * -1
print("bottom side")
if blc <= 0:
bx = bx * -1
print("left side")
if brc >= width:
bx = bx * -1
print("right side")
# move ball
if start and startballmove:
bx += bxm
by += bym
# draw circle if game start
if start:
screen.blit(circle, (int(bx), int(by)))
# draw pad 1
pygame.draw.rect(screen, WHITE, (x1, y1, squarew, squareh), 0)
# draw pad 2
pygame.draw.rect(screen, WHITE, (x2, y2, squarew, squareh), 0)
pygame.display.flip()
clock.tick(FPS)
왜 이것이 작동하지 않는지 알고 계십니까? 나는 최근에 파이 게임을 배웠다.
항상 [최소]로 코드를 줄이십시오 (https://stackoverflow.com/help/mcve) 당신이 여기에 게시하기 전에. 예를 들어 시작 및 패들 코드는 필요하지 않습니다. 그리고 더 읽기 쉬운 변수 이름을 사용하십시오 ('btc','ymb2' 등은 다소 나쁜 이름입니다). – skrx
은'PyGame.Rect()'를 사용하여 위치와 크기를 유지 한 다음'screen.get_rect()'에 사용 된 rect와 함께'rect.colliderect (other_rect)'를 사용할 수 있습니다. 그리고 당신은 여전히 원을 표시하기 위해 그것을 사용할 수 있습니다 -'screen.blit (circle, rect)' – furas