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()
So i am trying to move my enemy left (doing the run animation)enter image description here until (160 , 460) where it starts doing the attack animation coordinate while trying to get the animation working. I keep getting a error message about Enemey not having attribute char_type
CodePudding user response:
Sorry, here are all of the attack and run animationns
Run part [1]: https://i.stack.imgur.com/uAW1u.png [2]: https://i.stack.imgur.com/YVI2y.png [3]: https://i.stack.imgur.com/fYKZu.png [4]: https://i.stack.imgur.com/Y0y1V.png [5]: https://i.stack.imgur.com/F2gCv.png [6]: https://i.stack.imgur.com/a70Ce.png [7]: https://i.stack.imgur.com/Sa28h.png [8]: https://i.stack.imgur.com/79ClN.png
CodePudding user response:
Attack part [1]: https://i.stack.imgur.com/cQULm.png [2]: https://i.stack.imgur.com/SsaxG.png [3]: https://i.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]: https://i.stack.imgur.com/5EcRG.png