dremmy10
dremmy10

Reputation: 21

Pygame Timer Error (numbers print on top of eachother)

I am making an rpg and one of the elements of the game is hunger. Hunger increases by 1 every minute and if you get to 10 you "die" (pygame crashes). You can also buy food items to make your hunger go down, however I haven't added that in yet. Currently, the timer works to increase hunger level by 1 each minute but the minute values (1,2,3,4,5 etc) print on top of eachother on the screen. (Also, please ignore the odd food items in the store haha)

Can someone please help me fix this?

Here is my code:

import pygame, sys 

mainClock=pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.init()

def draw_text(text, font, color, surface, x, y):
    textobj=font.render(text, 1, color)
    textrect=textobj.get_rect()
    textrect.topleft=(x, y)
    surface.blit(textobj, textrect)

#Defining the store building on the map
 def store():
    global click

    font=pygame.font.SysFont('Comic Sans MS', 50,)
    colorC = (238,59,59)
    (widthC, heightC) = (1000, 800)
    screenC = pygame.display.set_mode((widthC, heightC))
    pygame.display.set_caption('Store')
    screenC.fill(colorC)
    draw_text('Inside The Store:', font, (0,0,0), screenC, 20,        20)

    mx, my=pygame.mouse.get_pos()
    cheetos_button=pygame.Rect(20, 325, 475, 50)
    mx, my=pygame.mouse.get_pos()                
    buttonC_color=(156,102,31)
    pygame.draw.rect(screenC, (buttonC_color), cheetos_button)
    text=font.render("Hot Cheetos: $5, -1 Hunger", True,(0,0,0))
    screenC.blit(text, (20,325))
    cheetoImg=pygame.image.load("hotcheetos.png")
    x=20
    y=50
    screenC.blit(cheetoImg, (x, y))

    mx, my=pygame.mouse.get_pos()
    icedtea_button=pygame.Rect(20, 585, 475, 50)
    mx, my=pygame.mouse.get_pos()                
    buttonC_color=(156,102,31)
    pygame.draw.rect(screenC, (buttonC_color), icedtea_button)
    text=font.render("Iced Tea: $5, -2 Hunger", True,(0,0,0))
    screenC.blit(text, (20,585))
    icedteaImg=pygame.image.load("icedtea.png")
    x=15
    y=380
    screenC.blit(icedteaImg, (x, y))

    mx, my=pygame.mouse.get_pos()
    pizza_button=pygame.Rect(525, 585, 475, 50)
    mx, my=pygame.mouse.get_pos()                
    buttonC_color=(156,102,31)
    pygame.draw.rect(screenC, (buttonC_color), pizza_button)
    text=font.render("Pizza Slice: $20, -5 Hunger", True,  (0,0,0))
    screenC.blit(text, (525,585))
    cheetoImg=pygame.image.load("pizza.png")
    x=450
    y=75
    screenC.blit(cheetoImg, (x, y))

    font=pygame.font.SysFont('Comic Sans MS', 25,)
    text=font.render("Your Money:", True,(0,0,0))
    screenC.blit(text, (400,20))
    currentM=0
    font=pygame.font.SysFont('Comic Sans MS', 25,)
    text=font.render(str(currentM), True,(0,0,0))
    screenC.blit(text, (510,20))

    #Hunger Timer
    pygame.init()

    done = False

    clock = pygame.time.Clock()

    font2 = pygame.font.Font(None, 25)

    frame_count = 0
    frame_rate = 60
    start_time = 90

    while not done:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT:  
                done = True  

        total_seconds = frame_count // frame_rate

        minutes = total_seconds // 60

        hunger_lvl = frame_count // frame_rate

        minutes = total_seconds // 60

        hunger_lvl = "Hunger Level: {0}".format(minutes)

        text2 = font2.render(hunger_lvl, True, (0,0,0))
        screenC.blit(text2, [700, 20])

        frame_count += 1

        clock.tick(frame_rate)

        pygame.display.flip()

    pygame.quit()

store()

Thank you to anyone who can help me out:)

Upvotes: 2

Views: 52

Answers (1)

Kingsley
Kingsley

Reputation: 14916

Since your "hunger level" is always increasing, using a PyGame timer event seems like a good solution.

It's fairly simple, define your own event based on pygame.USEREVENT, and ask pygame.timer.set_timer() to start sending you periodic events.

# Create an event to generate hunger
hunger_lvl   = 0  # not hungry
HUNGER_EVENT = pygame.USEREVENT + 1
STARVING     = 10
pygame.time.set_timer( HUNGER_EVENT, 60000 )   # Get a hunger event every 60000ms (1 minute)

Then it's just a matter of receiving the event in your main event loop:

# handle Event
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done = True
    elif event.type == HUNGER_EVENT:
        hunger_lvl += 1
        if hunger_lvl >= STARVING:
            print( "player starving in an age of abundance; why..." )

That's it.

Upvotes: 1

Related Questions