repl.it
@noobAtProgram/

Final Project

Pygame

No description

fork
loading
Files
  • main.py
  • images
  • save_1
  • save_2
  • nohup.out
  • README.txt
  • The_Dungeon_Game_Beta.py
main.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#Texto

#Imports and initializations
import random, os

import pygame as pg
pg.init()

FPS = 12
#Setting constant values
length = 800
height = 600
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)
brown = (89, 61, 13)
dark_pink = (198, 51, 176)
dull = (112, 117, 96)
purple = (133, 26, 204)
black = (0, 0, 0)
orange = (255, 153, 0)
yellow = (255, 255, 102)


#Setting up the pygame window
gameDisplay = pg.display.set_mode((length, height))
pg.display.set_caption("The Dungeon Game")

#Button

"""
Tells the program whether a user has clicked within button space.
Button is not drawn. Drawing the button must be performed after a gameDisplay.fill().
Button function performed inside the for loop of the events.

args:
    msg - The message displayed on the button
    font - the font (must end in .ttf)
    font_size - size of the text on the button
    x - the x position of the button
    y - the y position of the button
    w - width of the button
    h - height of the button
    ic - inactive color
    ac - active color (when player hovers over the button)
    fc - Font color
    func - function that is run
    
    



"""

def button(msg,font,font_size,x,y,w,h,ic,ac,fc):
    mouse = pg.mouse.get_pos()

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pg.draw.rect(gameDisplay, ac,(x,y,w,h))
    else:
        pg.draw.rect(gameDisplay, ic,(x,y,w,h))

    smallText = pg.font.Font(font,font_size)
    textSurf = smallText.render(msg, True, fc)
    textRect = textSurf.get_rect()
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)
    return x+w > mouse[0] > x and y+h > mouse[1] > y

def rquit():
    try:
        quit()
    except:
        quit()

def cFolder(folder):
  try:
    if not os.path.exists(folder):
      os.makedirs(folder)
  except OSError:
    print ('Failed')

#Text and color functions
def text_objects(text, font):
  textSurface = font.render(text, True, black)
  return textSurface, textSurface.get_rect()
largeText = pg.font.Font('freesansbold.ttf', 60)
def title_disp(text):
    global largeText
    global length, height
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((length/2), (height/4))
    gameDisplay.blit(TextSurf, TextRect)

insText = pg.font.Font('freesansbold.ttf', 30)
def ins_disp(text):
    global insText
    global length, height
    TextSurf, TextRect = text_objects(text, insText)
    TextRect.center = ((length/2), (height/2))
    gameDisplay.blit(TextSurf, TextRect)

tutText = pg.font.Font('freesansbold.ttf', 12)
def tut_disp(text, x, y):
    global insText
    global length, height
    TextSurf, TextRect = text_objects(text, tutText)
    TextRect.center = ((x), (y))
    gameDisplay.blit(TextSurf, TextRect)
    
def world_disp(text, pos):
  wText = pg.font.Font('freesansbold.ttf', 20)
  TextSurf, TextRect = text_objects(text, wText)
  TextRect.center = pos
  gameDisplay.blit(TextSurf, TextRect)
  pg.display.update()

def zone_disp(thingx, thingy, color):
  pg.draw.rect(gameDisplay, color, [thingx, thingy, 20, 20])

wallImage = pg.image.load('images/wall.png').convert()
def wall_disp(x, y):
    gameDisplay.blit(wallImage, (x, y))

floorImage = pg.image.load('images/floor.png').convert()
def floor_disp(x, y):
  gameDisplay.blit(floorImage, (x, y))

crateImage = pg.image.load('images/crate.png').convert()
def crate_disp(x, y):
  gameDisplay.blit(crateImage, (x, y))

def player_disp(x, y, direction):
  gameDisplay.blit(playerDirectionSprite(direction), (x, y))

lockedImage = pg.image.load('images/locked_door.png').convert()
def lock_disp(x, y):
    gameDisplay.blit(lockedImage, (x, y))

uArrow = pg.image.load('images/up_arrow.png').convert()
rArrow = pg.image.load('images/right_arrow.png').convert()
lArrow = pg.image.load('images/left_arrow.png').convert()
dArrow = pg.image.load('images/down_arrow.png').convert()

def uDisp(x, y):
    gameDisplay.blit(uArrow, (x, y))
def rDisp(x, y):
    gameDisplay.blit(rArrow, (x, y))
def lDisp(x, y):
    gameDisplay.blit(lArrow, (x, y))
def dDisp(x, y):
    gameDisplay.blit(dArrow, (x, y))
                       

playerImgs = {
  "dMove": [pg.image.load('images/player/down_move.png').convert(), pg.image.load('images/player/down_move2.png').convert()],
  "lMove": [pg.image.load('images/player/left_move.png').convert(), pg.image.load('images/player/left_move2.png').convert()],
  "rMove": [pg.image.load('images/player/right_move.png').convert(), pg.image.load('images/player/right_move2.png').convert()],
  "uMove": [pg.image.load('images/player/up_move.png').convert(), pg.image.load('images/player/up_move2.png').convert()],
  "dStill": pg.image.load('images/player/down_still.png').convert(),
  "lStill": pg.image.load('images/player/left_still.png').convert(),
  "rStill": pg.image.load('images/player/right_still.png').convert(),
  "uStill": pg.image.load('images/player/up_still.png').convert()

}
def playerDirectionSprite(direction):
  direction = direction.split()
  if len(direction) == 1:
    return playerImgs[direction[0]+"Move"][0]
  else:
    return playerImgs[direction[0]+"Still"]

def player(lst, x, y):
  lst[y][x] = '0'

  
#running clock
clock = pg.time.Clock()
pQuit = False


#Functions for saving and starting area

#debug function
def hi():
    print('hi')

def menu():
    global beginning
    beginning = 'menu'

def tutorial():
    global beginning
    beginning = 'tutorial'

def game():
    global beginning
    beginning = 'game'

def load():
    global beginning
    beginning = 'load_save_file'

def new():
    global beginning
    beginning = 'new_save_file'

def hub():
    global beginning
    beginning = 'hub'

def start():
    global beginning
    beginning = 'start'


save = 'save_1'
    
def oFile(filename):
  global save
  zone_c = open(save + '/zones/' + filename, 'r')
  zone_c_txt = zone_c.read()
  zone_c.close()
  lvl_c_lst = zone_c_txt.split('\n')[2:]
  lvl_c = ''
  lvl_c_obj = []
  for c in lvl_c_lst:
    lvl_c += c + '\n'
    lvl_c_obj.append(list(c))
  return lvl_c_obj

def wZoneFile(loc_name, save_file, lst):
  body = ''
  body += loc_name + '\n\n'
  for i in lst:
    for l in i:
      body += l
    body += '\n'
  with open(save_file + '/zones/' + loc_name, 'w') as f:
    f.write(body)

#Saving tile under player so when player moves off nothing happens
#setting original zone/change/position
lvl_obj = oFile('zone_A.txt')
prev_pos = (7, 12) # players y, then players x
prev_obj = 'X'
player(lvl_obj, 12, 7)

x_change = 0
y_change = 0
zone = 'a'
gameDisplay.fill(black)
beginning = 'start'
player_anim = False
pdirection = "d still"

#Pygame event/display loop
while not pQuit:


  #event loop
    #If the game is running
    if beginning == 'game':
      for event in pg.event.get():
          if event.type == pg.QUIT:
              pQuit = True
              break
          if event.type == pg.KEYDOWN:
            if event.key == pg.K_RIGHT or event.key == ord('d'):
              x_change = 1
              pdirection = 'r'
            if event.key == pg.K_LEFT or event.key == ord('a'):
              x_change = -1
              pdirection = 'l'
            if event.key == pg.K_DOWN or event.key == ord('s'):
              y_change = 1
              pdirection = 'd'
            if event.key == pg.K_UP or event.key == ord('w'):
              y_change = -1
              pdirection = 'u'
            if event.key == pg.K_LSHIFT:
              FPS = 60
          if event.type == pg.KEYUP:
            if event.key == pg.K_RIGHT or event.key == pg.K_LEFT or event.key == ord('a') or event.key == ord('d'):
              x_change = 0
            if event.key == pg.K_DOWN or event.key == pg.K_UP or event.key == ord('w') or event.key == ord('s'):
              y_change = 0
            if event.key == pg.K_LSHIFT:
              FPS = 30
              
                  






     #display updates
      
      #Setting up where player is
      if prev_pos[1] + x_change < 25 and prev_pos[1] + x_change >= 0:
        try:
          if lvl_obj[prev_pos[0]][prev_pos[1] + x_change] != '#' and lvl_obj[prev_pos[0]][prev_pos[1] + x_change] != 'P' and lvl_obj[prev_pos[0]][prev_pos[1] + x_change] != 'L':
            lvl_obj[prev_pos[0]][prev_pos[1]] = prev_obj
            prev_obj = lvl_obj[prev_pos[0]][prev_pos[1] + x_change]
            prev_pos = (prev_pos[0], prev_pos[1] + x_change)
            player(lvl_obj, prev_pos[1], prev_pos[0])
          if lvl_obj[prev_pos[0]][prev_pos[1] + x_change] == 'P':
            if x_change > 0:
              try:
                if lvl_obj[prev_pos[0]][prev_pos[1] + x_change + 1] == 'X':
                  lvl_obj[prev_pos[0]][prev_pos[1] + x_change + 1] = 'P'
                  lvl_obj[prev_pos[0]][prev_pos[1] + x_change] = 'X'
                  lvl_obj[prev_pos[0]][prev_pos[1]] = prev_obj
                  prev_pos = (prev_pos[0], prev_pos[1] + x_change)
                  player(lvl_obj, prev_pos[1], prev_pos[0])
              except IndexError:
                pass
            if x_change < 0:
              try:
                if lvl_obj[prev_pos[0]][prev_pos[1] + x_change - 1] == 'X':
                  lvl_obj[prev_pos[0]][prev_pos[1] + x_change - 1] = 'P'
                  lvl_obj[prev_pos[0]][prev_pos[1] + x_change] = 'X'
                  lvl_obj[prev_pos[0]][prev_pos[1]] = prev_obj
                  prev_pos = (prev_pos[0], prev_pos[1] + x_change)
                  player(lvl_obj, prev_pos[1], prev_pos[0])
              except IndexError:
                pass
        except IndexError:
          pass
      if prev_pos[0] + y_change < 14 and prev_pos[0] + y_change >= 0:
        try:
          if lvl_obj[prev_pos[0] + y_change][prev_pos[1]] != '#' and lvl_obj[prev_pos[0] + y_change][prev_pos[1]] != 'P' and lvl_obj[prev_pos[0]][prev_pos[1] + x_change] != 'L':
            lvl_obj[prev_pos[0]][prev_pos[1]] = prev_obj
            prev_obj = lvl_obj[prev_pos[0] + y_change][prev_pos[1]]
            prev_pos = (prev_pos[0] + y_change, prev_pos[1])
            player(lvl_obj, prev_pos[1], prev_pos[0])
          if lvl_obj[prev_pos[0] + y_change][prev_pos[1]] == 'P':
            if y_change > 0:
              try:
                if lvl_obj[prev_pos[0] + y_change + 1][prev_pos[1]] == 'X':
                  lvl_obj[prev_pos[0] + y_change + 1][prev_pos[1]] = 'P'
                  lvl_obj[prev_pos[0] + y_change][prev_pos[1]] = 'X'
                  lvl_obj[prev_pos[0]][prev_pos[1]] = prev_obj
                  prev_pos = (prev_pos[0] + y_change, prev_pos[1])
                  player(lvl_obj, prev_pos[1], prev_pos[0])
              except IndexError:
                pass
            if y_change < 0:
              try:
                if lvl_obj[prev_pos[0] + y_change - 1][prev_pos[1]] == 'X':
                  lvl_obj[prev_pos[0] + y_change - 1][prev_pos[1]] = 'P'
                  lvl_obj[prev_pos[0] + y_change][prev_pos[1]] = 'X'
                  lvl_obj[prev_pos[0]][prev_pos[1]] = prev_obj
                  prev_pos = (prev_pos[0] + y_change, prev_pos[1])
                  player(lvl_obj, prev_pos[1], prev_pos[0])
              except IndexError:
                pass
        except IndexError:
          pass

          
          
          
      #Displaying zone a
      for linea in range(len(lvl_obj)):
        for obja in range(len(lvl_obj[0])):
          tile = lvl_obj[linea][obja]
          posX = length/2 - (20 * (12 - obja))
          posY = height/2 - (20 * (7 - linea))
          if tile == '#':
            wall_disp(posX, posY)
          elif tile == 'P':
            crate_disp(posX, posY)
          elif tile == '0':
            player_disp(posX, posY, pdirection)
          elif tile == 'X':
            floor_disp(posX, posY)
          elif tile == 'L':
            lock_disp(posX, posY)
          elif tile == '^':
            uDisp(posX, posY)
          elif tile == '>':
            rDisp(posX, posY)
          elif tile == 'v':
            dDisp(posX, posY)
          elif tile == '<':
            lDisp(posX, posY)
          else:
            floor_disp(posX, posY)

      if len(pdirection) == 1:
        pdirection += " still"
    
        
      if prev_obj != '#' and prev_obj != 'P' and prev_obj != 'X' and prev_obj != '^' and prev_obj != '>' and prev_obj != '<' and prev_obj != '>' and prev_obj != 'L':

        gameDisplay.fill(black)
        zone = prev_obj
        lvl_obj[prev_pos[0]][prev_pos[1]] = prev_obj
        lvl_obj = oFile('zone_' + zone + '.txt')
        if prev_pos[0] == 0:
          prev_pos = (9, prev_pos[1])
        elif prev_pos[0] == 10:
          prev_pos = (1, prev_pos[1])
        elif prev_pos[1] == 0:
          prev_pos = (prev_pos[0], 23)
        elif prev_pos[1] == 24:
          prev_pos = (prev_pos[0], 1)
        else:
          prev_pos = (7, 12)         # players y, then players x
        prev_obj = lvl_obj[prev_pos[0]][prev_pos[1]]
        player(lvl_obj, prev_pos[1], prev_pos[0])
        x_change = 0
        y_change = 0
        pg.display.update()
          
      
      pg.display.update()
      clock.tick(FPS)
      gameDisplay.fill(black)




    #If they are in the hub
    elif beginning == 'hub':
      pass




    #If they are looking at the tutorial
    elif beginning == 'tutorial':
      for event in pg.event.get():
        if event.type == pg.MOUSEBUTTONUP:
            if event.button == 1:
                if b3 == True:
                    start()
        elif event.type == pg.QUIT:
            rquit()
      gameDisplay.fill(white)
      b3 = button('Back', 'freesansbold.ttf', 14, length/16 - 40, height/16, 80, 40, orange, yellow, black)
      tut_disp('Move around the world with WASD or arrow keys.', length/2, height/5)              #Change this to become the tutorial
      tut_disp('You must solve the puzzles involving crates and electric conductors.', length/2, 2*height/5)
      tut_disp('Rely on your own intuition.', length/2, 3*height/5)
      tut_disp('Have fun!', length/2, 4*height/5)
      pg.display.update()



    #If they are at the new/load menu
    elif beginning == 'menu':
      for event in pg.event.get():
        if event.type == pg.MOUSEBUTTONUP:
            if event.button == 1:
                if b4 == True:
                    start()
                elif b5 == True:
                    load()
                elif b6 == True:
                    new()
                elif b7 == True:
                    game()
        elif event.type == pg.QUIT:
            rquit()
      gameDisplay.fill(white)
      title_disp('The Dungeon Game')
      b4 = button('Back', 'freesansbold.ttf', 14, length/16 - 40, height/16, 80, 40, orange, yellow, black)
      b5 = button('New Save File', 'freesansbold.ttf', 14, length/2 - 40, 3*height/8, 120, 40, orange, yellow, black)
      b6 = button('Load Save File', 'freesansbold.ttf', 14, length/2 - 40, height/2, 120, 40, orange, yellow, black)
      b7 = button('Play', 'freesansbold.ttf', 14, length/2 - 40, 5*height/8, 120, 40, orange, yellow, black)        #Remove this once save files are ready

      pg.display.update()





    #When creating a new save file
    elif beginning == 'new_save_file':
      for event in pg.event.get():
        if event.type == pg.MOUSEBUTTONUP:
            if event.button == 1:
                if b8 == True:
                    start()
        elif event.type == pg.QUIT:
            rquit()
      gameDisplay.fill(white)
      b8 = button('Back', 'freesansbold.ttf', 14, length/16 - 40, height/16, 80, 40, orange, yellow, black)                 #Change everything. This is just stand in to show "Under Construction"
      
      ins_disp('Under Construction. Sorry for the inconvenience! ')
      pg.display.update()




    #When loading a new save file
    elif beginning == 'load_save_file':
      for event in pg.event.get():
        if event.type == pg.MOUSEBUTTONUP:
            if event.button == 1:
                if b9 == True:
                    start()
        elif event.type == pg.QUIT:
            rquit()
      gameDisplay.fill(white)
      b9 = button('Back', 'freesansbold.ttf', 14, length/16 - 40, height/16, 80, 40, orange, yellow, black)                 #Change everything. This is just stand in to show "Under Construction"
      ins_disp('Under Construction. Sorry for the inconvenience! ')
      pg.display.update()




    #at the beginning
    elif beginning == 'start':
      for event in pg.event.get():
        if event.type == pg.MOUSEBUTTONUP:
            if event.button == 1:
                if b1 == True:
                    menu()
                elif b2 == True:
                    tutorial()
        elif event.type == pg.QUIT:
            rquit()
      gameDisplay.fill(white)
      title_disp('The Dungeon Game')           #Change the title!
      b1 = button('Start', 'freesansbold.ttf', 14, length/2 - 40, 3*height/8, 80, 40, orange, yellow, black)
      b2 = button('Help', 'freesansbold.ttf', 14, length/2 - 40, height/2, 80, 40, orange, yellow, black)
      pg.display.update()

    #Do we really need an else?
    else:
      pass
	

pg.quit()
rquit()



  
Fetching token
?