/
Suplin3
/
PySimulation
Обзор
Документация
Войти
/
Suplin3
/
PySimulation
Код
Запросы
0
Задачи
Пакеты
0
Релизы
1
Аналитика
Безопасность
master
gui.py
104 строки
5 KB
Suplin3
Add requirements
26 сен 2024, 20:21
26 сен 2024, 20:21
fe16559
Код
Авторство
О чём код?
import pygame import phisycs from phisycs import * from math import * class Text: def __init__(self, text, font="Arial", font_size=10, color=(0, 0, 0), x=100, y=100): self.font = font self.font_size = font_size self.color = color self.x = x self.y = y self.text = text def draw(self, surface: pygame.Surface): text_surf = pygame.font.SysFont(self.font, self.font_size).render(self.text, True, self.color) surface.blit(text_surf, (self.x, self.y)) class CoordAxes: def __init__(self, gap=50, thickness=3, color=(255, 255, 255), oneStepPixels=20, xName="", yName="", stepsOn=True): self.gap = gap self.oneStepPixels = oneStepPixels self.yName = yName self.xName = xName self.thickness = thickness self.color = color self.stepsOn = stepsOn def draw(self, surface: pygame.Surface): x0 = self.gap y0 = surface.get_height() - self.gap Text("0", "Arial", 16, self.color, x0 - 10, y0).draw(surface) # x pygame.draw.rect(surface, self.color, (x0, y0, surface.get_width() - 2 * self.gap, self.thickness)) pygame.draw.polygon(surface, self.color, ((x0 + surface.get_width() - 2 * self.gap, y0 - 6 - self.thickness // 2), (x0 + surface.get_width() - 2 * self.gap, y0 + 6 + self.thickness // 2), (x0 + surface.get_width() - 1.5 * self.gap, y0))) Text("X, м" if self.xName == "" else self.xName, "Arial", 16, self.color, x0 + surface.get_width() - 2 * self.gap, y0 + 10).draw(surface) if self.stepsOn: for x in range(surface.get_width() - 2 * self.gap): if x == 0: continue if x % self.oneStepPixels == 0: pygame.draw.rect(surface, self.color, (x + self.gap, y0 - self.thickness, self.thickness // 2, self.thickness * 4)) Text(str(x // self.oneStepPixels), color=self.color, font_size=15, x=x - 5 + self.gap, y=y0 - self.thickness + 12).draw(surface) # y pygame.draw.rect(surface, self.color, (x0, self.gap, self.thickness, surface.get_height() - 2 * self.gap)) pygame.draw.polygon(surface, self.color, ((x0 - 6 + self.thickness // 2, self.gap), (x0 + 6 + self.thickness // 2, self.gap), (x0 + self.thickness // 2, self.gap // 2))) Text("Y, м" if self.xName == "" else self.xName, "Arial", 16, self.color, x0 + self.thickness // 2 - 40, self.gap // 2).draw(surface) if self.stepsOn: for y in range(surface.get_height() - 2 * self.gap): if y == 0: continue if y % self.oneStepPixels == 0: pygame.draw.rect(surface, self.color, (x0 - self.thickness, surface.get_height() - y - self.gap, self.thickness * 4, self.thickness // 2)) Text(str(y // self.oneStepPixels), color=self.color, font_size=15, x=x0 - (15 if y // self.oneStepPixels < 10 else 22), y=surface.get_height() - y - self.gap - 10).draw(surface) return self def getXLine(self, surface): x0 = self.gap y0 = surface.get_height() - self.gap return Vector2(x0, y0), Vector2(x0 + surface.get_width() - 2 * self.gap, y0) def getYLine(self, surface): x0 = self.gap y0 = surface.get_height() - self.gap return Vector2(x0, self.gap), Vector2(x0 + 1, surface.get_height() - 2 * self.gap + y0) def draw_vector(self, surface: pygame.Surface, vector: Vector2, pos: Vector2, color=(255, 255, 255)): x0 = pos[0] * self.oneStepPixels + self.gap y0 = surface.get_height() - (pos[1] * self.oneStepPixels + self.gap) x1 = x0 + (vector.x * self.oneStepPixels) y1 = y0 - (vector.y * self.oneStepPixels) pygame.draw.line(surface, color, (x0, y0), (x1, y1), 3) pygame.draw.circle(surface, (255, 0, 0), (x0, y0), 3)