[英]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 的錯誤消息
攻擊部分 [1]: https://i.stack.imgur.com/cQULm.png [2]: https://i.stack.imgur.com/SsaxG.png [3]: Z5E056C500A1C4BADE5Z://i.stack.imgur.com/SsaxG.png [3]: Z5E056C500A1C4BADE6A stack.imgur.com/C1iYf.png [4]: https://i.stack.imgur.com/SrtQl.png [5]: https://i.stack.imgur.com/l0Bye.png [6] : https://i.stack.imgur.com/wDIeS.png [7]: https://i.stack.imgur.com/ELIIt.png [8]: Z5E056C500A1C4B6A7110B. /5EcRG.png
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.