簡體   English   中英

當我不按按鈕時,為什么我的圖像消失了?

[英]Why does my image disappear when I'm not pressing a button?

代碼很好用,我的圖像按原樣翻轉,但是當我不按任何鍵時,我的圖像就會消失...我知道這與在屏幕中的每個“ 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()

這是一個建議,而不是一個答案,因為很難告訴您要執行的操作,但是我需要格式化代碼,因此注釋並不是真正有用。 將擴展功能擴展為具有以下默認行為是否可行:

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,則可以避免使用'updated'變量,但是更新后的測試允許上面的邏輯不做screen.blit,最終測試將確保始終將某些內容寫入屏幕。

與其允許更新功能將玩家帶到屏幕上,不如將玩家添加到默認的pygame sprite組中並通過while循環進行繪制! 我還避免每次更新都加載圖像(由於開銷),而是一次加載所有圖像,然后在需要時將其blit!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM