This repository was archived by the owner on Mar 20, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathHelperAPI.py
More file actions
143 lines (96 loc) · 4.2 KB
/
Copy pathHelperAPI.py
File metadata and controls
143 lines (96 loc) · 4.2 KB
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
136
137
138
139
140
141
142
143
# HelperAPI.py - Some helpful utilities so I won't have to type as much. :)
# Created by Josh Kennedy on 27 July 2014
#
# Pop a Dots
# Copyright 2014 Chad Jensen and Josh Kennedy
# Copyright 2015-2016 Sirkles LLC
import pygame
import pygame.gfxdraw
import datetime
import os
import math
import random
import Rectangle
import AssetCache
import Vector2
# Gets the window rectangle as a PyGame rect.
def getWindowRectangle():
return pygame.display.get_surface().get_rect()
# Gets the window rectangle as our custom Rectangle.
def getWindowRectangleAsRectangle():
return Rectangle.Rectangle(getWindowRectangle().x, getWindowRectangle().y, getWindowRectangle().width,
getWindowRectangle().height)
# Gets the window surface from PyGame.
def getWindowSurface():
return pygame.display.get_surface()
# Takes a screenshot as a BMP
def takeScreenshot():
directory = os.path.dirname(os.path.realpath(__file__)) + "/Screenshots/"
if not os.path.exists(directory):
os.makedirs(directory)
now = datetime.datetime.now()
# PNG changes some of the colors, like the score text from red to green. wtf?!
# BMP doesn't change anything, except the file size will be bigger. :\
filename = directory + ("Screenshot_%s-%s-%s_%s.%s.%s.%s.png" % (now.year, now.month, now.day, now.hour, now.minute,
now.second, math.floor(now.microsecond / 1000)))
temp_surface = pygame.display.get_surface()
temp_surface.convert_alpha()
pygame.image.save(temp_surface, filename)
del temp_surface
# Get the center of the screen.
def getCenterOfScreen():
return pygame.display.get_surface().get_rect().centerx, pygame.display.get_surface().get_rect().centery
# Plays a random pop sound.
def playRandomPopSound():
num = random.randint(0, 2)
if num is 0:
AssetCache.highPopSound.play()
elif num is 1:
AssetCache.lowPopSound.play()
elif num is 2:
AssetCache.highPop2Sound.play()
def getAspectRatio():
return AssetCache.screenResolution[0] / AssetCache.screenResolution[1]
def getTempScalar():
return AssetCache.screenResolution[0] / 1280
def getScreenScalar():
return getAspectRatio() * getTempScalar()
def scaleRadius(custom_size=0.0):
if custom_size == 0.0:
temp_radius = math.floor(random.uniform(0, 1) * 90) + 70
else:
temp_radius = custom_size
temp_number = temp_radius / getAspectRatio()
return temp_number * getScreenScalar()
def scaleVelocity(is_black=False): # -> (x, y)
signed_x_vel = -1 if random.randint(0, 1) == 1 else 1
signed_y_vel = 1 if random.randint(0, 1) == 0 else -1
if not is_black:
temp_x_vel = int(math.floor(random.uniform(0, 1) * 16) + 1) * signed_x_vel
temp_y_vel = int(math.floor(random.uniform(0, 1) * 16) + 1) * signed_y_vel
else:
temp_y_vel = 16 * signed_y_vel
temp_x_vel = 16 * signed_x_vel
temp_number_x = temp_x_vel / getAspectRatio()
temp_number_y = temp_y_vel / getAspectRatio()
return temp_number_x * getScreenScalar(), temp_number_y * getScreenScalar()
def scaleVelocityAsVector(is_black=False): # -> Vector2
vel = scaleVelocity(is_black)
return Vector2.Vector2(vel[0], vel[1])
def scaleXPos(custom_value=0):
return math.floor(random.uniform(0, 1) * AssetCache.screenResolution[0] + 1) if custom_value == 0 else custom_value
def scaleYPos(custom_value=0):
return math.floor(random.uniform(0, 1) * AssetCache.screenResolution[1] + 1) if custom_value == 0 else custom_value
# make general function (color as parameter)
# from https://bitbucket.org/schlangen/pexdra
def supersampled_aacircle(surf, pos, r, i):
temp_surf = pygame.Surface((r * 2 * i, r * 2 * i))
temp_surf.fill((255, 255, 255))
rect = pygame.draw.circle(temp_surf, (200, 0, 0), (r * i, r * i), r * i)
rect.center = pos
img = pygame.transform.smoothscale(temp_surf, (r * 2, r * 2))
surf.blit(img, img.get_rect(center=rect.center).topleft)
# from https://bitbucket.org/schlangen/pexdra
def draw_aacircle(surf, color, pos, rad):
pygame.gfxdraw.aacircle(surf, pos[0], pos[1], rad, color)
pygame.gfxdraw.filled_circle(surf, pos[0], pos[1], rad, color)