按键后图像的外观不会改变吗?

问题描述 投票:0回答:1
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/16 - Dragon Ball Z - title song.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):
        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)
        # pygame.draw.rect(win, (255,0,0), self.hitbox,2)

    def hit(self):
        self.isJump = False
        self.jumpCount = 10
        self.x = 100
        self.y = 410
        self.walkCount = 0
        font1 = pygame.font.SysFont('Papyrus', 100)
        text = font1.render('Attcked!', 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():
    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):
        pygame.draw.circle(win, (244, 232, 104), (self.x, self.y), self.radius)
        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))

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/b4.png")
        win.blit(ki, (self.x, self.y))




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):
        if self.health > 0:
            self.health -= 1
        else:
            self.visible = False
        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
        print("Hit!!")
        pass

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

    pygame.display.update()


# mainloop font = pygame.font.SysFont('comicsans', 30, True) man = player(200, 410, 64, 64) goblin = enemy(110, 410, 64, 64, 450) goblin2
= enemy2(50, 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

    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

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

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            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]:
                goblin.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))

    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]:
                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 // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))

        shootLoop = 1

**Here where am trying to see if my image changes when i press a key button**
    if keys[pygame.K_a] and shootLoop == 0:
        ki_stance = pygame.image.load("image/goku-ki.png")
        win.blit(ki_stance, (0, 0))
        if man.left:
            facing = -1
        else:
            facing = 1
        if len(bullets) < 3.30:
            bullets.append(projectile2(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))


    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()
python pygame pycharm
1个回答
0
投票

redrawGameWindow要做的第一件事是在窗口中绘制背景:

def redrawGameWindow():
   win.blit(bg, (0, 0))
   # [...]

调用redrawGameWindow之前绘制的所有内容都是“不可见的,因为背面覆盖了整个屏幕:

if keys[pygame.K_a] and shootLoop == 0:
   ki_stance = pygame.image.load("image/goku-ki.png")
   win.blit(ki_stance, (0, 0))  # <----- "INVISIBLE"

# [...]

redrawGameWindow() # <---- because it is covered here

在应用程序循环之前,在初始化时加载所有图像:

ki = pygame.image.load("image/ki.png")
ki_stance = pygame.image.load("image/goku-ki.png")

更改类projectilestance是该类的构造函数的新参数。您根本不需要类projectile2,(删除projectile2):

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

    def draw(self, win):
        pygame.draw.circle(win, (244, 232, 104), (self.x, self.y), self.radius)
        image = ki_stance if self.stance else ki
        win.blit(image, (self.x, self.y))

按下SPACEa时,会产生弹丸:

if keys[pygame.K_SPACE] and shootLoop == 0  and len(bullets) < 5:
    #bulletSound.play()
    facing = -1 if man.left else 1
    x, y = round(man.x + man.width // 2), round(man.y + man.height // 2)
    bullets.append(projectile(x, y, 6, (0, 0, 0), facing, False))
    shootLoop = 1

if keys[pygame.K_a] and shootLoop == 0  and len(bullets) < 3.30:
    facing = -1 if man.left else 1 
    x, y = round(man.x + man.width // 2), round(man.y + man.height // 2)
    bullets.append(projectile(x, y, 6, (0, 0, 0), facing, True))
    shootLoop = 1

如果要为播放器显示不同的精灵,则按下a时,必须在player.draw中进行操作。例如:

b4Image = pygame.image.load("image/b4.png")
class player(object):
    # [...]

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

        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            win.blit(b4Image, (self.x, self.y))

        elif 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)
© www.soinside.com 2019 - 2024. All rights reserved.