气球图片在此代码中,问题出在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
添加回答
举报
0/150
提交
取消