game.py
· 2.0 KiB · Python
Raw
import pygame
class GameContext :
def __init__(
self,
dt, events, keys,
game,
):
self.dt = dt
self.events = events
self.keys = keys
self.game = game
# ctx.screen -> ctx.game.screen
class Game:
def __init__(
self,
resolution=(640, 480),
bg_color=(0, 0, 0),
fps=30,
objects=[],
):
self.resolution = resolution
self.bg_color = bg_color
self.fps = fps
self.objects = objects
def run(self):
self.screen = pygame.display.set_mode(self.resolution)
clock = pygame.time.Clock()
running = True
while running :
# Здесь код на каждый кадр
dt = clock.tick(self.fps) / 1000.0
events = pygame.event.get()
for event in events :
if event.type == pygame.QUIT :
running = False
self.screen.fill(self.bg_color)
keys = pygame.key.get_pressed()
ctx = GameContext(dt, events, keys, self)
for o in self.objects :
o.update(ctx)
o.draw(ctx)
# Вывести всё нарисованное на окно
pygame.display.update()
pygame.quit()
class GameObject:
def update(self):
pass
def draw(self):
pass
# Rectangle - прямоугольник
class Rect(GameObject):
def __init__(
self,
x = 0, y = 0,
w = 100, h = 100,
fill_color=(255, 255, 255),
visible=True,
):
self.x = x
self.y = y
self.w = w
self.h = h
self.fill_color = fill_color
self.visible = visible
def draw(self, ctx):
if not self.visible :
return
pygame.draw.rect(
ctx.game.screen, self.fill_color,
(self.x, self.y,
self.w, self.h),
)
| 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 | |
| 84 |