-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_obj.py
126 lines (94 loc) · 3.36 KB
/
game_obj.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from typing import List, Tuple
import enum
import random
import pygame
from colors import Colors
Point = list
Dimensions = Tuple[int, int]
class Direction(enum.Enum):
up = 0
right = 1
down = 2
left = 3
class World:
def __init__(self, grid_dims: Dimensions, screen_dims: Dimensions):
self.grid_dims = grid_dims
self.screen_dims = screen_dims
self.sprites = {
'Bullet': pygame.sprite.Group(),
'Invader': pygame.sprite.Group()
}
def remove(self, obj):
self.sprites[obj.__class__.__name__].remove(obj)
def add(self, obj):
self.sprites[obj.__class__.__name__].add(obj)
def update(self):
for cls_name, sprite_group in self.sprites.copy().items():
for obj in sprite_group:
obj.counter_incr()
sprite_group.update()
class GameObject:
def __init__(self, world: World, pos: Point, callback=lambda: None):
cls_name = self.__class__.__name__
if cls_name not in world.sprites:
world.sprites[cls_name] = pygame.sprite.Group()
world.add(self)
self.callback = callback
self.world = world
self.pos = [pos[0] % world.grid_dims[0], pos[1] % world.grid_dims[1]]
self.counter = 0
img_width = img_height = world.screen_dims[0] / world.grid_dims[0]
self.image = pygame.Surface([0.8 * img_width, 0.8 * img_height])
self.image.fill(Colors.red)
self.rect = self.image.get_rect()
self.v = 0, 0
self.freq = 1
def counter_incr(self):
self.counter += 1
if self.counter % self.freq == 0:
self.move()
self.counter = 0
def move(self):
self.pos = self.pos[0] + self.v[0], self.pos[1] + self.v[1]
def _update(self):
rows_n, cols_n = self.world.grid_dims
screen_w, screen_h = self.world.screen_dims
x, y = self.pos
if not (0 <= x <= cols_n and 0 <= y <= rows_n):
self.world.remove(self)
self.callback()
return
self.rect.x = x * screen_w / cols_n
self.rect.y = y * screen_h / rows_n
def update(self):
self._update()
class Player(GameObject, pygame.sprite.Sprite):
def __init__(self, *args, **kwargs):
pygame.sprite.Sprite.__init__(self)
GameObject.__init__(self, *args, **kwargs)
def shoot(self):
return Bullet([0, -1], self.world, (self.pos[0], self.pos[1] - 1))
class Bullet(GameObject, pygame.sprite.Sprite):
def __init__(self, v, *args, **kwargs):
pygame.sprite.Sprite.__init__(self)
GameObject.__init__(self, *args, **kwargs)
self.v = v
self.freq = 10
class Invader(GameObject, pygame.sprite.Sprite):
def __init__(self, *args, **kwargs):
pygame.sprite.Sprite.__init__(self)
GameObject.__init__(self, *args, **kwargs)
self.freq = 10
self.reg = 0
def move(self):
self.reg += 1
if self.reg % 2 == 0:
self.v = 1, 0
else:
self.v = -1, 0
if self.reg % 4 == 0:
self.spawn()
self.reg %= 16
super().move()
def spawn(self):
return Bullet([0, 1], self.world, (self.pos[0], self.pos[1] + 1))