game.py
· 1.9 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)
):
self.x = x
self.y = y
self.w = w
self.h = h
self.fill_color = fill_color
def draw(self, ctx):
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 | ): |
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 | |
80 |
pong.py
· 908 B · Python
Raw
import game as game
import pygame
class PlayerRect(game.Rect):
def __init__(self, speed=200, **kwargs):
super().__init__(**kwargs)
self.speed = speed
def update(self, ctx):
dt = ctx.dt
speed = self.speed
if ctx.keys[pygame.K_a] :
self.x -= dt * speed
if ctx.keys[pygame.K_d] :
self.x += dt * speed
if ctx.keys[pygame.K_w] :
self.y -= dt * speed
if ctx.keys[pygame.K_s] :
self.y += dt * speed
import random
rects = []
for i in range(2000) :
rect = PlayerRect(
speed=random.randint(10, 250),
fill_color=(
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
),
x=random.randint(0, 300),
y = random.randint(0, 400),
)
rects.append(rect)
g = game.Game(
objects=rects,
)
g.run()
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() |
39 |