2012-08-09 1 views
3

저는 파이썬에서 초보자입니다. 마우스가있는 곳이라면 어디서든 원을 그리려고합니다. 마우스와 배경 이미지도 있습니다. 여기 내 코드는 다음과 같습니다.파이 게임이 원을 그리는 이유는 무엇입니까?

while True: 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit 

     if event.type == MOUSEBUTTONDOWN: 
      color = (100,100,100) 
      posx,posy = pygame.mouse.get_pos() 
      screen.lock() 
      pygame.draw.circle(screen, color, (posx,posy), 50) 
      screen.unlock() 

    screen.blit(background,(0,0)) 
    x,y = pygame.mouse.get_pos() 
    x -= mousec.get_width()/2 
    y -= mousec.get_height()/2 

    screen.blit(mousec, (x,y)) 

    pygame.display.update() 

클릭 할 때마다 아무런 변화가 없습니다. 왜 그것이 원을 그리지 않습니까? 도와 주셔서 감사합니다!

답변

4

나는 파이 게임에 대해 거의 아무것도 모른다. 그래서 나는 이것보다 훨씬 더 많은 도움을 줄 수 없다 ...하지만 내가 생각하는 것은 항상 당신의 원을 그리는 것이다. 이 밖으로 시도 :

pygame.init() 
screen = pygame.display.set_mode((640, 480)) 

screen.fill((0,0,0)) 

while True: 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

     if event.type == MOUSEBUTTONDOWN: 
      # draw background first (however) 
      screen.fill((0,0,0)) 

      # draw your other layers (mouse image) 

      # draw the circle 
      color = (255,255,255) 
      posx,posy = pygame.mouse.get_pos() 
      pygame.draw.circle(screen, color, (posx,posy), 50) 

    pygame.display.update() 

은 기본적으로 귀하의 예제에서 무슨 일이 일어나고 있는지에 마우스 다운 이벤트가 무승부를 수행 할 때, 당신은 다음 배경으로 다시 그 위에 그릴 것입니다. 내가 무엇을 mousec하지만 그 때마다 배경 위에 그려지는지 잘 모르겠습니다. 따라서 마우스 클릭으로 그려지는 서클을 보지 못할 것입니다.

예를 들어 배경이 한 번 채워지면 마우스가 아래쪽으로 움직이면 배경이 다시 채워져 이전 상태가 그려집니다 동호회.

또 다른 접근법은 정확한 예제를 사용하여 이벤트를 확인할 때 마우스 위치 만 기록하고 레이어 이후까지 원의 그림을 지연하는 것입니다. 당신은 마지막으로 마우스 위치는 지속적으로 모든 루프에 그 원을 다시 그리기 "기억"과 같습니다

last_mouse_pos = None 

while True: 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit 

     if event.type == MOUSEBUTTONDOWN: 
      last_mouse_pos = pygame.mouse.get_pos() 

     elif event.type == KEYDOWN and event.unicode == 'c': 
      # clear the circle when pressing the 'c' key 
      last_mouse_pos = None 

    screen.blit(background,(0,0)) 
    x,y = pygame.mouse.get_pos() 
    x -= mousec.get_width()/2 
    y -= mousec.get_height()/2 

    screen.blit(mousec, (x,y)) 

    if last_mouse_pos: 
     color = (100,100,100) 
     posx,posy = last_mouse_pos 
     pygame.draw.circle(screen, color, (posx,posy), 50) 

    pygame.display.update() 
이 접근 방식의 차이가 행사에 변화에 대한 응답으로 만 반대로 당신은 항상 각 루프에 모든 것을 그리는 것입니다

.

업데이트 코멘트에서 질문에 대한 응답

... set에 그들을 저장하고 다시 때마다 그들을 잡아하는 것입니다 모든 마우스 클릭을 유지하기 위해 그 두 번째 예제를 수정하는 방법 .

mouse_clicks = set() 

while True: 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit 

     if event.type == MOUSEBUTTONDOWN: 
      mouse_clicks.add(pygame.mouse.get_pos()) 

     elif event.type == KEYDOWN and event.unicode == 'c': 
      # clear the circle when pressing the 'c' key 
      mouse_clicks.clear() 

    screen.blit(background,(0,0)) 
    x,y = pygame.mouse.get_pos() 
    x -= mousec.get_width()/2 
    y -= mousec.get_height()/2 

    screen.blit(mousec, (x,y)) 

    for pos in mouse_clicks: 
     color = (100,100,100) 
     posx,posy = pos 
     pygame.draw.circle(screen, color, (posx,posy), 50) 

    pygame.display.update() 
+0

고마워요! 이것으로 해결했습니다. 다시 클릭 할 때마다 마지막 서클이 지워지므로 서클을 유지하는 방법을 알 수 있습니까? 루프에서 screen.blit (배경, (0,0))을 옮겨야한다고 생각합니다. 그러나 확실하지 않습니다./ –

+1

물론입니다. 배경과 마우스 이미지를 루프 전에 한 번만 blit하면 맨 위에 새로운 원을 그릴 수 있습니다. 이것은 첫 번째 예제에서 작동합니다. 두 번째 예제에서는 위치를 목록에 추가해야 루프가 반복 될 때마다 해당 목록을 반복하고 모든 원을 그릴 수 있습니다. "지우기"키는 목록을 비울 수 있습니다. – jdi

+0

감사합니다. 이제는 마우스 이미지가 루프 밖으로 나갔기 때문에 움직이지 않습니다. 지금 당장 목록에 올거야, 모든 도움을 주셔서 감사합니다! –