surdeus revised this gist . Go to revision
1 file changed, 110 insertions
SASHA.py(file created)
@@ -0,0 +1,110 @@ | |||
1 | + | import pygame | |
2 | + | ||
3 | + | class GameContext : | |
4 | + | def __init__( | |
5 | + | self, | |
6 | + | dt, events, keys, | |
7 | + | game, | |
8 | + | ): | |
9 | + | self.dt = dt | |
10 | + | self.events = events | |
11 | + | self.keys = keys | |
12 | + | self.game = game | |
13 | + | # ctx.screen -> ctx.game.screen | |
14 | + | ||
15 | + | class Game: | |
16 | + | def __init__( | |
17 | + | self, | |
18 | + | resolution=(640, 480), | |
19 | + | bg_color=(0, 0, 0), | |
20 | + | fps=30, | |
21 | + | objects=[], | |
22 | + | ): | |
23 | + | self.resolution = resolution | |
24 | + | self.bg_color = bg_color | |
25 | + | self.fps = fps | |
26 | + | self.objects = objects | |
27 | + | def run(self): | |
28 | + | self.screen = pygame.display.set_mode(self.resolution) | |
29 | + | clock = pygame.time.Clock() | |
30 | + | running = True | |
31 | + | while running : # gist.surdeus.su/surdeus | |
32 | + | # Здесь код на каждый кадр | |
33 | + | ||
34 | + | dt = clock.tick(self.fps) / 1000.0 | |
35 | + | events = pygame.event.get() | |
36 | + | for event in events : | |
37 | + | if event.type == pygame.QUIT : | |
38 | + | running = False | |
39 | + | ||
40 | + | self.screen.fill(self.bg_color) | |
41 | + | ||
42 | + | keys = pygame.key.get_pressed() | |
43 | + | ctx = GameContext(dt, events, keys, self) | |
44 | + | ||
45 | + | for o in self.objects : | |
46 | + | o.update(ctx) | |
47 | + | o.draw(ctx) | |
48 | + | ||
49 | + | # Вывести всё нарисованное на окно | |
50 | + | pygame.display.update() | |
51 | + | pygame.quit() | |
52 | + | ||
53 | + | class GameObject: | |
54 | + | def update(self, ctx): | |
55 | + | pass | |
56 | + | def draw(self, ctx): | |
57 | + | pass | |
58 | + | # Rectangle - прямоугольник | |
59 | + | class Rect(GameObject): | |
60 | + | def __init__( | |
61 | + | self, | |
62 | + | x = 0, y = 0, | |
63 | + | w = 100, h = 100, | |
64 | + | fill_color=(255, 255, 255), | |
65 | + | visible=True, | |
66 | + | ): | |
67 | + | self.x = x | |
68 | + | self.y = y | |
69 | + | self.w = w | |
70 | + | self.h = h | |
71 | + | self.fill_color = fill_color | |
72 | + | self.visible = visible | |
73 | + | ||
74 | + | def draw(self, ctx): | |
75 | + | if not self.visible : | |
76 | + | return | |
77 | + | pygame.draw.rect( | |
78 | + | ctx.game.screen, self.fill_color, | |
79 | + | (self.x, self.y, | |
80 | + | self.w, self.h), | |
81 | + | ) | |
82 | + | # Содержит ли точку. | |
83 | + | def contains_point(self, px, py): | |
84 | + | x, y, w, h = self.x, self.y, self.w, self.h | |
85 | + | return ( | |
86 | + | py >= y and py <= (y + h) and | |
87 | + | px >= x and px <= (x + w) | |
88 | + | ) | |
89 | + | def points(self): | |
90 | + | x, y, w, h = self.x, self.y, self.w, self.h | |
91 | + | return ((x, y), | |
92 | + | (x + w, y), | |
93 | + | (x + w, y + h), | |
94 | + | (x, y + h)) | |
95 | + | def intersects_with(self, other): | |
96 | + | # Проверить, пересекается ли текущий | |
97 | + | # прямоугольник с другим прямоугольником. | |
98 | + | # Если пересекаются вернуть True иначе False. | |
99 | + | # self.x, self.y, other.x, other.y ... | |
100 | + | # Декартова система координат x, y. | |
101 | + | # Свойства прямоугольников. | |
102 | + | for point in other.points() : | |
103 | + | if self.contains_point(point[0], point[1]) : | |
104 | + | return True | |
105 | + | for point in self.points() : | |
106 | + | if other.contains_point(point[0], point[1]) : | |
107 | + | return True | |
108 | + | return False | |
109 | + | ||
110 | + |
Newer
Older