Last active 1746876627

surdeus's Avatar surdeus revised this gist 1746876627. Go to revision

1 file changed, 83 insertions

game.py(file created)

@@ -0,0 +1,83 @@
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 :
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):
55 + pass
56 + def draw(self):
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 +
Newer Older