rearrange

This commit is contained in:
Nielson Tschá
2021-03-03 16:39:08 +01:00
parent b3fb08d074
commit da3a0b09b9
35 changed files with 782 additions and 52 deletions
+137
View File
@@ -0,0 +1,137 @@
import pygame
import math
class Compass():
def __init__(self, font_size, size):
pygame.font.init()
self.textColor = pygame.Color("white")
self.backgroundColor = pygame.Color("White")
self.needleColor = pygame.Color("black")
self.font = pygame.font.Font(pygame.font.get_default_font(), font_size)
self.surface = pygame.Surface(size, pygame.SRCALPHA)
self.size = size
self.x = 0
self.y = 0
self.backgroundSurface = None
def setX(self, x):
self.x = x
def getX(self):
return self.x
def setY(self, y):
self.y = y
def getY(self):
return self.y
def get(self):
return self.surface
def setTextColor(self, color):
try:
self.textColor = pygame.Color(color)
except:
self.textColor = pygame.Color("black")
def setBackgroundColor(self, color):
try:
self.backgroundColor = pygame.Color(color)
except:
self.backgroundColor = pygame.Color("white")
def setNeedleColor(self, color):
try:
self.needleColor = pygame.Color(color)
except:
self.needleColor = pygame.Color("black")
def update(self, value):
raise NotImplementedError()
class MiddleNeedle(Compass):
def update(self, value):
radius = min(self.size[0]//2, self.size[1]//2)
pygame.draw.circle( self.surface, self.backgroundColor, (self.size[0]//2, self.size[1]//2), radius)
arrow = pygame.Surface( ((self.size[0]//100*60)//2, self.size[1]//100*60), pygame.SRCALPHA )
polygon_list = [
( arrow.get_width()//2, 0 ), #Middle top
( 0, arrow.get_height() ), #bottom left
( arrow.get_width()//2, (arrow.get_height()-arrow.get_height()//100*30) ), #middle pos
( arrow.get_width(), arrow.get_height() ), #bottom right
]
pygame.draw.polygon(arrow, self.needleColor, polygon_list)
arrow = pygame.transform.rotate( arrow, value )
self.surface.blit( arrow, ( (self.size[0]//2)-(arrow.get_width()//2), (self.size[1]//2)-(arrow.get_height()//2)))
text = self.font.render("N", True, self.textColor)
self.surface.blit( text, ( (self.size[0]//2) - (text.get_width()//2), int(self.size[1]/100*10) - text.get_height()//2 ) )
text = self.font.render("W", True, self.textColor)
self.surface.blit( text, ( int(self.size[0]/100*10) - (text.get_width()//2), (self.size[1]//2) - (text.get_height()//2) ) )
text = self.font.render("O", True, self.textColor)
self.surface.blit( text, ( int(self.size[0]/100*90)- (text.get_width()//2), (self.size[1]//2) - (text.get_height()//2) ) )
text = self.font.render("S", True, self.textColor)
self.surface.blit( text, ( (self.size[0]//2) - (text.get_width()//2), int(self.size[1]/100*90) - text.get_height()//2 ) )
class OuterNeedle(Compass):
def update(self, value):
radius = min(self.size[0]//2, self.size[1]//2)
pygame.draw.circle( self.surface, self.backgroundColor, (self.size[0]//2, self.size[1]//2), radius)
if self.backgroundSurface:
self.surface.blit( self.backgroundSurface, (0,0) )
if self.lineLength is None:
self.lineLength = 5
# 0 is down -> +90
# alpha = 2 * arcsin (s / 2r); s is bottom line
alpha = 2 * math.asin( self.lineLength / (2*radius) )
value = math.radians(value) + math.pi/2 #put 0 to bottom
bottom_left = self.getPos(value +alpha/2, radius)
bottom_left = ( int(bottom_left[0] + radius), bottom_left[1]+radius) #offset to screen middle
bottom_right = self.getPos(value -alpha/2, radius)
bottom_right = ( int(bottom_right[0] + radius), bottom_right[1]+radius)
height_of_segment = self.lineLength/2 * math.tan( alpha / 4 )
height_of_triangle = math.sqrt(3) /2 * self.lineLength
top = self.getPos(value, radius-height_of_segment-height_of_triangle)
top = ( int(top[0] + radius), top[1]+radius )
points = [ bottom_left, top, bottom_right ]
pygame.draw.lines( self.surface, self.needleColor, False, points,3)
text = self.font.render("N", True, self.textColor)
self.surface.blit( text, ( (self.size[0]//2) - (text.get_width()//2), int(self.size[1]/100*10) - text.get_height()//2 ) )
text = self.font.render("W", True, self.textColor)
self.surface.blit( text, ( int(self.size[0]/100*10) - (text.get_width()//2), (self.size[1]//2) - (text.get_height()//2) ) )
text = self.font.render("O", True, self.textColor)
self.surface.blit( text, ( int(self.size[0]/100*90)- (text.get_width()//2), (self.size[1]//2) - (text.get_height()//2) ) )
text = self.font.render("S", True, self.textColor)
self.surface.blit( text, ( (self.size[0]//2) - (text.get_width()//2), int(self.size[1]/100*90) - text.get_height()//2 ) )
def getPos(self, alpha, radius):
return ( int( radius * math.cos(alpha) ), int( radius * math.sin(alpha) ) )
def setLineLength(self, length):
self.lineLength = length
def setBackgroundSurface(self, surface):
self.backgroundSurface = surface
+44
View File
@@ -0,0 +1,44 @@
import pygame
import math
class Point():
# constructed using a normal tupple
def __init__(self, point_t = (0,0)):
self.x = float(point_t[0])
self.y = float(point_t[1])
# define all useful operators
def __add__(self, other):
return Point((self.x + other.x, self.y + other.y))
def __sub__(self, other):
return Point((self.x - other.x, self.y - other.y))
def __mul__(self, scalar):
return Point((self.x*scalar, self.y*scalar))
def __truediv__(self, scalar):
return Point((self.x/scalar, self.y/scalar))
def __len__(self):
return int(math.sqrt(self.x**2 + self.y**2))
# get back values in original tuple format
def get(self):
return (self.x, self.y)
def draw_dashed_line(surf, color, start_pos, end_pos, width=1, dash_length=10):
origin = Point(start_pos)
target = Point(end_pos)
displacement = target - origin
length = len(displacement)
slope = displacement/length
for index in range(0, int(length/dash_length), 2):
start = origin + (slope * index * dash_length)
end = origin + (slope * (index + 1) * dash_length)
pygame.draw.line(surf, color, start.get(), end.get(), width)
if __name__ == "__main__":
start =(50,50)
end =(100,50)
surface = pygame.Surface( (200,200) )
draw_dashed_line( surface, pygame.Color("Black"), start, end, width=1 )
+53
View File
@@ -0,0 +1,53 @@
import pygame
class Text():
def __init__(self,txt, font_size):
pygame.font.init()
self.color = pygame.Color("black")
self.txt = txt
self.font = pygame.font.Font(pygame.font.get_default_font(), font_size)
self.text = self.font.render(self.txt, True, self.color)
self.x = 0
self.y = 0
def setX(self, x):
self.x = x
def getX(self):
return self.x
def setY(self, y):
self.y = y
def getY(self):
return self.y
def get(self):
return self.text
def getSize(self):
return self.text.get_size()
def getWidth(self):
return self.text.get_width()
def setColor(self, color):
try:
self.color = pygame.Color(color)
except:
self.color = pygame.Color("black")
self.text = self.font.render(self.txt, True, self.color)
class VariableText(Text):
def __init__(self, txt, font_size):
pygame.font.init()
self.color = pygame.Color("black")
self.txt = txt+"{}"
self.font = pygame.font.Font(pygame.font.get_default_font(), font_size)
self.text = self.font.render(self.txt.format(0), True, self.color)
def setUnit(self, unit):
self.txt = self.txt+unit
self.unit = unit
def update(self, value):
self.text = self.font.render(self.txt.format(value), True, self.color)
+55
View File
@@ -0,0 +1,55 @@
import pygame
class Textbox():
def __init__(self, size, x, y, font_size):
self.background = pygame.Color("grey")
self.surface = pygame.Surface( size )
self.surface.fill( self.background )
self.color = pygame.Color("White")
self.fontsize = 20
pygame.font.init()
self.font = pygame.font.Font(pygame.font.get_default_font(), font_size)
text = self.font.render("", True, self.color)
self.surface.blit( text, (0,0) )
self.x = x
self.y = y
def setText(self, text):
self.lines = text.split('\n')
self.nr_of_lines = len(self.lines)
def setTextColor(self, color):
try:
self.color = pygame.Color(color)
except:
self.color = pygame.Color("black")
def setBackgroundColor(self, color):
try:
self.background = pygame.Color(color)
except:
self.background = pygame.Color("grey")
def get(self):
return self.surface
def getX(self):
return self.x
def getY(self):
return self.y
def update(self, value):
self.surface.fill( self.background )
for lineNr, line in enumerate(self.lines):
text = self.font.render(line.format(value), True, self.color)
spacing = text.get_height()//10
x = self.surface.get_width()//2 - text.get_width()//2
height_total = self.nr_of_lines * (text.get_height() + spacing)
y = self.surface.get_height()//2 - height_total//2 + (lineNr * (text.get_height()+spacing))
self.surface.blit( text, (x,y) )