Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglemansweep committed Nov 16, 2023
1 parent 2fdfc67 commit 89e5017
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 72 deletions.
20 changes: 8 additions & 12 deletions testing/sprites/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@
import time
from typing import Dict, List, Tuple

from .helpers import (
Animator,
AnimatorState,
TileGrid,
TileGridColumn,
TileGridCell,
MyTileGrid,
)
from .tiles import CustomTileGrid
from .utils import render_text


logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

# Setup

SCREEN_WIDTH = 256
SCREEN_HEIGHT = 64

FONT_FILENAME = "fonts/bitstream-vera.ttf"
FONT_SIZE = 12

# Sprites

# Main Loop

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SCALED)
Expand All @@ -36,13 +31,14 @@
FPS = 50
DEBUG = True


frame = 0
state: Dict = dict()
running = True


tile_grid = CustomTileGrid(state)

sprite_group: pygame.sprite.Group = pygame.sprite.Group()
tile_grid = MyTileGrid(state)
sprite_group.add(tile_grid)

while running:
Expand Down
File renamed without changes.
71 changes: 11 additions & 60 deletions testing/sprites/helpers.py → testing/sprites/lib/tile_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

# ANIMATION HELPERS


class AnimatorState(enum.Enum):
OPEN = 1
Expand Down Expand Up @@ -93,27 +95,27 @@ class BaseSprite(pygame.sprite.Sprite):

# should be a pygame.sprite.Sprite
class TileGridCell(BaseSprite):
color_background: pygame.Color = pygame.Color(0, 0, 0, 0)
width: int = 64
height: int = 12
visible: bool = True
label: str = ""

def __init__(self, x=0, y=0):
def __init__(self):
super().__init__()
self.x = x
self.y = y
self.image = pygame.Surface((self.width, self.height))
self.image.fill((0, random.randint(1, 32), 32))
self.rect = self.image.get_rect()
self.setup_surface()

def update(self, state):
self.render(state)

def render(self, state):
self.image = pygame.Surface((self.rect.width, self.rect.height))
self.image.fill((0, random.randint(1, 32), 32))
def setup_surface(self):
self.image = pygame.Surface((self.width, self.height))
self.image.fill(self.color_background)
self.rect = self.image.get_rect()

def render(self, state):
self.setup_surface()

def __repr__(self):
return f"TileGridCell(size=({self.width}x{self.height}), visible={self.visible}, label='{self.label}')"

Expand Down Expand Up @@ -219,54 +221,3 @@ def open(self):

def __repr__(self):
return f"HorizontalCollapseTileGridColumn(open={self.open}, width={self.width_animator.value}, cells={len(self.cells)})"


# CUSTOM TILES


class CellSpeedTestDownload(VerticalCollapseTileGridCell):
label = "Download"

def __init__(self):
super().__init__()
self.height_animator = Animator(range=(2.0, 12.0), open=True, speed=1.0)

def update(self, state):
super().update(state)
v = int(state.get("download", 0))
open = v > 500
self.height_animator.set(open)


class CellSpeedTestUpload(VerticalCollapseTileGridCell):
label = "Upload"

def __init__(self):
super().__init__()
self.height_animator = Animator(range=(2.0, 12.0), open=True, speed=1.0)

def update(self, state):
super().update(state)
v = int(state.get("upload", 0))
open = v > 500
self.height_animator.set(open)


# CUSTOM COLUMNS


class GridColumn1(HorizontalCollapseTileGridColumn):
cells = [CellSpeedTestUpload()]


class GridColumn2(HorizontalCollapseTileGridColumn):
cells = [CellSpeedTestDownload()]


# CUSTOM GRID


class MyTileGrid(TileGrid):
x = 0
y = 0
columns = [GridColumn1(), GridColumn2()]
50 changes: 50 additions & 0 deletions testing/sprites/tiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pygame
from .lib.tile_grid import (
TileGrid,
HorizontalCollapseTileGridColumn,
VerticalCollapseTileGridCell,
)

# CUSTOM TILES


class CellSpeedTestDownload(VerticalCollapseTileGridCell):
label = "Download"
color_background = pygame.Color(32, 0, 0, 255)

def update(self, state):
super().update(state)
v = int(state.get("download", 0))
open = v > 500
self.height_animator.set(open)


class CellSpeedTestUpload(VerticalCollapseTileGridCell):
label = "Upload"
color_background = pygame.Color(0, 32, 0, 255)

def update(self, state):
super().update(state)
v = int(state.get("upload", 0))
open = v > 500
self.height_animator.set(open)


# CUSTOM COLUMNS


class GridColumn1(HorizontalCollapseTileGridColumn):
cells = [CellSpeedTestUpload(), CellSpeedTestDownload()]


class GridColumn2(HorizontalCollapseTileGridColumn):
cells = [CellSpeedTestDownload()]


# CUSTOM GRID


class CustomTileGrid(TileGrid):
x = 0
y = 0
columns = [GridColumn1(), GridColumn2()]

0 comments on commit 89e5017

Please sign in to comment.