我无法在点击游戏中设置计时器

-1 投票
0 回答
15 浏览
提问于 2025-04-12 01:57

我刚开始学pygame,正在做我的第一个游戏,经过一些训练后开始动手。我正在制作一个点击类游戏,玩家可以购买工厂来赚取收入。这个游戏有个特别之处,就是要交税,还得管理资源才能活到第二天。现在的问题是,计时器似乎不太对劲,它在一个函数里面,但我不知道该把它放在哪里。虽然我试着把这个函数放到不同的地方,但似乎没有什么效果。

另外,这是我第一次在Stack Overflow发帖,如果问题有任何不妥之处,请多多包涵。

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 个回答

暂无回答

撰写回答