-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
73 lines (59 loc) · 2.35 KB
/
entity.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from random import randint
from pygame.locals import *
from config import *
from assets import *
import random
import pygame
class Entity(pygame.sprite.Sprite):
def __init__(self, game_lost_callback):
super().__init__()
self.is_vegetable = random.choice([True, True, True, False])
self.image = self.random_image()
self.rect = self.image.get_rect()
self.rect.center = (randint(IMAGE_SIZE, SCREEN_WIDTH - IMAGE_SIZE), SCREEN_HEIGHT)
self.velocity_y = randint(-29, -22)
self.velocity_x = self.get_velocity_x()
self.game_lost_callback = game_lost_callback
def random_image(self):
if self.is_vegetable:
random_img = random.choice(vegetable_images)
else:
random_img = random.choice(object_images)
return pygame.transform.rotate(pygame.transform.scale(pygame.image.load(random_img), (IMAGE_SIZE, IMAGE_SIZE)), float(randint(0, 360)))
def update(self):
self.velocity_y += GRAVITY
if self.velocity_x > 0:
self.velocity_x -= HORIZONTAL_FORCE
else:
self.velocity_x += HORIZONTAL_FORCE
self.rect.y = self.rect.y + self.velocity_y
self.rect.x = self.rect.x + self.velocity_x
if self.rect.bottom > SCREEN_HEIGHT + IMAGE_SIZE:
if self.is_vegetable:
if self.rect.left + IMAGE_SIZE >= 0 and self.rect.right - IMAGE_SIZE < SCREEN_WIDTH:
self.game_lost_callback(True)
else:
self.reset()
else:
self.reset()
def get_velocity_x(self):
if self.rect.x < SCREEN_WIDTH // 2:
vel_x = randint(1, 3)
else:
vel_x = randint(-3, -1)
return vel_x
def reset(self):
self.velocity_y = randint(-29, -22)
self.velocity_x = self.get_velocity_x()
self.rect.center = (
randint(IMAGE_SIZE, SCREEN_WIDTH - IMAGE_SIZE), SCREEN_HEIGHT)
self.is_vegetable = random.choice([True, True, True, False])
self.image = self.random_image()
def destroy(self):
if not self.is_vegetable:
self.game_lost_callback(True)
self.reset()
def redraw(self, surface):
surface.blit(self.image, self.rect)
def has_been_clicked(self, mouse_pos):
return self.rect.collidepoint(mouse_pos)