I can't set up a timer in my clicker game

17 Views Asked by At

I am a pygame beginner and i was working on my first game after some training. I am making a clicker game in which you buy factories that generate revenue. The twist is that there are taxes and you have to manage resources to survive to the next day. The issue seems to be that the timer is detached and is located inside of a function however i don't know how and where to put it besides that place. I tried to put the function setup all over the place but it doesn't seem to affect it.

Note, this is my first post on stacks overflow so sorry for any issues with the question.

import pygame

GREEN = (3, 158, 16)
YELLOW = (255, 221, 0)
RED = (255, 0, 17)
BLACK = (18, 16, 16)
WHITE = (250, 250, 250)
WIDTH = 300
HEIGHT = 300
FPS = 60
F_HEIGHT = HEIGHT - 60

def price_check():
    global color, factory_limit
    check_factory_limit()
    if factory_limit == False:
        if money >= factory_price:
            color = GREEN
        else:
            color = RED
    if factory_limit == True:
        color = RED

def draw_factory(width, height):
    set_factory_timer()
    for i in factories:
        bought_factory = pygame.draw.rect(screen, RED, (width, height, 40, 40))
        factory_timer = small_font.render(f'{int(money_timer/10000)}', False, WHITE)
        factory_timer_rect = factory_timer.get_rect(topleft = (width, height))
        screen.blit(factory_timer, factory_timer_rect)
        width += 50

#problem area
def set_factory_timer():
    global money_timer, factory_timers
    for i in factory_timers:
        pygame.time.set_timer(money_timer, 3000)



def check_factory_limit():
    global factory_limit
    if factory_price >= 400:
        limit = small_font.render('Factory Limit reached', False, WHITE)
        limit_rect = limit.get_rect(midtop = (WIDTH/2, F_HEIGHT))
        screen.blit(limit, limit_rect)
        factory_limit = True
        return factory_limit

        

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
dt = clock.tick(FPS)/1000
running = True
money = 0
base_font = pygame.font.Font('fonts/Minecraft.ttf', 50)
small_font = pygame.font.Font('fonts/Minecraft.ttf', 20)
Game_States = True
factory_price = 150
factories = []
factory_limit = False
factory_timers = []

#timer
#problem area
money_timer = pygame.USEREVENT + 1

#Written


return_back = small_font.render('Back on the grind', False, GREEN)
return_rect = return_back.get_rect(topleft = (10, 10))

title = small_font.render('Small factory', False, GREEN)
title_rect = title.get_rect(midright = (WIDTH/2, HEIGHT/2))




while running:
    pygame.key.set_repeat(0, 0)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if Game_States:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    money += 1
            
            if event.type == pygame.MOUSEBUTTONUP:
                if Shop_rect.collidepoint(event.pos):
                    Game_States = False
                else:
                    Game_States = True
            
            if event.type == money_timer:
                money += 25
        
        else:
            if event.type == pygame.MOUSEBUTTONUP:
                if return_rect.collidepoint(event.pos):
                    Game_States = True
                else:
                    Game_States = False
                if buy_rect.collidepoint(event.pos):
                    check_factory_limit()
                    if color == GREEN:
                        money -= factory_price
                        factory_timers.append(money_timer)
                        factory_price += factory_price
                        factories.append('factory')
                        set_factory_timer()



    if Game_States:
        screen.fill('black')

        money_maker_font = base_font.render(f'{int(money)}$', False, GREEN)
        money_rect = money_maker_font.get_rect(center = (WIDTH/2, HEIGHT/2))
        screen.blit(money_maker_font, money_rect)
        Shop_Button = base_font.render('Shop', False, YELLOW)
        Shop_rect = Shop_Button.get_rect(center = (WIDTH/3, HEIGHT/6))
        screen.blit(Shop_Button, Shop_rect)
        draw_factory(25, 200)
    
    else:
        screen.fill(YELLOW)
        screen.blit(return_back, return_rect)
        factory = pygame.draw.rect(screen, RED, (int(WIDTH/2), int(HEIGHT/2), 40, 40))
        screen.blit(title, title_rect)
        price_check()
        buy = small_font.render(f'BUY? {factory_price}$', False, BLACK)
        buy_rect = buy.get_rect(topright = (140, 170))
        border_rect = buy_rect.inflate(8,8)
        button = pygame.draw.rect(screen, color, buy_rect)
        button_2 = pygame.draw.rect(screen, color, border_rect, 10, 6)
        screen.blit(buy, buy_rect)
        

    pygame.display.flip()

    clock.tick(FPS)

pygame.quit()
0

There are 0 best solutions below