簡體   English   中英

如何使我的精靈正確動畫並讓我的精靈向左移動?

[英]How do i make my sprite animate properly and make my sprite move left?

import pygame
import random
import os
import time

pygame.init()

#window and background
win = pygame.display.set_mode((1000,750))
pygame.display.set_caption("TD GaME")
background_img = pygame.image.load('best_backgroundv2.png')
background = pygame.transform.scale(background_img,(1000,750))

#framerate
clock = pygame.time.Clock()
FPS = 60

moving_left = False

#Class of enemy
class Enemy(pygame.sprite.Sprite):
    #attributes
    def __init__(self, x, y, scale, speed):
        pygame.sprite.Sprite.__init__(self)
        img = pygame.image.load('Animation/Run/HeavyBandit_Run_0.png')
        self.speed = speed
        self.animation_list = []
        self.frame_index = 0
        self.action = 0
        self.update_time = pygame.time.get_ticks()
        
        #load all images for the players
        animation_types = ['Attack', 'Run']
        for animation in animation_types:
            #reset temporary list of images
            temp_list = []
            #count number of files in the folder
            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.image = self.animation_list[self.action][self.frame_index]
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)

    def move(self, moving_left):
        #reset movement variables
        dx = 0
        dy = 0

        #assign movement left
        if moving_left:
            dx = -self.speed
            self.flip = True
            self.direction = -1

        #update rectangle position
        self.rect.x += dx
        self.rect.y += dy

    def update_animation(self):
        #update animation
        ANIMATION_COOLDOWN = 100
        #update image depending on current frame
        self.image = self.animation_list[self.action][self.frame_index]
        #check if enough time has passed since the last update
        if pygame.time.get_ticks() - self.update_time > ANIMATION_COOLDOWN:
            self.update_time = pygame.time.get_ticks()
            self.frame_index += 1
        #if the animation has run out the reset back to the start
        if self.frame_index >= len(self.animation_list[self.action]):
            self.frame_index = 0

    def update_action(self, new_action):
        #check if the new action is different to the previous one
        if new_action != self.action:
            self.action = new_action
            #update the animation settings
            self.frame_index = 0
            self.update_time = pygame.time.get_ticks()
            
    # Method    
    def draw(self):
        win.blit(self.image, self.rect)

#position
e_1 = Enemy(940 , 460 , 2, 5)



# game loop
run = True
while run:
    clock.tick(FPS)
    #Put on the Screen
    win.blit(background,(0,0))
    player.update_animation()
    e_1.draw()
    e_1.move(moving_left)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    pygame.display.update()


pygame.quit()

所以我試圖將我的敵人向左移動(做跑步動畫)在此處輸入圖像描述,直到 (160, 460) 它開始攻擊 animation 坐標,同時試圖讓 animation 工作。 我不斷收到關於 Enemey 沒有屬性 char_type 的錯誤消息

攻擊和運行圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM