This commit is contained in:
Benjamín 2021-09-01 11:26:29 +02:00
parent 47d06204ea
commit 57863eb08a
11 changed files with 88 additions and 193 deletions

25
UI/Objects/Scene.py Normal file

@ -0,0 +1,25 @@
import pygame.transform
from Objects.Screen import Screen
from numpy import array as a
import numpy as np
class Scene(Screen):
def __init__(self, width: float, height: float, font_size: int, screen_size: a, bg=(60, 60, 60)):
super().__init__(width, height, font_size, screen_size)
self.bg = bg
self.r_objects = [] # resizable Objects
self.nr_objects = [] # non-resizable Objects
def redraw(self):
self.s_.fill(self.bg)
for object_ in self.r_objects:
object_.blit()
rs = pygame.transform.scale(self.s_, self.screen_size)
self.s.blit(rs, [0, 0])
pygame.display.update()

24
UI/Objects/Screen.py Normal file

@ -0,0 +1,24 @@
from numpy import array as a
import numpy as np
import pygame
class Screen:
def __init__(self, width: float, height: float, font_size: int, screen_size: a):
pygame.init()
self.width = width
self.height = height
self.size = a([float(width), float(height)])
self.screen_size = a(screen_size)
self.matrix = a([
[screen_size[0] / width, 0],
[0, screen_size[1] / height]
])
self.s_ = pygame.Surface([width, height])
self.s = pygame.display.set_mode(screen_size)
self.font_size = font_size
self.center = a([width, height]) / 2

@ -0,0 +1,15 @@
import pygame.draw
from numpy import array as a
import numpy as np
class Rect:
def __init__(self, pos, size, color, scene):
self.pos = pos
self.size = size
self.color = color
self.rect = pygame.Rect(self.pos, self.size)
self.scene = scene
def blit(self):
pygame.draw.rect(self.scene.s_, self.color, self.rect)

18
UI/Scenes/BasicScene.py Normal file

@ -0,0 +1,18 @@
from Objects.Scene import Scene
from Objects.r_objects import *
from numpy import array as a
import numpy as np
from UI.Objects.r_objects.Rect import Rect
class BasicScene(Scene):
def __init__(self, font_size: int, screen_size: a, bg=(60, 60, 60)):
s__size = [600, 400]
super().__init__(s__size[0], s__size[1], font_size, screen_size, bg)
r0 = Rect(a([0, 0]), a([200, 200]), (160, 160, 160), self)
self.r_objects.append(r0)

@ -1,40 +0,0 @@
import pygame.draw
from rounded_rect import draw_rounded_rect
class Board:
def __init__(self, size, bg_image):
self.fs = False
self.dp = [0, 0]
self.bg_image = bg_image
self.size = size
self.width = size[0]
self.height = size[1]
self.width_fifth = self.width / 5
self.small_size_fraction = sum(self.size) / 60
self.mid_width = self.width - self.width_fifth - 2 * self.small_size_fraction
self.height_sixth = self.height / 6
self.transparent_objects_color = (237, 237, 250)
def draw_bg(self, display):
# drawing left rect
pygame.draw.rect(display, (49, 49, 54), pygame.Rect(0, 0, self.width_fifth, self.height))
# drawing bg image
display.blit(self.bg_image, [self.width_fifth, 0])
# drawing top and bottom rect
surf = pygame.Surface(self.size)
surf.fill((0, 0, 0))
draw_rounded_rect(surf, pygame.Rect(self.width_fifth + self.small_size_fraction,
self.small_size_fraction, self.mid_width,
100),
self.transparent_objects_color, int(self.height / 20))
pygame.draw.rect(surf, self.transparent_objects_color, pygame.Rect(self.width_fifth, self.height - self.height_sixth,
self.width, self.height_sixth))
surf.set_colorkey((0, 0, 0))
surf.set_alpha(100)
display.blit(surf, [0, 0])

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

@ -1,82 +1,11 @@
import pygame
from pygame.locals import *
import math
import numpy as np
import random
from Scenes.BasicScene import BasicScene
from numpy import array as a
import time
import json
import os
import sys
import copy
from board import Board
# basic config
pygame.mixer.pre_init(48000, -16, 2, 512)
pygame.init()
pygame.mixer.set_num_channels(16)
available = pygame.font.get_fonts()
monitor_size = [pygame.display.Info().current_w, pygame.display.Info().current_h]
font = pygame.font.SysFont('calibri', 60, True)
small_font = pygame.font.SysFont('calibri', 40, True)
tiny_font = pygame.font.SysFont('calibri', 20, True)
Window_size = [1200, 800]
Default_size = Window_size
screen = pygame.display.set_mode(Window_size)
display = pygame.Surface(Window_size)
pygame.display.set_caption("School_board")
pygame.display.set_icon(pygame.image.load("logo.png").convert())
clock = pygame.time.Clock()
Win_size = Window_size
scene = BasicScene(10, [600, 400])
alive = True
board = Board(Window_size, pygame.image.load("board_bg.png"))
while alive:
board.draw_bg(display)
# event loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit(0)
# keydown
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit(0)
elif event.key == K_f:
board.fs = not board.fs
if board.fs is False:
Win_size = Default_size
screen = pygame.display.set_mode(Win_size)
board.dp = [0, 0]
else:
screen = pygame.display.set_mode(monitor_size, pygame.FULLSCREEN)
d = screen
ratio = [Default_size[1] / Default_size[0], Default_size[0] / Default_size[1]]
# u chose width or height here
if Default_size[0] > Default_size[1]:
Win_size = [d.get_width(), int(d.get_width() * ratio[0])]
d = d.get_height()
dd = Win_size[1]
else:
Win_size = [int(d.get_height() * ratio[1]), d.get_height()]
d = pygame.display.get_surface().get_width()
dd = Win_size[0]
board.dp[0] = (d - dd) / 2
screen = screen
# basic loop config
screen.blit(pygame.transform.scale(display, Win_size), board.dp)
pygame.display.update()
clock.tick(10)
while True:
scene.redraw()
time.sleep(0.1)

@ -1,7 +0,0 @@
from numpy import array as a
import numpy as np
class Rect:
def __init__(self):
pass

@ -1,17 +0,0 @@
from numpy import array as a
import numpy as np
import pygame
class Screen:
def __init__(self, width: float, height: float, font_size: int, matrix: np.array):
self.width = width
self.height = height
self.size = a([width, height])
self.matrix = matrix
self.s = pygame.Surface([width, height])
self.font_size = font_size
self.center = a([width, height]) / 2

@ -1,52 +0,0 @@
import pygame.gfxdraw
def draw_rounded_rect(surface, rect, color, corner_radius):
if rect.width < 2 * corner_radius or rect.height < 2 * corner_radius:
raise ValueError(f"Both height (rect.height) and width (rect.width) must be > 2 * corner radius ({corner_radius})")
# need to use anti aliasing circle drawing routines to smooth the corners
pygame.gfxdraw.aacircle(surface, rect.left+corner_radius, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.right-corner_radius-1, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.left+corner_radius, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.right-corner_radius-1, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.left+corner_radius, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.right-corner_radius-1, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.left+corner_radius, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.right-corner_radius-1, rect.bottom-corner_radius-1, corner_radius, color)
rect_tmp = pygame.Rect(rect)
rect_tmp.width -= 2 * corner_radius
rect_tmp.center = rect.center
pygame.draw.rect(surface, color, rect_tmp)
rect_tmp.width = rect.width
rect_tmp.height -= 2 * corner_radius
rect_tmp.center = rect.center
pygame.draw.rect(surface, color, rect_tmp)
def draw_bordered_rounded_rect(surface, rect, color, border_color, corner_radius, border_thickness):
if corner_radius < 0:
raise ValueError(f"border radius ({corner_radius}) must be >= 0")
rect_tmp = pygame.Rect(rect)
center = rect_tmp.center
if border_thickness:
if corner_radius <= 0:
pygame.draw.rect(surface, border_color, rect_tmp)
else:
draw_rounded_rect(surface, rect_tmp, border_color, corner_radius)
rect_tmp.inflate_ip(-2*border_thickness, -2*border_thickness)
inner_radius = corner_radius - border_thickness + 1
else:
inner_radius = corner_radius
if inner_radius <= 0:
pygame.draw.rect(surface, color, rect_tmp)
else:
draw_rounded_rect(surface, rect_tmp, color, inner_radius)