Advertisement
Maluia

game.py

Nov 11th, 2024 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.41 KB | Gaming | 0 0
  1. import pygame.freetype
  2. import time
  3. from Traps import *
  4. from pathlib import Path
  5. pygame.freetype.init()
  6.  
  7. pygame.display.set_caption("Platformer")
  8. icon = pygame.image.load(Path('assets') / 'Icons' / 'MaskDude_icon.png')
  9. pygame.display.set_icon(icon)
  10.  
  11. FONT = pygame.freetype.Font("assets/Menu/Text/pixeloid-font/PixeloidMono-d94EV.ttf", 20)
  12. WIDTH, HEIGHT = 1000, 800
  13. FPS = 60
  14. PLAYER_VEL = 5
  15. window = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
  16.  
  17. from playerclass import Player
  18. from objectclass import Object
  19. from flagclass import Flag
  20. from fruitclass import Fruit
  21. from functions import get_background, handle_move
  22.  
  23. #define game variables
  24. clock = pygame.time.Clock()
  25. background, bg_image = get_background("Blue.png")      #blue background
  26. block_size = 96
  27. start_time = time.time()
  28. elapsed_time = 0
  29.  
  30. class World():
  31.     def __init__(self, data):
  32.         self.tile_list = []
  33.        
  34.         #load images
  35.         dirt_img = pygame.image.load('assets\Terrain\Dirt.png')
  36.         grass_img = pygame.image.load('assets\Terrain\Grass Block.png')
  37.         pink_dirt_img = pygame.image.load('assets\Terrain\Pink Dirt.png')
  38.         pink_grass_img = pygame.image.load('assets\Terrain\Pink Grass Block.png')
  39.        
  40.         row_count = 0
  41.         for row in data:
  42.             col_count = 0
  43.             for tile in row:
  44.                 if tile == 1:   #dirt
  45.                     img = pygame.transform.scale(dirt_img, (block_size, block_size))
  46.                     img_rect = img.get_rect()                  
  47.                     img_rect.x = col_count * block_size
  48.                     img_rect.y = row_count * block_size
  49.                     tile = (img, img_rect)
  50.                     self.tile_list.append(tile)
  51.                 if tile == 2:   #grass
  52.                     img = pygame.transform.scale(grass_img, (block_size, block_size))
  53.                     img_rect = img.get_rect()                  
  54.                     img_rect.x = col_count * block_size
  55.                     img_rect.y = row_count * block_size
  56.                     tile = (img, img_rect)
  57.                     self.tile_list.append(tile)
  58.                 if tile == 3:   #pink dirt
  59.                     img = pygame.transform.scale(pink_dirt_img, (block_size, block_size))
  60.                     img_rect = img.get_rect()                  
  61.                     img_rect.x = col_count * block_size
  62.                     img_rect.y = row_count * block_size
  63.                     tile = (img, img_rect)
  64.                     self.tile_list.append(tile)
  65.                 if tile == 4:       #pink grass
  66.                     img = pygame.transform.scale(pink_grass_img, (block_size, block_size))
  67.                     img_rect = img.get_rect()                  
  68.                     img_rect.x = col_count * block_size
  69.                     img_rect.y = row_count * block_size
  70.                     tile = (img, img_rect)
  71.                     self.tile_list.append(tile)
  72.      
  73.     def draw(self):
  74.         for tile in self.tile_list:
  75.             window.blit(tile[0], tile[1])
  76.  
  77.  
  78. world_data = [
  79. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  80. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  81. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  82. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  83. [1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  84. [1, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  85. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  86. [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
  87. ]
  88.  
  89. player = Player(100, 650, 50, 50) #(x, y, width, height) x, y = spawn location
  90. world = World(world_data)
  91.  
  92.  
  93.  
  94. def draw(window, background, bg_image, elapsed_time):
  95.     for tile in background:             #draw background
  96.         window.blit(bg_image, tile)
  97.        
  98.     # window.blit(FONT.render(f"Time: {round(elapsed_time)}s", "black")[0], (10, 60))
  99.    
  100.     if player.healthbar.health <= 0:        #loss screen
  101.         lost_text = FONT.render("You Lost!", "black")
  102.         quit_text = FONT.render("press escape to quit", "black")
  103.         restart_text = FONT.render("press R to restart", "black")
  104.         window.blit(lost_text[0], (WIDTH/2 - lost_text[0].get_width()/2, HEIGHT/2 - lost_text[0].get_height()/2))
  105.         window.blit(quit_text[0], (WIDTH/2.35 - quit_text[0].get_width()/4, HEIGHT/1.85 - quit_text[0].get_height()/1.85))
  106.         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))
  107.  
  108. offset = pygame.math.Vector2(0,0)
  109. scroll_area = 200     #how close to edge for scrolling backgroundddd
  110. run = True
  111. while run:
  112.     clock.tick(FPS)
  113.     if player.healthbar.health > 0:
  114.         elapsed_time = time.time() - start_time # seconds since while loop started
  115.     else:
  116.         start_time = time.time() - elapsed_time
  117.    
  118.     window.blit(bg_image, (0, 0))
  119.    
  120.     world.draw()
  121.     player.draw(window, offset)
  122.    
  123.     for event in pygame.event.get():
  124.         if event.type == pygame.QUIT:
  125.             run = False
  126.            
  127.     pygame.display.update()
  128.    
  129.    
  130.     draw(window, background, bg_image, elapsed_time)
  131.  
  132.          
  133. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement