-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventqueue.py
89 lines (73 loc) · 2.29 KB
/
eventqueue.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
import time
import pygame
#import psyco
#psyco.full()
#psyco.log()
class event:
def __init__(self, delay, function, argument):
if delay == "END":
self.time = "END"
else:
self.time = time.time() + delay
self.function = function
self.argument = argument
def execute(self):
#print self.function, self.argument
self.function(self.argument)
def delay(self,delay):
self.time = self.time + delay
def make_null(self):
self.time = "END"
self.function = ''
self.argument = ''
class event_heap:
def __init__(self):
self.nullevent = event("END","", "")
self.nullevent.make_null()
self.heap = [self.nullevent]
def put(self,event):
length = len(self.heap)
hops = (length - 1) / 2
offset = length / 2
pointer = length / 2
while hops > 0:
if self.heap[pointer].time <= event.time:
offset = -max(offset / 2,1)
else:
offset = max(offset / 2,1)
pointer = pointer + offset
hops = max(hops - 1,0)
if pointer == 0:
self.heap.insert(1,event)
elif self.heap[pointer].time <= event.time:
self.heap.insert(pointer,event)
else:
self.heap.insert(pointer + 1 ,event)
def get(self):
next = self.heap.pop()
if next.time == "END":
self.heap.append(self.nullevent)
return next
def next(self):
"""Returns the next node without removing it from the heap."""
return self.heap[-1]
class event_queue:
def __init__(self):
self.start_time = time.time()
self.heap = event_heap()
self.clock = pygame.time.Clock()
def run(self, inputhandler):
self.running = 1
while self.running:
self.reap()
#time.sleep(.01667)
def add(self,event):
self.heap.put(event)
def reap(self):
now = time.time()
next_event = self.heap.next()
while next_event.time != "END" and next_event.time <= now:
self.heap.get().execute()
next_event = self.heap.next()
self.clock.tick()
#print self.clock.get_fps()