Last active 1746350612

fedya.py Raw
1import pygame
2
3class GameContext :
4 def __init__(
5 self,
6 dt, events, keys,
7 screen,
8 ):
9 self.dt = dt
10 self.events = events
11 self.keys = keys
12 self.screen = screen
13
14class MyCube :
15 def __init__(
16 self,
17 x = 0, y = 0,
18 w = 100, h = 100,
19 speed=100,
20 fill_color=(255, 255, 255)
21 ):
22 self.x = x
23 self.y = y
24 self.w = w
25 self.h = h
26 self.speed = speed
27 self.fill_color = fill_color
28 def update(self, ctx):
29 dt = ctx.dt
30 speed = self.speed
31 if ctx.keys[pygame.K_a] :
32 self.x -= dt * speed
33 if ctx.keys[pygame.K_d] :
34 self.x += dt * speed
35 if ctx.keys[pygame.K_w] :
36 self.y -= dt * speed
37 if ctx.keys[pygame.K_s] :
38 self.y += dt * speed
39 def draw(self, ctx):
40 pygame.draw.rect(
41 ctx.screen, self.fill_color,
42 (self.x, self.y,
43 self.w, self.h),
44 )
45
46import random
47cubes = []
48for i in range(2000) :
49 cube = MyCube(
50 speed=random.randint(10, 250),
51 fill_color=(
52 random.randint(0, 255),
53 random.randint(0, 255),
54 random.randint(0, 255),
55 ),
56 x=random.randint(0, 300),
57 y = random.randint(0, 400),
58 )
59 cubes.append(cube)
60clock = pygame.time.Clock()
61screen = pygame.display.set_mode((640, 480))
62bg_color = (0, 0, 0)
63fps = 30
64running = True
65while running :
66 dt = clock.tick(fps) / 1000.0
67 events = pygame.event.get()
68 for event in events :
69 if event.type == pygame.QUIT :
70 running = False
71 keys = pygame.key.get_pressed()
72 ctx = GameContext(dt, events, keys, screen)
73 screen.fill(bg_color)
74 for cube in cubes :
75 cube.update(ctx)
76 cube.draw(ctx)
77
78 pygame.display.update()
79pygame.quit()
80
81