C
C
classicartt2021-12-08 21:21:33
pygame
classicartt, 2021-12-08 21:21:33

With what and how can I style the counter on pygame?

. Could someone suggest how to make the score counter on pygame pretty. Maybe some libraries or something in general that I can google and understand how to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Chetchasov, 2022-03-05
@TalismanChet

1. This task is doable, did not understand the reason for this issue.
2. I wrote an example program with a counter (how to use it is not difficult to understand)

import threading, sys
from pygame import *
font.init()

def thread(func):
    def _sugar(*args) -> threading.Thread:
        ret = threading.Thread(target = func, args = [*args,])
        ret.start()
        return ret
    return _sugar

class Counter(object):
    def __init__(s, w, h, bind, fg = Color(0, 255, 0), bg = Color(0, 0, 0),value=0, fnt = font.SysFont(None, 14)):
        s.w,\
              s.h,\
              s.fg,\
              s.bg,\
              s.vl,\
              s.fnt =\
              w,h,fg,bg,value,fnt
        s.bnd = bind
        pass
    def increase(s, step = 1):
        s.vl += step
        pass
    def decrease(s, step = 1):
        s.vl -= step
        pass
    def render(s):
        ret = Surface((s.w, s.h), HWSURFACE|SRCALPHA)
        ret.fill(s.bg)
        ret.blit(transform.smoothscale(s.fnt.render(str(s.vl), 1, s.fg), ret.get_size()), (0, 0))
        return ret
    @thread
    def bnd(s):
        pass
    pass
#
@thread
def bind(counter, event):
    if event.type in (MOUSEBUTTONDOWN, MOUSEBUTTONUP):
        counter.increase()
        pass
    pass
#
def main(argv):
    display.init()
    font.init()
    sc = display.set_mode((800, 600), HWSURFACE|SRCALPHA)
    counter = Counter(600, 400, bind, fnt = font.SysFont(None, 1024))
    ticker = time.Clock()
    target_FPS = 120
    real_FPS = 0
    while True:
        ticker.tick(target_FPS)
        real_FPS = ticker.get_fps()
        for e in event.get():
            if e.type == QUIT:
                display.quit()
                quit()
                sys.exit()
                pass
            counter.bnd(counter, e)
            pass
        sc.fill((255,100,100))
        sc.blit(counter.render(), (100, 100))
        display.flip()
        display.set_caption(f"Clicker based on counter [FPS: {int(real_FPS)}]")
        pass
    pass

if __name__ == '__main__':
    main(sys.argv)
    pass

If my answer was helpful, please mark it as correct. Thanks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question