为什么我在 Pygame 中绘制第二张图片后,第二张图片没有出现在第一张图片之上

Why isn't the second image appearing on top of the first image after I draw it in Pygame

我正在尝试使用 Pygame 为学校制作一款放置唱首歌游戏,我 运行 遇到了一些问题,我非常感谢您的帮助

我想在当前敌人的生命值达到零时替换屏幕上的敌人,但是当我在敌人生命值达到 0 时绘制第二张图像时,第一张图像没有被替换有人可以帮我解决这个问题出来

提前致谢

import pygame
pygame.init()
pygame.font.init()
colours = {"White" : (255, 255, 255), "Black" : (0, 0, 0), "Red" : (255, 0, 0), "Blue" : (0, 0, 255), "Green" : (0, 255, 0)}
orighpamt = 10

class Screen():
    def __init__(self, title, width=400, height=600, fill=colours["White"]):
        self.title=title
        self.width=width
        self.height = height
        self.fill = fill
        self.current = False

    def makeCurrent(self):
        pygame.display.set_caption(self.title)
        self.current = True
        self.screen = pygame.display.set_mode((self.width, self.height))

    def endCurrent(self):
        self.current = False

    def checkUpdate(self):
        return self.current
    def screenUpdate(self):
        if(self.current):
            self.screen.fill(self.fill)

    def returnTitle(self):
        return self.screen

class Button():
        def __init__(self, x, y, sx, sy, bcolour, fbcolour, font, fontsize, fcolour, text):
            self.x = x
            self.y = y
            self.sx = sx
            self.sy = sy
            self.bcolour = bcolour
            self.fbcolour = fbcolour
            self.fcolour = fcolour
            self.fontsize = fontsize
            self.text = text
            self.current = False
            self.buttonf = pygame.font.SysFont(font, fontsize)
        def showButton(self, display):
            if(self.current):
                pygame.draw.rect(display, self.fbcolour, (self.x, self.y, self.sx, self.sy))
            else:
                pygame.draw.rect(display, self.bcolour, (self.x, self.y, self.sx, self.sy))

            textsurface = self.buttonf.render(self.text, False, self.fcolour)
            display.blit(textsurface, ((self.x + (self.sx/2) - (self.fontsize/2)*(len(self.text)/2) - 5,(self.y + (self.sy/2) -(self.fontsize/2) - 4))))
        def focusCheck(self, mousepos, mouseclick):
            if(mousepos[0] >= self.x and mousepos[0] <= self.x + self.sx and mousepos[1] >= self.y and mousepos[1] <= self.y + self.sy):
                self.current = True
                return mouseclick
            else:
                self.current = False
                return False
class Enemy(pygame.sprite.Sprite):

    def __init__(self, dx, dy, filename):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(filename).convert()

        self.rect = self.image.get_rect()
        self.rect.x = dx
        self.rect.y = dy



    def draw(self, screen):
        screen.blit(self.image, self.rect)



menuScreen = Screen("Menu Screen")
screen2 = Screen("Screen 2")

win = menuScreen.makeCurrent()


done = False
font = pygame.font.Font('freesansbold.ttf', 32)
clickdamage = 1
hitpoints = orighpamt
DungeonButton = Button(125, 500, 150, 50, colours["Black"], colours["Blue"], "arial", 20, colours["White"], "Dungeon")
hitboxButton = Button(80, 50, 280, 400, colours["White"], colours["Red"], "arial", 20, colours["White"], "")
hitpointamount = Button(100, 0, 200, 50, colours["White"], colours["Black"], "arial", 20, colours["Black"], str(hitpoints))
goblin = Enemy(0 , 20, "images\goblin-resized.png")
goblin2 = Enemy(0 , 20, "images\goolin.png")
toggle = False
while not done:
    menuScreen.screenUpdate()
    screen2.screenUpdate()
    mouse_pos = pygame.mouse.get_pos()
    keys = pygame.key.get_pressed()

    mouse_click = False
    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_click = True

    if menuScreen.checkUpdate():
        attack = hitboxButton.focusCheck(mouse_pos, mouse_click)
        hitboxButton.showButton(menuScreen.returnTitle())
        goblin.draw(menuScreen.screen)
        hitpointamount.showButton(menuScreen.returnTitle())
        if attack:
            hitpoints -= clickdamage
            if hitpoints < 0:
                hitpoints = 0
            hitpointamount = Button(100, 0, 200, 50, colours["White"], colours["Black"], "arial", 20, colours["Black"], str(hitpoints))
            hitpointamount.showButton(menuScreen.returnTitle())
            if hitpoints == 0:
                goblin2.draw(menuScreen.screen)
                menuScreen.screenUpdate()
                orighpamt = round(orighpamt * 1.5)
                hitpoints = orighpamt
                hitpointamount = Button(100, 0, 200, 50, colours["White"], colours["Black"], "arial", 20, colours["Black"], str(hitpoints))
                hitpointamount.showButton(menuScreen.returnTitle())                
                pygame.display.update()

    elif screen2.checkUpdate():
        returnm = DungeonButton.focusCheck(mouse_pos, mouse_click)
        if returnm:
            win = menuScreen.makeCurrent()
            screen2.endCurrent()

    pygame.display.update()

pygame.quit()

[这是我的第一个回答,请放慢我的脚步]

在显示 sprite 时,跟踪显示的是哪个 sprite 至关重要。当前代码最干净的解决方案是简单地使用跟踪变量,例如 'currentGoblin'.

currentGoblin = 0

每次你的妖精被杀死时,将该变量递增 1。现在,渲染你的精灵。 删掉goblin2.draw(),没必要。在您绘制第一个精灵(地精)的地方,将代码替换为:

if currentGoblin == 0:
    goblin.draw(menuScreen.screen)
elif currentGoblin == 1:
    goblin2.draw(menuScreen.screen)
# etc, so you can add more goblin images

你的代码的缺陷是你没有永久改变图像/精灵,直到它重新显示。

本质上,此修复程序所做的是显示您正在做的任何地精,并在它被杀死时移动到下一个。