Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import math
- class Scroller:
- def __init__(self, screen, x, y, length, length_ratio=None, text=""):
- if length_ratio == None:
- length_ratio = length
- self.screen = screen
- self.x, self.y = x, y
- self.length, self.length_ratio = length, length_ratio
- self.text_right = text
- self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
- self.a, self.value = 0, 0
- self.bool_znach = 0
- self.font = pygame.font.SysFont('stxingkai', 20)
- self.mouse_pos = (0, 0)
- def paint(self):
- self.value = int(self.a/(self.length/self.length_ratio))
- self.button0 = pygame.Rect(self.x , self.y, self.length, self.display_height/50)
- self.button1 = pygame.Rect(self.a + self.x-self.display_width/80, self.y-self.display_height/50, self.display_width/40, self.display_height/30)
- pygame.draw.line(self.screen, (48, 38, 18),[self.a + self.x, self.y-self.display_height/50], [self.a + self.x, self.y-self.display_height/50+self.display_height/20], 10)
- pygame.draw.rect(self.screen, [144, 144, 144], self.button0) # draw button
- pygame.draw.line(self.screen, (48, 38, 18),[self.a + self.x, self.y-self.display_height/50], [self.a + self.x, self.y-self.display_height/50+self.display_height/20], 2)
- pygame.draw.rect(self.screen, "red", self.button1)
- text_name = self.font.render(str(self.value), True, (0, 0, 255))
- text_rect = text_name.get_rect()
- self.screen.blit(text_name, [self.a + self.x - text_rect.width/2, self.y - text_rect.height/2])
- text_right = self.font.render(self.text_right, True, (0, 0, 255))
- text_rect_right = text_right.get_rect()
- self.screen.blit(text_right, [self.x+ self.length + 10, (self.y + self.y+self.display_height/100)/2])
- #обработчик событий
- def _event_(self, event):
- if event.type == pygame.MOUSEBUTTONDOWN and not self.button0.collidepoint(self.mouse_pos):
- self.bool_znach = 0
- if event.type == pygame.MOUSEMOTION or event.type == pygame.MOUSEBUTTONDOWN:
- self.mouse_pos = event.pos
- if self.button0.collidepoint(self.mouse_pos):
- self.bool_znach = 1
- if pygame.mouse.get_pressed()[0] != 0 and self.bool_znach:
- self.a = pygame.mouse.get_pos()[0] - self.x
- if self.a < 0:
- self.a = 0
- if self.a > self.length :
- self.a = self.length
- class Init:
- def __init__(self, screen, list_scroller):
- scl = list_scroller[0].value #Получение значения с ползунка "Масштаб"
- DX = list_scroller[1].value #Получение значения с ползунка "Сдвиг по X"
- DY = list_scroller[2].value #Получение значения с ползунка "Сдвиг по Y"
- Rotate = list_scroller[3].value #Получение значения ползунка "Наклон"
- Rotate = (math.pi / 180) * Rotate #Перевод градусов в радианы
- nX = 2 * (math.sin(0) ** 3) * scl #Получение X координаты, для начальной точки графика
- nY = 2 * (math.cos(0) ** 3) * scl #Получение Y координаты, для начальной точки графика
- oldX = nX * math.cos(Rotate) - nY * math.sin(Rotate) #Вычисление координаты X с учетом поворота
- oldY = nX * math.sin(Rotate) + nY * math.cos(Rotate) #Вычисление координаты Y с учетом поворота
- #Отрисовка графика
- t = 0
- while (t < 10 * math.pi):
- t += 0.01
- nX = 2 * (math.sin(t) ** 3) * scl #Вычисление текущей X координаты, для графика
- nY = 2 * (math.cos(t) ** 3) * scl #Вычисление текущей Y координаты, для графика
- x = nX * math.cos(Rotate) - nY * math.sin(Rotate); #Вычисление координаты X с учетом поворота
- y = nX * math.sin(Rotate) + nY * math.cos(Rotate); #Вычисление координаты Y с учетом поворота
- pygame.draw.line(screen, (0, 0, 0),(oldX + DX , oldY + DY ), (x + DX , y + DY ))
- oldX = x; oldY = y; #Сохраняем значения X,Y для следующей итерации
- class Align:
- def __init__(self, screen, list_scroller):
- self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
- self.screen = screen
- self.list_scroller = list_scroller
- self.start_znach = [50, int(self.display_width/2), self.display_height/2, 0]
- self.font = pygame.font.SysFont('stxingkai', 50)
- def ran(self):
- for i in range(len(self.list_scroller)):
- self.list_scroller[i].a = int(self.start_znach[i] * self.list_scroller[i].length/self.list_scroller[i].length_ratio)
- def paint_button(self):
- text_name = self.font.render("Выровнить", True, (0, 0, 0))
- text_rect = text_name.get_rect()
- self.button0 = pygame.Rect(self.display_width/2 - 10 , self.display_height - text_rect.height/2 - 35 , text_rect.width + 20, text_rect.height+10)
- pygame.draw.rect(self.screen, [144, 144, 144], self.button0)
- self.screen.blit(text_name, [self.display_width/2 , self.display_height - text_rect.height/2 - 30])
- def _event_(self, event):
- if event.type == pygame.MOUSEBUTTONDOWN:
- if self.button0.collidepoint(event.pos):
- self.ran()
- class Main:
- def __init__(self, fps=60):
- pygame.init()
- self.screen = pygame.display.set_mode()
- self.clock = pygame.time.Clock()
- self.display_width, self.display_height = pygame.display.Info().current_w, pygame.display.Info().current_h
- self.a = 0
- text_list = ["Масштаб", "Сдвиг по х", "Сдвиг по у", "Наклон"]
- value_list = [(self.display_height/2 + self.display_width/2)/2, self.display_width, self.display_height, 360]
- self.list_scroller = []
- for i in range(4):
- self.list_scroller.append(Scroller(self.screen, 15, self.display_height - 100 * i/2 - 20, 300, value_list[i], text_list[i]))
- a = Align(self.screen, self.list_scroller)
- while True:
- self.screen.fill("white")
- Init(self.screen, self.list_scroller)
- a.paint_button()
- for scroller in self.list_scroller:
- scroller.paint()
- #обработчик событий
- for event in pygame.event.get():
- a._event_(event)
- for scroller in self.list_scroller:
- scroller._event_(event)
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN :
- if event.key == pygame.K_ESCAPE:
- pygame.quit()
- sys.exit()
- elif event.key == pygame.K_q:
- a.ran()
- pygame.display.flip()
- self.clock.tick(fps)
- #-------------------------------------------------------------------------------
- Main(24)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement