-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLib.py
More file actions
259 lines (179 loc) · 8.52 KB
/
GameLib.py
File metadata and controls
259 lines (179 loc) · 8.52 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import pygame
import math
import random
pygame.init()
INFO = pygame.display.Info()
pygame.font.init()
class tilemapManager():
def __init__(self):
self.dirtTiles = stripFromSheet(scaleSurfaceBy(pygame.image.load("Assets/Tiles/DirtTiles_1-3.png").convert(),5), (0,0), (16*5,16*5), 1, 3)
self.dimensions = pygame.Vector2(36,24)
self.dirtTilemap = self.generateRandomDirtmap()
self.tilemapImage = pygame.surface.Surface((36*5*16,24*5*16)).convert()
self.tilemapImage.fill((0,0,0,0))
self.drawDirtmap(self.dirtTiles,self.dirtTilemap,self.tilemapImage,pygame.Vector2(0,0))
# TILEMAP DIMENSIONS
# [240,135]
# [48,27]
def generateRandomDirtmap(self):
# ADD RANDOM ROTATION TO TILE DATA FOR MORE VARIATION
tilemap = []
for y in range(0,int(self.dimensions.y)):
row = []
for x in range(0,int(self.dimensions.x)):
row.append([random.randint(0,2),pygame.Vector2(x*5*16,y*5*16)])
tilemap.append(row)
return tilemap
def generateRandomGrassmap(self):
# ADD RANDOM ROTATION TO TILE DATA FOR MORE VARIATION
tilemap = []
for y in range(0,int(self.dimensions.y)):
row = []
for x in range(0,int(self.dimensions.x)):
row.append([random.randint(0,1),pygame.Vector2(x*5*16,y*5*16)])
tilemap.append(row)
return tilemap
def renderTilemap(self,camera,surface = pygame.surface.Surface):
#cameraRect = pygame.Rect(camera.position.x,camera.position.y,surface.get_width(),surface.get_height())
surface.blit(self.tilemapImage,-camera.position)
def drawDirtmap(self,tileset,tilemap,surface,offset):
for y in range(0,int(self.dimensions.y)):
for x in range(0,int(self.dimensions.x)):
tile = tilemap[y][x][0]
position = tilemap[y][x][1] - offset
if tile == 0:
surface.blit(tileset[0],position)
elif tile == 1:
surface.blit(tileset[1],position)
elif tile == 2:
surface.blit(tileset[2],position)
def drawGrassmap(self,tileset,tilemap,surface,offset):
pass
def sliceTilemap_15(tilemap_path, tile_scale, mode = 'both'):
tileDictionary = {}
tileArray = []
tilemapImage = pygame.image.load(tilemap_path).convert_alpha()
tilemapImage = scaleSurfaceBy(tilemapImage, tile_scale)
slicedTiles = stripFromSheet(tilemapImage, (0,0), (tile_scale,tile_scale), 4, 4, 'grid')
tileArray = slicedTiles
tileDictionary = {
'TopRightOuter' : tileArray[0][0],
'MidLeftEdge' : tileArray[0][1],
'BottomLeftInner' : tileArray[0][2],
'MidTopEdge' : tileArray[0][3],
'DiagonalDown' : tileArray[1][0],
'BottomRightInner' : tileArray[1][1],
'Center' : tileArray[1][2],
'TopLeftInner' : tileArray[1][3],
'BottomLeftOuter' : tileArray[2][0],
'MidBottomEdge' : tileArray[2][1],
'TopRightInner' : tileArray[2][2],
'MidRightEdge' : tileArray[2][3],
'BLANK_N/A' : tileArray[3][0],
'TopLeftOuter' : tileArray[3][1],
'DiagonalUp' : tileArray[3][2],
'BottomRightOuter' : tileArray[3][3]
}
if mode == 'Array':
return tileArray
elif mode == 'Dictionary':
return tileDictionary
elif mode == 'both':
return tileArray, tileDictionary
class Camera():
def __init__(self, position = pygame.Vector2(0,0)):
self.position = position
self.moveWithArrows = False
def updateKeydowns(self):
if self.moveWithArrows:
camspeed = 5
downKeys = pygame.key.get_pressed()
# SET A MOVEMENT ANGLE FOR CALCULATING WHERE TO GO
if downKeys[pygame.K_UP] == True or downKeys[pygame.K_DOWN] == True or downKeys[pygame.K_LEFT] == True or downKeys[pygame.K_RIGHT] == True:
self.currentSpeed = camspeed
else:
self.currentSpeed = 0
self.moveVector = pygame.Vector2(0,0)
if downKeys[pygame.K_UP]:
self.moveVector += pygame.Vector2(0,-self.currentSpeed)
if downKeys[pygame.K_DOWN]:
self.moveVector += pygame.Vector2(0,self.currentSpeed)
if downKeys[pygame.K_LEFT]:
self.moveVector += pygame.Vector2(-self.currentSpeed, 0)
if downKeys[pygame.K_RIGHT]:
self.moveVector += pygame.Vector2(self.currentSpeed, 0)
self.position += self.moveVector
def update(self):
self.updateKeydowns()
def scaleSurfaceBy(surface = pygame.surface.Surface, scale = float):
return pygame.transform.scale(surface,pygame.Vector2(surface.get_width()*scale,surface.get_height()*scale))
def getCenterOffset(surface = pygame.surface.Surface):
return pygame.Vector2(surface.get_width()/2, surface.get_height()/2)
def createOutline(inputSurface = pygame.surface.Surface, pixelSize = int, color = pygame.color.Color):
maskSurface = pygame.mask.from_surface(inputSurface.copy())
dimensions = pygame.Vector2(maskSurface.get_size()[0] + (2*pixelSize), maskSurface.get_size()[1] + (2*pixelSize))
newsurface = pygame.surface.Surface(dimensions).convert_alpha()
newsurface.fill((0,0,0,0))
if color == pygame.color.Color(0,0,0):
color = pygame.color.Color(1,1,1)
maskSurface = maskSurface.to_surface(newsurface.copy(),setcolor=color, unsetcolor=(0,0,0,0))
maskSurface.set_colorkey((0,0,0))
newsurface.blit(maskSurface,pygame.Vector2(0,pixelSize)) # Left
newsurface.blit(maskSurface,pygame.Vector2(pixelSize*2,pixelSize)) # Right
newsurface.blit(maskSurface,pygame.Vector2(pixelSize,0)) # Up
newsurface.blit(maskSurface,pygame.Vector2(pixelSize,pixelSize*2)) # Down
newsurface.blit(inputSurface,pygame.Vector2(pixelSize,pixelSize))
return newsurface
def stripFromSheet(sheet, start, size, columns, rows=1, mode = 'list'):
"""
Strips individual frames from a sprite sheet given a start location,
sprite size, and number of columns and rows.
"""
if mode == 'list':
frames = []
for j in range(rows):
for i in range(columns):
location = (start[0]+size[0]*i, start[1]+size[1]*j)
frames.append(sheet.subsurface(pygame.Rect(location, size)))
elif mode == 'grid':
frames = []
for j in range(rows):
row = []
for i in range(columns):
location = (start[0]+size[0]*i, start[1]+size[1]*j)
row.append(sheet.subsurface(pygame.Rect(location, size)))
frames.append(row)
return frames
def getMouseVector2():
return pygame.Vector2(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
def rotateAtCenter(surface, angle):
rotated_image = pygame.transform.rotate(surface, angle)
new_rect = getCenterOffset(rotated_image)
return rotated_image, new_rect
def drawLabelBox(surface = pygame.surface.Surface, color = pygame.color.Color, text = str, size = int):
pygame.draw.rect(surface,color,surface.get_rect(),2)
font = pygame.font.SysFont(pygame.font.get_default_font(),size)
textSurface = font.render(text,False,color)
surface.blit(textSurface,pygame.Vector2(2,2))
#def vectorDistance(inp1,inp2):
# return pygame.Vector2(math.sqrt(inp1.x**2 - inp2.x**2),math.sqrt(inp1.y**2 - inp2.y**2))
def getHalfScreenVector2():
return pygame.Vector2(INFO.current_w/2,INFO.current_h/2)
def getScreenVector2():
return pygame.Vector2(INFO.current_w,INFO.current_h)
def alignPixelsToGrid(surface, pixelSize):
newsurface = surface.copy()
newsurface = pygame.transform.scale(newsurface,pygame.Vector2(surface.get_width()/pixelSize,surface.get_height()/pixelSize))
newsurface = pygame.transform.scale(newsurface,pygame.Vector2(surface.get_width(),surface.get_height()))
return newsurface
def replaceColor(surface, currentColor, newColor):
new_surface = surface.copy()
pixel_array = pygame.PixelArray(new_surface)
pixel_array.replace(currentColor, newColor)
del pixel_array
return new_surface
if __name__ == '__main__':
try:
import main
except ModuleNotFoundError:
raise Exception("No associated main.py file found")