-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
116 lines (92 loc) · 3.13 KB
/
utils.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
# -*- coding: utf-8 -*-
###############################################################################
# Python AI Battle
#
# Copyright 2011 Matthew Thompson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
import sys, time
class Enum(object):
'''Crappy enum emulator'''
class Item(object):
def __init__(self, name):
self.__name = str(name)
def __repr__(self):
return self.__name
def __init__(self, *args):
self.values = []
for name in args:
item = Enum.Item(name)
setattr(self, name, item)
self.values.append(item)
class Rect:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def touches(self, o):
if o.x + o.w < self.x:
return False
if o.y + o.h < self.y:
return False
if o.x > self.x + self.w:
return False
if o.y > self.y + self.h:
return False
return True
def __repr__(self):
return "Rect(%s, %s, %s, %s)" % (self.x, self.y, self.w, self.h)
def debug_draw(self):
import pyglet
import pyglet.gl as gl
x,y,w,h = self.x, self.y, self.w, self.h
vertex_list = pyglet.graphics.vertex_list(4,
('v2f', (x,y, x+w,y, x+w,y+h, x,y+h)),
('c3B', (255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0))
)
vertex_list.draw(gl.GL_LINE_LOOP)
class Animation:
'''Handle a linear animation of a given value.'''
def __init__(self, start, stop, speed):
self.start = start
self.value = start
self.stop = stop
self.speed = speed
self.done = False
def unit(self):
return self.value / (self.stop - self.start)
def update(self, dt):
if not self.done:
self.value += self.speed * dt
if self.value >= self.stop:
self.done = True
def __str__(self):
return "Animation at %.2f of %.2f (+%.2f)" % (self.value, self.stop, self.speed)
class DebugWriter:
def __init__(self, label):
self.label = label
self.sink = sys.__stdout__
self.newline = True
def write(self, msg):
if self.newline:
s = "%5s [%4.4f]: %s" % (self.label, time.clock(), msg)
self.newline = False
else:
s = "%s" % msg
if msg.find('\n') != -1:
self.newline = True
self.sink.write(s)