Me and my acquaintance are working on creating a loading bar that takes forever to load for the fun of it. However, when making this loading bar, it seems to crash within the first second or two. Here's the code:
import pygame
import time
import random
pygame.init()
progress = 0
black = [0, 0, 0]
white = [255, 255, 255]
green = [0, 255, 0]
screenWidth = 600
screenHeigth = 800
size = [screenWidth, screenHeigth]
font = pygame.font.SysFont("Comic Sans MS", 25)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Loading...')
def textObjecte(text, color, size):
if size == "small":
textsuraface = font.render(text, True, color)
return textsuraface, textsuraface.get_rect()
def loading(progress):
if progress < 100:
text = font.render("loading: " + str(int(progress)) + "%", True, green)
screen.blit(text, (300, 100))
def message_to_screen(msg, color, y_displace, size="small"):
textSurf, textRect = textObjecte(msg, color, size)
textRect.center = (screenWidth/2), (screenHeigth/2) + y_displace
screen.blit(textSurf, textRect)
while progress/2 < 100:
timeCount = random.randint(15, 30)
increase = random.randint(1, 7)
progress += increase
screen.fill(black)
pygame.draw.rect(screen, white, [300, 50, 200, 50])
pygame.draw.rect(screen, black, [301, 51, 198, 48])
if (progress/2) > 100:
pygame.draw.rect(screen, white, [302, 52, 196, 46])
else:
pygame.draw.rect(screen, white, [302, 52, progress, 46])
loading(progress/2)
pygame.display.flip()
time.sleep(timeCount)
Any help with the code would be greatly appreciated.