-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
330 lines (255 loc) · 11.2 KB
/
game.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import pygame, player, os, sys, fileinput, random #@UnusedImport #@UnresolvedImport
#Setting Version Number
global version_number
version_number = "0.0.9"
class tile(pygame.sprite.Sprite):
"""
Takes and loads a sprite from a spritesheet
"""
def __init__(self, sheetx, sheety, width, height, blitx, blity, sheet):
#Calling the parent (Sprite) class constructor
pygame.sprite.Sprite.__init__(self)
"""
Sheetx/y are the coords on the spritesheet
width/height are the height of the spritesheet cells
blitx/y are where the image will be put on screen
"""
self.sheetx = sheetx
self.sheety = sheety
self.width = width
self.height = height
self.blitx = blitx
self.blity = blity
self.sheet = sheet
#Defining which spritesheet will be used (for now)
tile_sheet = pygame.image.load('assets/spritesheets/' + str(self.sheet)+ ".png")
#Getting specific image
tile_sheet.set_clip(pygame.Rect(self.sheetx, self.sheety,
self.width, self.height))
#Setting sprite to that image
self.image = tile_sheet.subsurface(tile_sheet.get_clip())
#Getting the size of the image
self.rect = self.image.get_rect()
#Chooseing where to blit the image
self.rect.x = blitx
self.rect.y = blity
def loadTiles(self):
"""Assigns sprites to go with each character from the level.dat files"""
x, y = self.blitx, self.blity
for line in fileinput.input(os.path.join\
("assets", "levels", "level" + str(currentLevel) + ".dat")): #@UndefinedVariable
for currentTile in line:
if currentTile == "1":
newTile = tile(253, 253, self.width, self.height, x, y, "tileSheet")
level_tiles.add(newTile)
elif currentTile == "4":
newTile = tile(26, 0, self.width, self.height, x, y, "tileSheet")
level_tiles.add(newTile)
elif currentTile == "3":
newTile = tile(26, 0, self.width, self.height, x, y, "tileSheet")
level_tiles.add(newTile)
elif currentTile == "*":
newTile = tile(26, 0, self.width, self.height, x, y, "tileSheet")
level_walls.add(newTile)
x += 22 #Edit this x
x = 0 #Don't touch this x; used to reset tiles to next line/row
y += 22 #And this Y to change space width and height between tiles respectively
def controls():
player_x = "x = " + str(player.rect.x) #@UndefinedVariable
player_y = "y = " + str(player.rect.y) #@UndefinedVariable
for event in pygame.event.get():
if event.type == pygame.QUIT:
"""If the red 'x' is hit, end for loop and close pygame_cal"""
pygame.quit()
sys.exit()
#Next two sections of events handle player movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-5,0) #@UndefinedVariable
if event.key == pygame.K_RIGHT:
player.changespeed(5,0) #@UndefinedVariable
if event.key == pygame.K_UP:
player.changespeed(0,-5) #@UndefinedVariable
if event.key == pygame.K_DOWN:
player.changespeed(0,5) #@UndefinedVariable
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(5,0) #@UndefinedVariable
if event.key == pygame.K_RIGHT:
player.changespeed(-5,0) #@UndefinedVariable
if event.key == pygame.K_UP:
player.changespeed(0,5) #@UndefinedVariable
if event.key == pygame.K_DOWN:
player.changespeed(0,-5) #@UndefinedVariable
if event.key == pygame.K_a:
print (player_x, player_y)
#Setting currentLevel for "level#.dat" # is replaced with the value here
global currentLevel
currentLevel = 1
#Sets a group (or list) of sprites
level_tiles = pygame.sprite.Group()
level_walls = pygame.sprite.Group()
player_sprite = pygame.sprite.Group()
#Creating the player sprite and adding it to its own group
player = player.Player(24,24)
player_sprite.add(player)
#Creating clock to keep track of ticks and (later) fps
clock = pygame.time.Clock()
def main():
"""Main Menu"""
#Keeps track of the current level
global currentLevel
global version_number
#Setting the width and height of the screen before creating the
#screen surface
SCREEN_X = 922
SCREEN_Y = 700
SIZE = (SCREEN_X, SCREEN_Y)
screen = pygame.display.set_mode(SIZE)
#Setting the text to be displayed at the top of the pygame_cal window
pygame.display.set_caption("Main Menu - " + version_number)
backgroundnum = random.randint(1, 1)
# Loading and position graphics
#Background-Related
background_image = pygame.image.load("assets/images/background" +
str(backgroundnum) + ".jpg").convert()
background_position = [0,0]
#Button-Related
button_image = pygame.image.load("assets/images/button.png")
button_size = 225
extra_space = 80 #Leaves extra space at the top for main menu/welcome/whatever
button_one_position = [SCREEN_X/4 - button_size/4, SCREEN_Y/4
+ button_size/4 + extra_space]
button_two_position = [SCREEN_X/4 * 2 + button_size/4, SCREEN_Y/4
+ button_size/4 + extra_space]
button_three_position = [SCREEN_X/4 - button_size /4, SCREEN_Y/4
+ button_size + extra_space]
button_four_position = [SCREEN_X/4 * 2 + button_size/4, SCREEN_Y/4
+ button_size + extra_space]
done = False
while not done:
#Retrieves the coordinates of the mouse pointer/cursor
pos = pygame.mouse.get_pos()
x = pos[0]
y = pos[1]
for event in pygame.event.get():
if event.type == pygame.QUIT:
"""If the red 'x' is hit, end for loop and close pygame_cal"""
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
#For now clicking gives you the mouse coordinates
print ("Mouse clicked")
print ("x coord: " + str(x))
print ("y coord: " + str(y))
print (" ")
if x >= 176 and x <= 398 and y >= 312 and y <= 370:
levelOne()
pygame.quit()
#Blitting images onto screen
screen.blit(background_image, background_position)
screen.blit(button_image, button_one_position)
screen.blit(button_image, button_two_position)
screen.blit(button_image, button_three_position)
screen.blit(button_image, button_four_position)
#Updates/applies changes the game window/screen surface
pygame.display.flip()
#Setting FPS for the games
clock.tick(60)
def levelOne():
#Keeps track of current Level
global currentLevel
#Defining the color to be used "behind" the tiles
BACKGROUNDCOLOR = (255,255,255)
#Creating object to test tiles
tileTest = tile(0, 0, 23 , 23, 0, 0, "tileSheet")
tileTest.loadTiles() #Loading information for tiles/walls
#Setting the width and height of the the current level
SCREEN_X = 500
SCREEN_Y = 500
SIZE = (SCREEN_X, SCREEN_Y)
screen = pygame.display.set_mode(SIZE)
#Setting the text to be displayed at the top of the pygame_cal window
pygame.display.set_caption("Level One Title")
done = False
while not done:
controls()
#Setting the background of the screen (as white)
screen.fill(BACKGROUNDCOLOR)
#Setting which tiles will be the player's walls
player.move(level_walls)
"""Actually drawing the walls and tiles on-screen.
Unlike the player, they do not need to be updated because
they are static and do not change throught the entirety of
a level """
level_tiles.draw(screen)
level_walls.draw(screen)
#Drawing and updating the player
player_sprite.draw(screen)
player_sprite.update() #Update is required to "move"
#Updates/applies changes the game window/screen surface
pygame.display.flip()
#Victory conditions for level
if player.rect.x >= 399 and player.rect.y >= 169:
print ("yay")
#Empty all levels/tile groups from this level
#Then remove them from the screen/game surface
level_tiles.empty()
level_tiles.remove()
level_walls.empty()
level_walls.remove()
pygame.display.flip() #Update change
#Sets the next level
currentLevel = 2
#Loads the next level and quits/exits current one
levelTwo()
pygame.quit()
#Setting FPS for the games
clock.tick(60)
def levelTwo():
global currentLevel
#Defining the color white to be used later
BACKGROUNDCOLOR = (0, 0, 255)
player.rect.x = 24
player.rect.y = 24
#Creating object to test tiles
print (currentLevel)
tileTest = tile(0, 0, 23 , 23, 0, 0, "tileSheet")
tileTest.loadTiles() #Loading information for tiles/walls
#Setting the width and height of the screen before creating the
#screen surface
SCREEN_X = 500
SCREEN_Y = 500
SIZE = (SCREEN_X, SCREEN_Y)
screen = pygame.display.set_mode(SIZE)
#Setting the text to be displayed at the top of the pygame_cal window
pygame.display.set_caption("Level Two Title")
done = False
while not done:
controls()
screen.fill(BACKGROUNDCOLOR)
player.move(level_walls)
level_tiles.draw(screen)
level_walls.draw(screen)
player_sprite.draw(screen)
player_sprite.update()
pygame.display.flip()
#Victory conditions for level
if player.rect.x >= 399 and player.rect.y >= 169:
print ("yay")
#Empty all levels/tile groups from this level
#Then remove them from the screen/game surface
level_tiles.empty()
level_tiles.remove()
level_walls.empty()
level_walls.remove()
pygame.display.flip() #Update change
#Sets the next level
currentLevel = 3
#Loads the next level and quits/exits current one
levelThree()
pygame.quit()
clock.tick(60)
# Call the main function, start up the game
if __name__ == "__main__":
main()