surdeus revised this gist . Go to revision
2 files changed, 117 insertions
game.py(file created)
| @@ -0,0 +1,79 @@ | |||
| 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 | + | ): | |
| 66 | + | self.x = x | |
| 67 | + | self.y = y | |
| 68 | + | self.w = w | |
| 69 | + | self.h = h | |
| 70 | + | self.fill_color = fill_color | |
| 71 | + | ||
| 72 | + | def draw(self, ctx): | |
| 73 | + | pygame.draw.rect( | |
| 74 | + | ctx.game.screen, self.fill_color, | |
| 75 | + | (self.x, self.y, | |
| 76 | + | self.w, self.h), | |
| 77 | + | ) | |
| 78 | + | ||
| 79 | + | ||
pong.py(file created)
| @@ -0,0 +1,38 @@ | |||
| 1 | + | import game as game | |
| 2 | + | import pygame | |
| 3 | + | ||
| 4 | + | class PlayerRect(game.Rect): | |
| 5 | + | def __init__(self, speed=200, **kwargs): | |
| 6 | + | super().__init__(**kwargs) | |
| 7 | + | self.speed = speed | |
| 8 | + | def update(self, ctx): | |
| 9 | + | dt = ctx.dt | |
| 10 | + | speed = self.speed | |
| 11 | + | if ctx.keys[pygame.K_a] : | |
| 12 | + | self.x -= dt * speed | |
| 13 | + | if ctx.keys[pygame.K_d] : | |
| 14 | + | self.x += dt * speed | |
| 15 | + | if ctx.keys[pygame.K_w] : | |
| 16 | + | self.y -= dt * speed | |
| 17 | + | if ctx.keys[pygame.K_s] : | |
| 18 | + | self.y += dt * speed | |
| 19 | + | ||
| 20 | + | import random | |
| 21 | + | rects = [] | |
| 22 | + | for i in range(2000) : | |
| 23 | + | rect = PlayerRect( | |
| 24 | + | speed=random.randint(10, 250), | |
| 25 | + | fill_color=( | |
| 26 | + | random.randint(0, 255), | |
| 27 | + | random.randint(0, 255), | |
| 28 | + | random.randint(0, 255), | |
| 29 | + | ), | |
| 30 | + | x=random.randint(0, 300), | |
| 31 | + | y = random.randint(0, 400), | |
| 32 | + | ) | |
| 33 | + | rects.append(rect) | |
| 34 | + | ||
| 35 | + | g = game.Game( | |
| 36 | + | objects=rects, | |
| 37 | + | ) | |
| 38 | + | g.run() | |
Newer
Older