Python Pygame中计算的值超出了预期值

Python Pygame中计算的值超出了预期值,python,pygame,Python,Pygame,在此代码中,问题在于检查\u Health()。无论何时我运行游戏,它都会将生命减少到极低的数值,这是我无意的。我只希望每一个气球在退出屏幕时都能减少我一个人的生命。特别是在第一波中,寿命应该减少到95,但当程序运行时,它会减少到-405。有人知道为什么吗 import pygame import sys import os import random import time pygame.init() WIDTH, HEIGHT = 800,600 win = pygame.display.


在此代码中,问题在于
检查\u Health()
。无论何时我运行游戏,它都会将生命减少到极低的数值,这是我无意的。我只希望每一个气球在退出屏幕时都能减少我一个人的生命。特别是在第一波中,寿命应该减少到95,但当程序运行时,它会减少到-405。有人知道为什么吗

import pygame
import sys
import os
import random
import time

pygame.init()
WIDTH, HEIGHT = 800,600
win = 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 = 0
Lives = 100
balloon_sprite = pygame.image.load("Logo.png")
rounds = [[5,50], [6,40], [8,40], [15,30], [15,20]]
FPS = 60
a3 = True
a1 = a2= False
bloons = []

'''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):
            if a1:
                FPS = 120
                a1=a3 = False
                a2 = True
            elif a2:
                FPS = 240
                a3 = True
                a1 = a2 = False
            elif a3:
                FPS = 60
                a1 = True
                a2 = a3 = False



class Balloon:
    def __init__(self, x,y,health, dests, speed):
        self.health = health
        self.dests = dests
        self.x = x
        self.y = y
        self.count = 0
        self.speed = speed
    def draw_Balloon(self):
        global balloon_sprite
        win.blit(balloon_sprite, pygame.Rect((self.x, self.y), (balloon_sprite.get_width(), balloon_sprite.get_height())))
        if not(self.count==len(self.dests)):
            if self.x == self.dests[self.count][0]:
                if self.y== self.dests[self.count][1]:
                    self.count+=1
                else:
                    if self.y< self.dests[self.count][1]:
                        self.y+=self.speed
                    else:
                        self.y-=self.speed
            else:
                if self.x< self.dests[self.count][0]:
                    self.x+=self.speed
                else:
                    self.x-=self.speed

    def Check_Health(self, pos):
        global Lives
        if self.x == pos[0]:
            if self.y == pos[1]:
                Lives-= self.health

def Main():
    def Check_Close(event):
        if event.type == pygame.QUIT:
            sys.exit()

    global run1
    running = True
    run1 = run2 = True
    while running:
        while run1:
            start_bt = Button(WIDTH//2, HEIGHT//2, "Start", 32, (255,255,0), (0,0,0))
            quit_bt = Button(WIDTH-25, HEIGHT-25, "QUIT", 16, (255,255,0), (0,0,0))
            clock.tick(FPS)

            for event in pygame.event.get():
                Check_Close(event)
                start_bt.Check_Collision(event)
                start_bt.Start()
                quit_bt.Check_Collision(event)
                quit_bt.Quit()
            pygame.display.update()
            #music_play()

        win.fill((255,255,255))
        while run2:
            for j in rounds:
                bloons = []
                for i in range(j[0]):
                        bloons.append(Balloon(0,(-(j[1])*i) ,1, ([0,50], [528,50], [528,500], [150,500], [150,300], [700,300], [700,-100]), 1))
                while run2:
                    win.fill((0,0,0))
                    clock.tick(FPS)
                    for bloon in bloons:
                        bloon.draw_Balloon()
                        bloon.Check_Health([700,-100])
                    Fast_For_bt = Button(WIDTH-25, HEIGHT-25, "[>>]", 16, (255,255,0), (0,0,0))
                    Lives_lb = Button(WIDTH//2, HEIGHT-25, f"Lives : {Lives}", 16, (255,255,0), (0,0,0))
                    for event in pygame.event.get():
                        Check_Close(event)
                        Fast_For_bt.Check_Collision(event)
                        Fast_For_bt.fast_forward()
                    #music_play()
                    pygame.display.update()
                    if ((bloons[-1].x == 700) and (bloons[-1].y ==-100)):
                        break

Main()
 
导入pygame
导入系统
导入操作系统
随机输入
导入时间
pygame.init()
宽度、高度=800600
win=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题(“我的游戏”)
clock=pygame.time.clock()
bg_音乐=[]
#对于os.walk(“音乐”)中的r、d、f:
#背景音乐。附加(f)
i=0
寿命=100
balloon\u sprite=pygame.image.load(“Logo.png”)
轮数=[[5,50]、[6,40]、[8,40]、[15,30]、[15,20]]
FPS=60
a3=正确
a1=a2=False
bloons=[]
''def music_play():
全球i
如果没有(pygame.mixer.music.get_busy()):
pygame.mixer.music.load('music/'+bg_music[0][i])
pygame.mixer.music.play()
i=随机。随机数(0,len(bg_音乐)-1)''
类别按钮:
定义初始值(self、x\u中心、y\u中心、文本、大小、文本列、背景列):
self.x=x_中心
self.y=y_中心
self.size=大小
self.text\u col=text\u col
self.bg\u col=bg\u col
self.hit=False
font=pygame.font.font('freesansbold.ttf',self.size)
self.text=font.render(text、True、self.text\u col、self.bg\u col)
self.textRect=self.text.get_rect()
self.textRect.center=(self.x,self.y)
win.blit(self.text,self.textRect)
def检查_碰撞(自身、事件):
如果event.type==pygame.MOUSEBUTTONDOWN:
pos_鼠标=event.pos
如果self.textRect.collidepoint(鼠标位置):
self.hit=True
def启动(自):
全局运行1
如果(self.hit):
run1=False
def退出(自我):
如果(self.hit):
sys.exit()
def快进(自):
全球a1、a2、a3、FPS
如果(self.hit):
如果a1:
FPS=120
a1=a3=False
a2=真
elif a2:
FPS=240
a3=正确
a1=a2=False
elif a3:
FPS=60
a1=真
a2=a3=False
类气球:
定义初始(自身、x、y、运行状况、目标、速度):
自我健康
self.dests=dests
self.x=x
self.y=y
self.count=0
自身速度=速度
def draw_引出序号(自):
全球气球精灵
win.blit(balloon\u sprite,pygame.Rect((self.x,self.y),(balloon\u sprite.get\u width(),balloon\u sprite.get\u height()))
如果不是(self.count==len(self.dests)):
如果self.x==self.dests[self.count][0]:
如果self.y==self.dests[self.count][1]:
self.count+=1
其他:
如果self.y>]”,16,(255255,0),(0,0,0))
生命=按钮(宽度//2,高度-25,f“生命:{lifes}”,16,(255255,0),(0,0,0))
对于pygame.event.get()中的事件:
检查关闭(事件)
快速检查碰撞(事件)
快进
#音乐(u play)
pygame.display.update()
如果((bloons[-1].x==700)和(bloons[-1].y==100)):
打破
Main()

导致该问题的原因是
寿命
在每次满足条件时都会减少。如果在连续帧中满足该条件,
寿命
每帧递减一次。您必须避免连续几次递减
寿命。
寿命
递减时,设置一个状态(
self.hit
)。设置
self.hit
时,不要减少
寿命
。当条件不再满足时,重置
self.点击

类气球:
定义初始(自身、x、y、运行状况、目标、速度):
# [...]
self.hit=False
def检查_运行状况(自身、pos):
全球生活