2012-12-24 18 views
0

我正在做一個遊戲,並在標題屏幕上,當我在座標278,365處懸停時按下返回鍵時,我想讓它閃爍新的背景(background1)。我該如何做到這一點,以便當我在這些座標處按Enter鍵時,它將刪除當前背景並將其替換爲新的背景1?在Pygame中,我如何清除blit以將其替換爲另一個?

screen=pygame.display.set_mode((1024,768),0,32) 

background=pygame.image.load(bif).convert() 
background1=pygame.image.load(wi1).convert() 
cursor=pygame.image.load(mif).convert_alpha() 

x,y =278,365 


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

     if event.type == KEYDOWN: 
      if event.key == K_DOWN: 
       x=420 
       y=508 
      elif event.key == K_UP: 
       x=278 
       y=365 

      elif event.key == K_RETURN and x==420 and y==508: 
       pygame.quit() 
       sys.exit() 

      elif event.key == K_RETURN and x==278 and y==365: 







    screen.blit(background, (0,0)) 
    screen.blit(cursor, (x,y)) 
    pygame.display.update() 
+0

您不必刪除舊的背景;只是將新的背景而不是舊的背景。 – Amber

+0

你不能「清除」blits,你只能在其他地方使用blit。 –

+0

您可以使用[screen.fill()](http://www.pygame.org/docs/ref/surface.html#Surface.fill)每個循環清除整個屏幕,而不使用rect參數。然後畫出你的新場景。 – ninMonkey

回答

1
# your code 
while True: 
    for event in pygame.event.get(): 
      # more of your code 
      elif event.key == K_RETURN and x==420 and y==508: 
       pygame.quit() 
       sys.exit() 

      elif event.key == K_RETURN and x==278 and y==365: 
       #just blit the other image on top of it over it 
       screen.blit(background1, 0, 0) 
      else: 
       #no action performed for other background, set normal background 
       screen.blit(background, (0,0)) 
    screen.blit(cursor, (x,y)) 
    pygame.display.update() 

我沒有測試它的時間,但我希望它的工作原理:)

+0

這不會覆蓋屏幕上的所有圖像嗎?如果您有兩張圖片並且只想移動其中一張圖片,該解決方案是否仍然有效? – swl1020

+0

它適用於這個問題,但如果你有更多的圖像,你應該首先blit背景,然後所有其他圖像。據我所知,你的圖片的順序很重要。 – Tom

相關問題