2012-08-09 66 views
3

我是一名Python初學者,我試圖在鼠標所在的位置繪製一個圓圈(我也有一個鼠標和背景圖片)。這裏是我的代碼:pygame爲什麼不畫圓圈?

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幾乎一無所知,所以我不能提供比這更多的幫助......但是我認爲你正在做的事情總是回過頭來看看你的圈子。嘗試了這一點:

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(background,(0,0))移出循環...但我不確定:/ – 2012-08-09 23:27:34

+1

當然。只需在循環之前將背景和鼠標圖像移動一次,這樣您就只能在頂部繪製新的圓圈。這將在我的第一個例子中工作。第二個示例將要求您將位置追加到列表中,以便每次循環時都可以遍歷該列表並繪製所有圓。 「清除」鍵可以清空列表。 – jdi 2012-08-09 23:29:38

+0

謝謝!現在鼠標圖像不會移動,因爲它不在循環中。我現在要在名單上工作,感謝所有的幫助! – 2012-08-09 23:33:36