-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.py
50 lines (43 loc) · 1.88 KB
/
items.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
import pygame
class Item(pygame.sprite.Sprite):
def __init__(self, x, y, item_type, animation_list, dummy_coin=False):
pygame.sprite.Sprite.__init__(self)
self.item_type = item_type # 0:coin, 1:health potion
self.animation_list = animation_list
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
self.image = self.animation_list[self.frame_index]
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.dummy_coin = dummy_coin
def update(self, screen_scroll, player, coin_fx, heal_fx):
# doesnt apply to the dummy coin that is always displayed at the top of screen
# reposition based on screen_scroll
if not self.dummy_coin:
self.rect.x += screen_scroll[0]
self.rect.y += screen_scroll[1]
# check to see if item has been collected by the player
if self.rect.colliderect(player.rect):
# coin collected
if self.item_type == 0:
player.score += 1
coin_fx.play()
elif self.item_type == 1:
player.health += 10
heal_fx.play()
if player.health > 100:
player.health = 100
self.kill()
# handle animation
animation_cooldown = 150
# update image
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.frame_index += 1
self.update_time = pygame.time.get_ticks()
# check if the animation has finsihed
if self.frame_index >= (len(self.animation_list)):
self.frame_index = 0
def draw(self, surface):
surface.blit(self.image, self.rect)