My original Solution was from this Comment on a similar question. However, if I do that and implement this into different parts of my code, it stops counting up the in-game Counter of CASH.
CASH = 0
DPS = 5
HPofMonster = 10
starttime=time.time()
def handle_monster():
global HPofMonster, CASH
if pygame.mouse.get_focused():
event = pygame.event.poll()
mouse_x, mouse_y = pygame.mouse.get_pos()
if GEN1.collidepoint(mouse_x, mouse_y) and event.type == MOUSEBUTTONDOWN and event.button == 1:
HPofMonster -= 1
print('click')
pass
else:
print('Not Pushed OR not on Monster')
pass
else:
print('Mouse not focused')
pass
if HPofMonster < 0:
CASH += 5
HPofMonster = 10
print('reset')
def handle_dps():
global HPofMonster
HPofMonster -= DPS
print("tick")
time.sleep(1.0 - ((time.time() - starttime) % 1.0))
def game_loop():
while True:
handle_dps()
handle_monster()
update_text()
handle_events()
def main():
pygame.display.set_caption('Clicker')
game_loop()
main()
When I don´t move my Mouse, my console looks like this:
tick
Not Pushed OR not on Monster
tick
Not Pushed OR not on Monster
tick
Not Pushed OR not on Monster
reset
tick
Not Pushed OR not on Monster
tick
Not Pushed OR not on Monster
tick
Not Pushed OR not on Monster
reset
tick
Not Pushed OR not on Monster
tick
Not Pushed OR not on Monster
tick
Not Pushed OR not on Monster
reset
Mouse not focused
Process finished with exit code 0
Here is the entire code:
import pygame, threading, time
from threading import Event, Thread
from pygame.locals import *
pygame.init()
WIDTH = 800
HEIGHT = 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
CASH = 0
DPS = 5
HPofMonster = 10
BACKGROUND_COLOR = (0, 0, 0)
BUTTON_COLOR = (0, 255, 0)
starttime=time.time()
FONT = pygame.font.SysFont('monospace', 15)
# Main Screen for drawing buttons
DRAW_SCREEN = pygame.Surface((WIDTH, HEIGHT))
DRAW_SCREEN.fill(BACKGROUND_COLOR)
# Buttons
GEN1 = pygame.draw.rect(DRAW_SCREEN, BUTTON_COLOR, pygame.Rect(200, 200, 200, 200), 1)
GEN1_LABEL = FONT.render('Defeat the Monster', 1, (255, 255, 0))
def handle_monster():
global HPofMonster, CASH
if pygame.mouse.get_focused():
event = pygame.event.poll()
mouse_x, mouse_y = pygame.mouse.get_pos()
if GEN1.collidepoint(mouse_x, mouse_y) and event.type == MOUSEBUTTONDOWN and event.button == 1:
HPofMonster -= 1
print('click')
pass
else:
print('Not Pushed OR not on Monster')
pass
else:
print('Mouse not focused')
pass
if HPofMonster < 0:
CASH += 5
HPofMonster = 10
print('reset')
def handle_events():
event_dict = {
pygame.QUIT: exit,
}
for event in pygame.event.get():
if event.type in event_dict:
event_dict[event.type]()
def update_text():
global CASH_LABEL, DPS_LABEL, GEN1_LABEL, DPS, CASH
WINDOW.blit(DRAW_SCREEN, (0, 0))
WINDOW.blit(GEN1_LABEL, (10, 108))
CASH_LABEL = FONT.render('Total Cash: ${}'.format(CASH), 1, (255, 255, 0))
DPS_LABEL = FONT.render('Total DPS: ${}'.format(DPS), 1, (255, 65, 0))
WINDOW.blit(CASH_LABEL, (0, 0))
WINDOW.blit(DPS_LABEL, (0, 20))
pygame.display.flip()
def handle_dps():
global HPofMonster
HPofMonster -= DPS
print("tick")
def game_loop():
while True:
handle_dps()
handle_monster()
update_text()
handle_events()
def main():
pygame.display.set_caption('Clicker')
game_loop()
main()