Created
December 14, 2021 03:25
-
-
Save f0ursqu4r3/19f47e844b9c80c38f10506ef011a55f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from pygame import Vector2 | |
pygame.init() | |
class Game: | |
def __init__(self): | |
self.window_size = Vector2(512) | |
self.window = pygame.display.set_mode(self.window_size) | |
pygame.display.set_caption('playground') | |
self.screen_scale = 4 | |
self.clock = pygame.time.Clock() | |
self.block_group = pygame.sprite.Group() | |
# strinng to hold our level data | |
level_string = ''' | |
0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 | |
1 1 1 0 0 0 1 1 | |
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 1 1 0 1 0 | |
1 1 1 1 1 1 1 1 | |
''' | |
# specifiy a tile size | |
self.tile_size = Vector2(16) | |
# a dict to hold our loaded images. | |
# we are just using surfaces for demo purposes | |
self.block_images = { | |
0: pygame.Surface(self.tile_size, pygame.SRCALPHA), | |
1: pygame.Surface(self.tile_size, pygame.SRCALPHA) | |
} | |
# give the surfaces some color | |
pygame.draw.rect(self.block_images[0], (100,100,100,100), ((0,0), self.block_images[0].get_size()), 1) | |
self.block_images[1].fill((200,0,0, 100)) | |
pygame.draw.rect(self.block_images[1], (200,0,0), ((0,0), self.block_images[1].get_size()), 1) | |
# iterate through the level data to populate our sprite group (self.block_group) | |
# we first need to strip out the leading and trailing white space | |
# then iterate through each line and replace out all the whitespace | |
# then we can create our block with the correct x and y coords | |
for y, line in enumerate(level_string.strip().splitlines()): | |
for x, c in enumerate(line.replace(' ', '')): | |
self.block_group.add(Block( | |
image=self.block_images[int(c)], | |
x=x * self.tile_size.x, | |
y=y * self.tile_size.y | |
)) | |
@property | |
def screen_size(self): | |
return Vector2(self.window_size/self.screen_scale) | |
def run(self): | |
while 1: | |
self.process_events() | |
self.update() | |
self.draw() | |
def process_events(self): | |
for event in pygame.event.get(): | |
if (event.type == pygame.QUIT or | |
(event.type == pygame.KEYDOWN and | |
event.key == pygame.K_ESCAPE)): | |
pygame.quit() | |
quit() | |
def update(self): | |
pygame.display.set_caption(f'playground - {int(self.clock.get_fps())}fps') | |
dt = self.clock.tick(60)*.001 | |
def draw(self): | |
self.window.fill((60,50,60)) | |
screen = pygame.Surface(self.screen_size, pygame.SRCALPHA) | |
# draw the sprite group (self.block_group) | |
self.block_group.draw(screen) | |
self.window.blit(pygame.transform.scale(screen, self.window_size),Vector2()) | |
pygame.display.flip() | |
class Block(pygame.sprite.Sprite): | |
def __init__(self, image, x, y): | |
super().__init__() | |
self.image = image | |
self.rect = self.image.get_rect() | |
self.rect.x = x | |
self.rect.y = y | |
if __name__ == '__main__': | |
Game().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment