-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.py
54 lines (47 loc) · 1.59 KB
/
enemy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pygame
import random
class Enemy(pygame.sprite.Sprite):
def __init__(self, SCREEN_WIDTH, y, sprite_sheet, scale):
pygame.sprite.Sprite.__init__(self)
#define variables
self.animation_list = []
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
self.direction = random.choice([-1, 1])
if self.direction == 1:
self.flip = True
else:
self.flip = False
#load images from spritesheet
animation_steps = 8
for animation in range(animation_steps):
image = sprite_sheet.get_image(animation, 32, 32, scale, (0, 0, 0))
image = pygame.transform.flip(image, self.flip, False)
image.set_colorkey((1, 0, 0))
self.animation_list.append(image)
#select starting image and create rectangle from it
self.image = self.animation_list[self.frame_index]
self.rect = self.image.get_rect()
if self.direction == 1:
self.rect.x = 0
else:
self.rect.x = SCREEN_WIDTH
self.rect.y = y
def update(self, scroll, SCREEN_WIDTH):
#update animation
ANIMATION_COOLDOWN = 50
#update image depending on current frame
self.image = self.animation_list[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 then reset back to the start
if self.frame_index >= len(self.animation_list):
self.frame_index = 0
#move enemy
self.rect.x += self.direction * 2
self.rect.y += scroll
#check if gone off screen
if self.rect.right < 0 or self.rect.left > SCREEN_WIDTH:
self.kill()