Advertisement
shh_algo_PY

Arcade Game - Final

Dec 3rd, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.79 KB | None | 0 0
  1. from random import randint
  2. from pygame import*
  3.  
  4. # ⭐ (1) Add font - WIN, LOSE
  5.  
  6. font.init()
  7. font = font.Font(None, 72) #font.SysFont("Helvetica", 80)
  8.  
  9. win_width = 800
  10. win_height = 600
  11.  
  12. # ⭐ (2) Add screen boundaries
  13.  
  14. left_bound = win_width / 40            
  15. right_bound = win_width - 8 * left_bound
  16.  
  17. # ⭐ (3) Initial shift = 0
  18.  
  19. shift = 0
  20.  
  21. x_start, y_start = 20, 10
  22.  
  23. # ⭐ (4) Add in your target character
  24.  
  25. img_file_back = 'cave.png'
  26. img_file_hero = 'm1.png'
  27. img_file_enemy = 'enemy.png'
  28. img_file_princess = 'princess.png'
  29.  
  30. # ⭐ (5) Update colour library
  31.  
  32. C_GREEN = (32, 128, 32)     # WALL COLOUR
  33. C_RED = (255, 0, 0)         # TEXT COLOUR
  34. C_BLACK = (0, 0, 0)         # BACKGROUND COLOUR
  35.  
  36. class Hero(sprite.Sprite):
  37.     def __init__(self, filename, x_speed=0, y_speed=0, x=x_start, y=y_start, width=120, height=120):
  38.         sprite.Sprite.__init__(self)
  39.        
  40.         self.image = transform.scale(image.load(filename), (width, height)).convert_alpha()
  41.    
  42.         self.rect = self.image.get_rect()
  43.        
  44.         self.rect.x = x
  45.         self.rect.y = y
  46.        
  47.         self.x_speed = x_speed
  48.         self.y_speed = y_speed
  49.    
  50.     def gravitate(self):
  51.         self.y_speed += 0.25
  52.    
  53.     def jump(self, y):
  54.         if self.stands_on:
  55.             self.y_speed = y
  56.    
  57.     def update(self):
  58.         self.rect.x += self.x_speed
  59.  
  60.         # Touching check: Platforms
  61.         # Just checking, they don't disappear!
  62.         platforms_touched = sprite.spritecollide(self, barriers, False)
  63.  
  64.         # Processing movement: R
  65.         if self.x_speed > 0:
  66.             for p in platforms_touched:
  67.                 self.rect.right = min(self.rect.right, p.rect.left)
  68.        
  69.         # Processing movement: L
  70.         elif self.x_speed < 0:
  71.             for p in platforms_touched:
  72.                 self.rect.left = max(self.rect.left, p.rect.right)
  73.        
  74.         self.gravitate()
  75.         self.rect.y += self.y_speed
  76.  
  77.         platforms_touched = sprite.spritecollide(self, barriers, False)
  78.        
  79.         if self.y_speed > 0:
  80.             for p in platforms_touched:
  81.                 self.y_speed = 0
  82.                 if p.rect.top < self.rect.bottom:
  83.                     self.rect.bottom = p.rect.top
  84.                     self.stands_on = p
  85.        
  86.         elif self.y_speed < 0:
  87.             self.stands_on = False
  88.             for p in platforms_touched:
  89.                 self.y_speed = 0  
  90.                 self.rect.top = max(self.rect.top, p.rect.bottom)
  91.  
  92. class Wall(sprite.Sprite):
  93.     def __init__(self, x=20, y=0, width=120, height=120, color=C_GREEN):
  94.         sprite.Sprite.__init__(self)
  95.         self.image = Surface([width, height])
  96.         self.image.fill(color)
  97.    
  98.         self.rect = self.image.get_rect()
  99.         self.rect.x = x
  100.         self.rect.y = y
  101.  
  102. class Enemy(sprite.Sprite):
  103.     def __init__(self, x=20, y=0, filename=img_file_enemy, width=100, height=100):
  104.         sprite.Sprite.__init__(self)
  105.    
  106.         self.image = transform.scale(image.load(filename), (width, height)).convert_alpha()
  107.         self.rect = self.image.get_rect()
  108.         self.rect.x = x
  109.         self.rect.y = y
  110.    
  111.     # NEW
  112.     def update(self):
  113.         self.rect.x += randint(-5, 5)
  114.         self.rect.y += randint(-5, 5)
  115.  
  116. # ⭐ (6) New class: Final Sprite
  117.  
  118. class FinalSprite(sprite.Sprite):
  119.     def __init__(self, player_image, player_x, player_y, player_speed):
  120.         sprite.Sprite.__init__(self)
  121.  
  122.         self.image = transform.scale(image.load(player_image), (60, 120))
  123.        
  124.         self.speed = player_speed
  125.    
  126.         self.rect = self.image.get_rect()
  127.         self.rect.x = player_x
  128.         self.rect.y = player_y
  129.  
  130. display.set_caption("ARCADE")
  131. window = display.set_mode([win_width, win_height])
  132.  
  133. back = transform.scale(image.load(img_file_back).convert(), (win_width, win_height))
  134.  
  135. all_sprites = sprite.Group()
  136. barriers = sprite.Group()
  137. enemies = sprite.Group()
  138.  
  139. player = Hero(img_file_hero)
  140. all_sprites.add(player)
  141.  
  142. w1 = Wall(50, 150, 480, 20)
  143. barriers.add(w1)
  144. all_sprites.add(w1)
  145.  
  146. #w2 = Wall(700, 50, 50, 360)
  147. #barriers.add(w2)
  148. #all_sprites.add(w2)
  149.  
  150. w3 = Wall(350, 380, 640, 20)
  151. barriers.add(w3)
  152. all_sprites.add(w3)
  153.  
  154. w4 = Wall(-200, 590, 1600, 20)
  155. barriers.add(w4)
  156. all_sprites.add(w4)
  157.  
  158. e1 = Enemy(50, 480)
  159. all_sprites.add(e1)
  160. enemies.add(e1)
  161.  
  162. e2 = Enemy(400, 480)
  163. all_sprites.add(e2)
  164. enemies.add(e2)
  165.  
  166. # ⭐ (7) Create the Final Sprite
  167.  
  168. pr = FinalSprite(img_file_princess, win_width + 500, win_height - 150, 0)
  169. all_sprites.add(pr)
  170.  
  171. run = True
  172. finished = False
  173.  
  174. while run:
  175.  
  176.     for e in event.get():
  177.        
  178.         if e.type == QUIT:
  179.             run = False
  180.        
  181.         elif e.type == KEYDOWN:
  182.             if e.key == K_LEFT:
  183.                 player.x_speed = -5
  184.             elif e.key == K_RIGHT:
  185.                 player.x_speed = 5
  186.             elif e.key == K_UP:
  187.                 player.jump(-7)
  188.        
  189.         elif e.type == KEYUP:
  190.             if e.key == K_LEFT:
  191.                 player.x_speed = 0
  192.             elif e.key == K_RIGHT:
  193.                 player.x_speed = 0
  194.    
  195.     if not finished:
  196.         # window.blit(back, (0,0))
  197.         # We're replacing this later to make the background shift
  198.  
  199.         all_sprites.update()
  200.        
  201.         # all_sprites.draw(window)
  202.         # We're moving this down, so that it's always on top!
  203.  
  204.         # (8) Collision checks
  205.  
  206.         if sprite.spritecollide(player, enemies, False):
  207.             player.kill()
  208.        
  209.         if (
  210.             # The player hasn't gone beyond the boundaries and moving
  211.             player.rect.x > right_bound and player.x_speed > 0
  212.             or
  213.             player.rect.x < left_bound and player.x_speed < 0
  214.         ):
  215.             # We will shift the screen by the player's movement
  216.             shift -= player.x_speed
  217.  
  218.             # We'll also shift all the sprites!
  219.             for s in all_sprites:
  220.                 s.rect.x -= player.x_speed
  221.        
  222.         # Background shift time!
  223.  
  224.         local_shift = shift % win_width
  225.         window.blit(back, (local_shift, 0))
  226.        
  227.         if local_shift != 0:
  228.             window.blit(back, (local_shift - win_width, 0))
  229.  
  230.         # Drawing sprites moved here!
  231.  
  232.         all_sprites.draw(window)
  233.  
  234.         # ⭐ (9) Winning check
  235.  
  236.         if sprite.collide_rect(player, pr):
  237.             finished = True
  238.             window.fill(C_BLACK)
  239.             text = font.render("YOU WIN!", 1, C_RED)
  240.             window.blit(text, (250, 250))
  241.  
  242.         # ⭐ (10) Losing check
  243.  
  244.         if player not in all_sprites or player.rect.top > win_height:
  245.             finished = True          
  246.             window.fill(C_BLACK)
  247.             text = font.render("GAME OVER", 1, C_RED)
  248.             window.blit(text, (250, 250))
  249.  
  250.     display.update()
  251.     time.delay(20)
  252.  
  253.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement