pygame_wrap_class #1

Merged
s473555 merged 5 commits from pygame_wrap_class into main 2023-03-12 16:32:16 +01:00
3 changed files with 66 additions and 46 deletions
Showing only changes of commit 85281d93e8 - Show all commits

59
Interface/grid_draw.py Normal file
View File

@ -0,0 +1,59 @@
import sys
import pygame
class Colors():
BLACK = 0, 0, 0
WHITE = 255, 255, 255
RED = 255, 0, 0
GREEN = 0, 255, 0
DEFAULT_COLOR = Colors.WHITE
def default_color(func):
def wrap(*args, **kwargs):
if 'color' not in kwargs:
kwargs['color'] = DEFAULT_COLOR
result = func(*args, **kwargs)
return result
return wrap
class GridDraw():
def __init__(
self,
width = None,
height = None,
background = None
):
self.width = width if width != None else 100
self.height = height if height != None else 100
self.background = background if background != None else Colors.BLACK
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
def start_draw(self):
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
self.screen.fill(Colors.BLACK)
def end_draw(self):
pygame.display.flip()
@default_color
def line(self, x_1, y_1, x_2, y_2, color = None):
pygame.draw.line(self.screen, color, (x_1, y_1), (x_2, y_2))
@default_color
def board(self, tiles_x, tiles_y, color = None):
tiles_width = self.width / tiles_x
tiles_height = self.height / tiles_y
for i in range(1, tiles_x):
self.line(tiles_width * i, 0, tiles_width * i, self.height, color=color)
for i in range(1, tiles_y):
self.line(0, tiles_height * i, self.width, tiles_height * i, color=color)
@default_color
def circle(self, x, y, radius, color = None):
pygame.draw.circle(self.screen, color, (x, y), radius)

View File

@ -1,28 +0,0 @@
import pygame
BLACK = 0, 0, 0
WHITE = 255, 255, 255
def rect(screen, color, position_x, position_y, size_x, size_y):
pygame.draw.rect(screen, color, (position_x,position_y,size_x,size_y))
def line(screen, color, x_1, y_1, x_2, y_2):
pygame.draw.line(screen, color, (x_1, y_1), (x_2, y_2))
def create_screen(size):
return pygame.display.set_mode(size)
def draw_board(screen, color, width, height, tiles_x, tiles_y):
tiles_width = width / tiles_x
tiles_height = height / tiles_y
temp_x = tiles_width
for i in range(tiles_x-1):
line(screen, color, temp_x, 0, temp_x, height)
temp_x += tiles_width
temp_y = tiles_height
for i in range(tiles_y-1):
line(screen, color, 0, temp_y, width, temp_y)
temp_y += tiles_height

View File

@ -1,23 +1,12 @@
import sys, pygame
from helpers import *
pygame.init()
size = width, height = 1000, 1000
screen = create_screen(size)
tiles_x, tiles_y = 10, 10
from grid_draw import GridDraw, Colors
grid = GridDraw(500, 500)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
grid.start_draw()
grid.board(10, 10)
grid.circle(75, 75, 20, color=Colors.RED)
grid.circle(225, 175, 20, color=Colors.GREEN)
screen.fill(BLACK)
# rect(screen, WHITE, 50,50,90,90)
draw_board(screen, WHITE, width, height, tiles_x, tiles_y)
pygame.display.flip()
grid.end_draw()