Added changable scale
This commit is contained in:
parent
41c2aecf88
commit
fbc1849079
Binary file not shown.
Binary file not shown.
Binary file not shown.
51
slider.py
51
slider.py
@ -7,29 +7,30 @@ RED = (255, 0, 0)
|
||||
|
||||
class Slider:
|
||||
|
||||
def __init__(self, x, y, w, h, min_val, max_val, initial_val):
|
||||
self.rect = pygame.Rect(x, y, w, h)
|
||||
self.min_val = min_val
|
||||
self.max_val = max_val
|
||||
self.value = initial_val
|
||||
def __init__(self, x, y, w, h, min_val, max_val, initial_val):
|
||||
self.rect = pygame.Rect(x, y, w, h)
|
||||
self.min_val = min_val
|
||||
self.max_val = max_val
|
||||
self.value = initial_val
|
||||
self.grabbed = False
|
||||
|
||||
def draw(self, screen):
|
||||
# Draw the background
|
||||
pygame.draw.rect(screen, GRAY, self.rect)
|
||||
# Draw the handle (circle)
|
||||
handle_x = self.rect.x + (self.value - self.min_val) / (self.max_val - self.min_val) * self.rect.width
|
||||
pygame.draw.circle(screen, RED, (int(handle_x), self.rect.centery), self.rect.height // 2)
|
||||
|
||||
def handle_event(self, event):
|
||||
print("hi")
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
if self.rect.collidepoint(event.pos):
|
||||
self.grabbed = True
|
||||
elif event.type == pygame.MOUSEBUTTONUP:
|
||||
self.grabbed = False
|
||||
|
||||
def draw(self, screen):
|
||||
# Draw the background
|
||||
pygame.draw.rect(screen, GRAY, self.rect)
|
||||
# Draw the handle (circle)
|
||||
handle_x = self.rect.x + (self.value - self.min_val) / (self.max_val - self.min_val) * self.rect.width
|
||||
pygame.draw.circle(screen, RED, (int(handle_x), self.rect.centery), self.rect.height // 2)
|
||||
|
||||
def handle_event(self, event):
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
if self.rect.collidepoint(event.pos):
|
||||
self.grabbed = True
|
||||
elif event.type == pygame.MOUSEBUTTONUP:
|
||||
self.grabbed = False
|
||||
elif event.type == pygame.MOUSEMOTION:
|
||||
if self.grabbed:
|
||||
mouse_x = event.pos[0]
|
||||
# Constrain the handle within the slider
|
||||
new_value = (mouse_x - self.rect.x) / self.rect.width * (self.max_val - self.min_val) + self.min_val
|
||||
self.value = max(self.min_val, min(self.max_val, new_value))
|
||||
elif event.type == pygame.MOUSEMOTION:
|
||||
if self.grabbed:
|
||||
mouse_x = event.pos[0]
|
||||
# Constrain the handle within the slider
|
||||
new_value = (mouse_x - self.rect.x) / self.rect.width * (self.max_val - self.min_val) + self.min_val
|
||||
self.value = max(self.min_val, min(self.max_val, new_value))
|
||||
|
||||
18
test2.py
18
test2.py
@ -1,12 +1,17 @@
|
||||
import pygame
|
||||
import sys
|
||||
import math
|
||||
from sensor import Sensor
|
||||
from particle import Particle
|
||||
from slider import Slider
|
||||
|
||||
SCREEN_WIDTH = 1352
|
||||
SCREEN_HEIGHT = 878
|
||||
|
||||
scale = 1 * pow(10, -8)
|
||||
|
||||
sensor = Sensor((50*pow(10, -9)) / scale, (200 * pow(10,-9)) / scale, (300 * pow(10, -9)) / scale)
|
||||
|
||||
pygame.init()
|
||||
pygame.display.set_caption("CytoSim")
|
||||
|
||||
@ -20,16 +25,25 @@ while True:
|
||||
sys.exit()
|
||||
if event.type == pygame.VIDEORESIZE:
|
||||
screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
|
||||
if event.type == pygame.MOUSEWHEEL:
|
||||
print("SCROLL")
|
||||
if event.y == 1:
|
||||
scale = scale / 1.1
|
||||
elif event.y == -1:
|
||||
scale = scale * 1.1
|
||||
|
||||
x, y = screen.get_size()
|
||||
|
||||
screen.fill((0,0,105))
|
||||
screen.fill((200,100,5))
|
||||
|
||||
sensor.generate(x, y, screen)
|
||||
|
||||
pygame.draw.circle(screen, (150,255,10), (x / 2, y /2), 3 * pow(10, -6) / scale)
|
||||
|
||||
pygame.draw.line(screen, (255,255,255), (x - (x * .1), y - (y * .1)), ((x - (x * .1)) - (1 * pow(10, -6) / scale), y - (y * .1)))
|
||||
|
||||
print((1 *pow(10, -6)) / scale)
|
||||
print(scale)
|
||||
#print((1 *pow(10, -6)) / scale)
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user