2016-02-21 122 views
1

這段代碼很好,我的圖片翻轉了,但是當我沒有按下某個鍵時,我的圖像就消失了......我知道這與在每個'if'語句中調用screen.blit方法時有關在更新功能,但我不知道如何解決這個問題?爲什麼我不按下按鈕時圖像消失?

import pygame, sys, glob 
from pygame import * 

h=400 
w=800 

screen = pygame.display.set_mode((w,h)) 

clock = pygame.time.Clock() 

class player: 
    def __init__(self): 
     self.x = 200 
     self.y = 300 

     self.ani_speed_init = 8 
     self.ani_speed=self.ani_speed_init 

     self.Lani_speed_init = 8 
     self.Lani_speed=self.Lani_speed_init 

     self.ani = glob.glob("D:\Projects\pygame\TestGame\sprite*.png") 
     self.Lani = glob.glob("D:\Projects\pygame\TestGame\Lsprite*.png") 

     self.ani.sort() 
     self.ani_pos=0 

     self.Lani.sort() 
     self.Lani_pos=0 
     #takes length of the amount of images and subtracts 1 since count starts at 0 
     self.ani_max=len(self.ani) -1 
     self.Lani_max=len(self.Lani) -1 

     self.img = pygame.image.load(self.ani[0]) 
     self.Limg = pygame.image.load(self.Lani[0]) 

     self.update(0, 0) 

    def update(self, pos, posL): 

     if pos != 0: 
      #subtracts 1 from 10 
      self.ani_speed-=1 
      #adds self.x to itself 
      self.x+=pos 
      if self.ani_speed==0: 
       self.img = pygame.image.load(self.ani[self.ani_pos]) 
       self.ani_speed = self.ani_speed_init 
       if self.ani_pos == self.ani_max: 
        self.ani_pos = 0 
       else: 
        self.ani_pos+=1 

      screen.blit(self.img,(self.x,self.y)) 

     if posL != 0: 
      #subtracts 1 from 10 
      self.Lani_speed-=1 
      #adds self.x to itself 
      self.x+=posL 
      if self.Lani_speed==0: 
       self.Limg = pygame.image.load(self.Lani[self.Lani_pos]) 
       self.Lani_speed = self.Lani_speed_init 
       if self.Lani_pos == self.Lani_max: 
        self.Lani_pos = 0 
       else: 
        self.Lani_pos+=1  

      screen.blit(self.Limg,(self.x,self.y))  

player1 = player() 
pos = 0 
posL = 0 

while 1: 
    screen.fill((255,255,255)) 
    clock.tick(60) 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 
      pygame.quit() 
     elif event.type == KEYDOWN and event.key == K_RIGHT: 
      pos = 1 

     elif event.type == KEYUP and event.key == K_RIGHT: 
      pos = 0 
     elif event.type == KEYDOWN and event.key == K_LEFT: 
      posL = -1 

     elif event.type == KEYUP and event.key == K_LEFT: 
      posL = 0 

    player1.update(pos, posL) 
    pygame.display.update() 

回答

0

這是一個建議,而不是因爲它是很難告訴你要去做的答案,但我需要格式的代碼,所以評論是不是真的有用。將它的工作,延長更新功能有一個默認的行爲如下:

def update(self, pos, posL): 
    updated = False 

    if pos != 0: 
     (logic removed) 
     screen.blit(self.img,(self.x,self.y)) 
     updated = True 

    if posL != 0: 
     (logic removed) 
     screen.blit(self.Limg,(self.x,self.y))  
     updated = True 

    if not updated: 
     screen.blit(something sensible for arguments of 0,0) 

你可以避開「更新」變量,如果你只是測試POS == 0和POSL == 0,但更新的測試允許上面的邏輯可能不會執行screen.blit,最後的測試將確保某些東西總是寫入屏幕。

0

與其允許更新功能將播放器彈出到屏幕上,我會將播放器添加到默認的pygame精靈組,並通過while循環繪製它!我也會避免每次更新(由於開銷)加載圖像,而是一次加載所有圖像,然後在需要時將其刷新!

相關問題