ibitfit/electric_sieve/land/Mask.py

74 lines
2.1 KiB
Python

from math import tan, radians
import pygame
from pygame import Surface
from pygame.draw import line
from lib.pgfw.pgfw.GameChild import GameChild
class Mask(GameChild, Surface):
def __init__(self, parent):
GameChild.__init__(self, parent)
self.display_surface = self.get_display_surface()
self.load_configuration()
self.init_surface()
self.set_background()
self.reset()
def load_configuration(self):
config = self.get_configuration("land")
self.height = config["height"]
self.spacing_factor = config["spacing-factor"]
self.gradient = config["gradient"]
self.x_step = config["x-step"]
self.velocity_ratio = config["velocity-ratio"]
def init_surface(self):
Surface.__init__(self, (self.get_display_surface().get_width(), self.height))
def set_background(self):
background = Surface(self.get_size())
background.fill((0, 0, 0))
self.background = background
def reset(self):
self.x_offset = 0
def update(self):
self.clear()
self.draw_y()
self.draw_x()
def clear(self):
self.blit(self.background, (0, 0))
def draw_y(self):
yy = 0
ii = 0
rect = self.get_rect()
while yy < rect.bottom:
line(self, (255, 255, 255), (0, yy), (rect.right, yy))
yy += int(self.spacing_factor ** ii)
ii += 1
def draw_x(self):
gradient = self.gradient
step = self.x_step
rect = self.get_rect()
edge = rect.right
xx = int(self.x_offset) + step
adjacent = rect.h
while xx < edge:
angle = (edge - float(xx)) / edge * 2 * gradient + (90 - gradient)
opposite = int(tan(radians(90 - angle)) * adjacent)
line(self, (255, 255, 255), (xx, 0),
(xx + opposite, adjacent))
xx += step
self.decrement_x_offset()
def decrement_x_offset(self):
self.x_offset -= self.parent.parent.velocity[0] * self.velocity_ratio
if self.x_offset <= -self.x_step:
self.x_offset += self.x_step