Advertisement
Maluia

Level 1

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