Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 381b43f

Browse files
authoredDec 26, 2021
General cleanup and fixes
1 parent c4f1090 commit 381b43f

File tree

11 files changed

+233
-307
lines changed

11 files changed

+233
-307
lines changed
 

‎gameobjects/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
14
__all__ = [
2-
'vector2',
3-
'vector3',
4-
'util',
5-
'sphere',
6-
'matrix44',
7-
'color',
8-
'gametime',
9-
'grid'
5+
'vector2',
6+
'vector3',
7+
'util',
8+
'sphere',
9+
'matrix44',
10+
'color',
11+
'gametime',
12+
'grid'
1013
]
1114

12-
13-
__version__ = "0.0.3"
15+
__version__ = '0.0.3'

‎gameobjects/color.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
from math import *
2-
from util import format_number
3-
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# Color Module
44

5-
class ColorRGBA(object):
5+
"Color module"
66

7-
__slots__ = ('_c',)
7+
__title__ = 'color'
88

9+
from util import format_number
910

11+
class ColorRGBA:
12+
"Color with red, green, blue, and alpha components."
13+
__slots__ = ('_c',)
14+
1015
def __init__(self, *args):
11-
12-
"""Creates a color object."""
13-
16+
"Creates a color object."
17+
1418
if not args:
1519
self._c = [0.0, 0.0, 0.0, 1.0]
1620
return
17-
21+
1822
if len(args) == 1:
1923
args = args[0]
20-
24+
2125
if len(args) == 3:
2226
r, g, b = args
2327
self._c = [float(r), float(g), float(b), 1.0]
@@ -26,26 +30,23 @@ def __init__(self, *args):
2630
r, g, b, a = args
2731
self._c = [float(r), float(g), float(b), float(a)]
2832
return
29-
30-
raise ValueError("0, 1, 3 or 4 values required")
31-
33+
34+
raise ValueError('0, 1, 3 or 4 values required')
35+
3236
def __str__(self):
33-
34-
return "(" + ", ".join(format_number(c) for c in self._c) + ")"
37+
return '(' + ', '.join(format_number(c) for c in self._c) + ')'
3538
#return "(" + ", ".join(map(str, self._c)) + ")"
3639

3740
def __repr__(self):
38-
41+
3942
return "ColorRGBA(" + ", ".join(map(str, self._c)) + ")"
4043

4144
@classmethod
4245
def black(cls):
43-
44-
"""Create a color object representing black."""
45-
46-
c = cls.__new__(cls, object)
47-
c._c = [0.0, 0.0, 0.0, 1.0]
48-
return c
46+
"Create a color object representing black."
47+
self = cls.__new__(cls, object)
48+
self._c = [0.0, 0.0, 0.0, 1.0]
49+
return self
4950

5051
@classmethod
5152
def white(cls):
@@ -996,10 +997,7 @@ def mul_alpha(self):
996997
'lightgreen' : (0.564705882353, 0.933333333333, 0.564705882353),
997998
}
998999

999-
1000-
1001-
if __name__ == "__main__":
1002-
1000+
def test():
10031001
c1 = Color(.5, .2, .8)
10041002
c2 = Color(1., 0., .2)
10051003
print(c1)
@@ -1010,3 +1008,6 @@ def mul_alpha(self):
10101008
print(c1('rrrgggbbbaaa'))
10111009
print(Color.from_palette('magenta').rgba8)
10121010
#palette.red += palette.blue
1011+
1012+
if __name__ == "__main__":
1013+
test()

‎gameobjects/gametime.py

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# Game Time Module
14

5+
"Game Time Module"
26

3-
class GameClock(object):
4-
5-
"""Manages time in a game."""
6-
7+
__title__ = "gametime"
8+
9+
import time as _time
10+
11+
class GameClock:
12+
"Manages time in a game."
713
def __init__(self, game_ticks_per_second=20):
8-
914
"""Create a Game Clock object.
1015
1116
game_ticks_per_second -- The number of logic frames a second.
1217
1318
"""
14-
19+
1520
self.game_ticks_per_second = float(game_ticks_per_second)
1621
self.game_tick = 1. / self.game_ticks_per_second
1722
self.speed = 1.
@@ -30,11 +35,10 @@ def __init__(self, game_ticks_per_second=20):
3035
self.fps_sample_start_time = 0.0
3136
self.fps_sample_count = 0
3237
self.average_fps = 0
33-
34-
38+
39+
3540
def start(self):
36-
37-
"""Starts the Game Clock. Must be called once."""
41+
"Starts the Game Clock. Must be called once."
3842

3943
if self.started:
4044
return
@@ -54,7 +58,6 @@ def start(self):
5458

5559

5660
def set_speed(self, speed):
57-
5861
"""Sets the speed of the clock.
5962
6063
speed -- A time factor (1 is normal speed, 2 is twice normal)
@@ -66,70 +69,53 @@ def set_speed(self, speed):
6669
raise ValueError("Negative speeds not supported")
6770

6871
self.speed = speed
69-
70-
72+
7173
def pause(self):
72-
73-
"""Pauses the Game Clock."""
74-
74+
"Pauses the Game Clock."
7575
self.pause = True
76-
76+
7777
def unpause(self):
78-
79-
"""Un-pauses the Game Clock."""
80-
78+
"Un-pauses the Game Clock."
8179
self.pause = False
82-
83-
80+
8481
def get_real_time(self):
85-
8682
"""Returns the real time, as reported by the system clock.
8783
This method may be overriden."""
88-
89-
import time
90-
return time.clock()
91-
92-
84+
85+
return _time.time()
86+
9387
def get_fps(self):
94-
9588
"""Retrieves the current frames per second as a tuple containing
9689
the fps and average fps over a second."""
97-
9890
return self.fps, self.average_fps
9991

100-
10192
def get_between_frame(self):
102-
10393
"""Returns the interpolant between the previous game tick and the
10494
next game tick."""
10595

10696
return self.between_frame
10797

108-
10998
def update(self, max_updates = 0):
110-
11199
"""Advances time, must be called once per frame. Yields tuples of
112100
game frame count and game time.
113101
114102
max_updates -- Maximum number of game time updates to issue.
115-
116103
"""
117104

118-
assert self.started, "You must call 'start' before using a GameClock."
119-
105+
assert self.started, "You must call 'start' before using a GameClock."
106+
120107
real_time_now = self.get_real_time()
121108

122109
self.real_time_passed = real_time_now - self.real_time
123110
self.real_time = real_time_now
124111

125112
self.clock_time += self.real_time_passed
126-
113+
127114
if not self.paused:
128115
self.virtual_time += self.real_time_passed * self.speed
129116

130117
update_count = 0
131-
while self.game_time + self.game_tick < self.virtual_time:
132-
118+
while self.game_time + self.game_tick < self.virtual_time:
133119
self.game_frame_count += 1
134120
self.game_time = self.game_frame_count * self.game_tick
135121
yield (self.game_frame_count, self.game_time)
@@ -138,7 +124,7 @@ def update(self, max_updates = 0):
138124
break
139125

140126
self.between_frame = ( self.virtual_time - self.game_time ) / self.game_tick
141-
127+
142128
if self.real_time_passed != 0:
143129
self.fps = 1.0 / self.real_time_passed
144130
else:
@@ -151,22 +137,19 @@ def update(self, max_updates = 0):
151137
self.average_fps = self.fps_sample_count / (self.real_time - self.fps_sample_start_time)
152138
self.fps_sample_start_time = self.real_time
153139
self.fps_sample_count = 0
154-
155-
156-
if __name__ == "__main__":
157-
158-
import time
140+
141+
def test():
159142
t = GameClock(20) # AI is 20 frames per second
160143
t.start()
161144

162145
while t.virtual_time < 2.0:
163-
164146
for (frame_count, game_time) in t.update():
165-
166147
print("Game frame #%i, %2.4f" % (frame_count, game_time))
167-
148+
168149
virtual_time = t.virtual_time
169150
print("\t%2.2f%% between game frame, time is %2.4f"%(t.between_frame*100., virtual_time))
170151

171-
172-
time.sleep(0.2) # Simulate time to render frame
152+
_time.sleep(0.2) # Simulate time to render frame
153+
154+
if __name__ == '__main__':
155+
test()

0 commit comments

Comments
 (0)
Please sign in to comment.