Advertisement
shh_algo_PY

Maze Game - With Walls

Jun 18th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. from pygame import *
  2.  
  3. class GameSprite(sprite.Sprite):
  4.    #class constructor
  5.     def __init__(self, player_image, player_x, player_y, player_speed):
  6.         super().__init__()
  7.         # each sprite must store an image property
  8.         self.image = transform.scale(image.load(player_image), (55, 55))
  9.         self.speed = player_speed
  10.         # each sprite must store the rect property it is inscribed in
  11.         self.rect = self.image.get_rect()
  12.         self.rect.x = player_x
  13.         self.rect.y = player_y
  14.  
  15.     def reset(self):
  16.         window.blit(self.image, (self.rect.x, self.rect.y))
  17.  
  18. #derived class for the player sprite (controlled by arrows)
  19. class Player(GameSprite):
  20.     def update(self):
  21.         keys = key.get_pressed()
  22.         if keys[K_LEFT] and self.rect.x > 5:
  23.             self.rect.x -= self.speed
  24.         if keys[K_RIGHT] and self.rect.x < win_width - 80:
  25.             self.rect.x += self.speed
  26.         if keys[K_UP] and self.rect.y > 5:
  27.             self.rect.y -= self.speed
  28.         if keys[K_DOWN] and self.rect.y < win_height - 80:
  29.             self.rect.y += self.speed
  30.  
  31. #derived class for the enemy sprite (moves itself)
  32. class Enemy(GameSprite):
  33.     def update(self):
  34.         if self.rect.x <= 470:
  35.             self.side = "right"
  36.         if self.rect.x >= win_width - 85:
  37.             self.side = "left"
  38.         if self.side == "left":
  39.             self.rect.x -= self.speed
  40.         else:
  41.             self.rect.x += self.speed
  42.  
  43. #class for obstacle sprites
  44. class Wall(sprite.Sprite):
  45.     def __init__(self, color_1, color_2, color_3, wall_x, wall_y, wall_width, wall_height):
  46.         super().__init__()
  47.         self.color_1 = color_1
  48.         self.color_2 = color_2
  49.         self.color_3 = color_3
  50.         self.width = wall_width
  51.         self.height = wall_height
  52.         # picture of the wall — creates a rectangle of the desired size and color
  53.         self.image = Surface((self.width, self.height))
  54.         self.image.fill((color_1, color_2, color_3))
  55.         # each sprite must store a rect property
  56.         self.rect = self.image.get_rect()
  57.         self.rect.x = wall_x
  58.         self.rect.y = wall_y
  59.  
  60.     def draw_wall(self):
  61.         window.blit(self.image, (self.rect.x, self.rect.y))
  62.        
  63. #Game stage:
  64. win_width = 700
  65. win_height = 500
  66.  
  67. window = display.set_mode((win_width, win_height))
  68. display.set_caption("Maze")
  69. background = transform.scale(image.load("background.jpg"), (win_width, win_height))
  70.  
  71. #Game characters:
  72. pacman = Player('hero.png', 5, win_height - 80, 4)
  73. monster = Enemy('cyborg.png', win_width - 80, 280, 2)
  74. final = GameSprite('treasure.png', win_width - 120, win_height - 80, 0)
  75.  
  76. # wall_name = Wall(R, G, B, x_cor, y_cor, width, height)
  77. w1 = Wall(154, 205, 50, 100, 20 , 450, 10) # Horizontal wall
  78. w2 = Wall(154, 205, 50, 100, 480, 350, 10)
  79. w3 = Wall(154, 205, 50, 100, 20 , 10, 380) # Vertical wall
  80.  
  81. game = True
  82. finish = False
  83. clock = time.Clock()
  84. FPS = 60
  85.  
  86. '''Add our font + text for win/lose'''
  87. font.init()
  88. font = font.SysFont('comicsansms', 70)
  89. win = font.render('YOU WIN!', True, (255, 215, 0))
  90. lose = font.render('YOU LOSE!', True, (180, 0, 0))
  91.  
  92. #music
  93. mixer.init()
  94. mixer.music.load('jungles.ogg')
  95. mixer.music.play()
  96.  
  97. '''Adding specific sounds for win/lose'''
  98. money = mixer.Sound('money.ogg')
  99. kick = mixer.Sound('kick.ogg')
  100.  
  101. while game:
  102.     for e in event.get():
  103.         if e.type == QUIT:
  104.             game = False
  105.  
  106.     if finish != True:
  107.         window.blit(background,(0, 0))
  108.         pacman.update()
  109.         monster.update()
  110.      
  111.         pacman.reset()
  112.         monster.reset()
  113.         final.reset()
  114.  
  115.         w1.draw_wall()
  116.         w2.draw_wall()
  117.         w3.draw_wall()
  118.  
  119.         #“Losing” situation
  120.         if sprite.collide_rect(pacman, monster) or sprite.collide_rect(pacman, w1) or sprite.collide_rect(pacman, w2) or sprite.collide_rect(pacman, w3):
  121.             finish = True
  122.             window.blit(lose, (200, 200))
  123.             kick.play()
  124.  
  125.         #“Winning” situation
  126.         if sprite.collide_rect(pacman, final):
  127.             finish = True
  128.             window.blit(win, (200, 200))
  129.             money.play()
  130.  
  131.     display.update()
  132.     clock.tick(FPS)
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement