Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame.freetype
- import time
- from Traps import *
- from pathlib import Path
- pygame.freetype.init()
- pygame.display.set_caption("Platformer")
- icon = pygame.image.load(Path('assets') / 'Icons' / 'MaskDude_icon.png')
- pygame.display.set_icon(icon)
- FONT = pygame.freetype.Font("assets/Menu/Text/pixeloid-font/PixeloidMono-d94EV.ttf", 20)
- WIDTH, HEIGHT = 1000, 800
- FPS = 60
- PLAYER_VEL = 5
- window = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
- from playerclass import Player
- from objectclass import Object
- from flagclass import Flag
- from fruitclass import Fruit
- from functions import get_background, handle_move
- #define game variables
- clock = pygame.time.Clock()
- background, bg_image = get_background("Blue.png") #blue background
- block_size = 96
- start_time = time.time()
- elapsed_time = 0
- class World():
- def __init__(self, data):
- self.tile_list = []
- #load images
- dirt_img = pygame.image.load('assets\Terrain\Dirt.png')
- grass_img = pygame.image.load('assets\Terrain\Grass Block.png')
- pink_dirt_img = pygame.image.load('assets\Terrain\Pink Dirt.png')
- pink_grass_img = pygame.image.load('assets\Terrain\Pink Grass Block.png')
- row_count = 0
- for row in data:
- col_count = 0
- for tile in row:
- if tile == 1: #dirt
- img = pygame.transform.scale(dirt_img, (block_size, block_size))
- img_rect = img.get_rect()
- img_rect.x = col_count * block_size
- img_rect.y = row_count * block_size
- tile = (img, img_rect)
- self.tile_list.append(tile)
- if tile == 2: #grass
- img = pygame.transform.scale(grass_img, (block_size, block_size))
- img_rect = img.get_rect()
- img_rect.x = col_count * block_size
- img_rect.y = row_count * block_size
- tile = (img, img_rect)
- self.tile_list.append(tile)
- if tile == 3: #pink dirt
- img = pygame.transform.scale(pink_dirt_img, (block_size, block_size))
- img_rect = img.get_rect()
- img_rect.x = col_count * block_size
- img_rect.y = row_count * block_size
- tile = (img, img_rect)
- self.tile_list.append(tile)
- if tile == 4: #pink grass
- img = pygame.transform.scale(pink_grass_img, (block_size, block_size))
- img_rect = img.get_rect()
- img_rect.x = col_count * block_size
- img_rect.y = row_count * block_size
- tile = (img, img_rect)
- self.tile_list.append(tile)
- def draw(self):
- for tile in self.tile_list:
- window.blit(tile[0], tile[1])
- world_data = [
- [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],
- [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],
- [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],
- [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],
- [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],
- [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],
- [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],
- [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]
- ]
- player = Player(100, 650, 50, 50) #(x, y, width, height) x, y = spawn location
- world = World(world_data)
- def draw(window, background, bg_image, elapsed_time):
- for tile in background: #draw background
- window.blit(bg_image, tile)
- # window.blit(FONT.render(f"Time: {round(elapsed_time)}s", "black")[0], (10, 60))
- if player.healthbar.health <= 0: #loss screen
- lost_text = FONT.render("You Lost!", "black")
- quit_text = FONT.render("press escape to quit", "black")
- restart_text = FONT.render("press R to restart", "black")
- window.blit(lost_text[0], (WIDTH/2 - lost_text[0].get_width()/2, HEIGHT/2 - lost_text[0].get_height()/2))
- window.blit(quit_text[0], (WIDTH/2.35 - quit_text[0].get_width()/4, HEIGHT/1.85 - quit_text[0].get_height()/1.85))
- 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))
- offset = pygame.math.Vector2(0,0)
- scroll_area = 200 #how close to edge for scrolling backgroundddd
- run = True
- while run:
- clock.tick(FPS)
- if player.healthbar.health > 0:
- elapsed_time = time.time() - start_time # seconds since while loop started
- else:
- start_time = time.time() - elapsed_time
- window.blit(bg_image, (0, 0))
- world.draw()
- player.draw(window, offset)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- pygame.display.update()
- draw(window, background, bg_image, elapsed_time)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement