-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
71 lines (49 loc) · 1.84 KB
/
game.py
File metadata and controls
71 lines (49 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pygame
from pygame.locals import *
import numpy as np
from random import random
from objects import *
import time as time
class Game:
def __init__(self, size, num_pends=1):
pygame.init()
self._running = True
self.size = self.width, self.height = size
self.screen = pygame.display.set_mode(self.size)
self.player = Player([320.0, 300.0], [50.0, 10.0], '_player')
self.objects = [self.player]
for i in range(num_pends):
self.objects.append(Pendulum([320.0, 100.0], [10, 10], f'_pendulum_{i}', self.objects[i]))
l_wall = Object([-10, 0], [10, self.height], '_l_wall')
r_wall = Object([self.width, 0], [10, self.height], '_r_wall')
self.objects.extend([l_wall, r_wall])
def background(self):
background = pygame.Surface(self.screen.get_size())
background = background.convert()
colour = (240, 240, 240)
background.fill(colour)
return background
def render(self):
self.screen.blit(self.background(), (0, 0))
for obj in self.objects:
obj.render(self.screen)
pygame.display.flip()
def update(self):
keys = pygame.key.get_pressed()
for obj in self.objects:
obj.update(keys, self.objects)
def check_quit(self):
for event in pygame.event.get():
if event.type == QUIT:
self._running = False
def run_step(self):
self.check_quit()
self.update()
self.render()
def run(self):
while self._running:
self.run_step()
pygame.display.flip()
pygame.quit()
g = Game((640, 480), num_pends=3)
g.run()