2015-04-01 52 views
0
def function(): 

    import pygame 
    import time 
    from pygame.locals import * 

    pygame.init() 

    Width = 272 
    Height = 552 

    white = 255,255,255 
    blue = 0,255,255 
    red = 255,0,0 

    Left_Rect = pygame.Rect(0,452,135,100) 
    Right_Rect = pygame.Rect(137,452,135,100) 

    Location = 136 

    WaterLevel = 452 
    Depth = 100 

    CLOCK = pygame.time.Clock() 
    FPS = 30 

    gameDisplay = pygame.display.set_mode((Width,Height)) 
    pygame.display.set_caption('boat game') 

    stop = False 

    gameDisplay.fill(white) 

    while not stop: 

####### CONTROLLES #################################################### 

     pygame.draw.rect(gameDisplay, red, Left_Rect) 

     pygame.draw.rect(gameDisplay, red, Right_Rect) 


####### BOAT ######################################################### 
     pygame.draw.rect(gameDisplay, red, (Location,WaterLevel-20,40,20)) 

####### WATER ########################################################   
     pygame.draw.rect(gameDisplay,blue,(0,WaterLevel,272,Depth)) 

     WaterLevel -= 1 
     Depth += 1 


###################################################################### 

     for event in pygame.event.get(): 

      print event 

      if event.type == pygame.MOUSEBUTTONDOWN: 
       is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos()) 

       if is_Left == 1: 
        Location -= 5 

      if event.type == pygame.MOUSEBUTTONDOWN: 
       is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos()) 

       if is_Right == 1: 
        Location += 5 

      if event.type == pygame.QUIT: 
       stop = True 
       pygame.quit() 
       quit() 

     CLOCK.tick(FPS) 

     pygame.display.update() 

function() 

我有一個藍色的矩形,上升屏幕,一個紅色的矩形,坐在上升的藍色矩形的頂部。在左下角和右下角放置兩個盒子,當它們被點擊時,紅色盒子左右水平移動。我怎樣才能做到這一點,我可以按住鼠標放在其中一個盒子上,矩形將繼續移動,直到我放開去。如何在pygame中獲得連續運動?

+0

'MOUSEBUTTONDOWN'只有當鼠標如果第一次點擊情況,可以檢查是否有'pygame.mouse.get_pressed()'按鈕仍然按住 - 參見[文檔](http://www.pygame.org/docs/ref/mouse.html) – Marius 2015-04-01 01:03:51

回答

0

許多不同的方法來解決這個問題。 :)

一個非常簡單和快速和骯髒的方法是

  • 設置兩個叫is_Leftis_Right每一個pygame.MOUSEBUTTONDOWN事件發生時間全局變量的一個相應的.collidepoint()方法返回true
  • 在檢測到pygame.MOUSEBUTTONUP時重置兩個變量

您的這種方式更新的代碼:

#your original code 

#declare global variables is_Left and is_Right 
is_Left = False 
is_Right = False 

while not stop: 
    #draw controls, boat and water 
    #increment WaterLevel and Depth 

    #event loop 
    for event in pygame.event.get(): 
     #detect MOUSEBUTTONDOWN event and collision 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos()) 
      is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos()) 

     if event.type == pygame.MOUSEBUTTONUP: 
      #reset is_Left and is_Right 
      is_Left = False 
      is_Right = False    

     if event.type == pygame.QUIT: 
      pygame.quit() 
      quit() 

    #change Location 
    if is_Left: 
     Location -= 5 
    elif is_Right: 
     Location += 5 

    CLOCK.tick(FPS) 

    pygame.display.update() 

另一種方法是,馬呂斯已經提到,要使用PyGames鼠標模塊pygame.mouse.get_pressed()功能讓鼠標按鍵的狀態。

該函數返回三個布爾值的tupel,代表所有鼠標按鈕的狀態。在你的情況下,我們只需要第一個值,它代表鼠標左鍵的狀態。

我們稱之爲pygame.mouse.get_pressed()[0]主遊戲循環內得到鼠標左鍵的狀態和檢查的同時,如果各.collidepoint()方法返回true

#your original code 

while not stop: 
    #draw controls, boat and water 
    #increment WaterLevel and Depth 

    #event loop 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      quit() 

    #get state of left mouse button 
    isPressed = pygame.mouse.get_pressed()[0] 

    #change location 
    if ispressed and Left_Rect.collidepoint(pygame.mouse.get_pos()): 
     Location -= 5 
    elif ispressed and Right_Rect.collidepoint(pygame.mouse.get_pos()): 
     Location += 5 

    CLOCK.tick(FPS) 

    pygame.display.update() 

    CLOCK.tick(FPS) 

    pygame.display.update() 

在個人比較喜歡第二個變體,因爲我們不需要任何額外的全局變量(is_Left,is_Right)。

我希望這有助於:)

+0

謝謝,這有幫助。 – 2015-04-01 20:30:42

+0

@RomanFormicola:沒問題,我很高興能幫助你! :) – elegent 2015-04-02 18:05:56