-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
135 lines (112 loc) · 4.41 KB
/
utility.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'''
Author: Kyle Charters
Description:
The utility module contains code that doesn't exactly fit anywhere else, these are strictly used for utility
Contents:
- exitPygame()
- aspect()
- splitSurface()
- darkenSurface()
- KeyListener
- Debugger
Notes:
None
'''
from pygame.constants import K_F1, K_F2
import pygame
import constants
#Use lambda because it is a one-liner
exitPygame = lambda pygame: pygame.event.post(pygame.event.Event(pygame.QUIT))
def aspect(image, parentsize):
#Store image size
imagesize = image.get_size()
#If image width / height is equal to parent width / height respectively
if imagesize[0] == parentsize[0] or imagesize[1] == parentsize[1]:
return image
#If width is bigger than height, multiply height by size ratio
elif imagesize[0] > imagesize[1]:
size = parentsize[0], int(imagesize[1] * (parentsize[0] / imagesize[0]))
#If height is bigger than width, multiply width by size ratio
else:
size = int(imagesize[0] * (parentsize[1] / imagesize[1])), parentsize[1]
#Return the scaled image
return pygame.transform.scale(image, size)
def splitSurface(image, colorKey=None, convert=False):
#Create an empty surfaces array to hold every strip
surfaces = []
#Iterate over every strip on the x-axis
for x in range(image.get_width()):
#Create a surface 1 pixel wide
surface = pygame.Surface((1, image.get_height()))
#Converts the surface to the display surface pixel format for fast blitting
if convert:
surface = surface.convert()
#Sets the newly generated surface's color key to the color key supplied. (None is accepted by pygame)
surface.set_colorkey(colorKey)
#Render the image onto the 1 pixel wide surface
surface.blit(image, (-x, 0))
#Add the new surface to the surfaces array
surfaces.append(surface)
return surfaces
def darkenSurface(image, value):
#Creates a new surface with the images size, default color is black
darkness = pygame.Surface(image.get_size())
#Changes the blank black surface's alpha, this is transparency
darkness.set_alpha(255 - value)
#Set color key to white to prevent textures becoming invisible if they are too dark
darkness.set_colorkey((255, 255, 255))
#Render the blank black surface over the texture
image.blit(darkness, (0, 0))
#Return image to allow one-liners
return image
class KeyListener(object):
def __init__(self, key):
self.key = key
self.clicked = False
self.pressed = False
def update(self, keys):
self.pressed = keys[self.key]
if not self.pressed and self.clicked:
self.clicked = False
def isClicked(self):
if self.pressed:
if not self.clicked:
self.clicked = True
return True
else:
self.clicked = False
return False
class Debugger():
def __init__(self):
self.listener1 = KeyListener(K_F1)
self.listener2 = KeyListener(K_F2)
self.log = []
self.tracking = False
self.showfps = False
def update(self, surface, delta, events, keys, fpsclock):
self.listener1.update(keys)
if self.listener1.isClicked():
self.showfps = not self.showfps
#Start and stop fps tracking if the listener's key is pressed
self.listener2.update(keys)
if self.listener2.isClicked():
self.tracking = not self.tracking
if self.tracking:
#If tracking just started show a message
print("++++++++++++++++++++")
print("FPS Tracking started")
print("--------------------")
else:
#If tracking just stopped, print the average fps and clear the fps log
print("Average FPS:")
print(sum(self.log)/len(self.log))
print("--------------------")
self.log.clear()
fps = fpsclock.get_fps()
if self.tracking:
#Append fps to the log
self.log.append(fps)
if self.showfps:
#Display FPS in the top right corner
label = constants.F_REGULAR.render(str(int(fps)), 1, (255, 255, 255))
surface.blit(label, (surface.get_width() - label.get_width() - 5, 0))