-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.py
56 lines (44 loc) · 1.56 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
51
52
53
54
55
56
# Scripts:
from constants import *
# Modules:
import pygame
import random
import secrets
class Item(pygame.sprite.Sprite):
def __init__(self, img_path, scale, vel, bounce):
super().__init__()
# Image:
self.img_path = img_path
self.image = pygame.image.load(f'{self.img_path}.png').convert_alpha()
self.scale = scale
self.image = pygame.transform.scale(self.image, (self.image.get_width() * self.scale, self.image.get_height() * self.scale))
self.rect = self.image.get_rect()
self.rect.center = [random.randint(int(0 + self.rect.width/2), int(WIDTH - self.rect.width/2)), -40]
# Movement:
self.vel = self.vel_x, self.vel_y = vel
self.vel_x = self.vel_x * secrets.choice([-1, 1])
self.bounce = bounce
def move_x(self):
self.rect.x += self.vel_x
if self.bounce:
if self.rect.left <= 0:
self.vel_x = self.vel_x * -1
elif self.rect.right >= WIDTH:
self.vel_x = self.vel_x * -1
def move_y(self):
self.rect.y += self.vel_y
def handle_movement(self):
if self.rect.top > HEIGHT:
self.kill()
else:
self.move_x()
self.move_y()
def update(self):
# Movement:
self.handle_movement()
class HP(Item):
def __init__(self, img_path, scale, vel, bounce):
super().__init__(img_path, scale, vel, bounce)
class Life(Item):
def __init__(self, img_path, scale, vel, bounce):
super().__init__(img_path, scale, vel, bounce)