【问题标题】:How would I go about deleting a sprite? [duplicate]我将如何删除精灵? [复制]
【发布时间】:2020-05-25 03:34:48
【问题描述】:

所以问题是试图让我的精灵在健康栏变为 0 时消失,但我似乎无法做到我能做的最多就是让他不可见,但我试图删除我的精灵不是让它不可见

import pygame import sys

pygame.init()#We always need to initalize our pygame IN EVERY PROJECT/FILE

win = pygame.display.set_mode((500, 480))# Here win is representing "window" for our screen which we have set at 500 by 480

pygame.display.set_caption("Dragon Ball Z Mini-game")#We are giving our Window/Screen a name

walkRight = [pygame.image.load('image/young_goku_right_image0 - t.png'), pygame.image.load('image/young_goku_right_image1 - t.png'), pygame.image.load('image/young_goku_right_image2 - t.png'), pygame.image.load('image/young_goku_right_image3 - t.png'), pygame.image.load('image/young_goku_right_image4 - t.png')] walkLeft = [pygame.image.load('image/young_goku_left_image0 - t.png'), pygame.image.load('image/young_goku_left_image1 - t.png'), pygame.image.load('image/young_goku_left_image2 - t.png'), pygame.image.load('image/young_goku_left_image3 - t.png'), pygame.image.load('image/young_goku_left_image4 - t.png')] bg = pygame.image.load('image/bg2.jpg') char = pygame.image.load('image/young_goku - standing - t.png')

clock = pygame.time.Clock()

bulletSound = pygame.mixer.Sound("image/kiblast.wav") hitSound = pygame.mixer.Sound("image/Bomb+1.wav")
#bulletSound.play() music = pygame.mixer.music.load("image/Dragon Ball Z - Rock The Dragon.mp3") pygame.mixer.music.play(-1)

score = 0

class player(object): def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 5
    self.isJump = False
    self.left = False
    self.right = False
    self.walkCount = 0
    self.jumpCount = 10
    self.standing = True
    self.hitbox = (self.x + 17, self.y + 11, 29, 52)

def draw(self, win):
    font2 = pygame.font.SysFont("Papyrus", 45)
    text2 = font2.render("DBZ Adventure!", 1, (255, 50, 25))
    win.blit(text2, (120 - (text2.get_width() / 2), 15))
    #pygame.display.flip()
    if self.walkCount + 1 >= 10:
        self.walkCount = 0
    if not (self.standing):
        if self.left:
            win.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
        elif self.right:
            win.blit(walkRight[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
    else:
        if self.right:
            win.blit(walkRight[0], (self.x, self.y))
        else:
            win.blit(walkLeft[0], (self.x, self.y))
    self.hitbox = (self.x + 17, self.y + 11, 29, 52)
    if keys[pygame.K_a]:
        if self.left:
            ki_stance_left = pygame.image.load("image/goku-ki_left.png")
            win.blit(ki_stance_left, (self.x, self.y))

        elif self.right:
            ki_stance = pygame.image.load("image/goku-ki.png")
            win.blit(ki_stance, (self.x, self.y))

    if keys[pygame.K_SPACE]:
        if self.left:#needed to put self.left and self the right
            ki_stance_left = pygame.image.load("image/goku-ki_left.png")
            win.blit(ki_stance_left, (self.x, self.y))

        elif self.right:
            ki_stance = pygame.image.load("image/goku-ki.png")
            win.blit(ki_stance, (self.x, self.y))

            pygame.display.flip()

def hit(self):
    self.isJump = False
    self.jumpCount = 10
    self.x = 100
    self.y = 410
    self.walkCount = 0
    font1 = pygame.font.SysFont('comicsans', 100)
    text = font1.render('-5', 1, (255, 0, 0))
    win.blit(text, (250 - (text.get_width() / 2), 200))
    pygame.display.update()
    i = 0
    while i < 200:
        pygame.time.delay(10)
        i += 1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                i = 201
                pygame.quit()


class projectile(object): def __init__(self, x, y, radius, color, facing):
    self.x = x
    self.y = y
    self.radius = radius
    self.color = color
    self.facing = facing
    self.vel = 8 * facing

def draw(self, win):
    ki = pygame.image.load("image/ki.png")
    win.blit(ki, (self.x, self.y))
    ki_stance = pygame.image.load("image/goku-ki.png")
    win.blit(ki, (self.x, self.y))
    pygame.draw.circle(win, (255,255,0), (self.x, self.y), self.radius) """ def draw2(self, win):
    ki2 = pygame.image.load("image/b4.png")
    win.blit(ki2, (self.x, self.y))
    ki_stance = pygame.image.load("image/goku-ki.png")
    win.blit(ki2, (self.x, self.y))
    pygame.draw.rect(win, (255, 255, 0), (self.x, self.y), self.radius) """

class projectile2(): def __init__(self, x, y, radius, color, facing):
    self.x = x
    self.y = y
    self.radius = radius
    self.color = color
    self.facing = facing
    self.vel = 8 * facing

def draw(self, win):
    ki = pygame.image.load("image/b4.png")
    win.blit(ki, (self.x, self.y))
    ki_stance = pygame.image.load("image/goku-ki.png")
    win.blit(ki, (self.x, self.y))
    #pygame.draw.circle(win, (0,0,139), (self.x, self.y), self.radius)








class enemy(object): walkRight = [pygame.image.load("image/R1E.png"), pygame.image.load("image/R2E.png"),
             pygame.image.load("image/R3E.png"), pygame.image.load("image/R4E.png"),
             pygame.image.load("image/R5E.png"), pygame.image.load("image/R6E.png"),
             pygame.image.load("image/R7E.png"), pygame.image.load("image/R8E.png"),
             pygame.image.load("image/R9E.png"), pygame.image.load("image/R10E.png"),
             pygame.image.load("image/R11E.png")] walkLeft = [pygame.image.load("image/L1E.png"), pygame.image.load("image/L2E.png"),
            pygame.image.load("image/L3E.png"), pygame.image.load("image/L4E.png"),
            pygame.image.load("image/L5E.png"), pygame.image.load("image/L6E.png"),
            pygame.image.load("image/L7E.png"), pygame.image.load("image/L8E.png"),
            pygame.image.load("image/L9E.png"), pygame.image.load("image/L10E.png"),
            pygame.image.load("image/L11E.png")]

def __init__(self, x, y, width, height, end):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.end = end
    self.path = [self.x, self.end]
    self.walkCount = 0
    self.vel = 3
    self.hitbox = (self.x + 17, self.y + 2, 31, 57)
    self.health = 10
    self.visible = True

def draw(self, win):
    self.move()
    if self.visible:
        if self.walkCount + 1 >= 33:
            self.walkCount = 0

        if self.vel > 0:
            win.blit(self.walkRight[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
        else:
            win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1

        pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
        pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)
        # pygame.draw.rect(win, (255,0,0), self.hitbox,2)

def move(self):
    if self.vel > 0:
        if self.x + self.vel < self.path[1]:
            self.x += self.vel
        else:
            self.vel = self.vel * -1
            self.walkCount = 0
    else:
        if self.x - self.vel > self.path[0]:
            self.x += self.vel
        else:
            self.vel = self.vel * -1
            self.walkCount = 0 

在这里我可以继续尝试删除我的精灵,但它似乎不起作用?我能做的最多就是让它不可见,但它不会永久消失。

    def hit(self):
            hit = enemy
            if self.health > 0:
                self.health -= 1


            else:
                self.visible = False
                font2 = pygame.font.SysFont("Papyrus", 45)
                text2 = font2.render("You won!", 5, (45, 50, 45))
                win.blit(text2, (95 - (text2.get_width() / 3), 45))
                pygame.display.flip()
            print('hit')


class enemy2(): walkRight = [pygame.image.load("image/enemy-sprite-standing-T.gif"), pygame.image.load("image/enemy-sprite-moving_T.gif"), pygame.image.load("image/enemy-sprite-moving_T.gif"), pygame.image.load("image/enemy-sprite-move_right-T.gif")] walkLeft = [pygame.image.load("image/enemy-sprite-standing-T.gif"), pygame.image.load("image/enemy-sprite - moving_left-T.gif"), pygame.image.load("image/enemy-sprite - moving_left-T.gif"), pygame.image.load("image/enemy-sprite - moving_left2-T.gif")] def
__init__(self, x, y, width, height, end):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.end = end
    self.path = [self.x, self.end]
    self.walkcount = 0
    self.vel = 3
    self.hitbox = (self.x + 20, self.y, 28, 60)
    self.health = 10
    self.visible = True

def draw(self, win):
    self.move()
    if self.visible:
        if self.walkcount + 0 >= 10:
            self.walkcount = 0

        if self.vel > 0:
            win.blit(self.walkRight[self.walkcount // 3], (self.x, self.y))
            self.walkcount += 1
        else:
            win.blit(self.walkLeft[self.walkcount // 3], (self.x, self.y))
            self.walkcount += 1
        pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 20, 50, 10))
        pygame.draw.rect(win, (0, 128, 0), (self.hitbox[0], self.hitbox[1] - 20, 50 - (5 * (10 - self.health)), 10))
        self.hitbox = (self.x + 20, self.y, 28, 60)

def move(self):
    if self.vel > 0:
        if self.x + self.vel < self.path[1]:
            self.x += self.vel
        else:
            self.vel = self.vel * -1
            self.walkcount = 0

    else:
        if self.x - self.vel > self.path[0]:
            self.x += self.vel
        else:
            self.vel = self.vel * -1
            self.walkcount = 0

def hit(self):
    if self.health > 0:
        self.health -= 1
    else:
        self.visible = False
        self.visible = False
        font2 = pygame.font.SysFont("Papyrus", 45)
        text2 = font2.render("You won!", 5, (45, 50, 45))
        win.blit(text2, (95 - (text2.get_width() / 3), 45))
        pygame.display.flip()

    print("Hit!!")
    pass


def redrawGameWindow(): win.blit(bg, (0, 0)) text = font.render('Score: ' + str(score), 1, (0, 0, 0)) win.blit(text, (390, 5)) man.draw(win) goblin.draw(win) goblin2.draw(win) for bullet in bullets:
    bullet.draw(win)

pygame.display.update()


# mainloop font = pygame.font.SysFont("comicsans", 30, True, True) man = player(200, 420, 64, 64) goblin = enemy(50, 410, 64, 64, 450) goblin2 = enemy2(250, 410, 64, 64, 450) shootloop = 0 bullets = [] run
= True while run: clock.tick(27) if goblin.visible == True:
    if man.hitbox[1] < goblin.hitbox[1] + goblin.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin.hitbox[1]:
        if man.hitbox[0] + man.hitbox[2] > goblin.hitbox[0] and man.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[2]:
            man.hit()
            score -= 5
            print("Hurt")

if goblin2.visible == True:
    if man.hitbox[1] < goblin2.hitbox[1] + goblin2.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin2.hitbox[1]:
        if man.hitbox[0] + man.hitbox[2] > goblin2.hitbox[0] and man.hitbox[0] < goblin2.hitbox[0] + goblin2.hitbox[2]:
            man.hit()
            score -= 5
            print("Hurt")

if shootloop > 0:
    shootloop += 1 if shootloop > 3:
    shootloop = 0

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        #pygame.quit()
        sys.exit()
        run = False

for bullet in bullets:
    if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
        if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
            #hitSound.play()
            goblin.hit()
            score += 1
            #bullets.pop(bullets.index(bullet))

for bullet in bullets:
    if bullet.y - bullet.radius < goblin2.hitbox[1] + goblin2.hitbox[3] and bullet.y + bullet.radius > goblin2.hitbox[1]:
         if bullet.x + bullet.radius > goblin2.hitbox[0] and bullet.x - bullet.radius < goblin2.hitbox[0] + goblin2.hitbox[2]:
            #hitSound.play()
            goblin2.hit()
            score += 1
            #bullets.pop(bullets.index(bullet))

    if bullet.x < 500 and bullet.x > 0:
        bullet.x += bullet.vel
    else:
        bullets.pop(bullets.index(bullet))

keys = pygame.key.get_pressed()

if keys[pygame.K_SPACE] and shootloop == 0:
    bulletSound.play()
    if man.left:
        facing = -1
    else:
        facing = 1

    if len(bullets) < 5:
        bullets.append(projectile(round(man.x + man.width // 3), round(man.y + man.height // 3), 6, (0, 0, 0), facing))
    shootloop = 1

if keys[pygame.K_a] and shootloop == 0:
    bulletSound.play()
    if man.left:
        facing = -1
    else:
        facing = 1

    if len(bullets) < 5:
        bullets.append(projectile2(round(man.x + man.width // 3), round(man.y + man.height // 3), 12, (0, 0, 0), facing))
    shootloop = 1

if keys[pygame.K_LEFT] and man.x > man.vel:
    man.x -= man.vel
    man.left = True
    man.right = False
    man.standing = False elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
    man.x += man.vel
    man.right = True
    man.left = False
    man.standing = False else:
    man.standing = True
    man.walkCount = 0

if not (man.isJump):
    if keys[pygame.K_UP]:
        man.isJump = True
        man.right = False
        man.left = False
        man.walkCount = 0 else:
    if man.jumpCount >= -10:
        neg = 1
        if man.jumpCount < 0:
            neg = -1
        man.y -= (man.jumpCount ** 2) * 0.5 * neg
        man.jumpCount -= 1
    else:
        man.isJump = False
        man.jumpCount = 10

redrawGameWindow()

pygame.quit()

【问题讨论】:

  • 这能回答你的问题吗? Do not display removed sprites, Pygame
  • 所以我已经尝试过了,但是 .Kill 或 .remove 没有作为我的选项弹出?
  • 你的问题太长了。试着总结一下。
  • 请阅读我试图删除我的精灵的部分@AlirezaHI 我只是把其余的代码放在以防万一
  • 您没有看到kill() 作为选项的原因是您的玩家、射弹和敌人类不是 pygame.Sprite 的子类,而且您也没有使用 Sprite 组。你应该使用那些。它会让事情变得更简单。

标签: python pygame sprite


【解决方案1】:

要将创建的图像移出屏幕,您可以执行以下任一解决方案。

1.将图像移出屏幕。

将精灵移出屏幕的示例如下:

someImg = pygame.image.load("coolWater.png") 
screen.blit(someImg, (int(someXValueOffScreen), int(someYValueOffScreen))

2.使图像透明

这是一个例子:

someImg = pygame.image.load("coolWater.png")
someImg.image.fill((0,0,0,0)) #Makes the image transparent. The last parameter is the alpha value. An alpha value of 0 makes it transparent. The first 3 zeros is the RGB value

快速提示:您应该正确格式化这篇文章中的代码,并使用这三个字符“```”封装您的代码。

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多