forked from TheAwesomePossum/PythonCS0
/
Engine.py
75 lines (64 loc) · 1.67 KB
/
Engine.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
'''
Author: Graham Montgomery
Western State Colorado University
This file contains all the graphic logic for the project.
'''
# External Imports
import threading # used to create graphics thread(perhaps one day move this to graphics chip)
import time
# Internal Imports
import Globals as g # project global variables
import Events
def flipDisplay():
'''
this will be the graphics thread
'''
again = True
stopFlag = False
while(g.threadCount is threading.activeCount() and again):
flipOnce()
g.clock.tick(g.deltaTime)
if not g.running:
stopFlag = True
if stopFlag:
again = False
def flipOnce():
Events.updateEvents()
g.window.fill(g.world.color)
world = g.world
world.acquire()
for gobj in world.world:
gobj.acquire()
gobj.draw(g.window)
gobj.release()
world.release()
g.pygame.display.flip()
GraphicThread = threading.Thread(target=flipDisplay, args=())
def start(world, thread):
'''
Sets up pygame
Sets up the window
Starts graphics thread
'''
g.threading = thread
size = (world.width, world.height)
g.window = g.pygame.display.set_mode(size)
g.pygame.display.set_caption(world.caption)
g.window.fill(world.color)
g.pygame.display.flip()
g.startTime = time.time()
if thread:
g.running = True
g.threadCount += 1
GraphicThread.start()
def stop():
'''
stops the graphic thread
'''
if g.multithreading:
g.running = False
GraphicThread.join()
Events.eraseEvents()
while True:
Events.updateEvents()
g.clock.tick(g.deltaTime)