forked from crephoto/tipcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
126 lines (102 loc) · 4.23 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pygame
d = pygame.display.set_mode((500, 500))
pygame.init()
#Takes rectangle's size, position and a point. Returns true if that
#point is inside the rectangle and false if it isnt.
def pointInRectanlge(px, py, rw, rh, rx, ry):
if px > rx and px < rx + rw:
if py > ry and py < ry + rh:
return True
return False
#Blueprint to make sliders in the game
class Slider:
def __init__(self, position:tuple, upperValue:int=10, sliderWidth:int = 30, text:str="Editing features for simulation",
outlineSize:tuple=(300, 100))->None:
self.position = position
self.outlineSize = outlineSize
self.text = text
self.sliderWidth = sliderWidth
self.upperValue = upperValue
#returns the current value of the slider
def getValue(self)->float:
return self.sliderWidth / (self.outlineSize[0] / self.upperValue)
#renders slider and the text showing the value of the slider
def render(self, display:pygame.display)->None:
#draw outline and slider rectangles
pygame.draw.rect(display, (0, 0, 0), (self.position[0], self.position[1],
self.outlineSize[0], self.outlineSize[1]), 3)
pygame.draw.rect(display, (0, 0, 0), (self.position[0], self.position[1],
self.sliderWidth, self.outlineSize[1] - 10))
#determite size of font
self.font = pygame.font.Font(pygame.font.get_default_font(), int((15/100)*self.outlineSize[1]))
#create text surface with value
valueSurf = self.font.render(f"{self.text}: {round(self.getValue())}", True, (255, 0, 0))
#centre text
textx = self.position[0] + (self.outlineSize[0]/2) - (valueSurf.get_rect().width/2)
texty = self.position[1] + (self.outlineSize[1]/2) - (valueSurf.get_rect().height/2)
display.blit(valueSurf, (textx, texty))
#allows users to change value of the slider by dragging it.
def changeValue(self)->None:
#If mouse is pressed and mouse is inside the slider
mousePos = pygame.mouse.get_pos()
if pointInRectanlge(mousePos[0], mousePos[1]
, self.outlineSize[0], self.outlineSize[1], self.position[0], self.position[1]):
if pygame.mouse.get_pressed()[0]:
#the size of the slider
self.sliderWidth = mousePos[0] - self.position[0]
#limit the size of the slider
if self.sliderWidth < 1:
self.sliderWidth = 0
if self.sliderWidth > self.outlineSize[0]:
self.sliderWidth = self.outlineSize[0]
slider = Slider((100, 100))
while True:
pygame.event.get()
d.fill((255, 255, 255))
slider.render(d)
slider.changeValue()
print(slider.getValue())
pygame.display.update()
#pygame.time.wait()
#!/usr/bin/python
'''
import os
import pygame, sys
from pygame.locals import *
# set window size
width = 640
height = 300
# initilaise pygame
pygame.init()
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
redColor = pygame.Color(203,115,154)
blackColor = pygame.Color(112,85,98)
#starting position
x = 320
y = 150
pygame.draw.rect(windowSurfaceObj,redColor,Rect(x,5,10,90))
pygame.display.update(pygame.Rect(0,0,width,height))
s = 0
while s == 0:
button = pygame.mouse.get_pressed()
if button[0] != 0:
pos = pygame.mouse.get_pos()
x = pos[0]
y = pos[1]
a = x - 5
if a < 0:
a = 0
pygame.draw.rect(windowSurfaceObj,blackColor,Rect(0,0,width,height))
#pygame.display.update(pygame.Rect(0,0,width,height))
pygame.draw.rect(windowSurfaceObj,redColor,Rect(a,100,10,90))
pygame.display.update(pygame.Rect(0,0,width,height))
# check for ESC key pressed, or pygame window closed, to quit
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
'''