I have made a Button
class that basically draws a Rect
object and a text if specified and I implemented an animation that basically shrinks the button and inflates it back quickly on click
class Button:
def __init__(self, surface, pos, size, bg_color, hover_color):
self.surface = surface
self.x, self.y = pos[0], pos[1]
self.width, self.height = size[0], size[1]
self.bgColor = bg_color
self.hoverColor = hover_color
self.rect = pygame.Rect(pos, size)
self.text = None
self.clicked = False
def addText(self, font, text, txt_color):
self.text = font.render(text, True, txt_color)
self.textRect = self.text.get_rect(center = self.rect.center)
def update(self):
if self.isHovered():
pygame.draw.rect(self.surface, self.hoverColor, self.rect, border_radius=20)
else:
pygame.draw.rect(self.surface, self.bgColor, self.rect, border_radius=20)
if(self.text):
self.surface.blit(self.text, self.textRect)
self.checkClicked()
def checkClicked(self):
if(self.rect.collidepoint(pygame.mouse.get_pos())):
if(pygame.mouse.get_pressed()[0]):
if(not self.clicked):
self.clicked = True
self.rect.inflate_ip(-7, -7)
else:
if(self.clicked):
self.clicked = False
self.rect.inflate_ip(7, 7)
def isHovered(self):
return True if self.rect.collidepoint(pygame.mouse.get_pos()) else False
It's probably not the most effecient way to do it, but I'll worry about that later.
The implementation then would be :
startBtn = Button(display, (100, 160), (200, 40), (40, 40, 40), (70, 70, 70))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
if(startBtn.rect.collidepoint(pygame.mouse.get_pos())):
showNextScreen() # Here lies the issue
print('pressed')
display.fill((50, 50, 50))
startBtn.update()
pygame.display.update()
I have a function that would draw a new screen and bascially starts the game, however it happens too quick that you can't see the animation of the button and I am struggling to find a way to let the script wait for the button animation to finish then do stuff, I have tried pygame.time.wait()
and pygame.time.delay()
but the whole script freezes and makes it worse, How could I make that happen?
CodePudding user response:
You could just use time.sleep()
CodePudding user response:
I wrote a function called sleep
that waits a given amount of time without freezing the game.
Function
def sleep(ms): # ms --> time in milliseconds
start = pygame.time.get_ticks()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
now = pygame.time.get_ticks()
if now - start == ms:
running = False
Explanation
It takes one parameter: ms
, which is the amount of time that you want the program to wait in milliseconds (1 second = 1000 milliseconds). In the function, I set a variable called start
to pygame.time.get_ticks()
, which returns the current time. Then, inside a while loop, I created an event loop which checks for the pygame.QUIT
event so that if the user clicks X
while the function is still running, the program will respond and quit the program. After the event loop, I set a variable called now
to pygame.time.get_ticks()
to get the current time. Then I checked if now
(the current time) subtracted by start
(the start time) is equal to ms
(the given amount of waiting time in milliseconds). This checks if the given amount of time has passed. If it has, the while loop ends. If not, the while loop keeps running until that condition is True
.