垂直 Pygame 状态栏有问题
Trouble with verticle Pygame status bar
下面的代码可以工作,但对我来说看起来有点笨拙。我想知道是否有比我的方法更好的方法来编写它。它的分辨率也太高了。如果有人对更好的方法以及如何减慢分辨率有任何想法,我很想听听。谢谢!
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()
我建议实施 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()
下面的代码可以工作,但对我来说看起来有点笨拙。我想知道是否有比我的方法更好的方法来编写它。它的分辨率也太高了。如果有人对更好的方法以及如何减慢分辨率有任何想法,我很想听听。谢谢!
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()
我建议实施 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()