2017-12-20 3 views
0

저는 현재 탁구 게임을 만들려고 노력하고 있습니다. 그러나 저는 현재 그 부분에서 고심하고 있습니다. 나는 화면의 가장자리가 튀어 오르면 공을 튀게 할 필요가 있지만, 어떤 이유로해서 앞으로 계속해서 충돌을 감지하지 못합니다. 여기 내 코드는 다음과 같습니다.원이 창 가장자리에 있는지 확인하십시오.

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) 

왜 이것이 작동하지 않는지 알고 계십니까? 나는 최근에 파이 게임을 배웠다.

+0

항상 [최소]로 코드를 줄이십시오 (https://stackoverflow.com/help/mcve) 당신이 여기에 게시하기 전에. 예를 들어 시작 및 패들 코드는 필요하지 않습니다. 그리고 더 읽기 쉬운 변수 이름을 사용하십시오 ('btc','ymb2' 등은 다소 나쁜 이름입니다). – skrx

+0

은'PyGame.Rect()'를 사용하여 위치와 크기를 유지 한 다음'screen.get_rect()'에 사용 된 rect와 함께'rect.colliderect (other_rect)'를 사용할 수 있습니다. 그리고 당신은 여전히 ​​원을 표시하기 위해 그것을 사용할 수 있습니다 -'screen.blit (circle, rect)' – furas

답변

1

"공이 범위를 벗어 났는지"를 알아 내기 위해 확인하는 변수 (주석, 감사합니다!)는 절대로 변경되지 않습니다. E. g. btc은 처음에는 루프 외부에서 한 번 할당 된 다음 다시는 할당되지 않습니다.

by (등)이 변경 될 때마다 변경해야 할 것입니다.

이것은 좋은 코딩 스타일이 아닙니다. 그러나 그것을 고치려고하면 웜이 열리게됩니다 .-

EDIT : 또한 위치 변수 대신 속도 변수를 부정해야합니다. 이자형. by 대신 bym : bym = bym * -1입니다. 이 두 가지 문제를 해결 한 후, 내 공은 창의 모든 가장자리에서 끊임없이 튀어 오르고있었습니다. 페달는하지만, 아직 영향력했다 :

# check if ball is out of bounds 
    btc = by 
    blc = bx 
    bbc = by + br + br 
    brc = bx + br + br 
    if start and startballmove: 
     if btc <= 0: 
      bym = bym * -1 
      print("top side") 
     if bbc >= height: 
      bym = bym * -1 
      print("bottom side") 
     if blc <= 0: 
      bxm = bxm * -1 
      print("left side") 
     if brc >= width: 
      bxm = bxm * -1 
      print("right side") 
+0

감사합니다. 정말 도움이되었습니다! – 05bs001

관련 문제