-1

How do you do x every x times. Im trying to in my code, town hall class add money(which is apart of the city class called) every 50 secs using the def function adding_money, but I'm having trouble finding or doing the code, I thought that you can use time.sleep() but that did not work as it slowed down my code, how do I do this without obstructing the main event code or in a class def?

import pygame
from pygame.locals import *
import datetime
import random
import time
import pickle
import os
import math
import threading 

os.system
vec = pygame.math.Vector2
pygame.init()
FPS = 30
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
GRAY = (127, 127, 127)
BLACK = (0, 0, 0)
HEIGHT = 800
WIDTH = 500
Font = pygame.font.SysFont('timesnewroman', 30)



gamedisplay = pygame.display.set_mode((HEIGHT, WIDTH))
clock = pygame.time.Clock()









    

class city():
    def __init__(self):
        self.city_x =300
        self.city_y = 300
        self.money = 0
        self.level = 0
        self.city_happiness = 0
        self.food = 0
        self.experience = 0
        self.water = 0


    def draw(self, display):
        self.text = Font.render('citiesmoney:' + str(self.money),2,(BLACK))
        gamedisplay.blit(self.text, (self.city_x, self.city_y-300))

    def add_money(self):
        
           self.money += 1
        
        


class c_panel():
    def __init__():
        pass

class buildings():
    def __init__():
        self.b_health = 100

class town_hall(buildings):
    def __init__(self):
        self.cit = city()

    
        

    def adding_money(self):
       
            self.cit.add_money
            

    
         



class monster():
    def __init__():
        pass

class citizens():
    def __init__(self):
        self.x = 100
        self.y = 100
        self.image = pygame.Surface((100, 100))
        self.raidimg = pygame.Surface((100, 100))
        self.joinimg = pygame.Surface((200, 100))
        self.recjin = self.raidimg.get_rect()
        self.recri = self.raidimg.get_rect()
        self.recri.center = 300//2, 300//2
        self.rect = self.image.get_rect(center = (self.x, self.y))
        self.rect.center = 300//2, 300//2
        self.moving = False
        self.c_health = 100
        self.c_mood = 100
        self.c_hunger = 100
        self.c_intelligence = 0
        self.c_tempature = 50
        self.c_firstname = ["Valerie", "Bill","Jill"]
        self.c_lastname = ["Harper","Benedict","Smith"]
        self.c_speed = 0
        self.c_strength = 0
        self.c_gender = random.randint(0,5)
        self.fullname = random.choice(self.c_firstname) + random.choice(self.c_lastname)

   
    def c_mousedrag(self):
        if event.type == MOUSEBUTTONDOWN:
            if self.rect.collidepoint(event.pos):
                  
                self.moving = True
 
        elif event.type == MOUSEBUTTONUP:
            self.moving = False
 
       
        elif event.type == MOUSEMOTION and self.moving:
            self.rect.move_ip(event.rel)


    def c_raidfight(self):
        if event.type == MOUSEBUTTONDOWN:
            if self.recri.collidepoint(event.pos):
                self.c_health -= 1
                
        elif event.type == MOUSEBUTTONUP:
            pass
 
       
                    
            
    
        
                        
    
    
   
    
    
           
           
    def draw(self, display):
        self.text = Font.render('fullname:' + (self.fullname),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-100))
        
        self.text = Font.render('Health:' + str(self.c_health),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-70))
        
        self.text = Font.render('mood:' + str(self.c_mood),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-60))
        
        self.text = Font.render('intelligence:' + str(self.c_intelligence),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-50))
        
        self.text = Font.render('tempature:' + str(self.c_tempature),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-40))
        
        self.text = Font.render('speed:'+ str(self.c_speed),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-30))
        
        self.text = Font.render('strength:'+ str(self.c_strength),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-20))

        self.text = Font.render('hunger:'+ str(self.c_hunger),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-10))

        self.text = Font.render('gender:'+ str(self.c_gender),2,(BLACK))
        gamedisplay.blit(self.text, (self.rect.x-100, self.rect.y-2))

        gamedisplay.blit(self.image, self.rect)
        gamedisplay.blit(self.raidimg, self.recri)







town = city()
townhall = town_hall()
citizen = citizens()


while True:
        clock.tick(FPS)

        
        for event in pygame.event.get():
          if event.type == pygame.QUIT:
                pygame.quit()

          

                #exit()
          
        gamedisplay.fill(WHITE)
        citizen.draw(gamedisplay)
        citizen.c_mousedrag()
        citizen.c_raidfight()
        townhall.adding_money()
        town.draw(gamedisplay)
        
        pygame.display.update()
        
        pygame.display.flip()



   
  • [How do I use a PyGame timer event? How to add a clock to a pygame screen using a timer?](https://stackoverflow.com/a/59944869) Or, create a thread timer. – 001 Jul 11 '22 at 15:08

1 Answers1

0

First, you should set some constant variables for the frame count and the game time. Then in your adding money method you have a couple of things going on. One: Add one to the frame count, since the method is called once every iteration of the game loop. Two: check to see if the frame count divided by your FPS is equal to zero, with this logic: if frame_count % FPS == 0: Then in this if statement, add to the game time and set the frame count back to zero. Then you will have a variable that counts up every second. Finally, you can check make a check for the game time like this: if game_time % 50 == 0: and add to your city's cash supply, finally setting the game time back to 0 since none of your other code relies on the timer.

Dominic B.
  • 77
  • 7
  • Is it something that I can do multiple times – loudmounth artist Jul 12 '22 at 05:46
  • Could I do counter = 0 counter += 1 if counter == 100: counter = 0 – loudmounth artist Jul 12 '22 at 05:59
  • You can do it multiple times. The pseudo code I layed out sets the game time back to 0 when it divided by 50 is zero and you add the cash to your city, and it will keep doing so every 50 seconds. If you just have one counter, it will count up at the rate of the script running, so having something count the frames might be easier to do since it just makes the logistics of the code easier to understand. Basically the game time variable counts the actual seconds which is easier on your brain, but to do that you need a frame count variable that increments it. – Dominic B. Jul 12 '22 at 21:44