亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pygame 中的值計算量超出預期

Pygame 中的值計算量超出預期

烙印99 2023-12-09 16:26:52
氣球圖片在此代碼中,問題出在Check_Health(). 每當我運行游戲時,它都會將生命值減少到極低的數字,這是不希望的。我只希望每個氣球在離開屏幕時都能減少我的生命。具體來說,在第一波生命應該減少到95,但是當程序運行時,它減少到-405。有人知道為什么嗎?import pygameimport sysimport osimport randomimport timepygame.init()WIDTH, HEIGHT = 800,600win = pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption("My Game")clock = pygame.time.Clock()bg_music = []#for r,d,f in os.walk("Music"):    #bg_music.append(f)i = 0Lives = 100balloon_sprite = pygame.image.load("Logo.png")rounds = [[5,50], [6,40], [8,40], [15,30], [15,20]]FPS = 60a3 = Truea1 = a2= Falsebloons = []'''def music_play():    global i    if not(pygame.mixer.music.get_busy()):        pygame.mixer.music.load('Music/'+bg_music[0][i])        pygame.mixer.music.play()        i = random.randint(0,len(bg_music)-1)'''class Button:    def __init__(self, x_center, y_center, text, size, text_col, bg_col):        self.x = x_center        self.y = y_center        self.size = size        self.text_col = text_col        self.bg_col = bg_col        self.hit = False        font = pygame.font.Font('freesansbold.ttf', self.size)        self.text = font.render(text, True, self.text_col, self.bg_col)        self.textRect = self.text.get_rect()        self.textRect.center = (self.x, self.y)        win.blit(self.text, self.textRect)    def Check_Collision(self, event):        if event.type == pygame.MOUSEBUTTONDOWN:            pos_mouse = event.pos            if self.textRect.collidepoint(pos_mouse):                self.hit = True                    def Start(self):        global run1        if (self.hit):            run1 = False    def Quit(self):        if (self.hit):            sys.exit()    def fast_forward(self):        global a1,a2,a3, FPS        if(self.hit): 
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

導致此問題的原因Lives是每次滿足條件時都會遞減。如果連續幀中滿足條件,Lives則每幀遞減一次。您必須避免Lives連續多次遞減。

當Lives遞減時,設置狀態 ( self.hit)。設置Lives時不要遞減。當條件不再滿足時self.hit重置:self.hit


class Balloon:

    def __init__(self, x,y,health, dests, speed):

        # [...]


        self.hit = False


    def Check_Health(self, pos):

        global Lives

        if self.x == pos[0] and self.y == pos[1]:

            if not self.hit:

                Lives -= self.health

            self.hit = True

        else:

            self.hit = False


查看完整回答
反對 回復 2023-12-09
  • 1 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號