fedya.py
· 1.8 KiB · Python
Raw
import pygame
class GameContext :
def __init__(
self,
dt, events, keys,
screen,
):
self.dt = dt
self.events = events
self.keys = keys
self.screen = screen
class MyCube :
def __init__(
self,
x = 0, y = 0,
w = 100, h = 100,
speed=100,
fill_color=(255, 255, 255)
):
self.x = x
self.y = y
self.w = w
self.h = h
self.speed = speed
self.fill_color = fill_color
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
def draw(self, ctx):
pygame.draw.rect(
ctx.screen, self.fill_color,
(self.x, self.y,
self.w, self.h),
)
import random
cubes = []
for i in range(2000) :
cube = MyCube(
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),
)
cubes.append(cube)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((640, 480))
bg_color = (0, 0, 0)
fps = 30
running = True
while running :
dt = clock.tick(fps) / 1000.0
events = pygame.event.get()
for event in events :
if event.type == pygame.QUIT :
running = False
keys = pygame.key.get_pressed()
ctx = GameContext(dt, events, keys, screen)
screen.fill(bg_color)
for cube in cubes :
cube.update(ctx)
cube.draw(ctx)
pygame.display.update()
pygame.quit()
1 | import pygame |
2 | |
3 | class 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 | |
14 | class 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 | |
46 | import random |
47 | cubes = [] |
48 | for 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) |
60 | clock = pygame.time.Clock() |
61 | screen = pygame.display.set_mode((640, 480)) |
62 | bg_color = (0, 0, 0) |
63 | fps = 30 |
64 | running = True |
65 | while 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() |
79 | pygame.quit() |
80 | |
81 |