1

The following code works, but it looks a bit kludgy to me. I wander if there is a better way to write it than my approach. Also its resolution is far too high. If anyone has any ideas as to a better approach and how to slow down the resolution, I would love to hear it. Thank you!

import pygame as pg
from random import randint###RANDOM LIBRARY

pg.init()

screen = pg.display.set_mode((640, 480))
FONT = pg.font.Font(None, 36)
BACKGROUND_COLOR = (37, 225, 192)
BLUE = (0, 0, 199)
GRAY = (0, 0, 55)

#Rectangle variables.
measure_rect = pg.Rect(200, 40, 100, 400)

done = False

while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    random_bar=randint(33,34)#Manipulation of bar graph.
    value_print=random_bar
    length = random_bar*4
    random_bar = 100 - random_bar
    top = 40+(random_bar*4)

    screen.fill(BACKGROUND_COLOR)
    pg.draw.rect(screen, BLUE, (200, top, 100, length))
    pg.draw.rect(screen, GRAY, measure_rect, 2)
    txt = FONT.render(str(round(value_print, 2)), True, GRAY)
    screen.blit(txt, (120, 20))

    pg.display.flip()
    
pg.quit()
  • 1
    I tried to test your code...no idea what your tying to do. What I do is look up examples of lvl bars for games. Get specific, Lvl bars for RPG's using pygame. take a look at this: https://stackoverflow.com/questions/57033885/how-to-show-shields-level-by-changing-color-of-the-progress-bar. is this close to what you want to do? – BlackFox Jul 28 '21 at 08:27
  • https://www.techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/scoring-health-bars/ – BlackFox Jul 28 '21 at 08:29
  • https://coderslegacy.com/python/pygame-rpg-status-bar/ – BlackFox Jul 28 '21 at 08:29
  • Anytime man. Refactor your question so I can try and win some rep points too! – BlackFox Jul 28 '21 at 16:50

1 Answers1

0

I recommend to implement a Bar class:

import pygame as pg
from random import randint###RANDOM LIBRARY

pg.init()

screen = pg.display.set_mode((640, 480))
FONT = pg.font.Font(None, 36)
BACKGROUND_COLOR = (37, 225, 192)
BLUE = (0, 0, 199)
GRAY = (0, 0, 55)

class Bar():
    def __init__(self, rect, bar = BLUE, outline = GRAY):
        self.rect = pg.Rect(rect)
        self.bar = bar
        self.outline = outline
        self.value = 0
    def draw(self, surf):
        length = round(self.value * self.rect.height / 100)
        top = self.rect.height - length
        pg.draw.rect(surf, self.bar, (self.rect.x, self.rect.y + top, self.rect.width, length))
        pg.draw.rect(surf, self.outline, self.rect, 2) 
        txt = FONT.render(str(round(self.value, 2)), True, GRAY)
        txt_rect = txt.get_rect(topright = (self.rect.x - 10, self.rect.y))
        screen.blit(txt, txt_rect)   

bar = Bar((200, 40, 100, 400))

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    bar.value = randint(33, 34)

    screen.fill(BACKGROUND_COLOR)
    bar.draw(screen)
    pg.display.flip()
    
pg.quit()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks Rabbid76 ! How do you slow it down if pg.display.flip() takes no arguments? I only need three frames a second. Your cleanup of the initial code wonderful. – Harry Hobson Jul 29 '21 at 03:31
  • I added pg.time.wait(500) before pg.display.flip() to slow down the frame rate. Wish pg.display.flip() accepted an argument. – Harry Hobson Jul 29 '21 at 07:49