-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (79 loc) · 3.04 KB
/
main.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pygame as pg
import pymunk as pm
import pymunk.pygame_util
from random import randint
from setting import *
debugFPS = 0
class Game:
def __init__(self, width, height):
self.Running = True
pg.init()
pg.mixer.init()
pg.key.set_repeat(100,100)
self.width = width
self.height = height
self.screen = pg.display.set_mode((self.width,self.height))
self.draw_options = pm.pygame_util.DrawOptions(self.screen)
self.space = pm.Space()
self.space.gravity = 0, -900
pg.display.set_caption('Pymunk Test')
self.begin()
def begin(self):
b0 = self.space.static_body
'''
heres the problem point 0,0 in pymunk is in bottom left corner not in top left corner
'''
self.segment1 = pm.Segment(b0, (0, 0), (self.width, 0), 4)
self.segment1.elasticity = 0
self.segment2 = pm.Segment(b0, (0, 0), (0, self.height), 4)
self.segment2.elasticity = 0
self.segment3 = pm.Segment(b0, (0, self.height), (self.width, self.height), 4)
self.segment3.elasticity = 0
self.segment4 = pm.Segment(b0, (self.width, 0), (self.width, self.height), 4)
self.segment4.elasticity = 0
self.space.add(self.segment1, self.segment2, self.segment3, self.segment4)
for i in range(20):
self.body = pm.Body(mass=10, moment=10)
# remember position in pyunk start at bottom left corner
self.body.position = randint(20,self.width-20), randint(100, self.height-20)
self.circle = pm.Circle(self.body, radius=50)
self.circle.elasticity = 0
self.space.add(self.body, self.circle)
self.clock = pg.time.Clock()
self.run()
def event(self):
pressed = pg.key.get_pressed()
alt_held = pressed[pg.K_LALT] or pressed[pg.K_RALT]
ctrl_held = pressed[pg.K_LCTRL] or pressed[pg.K_RCTRL]
for event in pg.event.get():
if event.type == pg.QUIT:
self.Running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_F4 and alt_held:
self.Running = False
def update(self):
pass
def draw(self):
self.screen.fill(RED)
self.space.debug_draw(self.draw_options)
pg.display.update()
'''
What out for this. some time if delta time > 1 colliusion will crashh.
so lest waht i can to fix it
'''
self.space.step(self.dt)
pg.display.set_caption(str(1/self.dt))
# pg.display.flip()
def run(self):
while self.Running:
self.dt = self.clock.tick(FPS) /1000
if 1/self.dt< FPStolerance:
self.dt = 1/FPStolerance
if debugFPS:
print('FPS: ',1/(self.dt),'\tDt: ',self.dt)
self.event()
self.update()
self.draw()
if __name__ == '__main__':
g = Game(WIDTH, HEIGHT)
pg.quit()