【问题标题】:the attack animation is not executing攻击动画未执行
【发布时间】:2021-06-12 11:58:27
【问题描述】:

我试过但我找不到错误请帮助我如何执行它? 我是新人,如果你能告诉我一个添加攻击动画的干净方法,那将是一个很大的帮助

import pygame
import os
pygame.init

t = pygame.display.set_mode((1280,720))
pygame.display.set_caption("my first game moce")


clock = pygame.time.Clock()
FPS = 60
RED = (255,0,0)
#game variables
GRAVITY = 0.75

moving_left = False
moving_right = False

#load images
R =[pygame.image.load('img/hero/attack/0.png'),pygame.image.load('img/hero/attack/1.png'),pygame.image.load('img/hero/attack/2.png'),pygame.image.load('img/hero/attack/3.png'),pygame.image.load('img/hero/attack/4.png'),pygame.image.load('img/hero/attack/5.png'),pygame.image.load('img/hero/attack/6.png'),pygame.image.load('img/hero/attack/7.png'),pygame.image.load('img/hero/attack/8.png'),pygame.image.load('img/hero/attack/9.png')]
L =[pygame.image.load('img/hero/attack L/0.png'),pygame.image.load('img/hero/attack L/1.png'),pygame.image.load('img/hero/attack L/2.png'),pygame.image.load('img/hero/attack L/3.png'),pygame.image.load('img/hero/attack L/4.png'),pygame.image.load('img/hero/attack L/5.png'),pygame.image.load('img/hero/attack L/6.png'),pygame.image.load('img/hero/attack L/7.png'),pygame.image.load('img/hero/attack L/8.png'),pygame.image.load('img/hero/attack L/9.png')]



class soldier(pygame.sprite.Sprite):
    def __init__(self, char_type,x,y,scale,speed):
        pygame.sprite.Sprite.__init__(self)
        self.alive = True
        self.char_type = char_type
        self.speed = speed
        self.direction = 1
        self.vel_y = 0
        self.jump = False
        self.in_air = True
        self.flip = False
        self.attacking = False
        self.attack_frame = 0
        self.animation_list = []
        self.frame_index = 0
        self.action = 0
        self.update_time = pygame.time.get_ticks()
        animation_types = ['idle','jump','run']
        for animation in animation_types:            
            temp_list = []
            num_of_frames = len(os.listdir(f'img/{self.char_type}/{animation}'))
            for i in range(num_of_frames):
                img = (pygame.image.load(f'img/{self.char_type}/{animation}/{i}.png'))
                img = pygame.transform.scale(img,(int(img.get_width()*scale),int(img.get_height()*scale)))
                temp_list.append(img)
            self.animation_list.append(temp_list)

        self.img = self.animation_list[self.action][self.frame_index]
        self.rect = self.img.get_rect()
        self.rect.center = (x,y)

    def move(self, moving_left, moving_right):
        
        dx = 0
        dy = 0
        if moving_left:
            dx = -self.speed
            self.flip = True
            self.direction = -1
        if moving_right:
            dx = self.speed
            self.flip = False
            self.direction = 1
        if self.jump == True and self.in_air == False:
            self.vel_y = -11
            self.jump = False
            self.in_air = True
        #appy gravity
        self.vel_y += GRAVITY
        if self.vel_y > 10:
            self.vel_y
        dy += self.vel_y

        #check collison with floor
        if self.rect.bottom +dy > 300:
            dy = 300 - self.rect.bottom
            self.in_air = False
    
        self.rect.x += dx
        self.rect.y += dy
        
    def update_animation(self):
        ANIMATION_COOLDOWN = 100
        if pygame.time.get_ticks() - self.update_time > ANIMATION_COOLDOWN:
            self.img = self.animation_list[self.action][self.frame_index]
            self.update_time = pygame.time.get_ticks()
            self.frame_index += 1
            #if out of animation
            if self.frame_index >= len(self.animation_list[self.action]):
                self.frame_index = 0

    def update_action(self, new_action):
        if new_action != self.action:
            self.action = new_action
            self.frame_index = 0
            self.update_time = pygame.time.get_ticks()
            
    def attack(self):
        if self.attack_frame > 10:
            self.attack_frame = 0
            self.attacking = False

        if self.direction == 'RIGHT':
            self.image = R[self.attack_frame]
        elif self.direction == 'LEFT':
            self.correction()
            self.image = L[self.attack_frame]
        self.attack_frame += 1
        
    def correction(self):
        # Function is used to correct an error
        # with character position on left attack frames
        if self.attack_frame == 1:
            self.pos.x -= 20
        if self.attack_frame == 10:
            self.pos.x += 20
    
    def draw(self):
        t.blit(pygame.transform.flip(self.img,self.flip,False),self.rect)

    
            
    
player = soldier("hero",200,200,2,5)

width = 30
height = 60
x = 300
y = 0


jump = False
jump_count = 10

run = True


background_image = pygame.image.load("C:/Users/Owner/Desktop/q1.png").convert()


while run:
    
    t.blit(background_image,(0,0))
    
    clock.tick(FPS)
    player.update_animation()
    player.draw()

    player.move(moving_left, moving_right)
    if player.alive:
        if player.attacking == True:
            player.attack()
            
        if player.in_air:
            player.update_action(1)

        elif moving_left or moving_right:
            player.update_action(2)
        else:
            player.update_action(0)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                moving_left = True
            if event.key == pygame.K_d:
                moving_right = True
                moving_left = False
            if event.key == pygame.K_SPACE and player.alive:
                player.jump = True
            if event.key == pygame.K_ESCAPE:
                run = False
            if event.key == pygame.K_LSHIFT:
                player.speed = 7
            if event.key == pygame.K_e:
                if player.attacking == False:
                    player.attack()
                    player.attacking = True

                
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                moving_left = False
            if event.key == pygame.K_d:
                moving_right = False
            if event.key == pygame.K_SPACE:
                player.jump = False
            if event.key == pygame.K_LSHIFT:
                player.speed = 5
            if event.key == pygame.K_e:
                if player.attacking == True:
                    player.attacking = False
                    


    pygame.display.update()

pygame.quit()

【问题讨论】:

  • 为什么释放键(e)时停止攻击?

标签: python python-3.x pygame


【解决方案1】:

根据你想要达到的效果,你有不同的选择。

在您当前的实现中,当释放 e 按钮时,攻击会停止。我建议删除该代码:

while run:
    # [...]

    for event in pygame.event.get():
        # [...]

        if event.type == pygame.KEYUP:
            # [...]

            # DELETE
            # if event.key == pygame.K_e:
            #    if player.attacking == True:
            #        player.attacking = False
            #        self.attack_frame = 0

如果你想在释放e按钮时停止攻击,你还必须重置self.attack_frame

while run:
    # [...]

    for event in pygame.event.get():
        # [...]

        if event.type == pygame.KEYUP:
            # [...]

            if event.key == pygame.K_e:
                if player.attacking == True:
                    player.attacking = False
                    self.attack_frame = 0

如果要允许连续攻击并中断攻击,则需要更改KEYDOWN事件中的实现并在按下e时重置self.attack_frame

while run:
    # [...]

    for event in pygame.event.get():
        # [...]

        if event.type == pygame.KEYDOWN:
            # [...] 

            if event.key == pygame.K_e:
                player.attacking = True
                self.attack_frame = 0

        if event.type == pygame.KEYUP:
            # [...]

            # DELETE
            # if event.key == pygame.K_e:
            #    if player.attacking == True:
            #        player.attacking = False
            #        self.attack_frame = 0

【讨论】:

    最近更新 更多