Maluia

level 2

Sep 7th, 2024 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.41 KB | Gaming | 0 0
  1. import pygame
  2. import pygame.freetype
  3. import time
  4. from Traps import *
  5. from os.path import join
  6. from Traps import Blade
  7. pygame.freetype.init()
  8.  
  9. pygame.display.set_caption("Platformer")
  10. icon = pygame.image.load(join("assets", "Icons", "MaskDude_Icon.png"))
  11. pygame.display.set_icon(icon)
  12.  
  13. FONT = pygame.freetype.Font("assets\Menu\Text\pixeloid-font\PixeloidMono-d94EV.ttf", 20)
  14.  
  15. WIDTH, HEIGHT = 1000, 800
  16. FPS = 60
  17. PLAYER_VEL = 5
  18.  
  19. window = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
  20.  
  21. from playerclass import Player
  22. from objectclass import Object
  23. from flagclass import Flag
  24. from functions import get_purpleGrass_block, get_background, handle_move
  25.  
  26. class Block(Object):
  27.     def __init__(self, x, y, size):
  28.         super().__init__(x, y, size, size)
  29.         block = get_purpleGrass_block(size)
  30.         self.image.blit(block, (0, 0))
  31.         self.mask = pygame.mask.from_surface(self.image)
  32.  
  33. def draw(window, background, bg_image, player, objects, offset, elapsed_time):
  34.     for tile in background:
  35.         window.blit(bg_image, tile)
  36.  
  37.     for obj in objects:
  38.         obj.draw(window, offset)
  39.  
  40.     time_text = FONT.render(f"Time: {round(elapsed_time)}s", "black")
  41.     window.blit(time_text[0], (10, 60))
  42.    
  43.     player.draw(window, offset)
  44.    
  45.     if player.healthbar.health <= 0:
  46.         lost_text = FONT.render("You Lost!", "black")
  47.         quit_text = FONT.render("press escape to quit", "black")
  48.         restart_text = FONT.render("press R to restart", "black")    
  49.         window.blit(lost_text[0], (WIDTH/2 - lost_text[0].get_width()/2, HEIGHT/2 - lost_text[0].get_height()/2))
  50.         window.blit(quit_text[0], (WIDTH/2.35 - quit_text[0].get_width()/4, HEIGHT/1.85 - quit_text[0].get_height()/1.85))
  51.         window.blit(restart_text[0], (WIDTH/2.25 - restart_text[0].get_width()/3.5, HEIGHT/1.7 - restart_text[0].get_height()/1.7))
  52.  
  53.  
  54.     pygame.display.update()
  55.  
  56. def main_2(window):
  57.     clock = pygame.time.Clock()
  58.     background, bg_image = get_background("Green.png")
  59.     block_size = 96
  60.     start_time = time.time()
  61.     elapsed_time = 0
  62.  
  63.  
  64.     player = Player(100, 704, 50, 50) #(x, y, width, height) x, y = spawn location
  65.     trap = Blade(300, HEIGHT - block_size - 37, 38, 38) #(x, y, width, height)
  66.     trap.on()
  67.     trap2 = Blade(400, HEIGHT - block_size - 37, 38, 38) #(x, y, width, height)
  68.     trap2.on()
  69.     flag = Flag(block_size * 5, (HEIGHT - block_size) - 704, 64, 64) #(x, y, width, height)
  70.    
  71.     floor = [Block(i * block_size, HEIGHT - block_size, block_size)
  72.              for i in range(-WIDTH // block_size, (WIDTH * 1) // block_size)]
  73.  
  74.     objects = [trap, trap2, flag, *floor, Block(0, HEIGHT - block_size * 2, block_size), Block(0, HEIGHT - block_size * 3, block_size), Block(0, HEIGHT - block_size * 4, block_size),Block(0, HEIGHT - block_size * 5, block_size),  Block(0, HEIGHT - block_size * 6, block_size), Block(0, HEIGHT - block_size * 7, block_size), Block(0, HEIGHT - block_size * 8, block_size),Block(0, HEIGHT - block_size * 9, block_size),
  75.                Block(block_size* 7, HEIGHT - block_size * 2, block_size), Block(block_size * 7, HEIGHT - block_size * 3, block_size), Block(block_size * 7, HEIGHT - block_size * 4, block_size),Block(block_size * 7, HEIGHT - block_size * 5, block_size),  Block(block_size * 7, HEIGHT - block_size * 6, block_size), Block(block_size * 7, HEIGHT - block_size * 7, block_size), Block(block_size * 7, HEIGHT - block_size * 8, block_size),Block(block_size * 7, HEIGHT - block_size * 9, block_size),
  76.                Block(block_size* 6, HEIGHT - block_size * 2, block_size), Block(block_size, HEIGHT - block_size * 6, block_size), Block(block_size * 3, HEIGHT - block_size * 4, block_size), Block(block_size * 4, HEIGHT - block_size * 4, block_size), Block(block_size * 5, HEIGHT - block_size * 7, block_size)]
  77.     #offset_x = 0
  78.     offset = pygame.math.Vector2(0,0)
  79.     scroll_area_width = 200     #how close to edge for scrolling background
  80.  
  81.     run = True
  82.     while run:
  83.         clock.tick(FPS)
  84.         if player.healthbar.health > 0:
  85.             elapsed_time = time.time() - start_time # seconds since while loop started
  86.         else:
  87.             start_time = time.time() - elapsed_time
  88.  
  89.         for event in pygame.event.get():
  90.             if event.type == pygame.QUIT:
  91.                 run = False
  92.                 break
  93.  
  94.             if event.type == player.hit_cooldown_event:
  95.                 pygame.time.set_timer(player.hit_cooldown_event, 0)
  96.                 player.hit_cooldown = False
  97.  
  98.             if event.type == pygame.KEYDOWN:
  99.                 if (event.key == pygame.K_SPACE or event.key == pygame.K_w or event.key == pygame.K_UP) and player.jump_count < 2  and player.healthbar.health > 0:     # jump if W, space, or up arrow is pressed, double jump if already jumped
  100.                     player.jump()
  101.  
  102.             if player.finished:
  103.                 run=False
  104.  
  105.             if player.healthbar.health <= 0:
  106.                 if pygame.key.get_pressed()[pygame.K_r]:    #restart_level
  107.                     main_2(window)
  108.                 if pygame.key.get_pressed()[pygame.K_ESCAPE]:   #close window
  109.                     run = False
  110.                  
  111.             if player.x_vel < 0:                                                # scrolling backgrpund for both horizpntal and vertical (doesn't work)
  112.                 if player.rect.left + offset.x < scroll_area_width:                
  113.                     offset.x += player.x_vel
  114.             elif player.x_vel > 0:
  115.                 if player.rect.right + offset.x > WIDTH - scroll_area_width:
  116.                     offset.x += player.x_vel
  117.             if player.y_vel < 0:
  118.                 if player.rect.top + offset.y < scroll_area_width:
  119.                     offset.y += player.x_vel
  120.             elif player.y_vel > 0:
  121.                 if player.rect.bottom + offset.y > WIDTH - scroll_area_width:
  122.                     offset.y += player.x_vel
  123.        
  124.         player.loop(FPS)
  125.         trap.loop()
  126.         trap2.loop()
  127.         flag.loop()
  128.  
  129.         handle_move(player, objects)
  130.         draw(window, background, bg_image, player, objects, offset, elapsed_time)
  131.        
  132.  
  133.     '''
  134.        if ((player.rect.top - offset_x >= HEIGHT - scroll_area_width) and player.y_vel > 0) or (      #scrolling background
  135.                (player.rect.bottom - offset_x <= scroll_area_width) and player.y_vel < 0):
  136.            offset_x += player.y_vel
  137.    '''
  138.  
  139.  
  140. if __name__ == "__main__":
  141.     main_2(window)
Advertisement
Add Comment
Please, Sign In to add comment