Python Forum
pygame, sprites, and rects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame, sprites, and rects
#5
Something like this?
import pygame
import sys
import math


class StatusBar(pygame.sprite.Sprite):
    def __init__(
        self,
        rect=(0, 0, 100, 10),
        max_=1,
        min_=0,
        fg="white",
        bg="black",
        center=None,
        colormap=None,
    ):
        super().__init__()
        self.rect = pygame.Rect(rect)
        self.bar = pygame.Rect(0, 0, self.rect.width, self.rect.height)
        self.image = pygame.Surface(self.rect.size)
        self.colormap = dict(sorted(colormap.items())) if colormap else {}
        self.fg = fg
        self.bg = bg
        self.max = max_
        self.min = min_
        self.range = self.max - self.min
        self.center = center
        self._value = 0

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, new_value):
        self._value = max(self.min, min(new_value, self.max))

    def update(self):
        if self.center is None:
            self.bar.width = int(
                self.rect.width * (self.value - self.min) / (self.max - self.min)
            )
        else:
            a = self.rect.width * (self._value - self.min) / self.range
            b = self.rect.width * (self.center - self.min) / self.range
            self.bar.x = min(a, b)
            self.bar.width = abs(a - b)
        self.image.fill(self.bg)
        fg = self.fg
        for value, color in self.colormap.items():
            if value <= self._value:
                fg = color
            else:
                break
        pygame.draw.rect(self.image, fg, self.bar)


pygame.init()
screen = pygame.display.set_mode((220, 90))
b1 = StatusBar((10, 10, 200, 10), min_=-1)
b2 = StatusBar(
    (10, 30, 200, 10), min_=-1, colormap={-1: "green", 0: "yellow", 0.5: "red"}
)
b3 = StatusBar((10, 50, 200, 10), min_=-1, center=0)
b4 = StatusBar(
    (10, 70, 200, 10),
    min_=-1,
    center=0,
    colormap={
        -1: "red",
        -0.75: "orange",
        -0.5: "yellow",
        -0.25: "green",
        0.25: "yellow",
        0.5: "orange",
        0.75: "red",
    },
)
group = pygame.sprite.Group()
group.add((b1, b2, b3, b4))
clock = pygame.time.Clock()
for i in range(1000):
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        sys.exit()
    b1.value = math.sin(math.radians(i))
    b2.value = math.cos(math.radians(i))
    b3.value = math.sin(math.radians(i))
    b4.value = math.cos(math.radians(i))
    group.update()
    group.draw(screen)
    pygame.display.update()
    clock.tick(60)
pygame.quit()
Reply


Messages In This Thread
pygame, sprites, and rects - by menator01 - Oct-25-2023, 04:25 AM
RE: pygame, sprites, and rects - by deanhystad - Oct-25-2023, 01:34 PM
RE: pygame, sprites, and rects - by Windspar - Oct-25-2023, 08:09 PM
RE: pygame, sprites, and rects - by menator01 - Oct-25-2023, 09:59 PM
RE: pygame, sprites, and rects - by deanhystad - Oct-26-2023, 10:00 PM
RE: pygame, sprites, and rects - by menator01 - Oct-27-2023, 01:40 AM
RE: pygame, sprites, and rects - by menator01 - Oct-28-2023, 03:59 PM
RE: pygame, sprites, and rects - by Windspar - Oct-31-2023, 06:53 AM
RE: pygame, sprites, and rects - by menator01 - Oct-31-2023, 10:45 AM
RE: pygame, sprites, and rects - by Windspar - Nov-01-2023, 03:23 AM
RE: pygame, sprites, and rects - by deanhystad - Nov-01-2023, 09:36 PM
RE: pygame, sprites, and rects - by Windspar - Nov-02-2023, 12:52 AM
RE: pygame, sprites, and rects - by Benixon - Dec-07-2023, 02:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 2,093 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] I found a way to generate sprites without .blit. Is it effecient? xBlackHeartx 19 8,770 Dec-07-2019, 01:06 PM
Last Post: metulburr
  [pygame] transparent rects SheeppOSU 2 2,363 Jun-10-2019, 03:41 PM
Last Post: SheeppOSU
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 4,121 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 9,528 Jan-30-2019, 12:50 AM
Last Post: metulburr
  draw not showing all sprites ethanstrominger 0 2,670 Jan-25-2019, 10:10 PM
Last Post: ethanstrominger
  [PyGame] move randomly sprites reutB 4 8,350 Mar-29-2017, 01:12 PM
Last Post: metulburr
  How can I get rid of the line following the sprites? Houston11 1 3,797 Jan-06-2017, 10:14 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020