2014-12-09 8 views
1

나는 파이 게임에서 단순한 tic-tac-toe 게임을 쓰고 있으며, 필요한 답을 찾을 수 없습니다. 마우스를 특정 좌표 평면 내에서 클릭하면 "X"가 나타나길 원합니다. 지금 가지고있는 코드는 마우스 버튼을 누르고있을 때 X 만 표시합니다. 마우스 위치 :간단한 Tic-Tac_Toe 게임 파이 게임

mouse_pos = mouse.get_pos() 

그리고 RECT :

rect1 = Rect(top_left_corner_x, top_left_corner_y, width, height) 

는 그런 다음 확인을 마우스이 RECT에 클릭하면 : 감사

여기
import pygame 
import sys 

red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 
darkBlue = (0,0,128) 
white = (255,255,255) 
black = (0,0,0) 
pink = (255,200,200) 

#iconChoice = input("Would you like to be X's or O's?(X/O)?:") 

iconChoice = "X" 

# initialize game engine 
pygame.init() 
pygame.font.init() 
font = pygame.font.SysFont("Century Schoolbook",12) 
# set screen width/height and caption 
size = [500,500] 
screen = pygame.display.set_mode(size) 
pygame.display.set_caption('My Game') 
# initialize clock. used later in the loop. 
clock = pygame.time.Clock() 

# Loop until the user clicks close button 
done = False 
while done == False: 
    # write event handlers here 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
    # write game logic here 

    sys_font = pygame.font.SysFont("None",60) 
    rendered = sys_font.render(iconChoice, 0, black) 
    mousexpos, mouseypos = pygame.mouse.get_pos() 
    pygame.event.get() 

    '''     
    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 166 and mousexpos < 322 and mouseypos < 156: 
     print("2") 

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 332 and mouseypos < 156: 
     print("3") 
     done = True 

    elif pygame.mouse.get_pressed()[0] == True and mousexpos < 156 and mouseypos > 166 and mouseypos < 322: 
     print("4") 
     done= True 

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 166 and mousexpos < 322 and mouseypos > 166 and mouseypos < 322: 
     print("5") 
     done= True 

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 332 and mouseypos > 166 and mouseypos < 322: 
     print("6") 
     done = True 

    elif pygame.mouse.get_pressed()[0] == True and mousexpos < 156 and mouseypos > 332: 
     print("7") 
     done= True 

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 166 and mousexpos < 322 and mouseypos > 332: 
     print("8") 
     done= True 

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 332 and mouseypos > 332: 
     print("9") 
     done = True 

    ''' 

    # clear the screen before drawing 
    screen.fill((255, 255, 255)) 


    # draw 
    pygame.draw.rect(screen, black, (10,156,480,15), 0) 
    pygame.draw.rect(screen, black, (10,322,480,15), 0) 
    pygame.draw.rect(screen, black, (156,10,15,480), 0) 
    pygame.draw.rect(screen, black, (322,10,15,480), 0) 
    pygame.display.flip() 

    if pygame.mouse.get_pressed()[0] == True and mousexpos < 156 and mouseypos < 156: 
     print("1") 
     screen.blit(rendered, (20,15)) 
     pygame.display.update(10,10,166,166) 


    # display what’s drawn. this might change. 
    pygame.display.update() 

    # run at 20 fps 
    clock.tick(20) 

# close the window and quit  
pygame.quit() 
+1

내 머리 꼭대기에서, pygame.mouse.get_pressed() 값을 변수에 저장하고 후속 평가에 변수를 사용해야합니다. – Josh

+0

답변 해 주셔서 감사합니다. 키가 마우스 버튼을 눌렀을 때만 변수가 true가되기 때문에 같은 일이 아니겠습니까? –

답변

1

는 당신이 필요합니다 무엇

if event.type == MOUSEBUTTONDOWN and rect1.collidepoint(mouse_pos): 
    #draw X 

이것을 이벤트 루프 또는 외부 (그러나 바람직하게는 내부)에 넣을 수 있습니다.

+0

답변 해 주셔서 감사 드리지만 코드를 편집 할 때 결과는 같습니다. 마우스가 눌려 졌을 때만 X가 나타납니다 –

+0

이벤트 루프에 이것을 넣으십시오. 그 트릭을해야합니다. – sirvar