'Python code stops running after I start my threads
I'm Trying to make a simple clicker game with pygame, and I'm trying to add a text that says '+1' when you click the button, but so it doesn't delay the code, I wanna put it in a second thread. I tried this but when i start both of the threads, the code just stops without any error messages or anything.
Here's The Code:
#Imports
from numerize import numerize
import schedule
import pygame
import time
from time import sleep
import sys
import random
import threading
#All The Variables#
running = True
SmallFuncRunning = True
#colors
red = (255, 45, 0)
orange = (255, 173, 0)
yellow = (255, 245, 0)
green = (0, 255, 0)
light_blue = (0, 255, 230)
blue = (0, 0, 128)
purple = (140, 0, 255)
pink = (255, 0, 243)
white = (255, 255, 255)
black = (0, 0, 0)
#windows
number_position = 125, 100
image_size = 500, 400
text_size = 32
image_place = 0, 0
windows_one_size = 250, 400
#animals
cat_count = 0
cat_price = 60
cat_EPS = cat_count * 1
dog_count = 0
dog_price = 100
dog_EPS = dog_count * 2
eagle_count = 0
eagle_price = 1000
eagle_EPS = eagle_count * 25
human_count = 0
human_price = 3500
human_EPS = human_count * 100
#numbers
UpgradePrice = 100
EPC = 1
number = 0
#Fuctions
#save numbers
def save_to_file_number():
with open('Saves.txts/Save_number.txt', 'a') as text:
text.truncate(1)
text.write(str(number))
def save_to_file_EPC():
with open('Saves.txts/Save_EPC.txt', 'a') as text:
text.truncate(1)
text.write(str(EPC))
def save_to_file_UpgradePrice():
with open('Saves.txts/Save_UP.txt', 'a') as text:
text.truncate(1)
text.write(str(UpgradePrice))
def save_to_file_cat_count():
with open('Saves.txts/Save_catcount.txt', 'a') as text:
text.truncate(1)
text.write(str(cat_count))
def save_to_file_dog_count():
with open('Saves.txts/Save_dogcount.txt', 'a') as text:
text.truncate(1)
text.write(str(dog_count))
def save_to_file_eagle_count():
with open('Saves.txts/Save_eaglecount.txt', 'a') as text:
text.truncate(1)
text.write(str(eagle_count))
def save_to_file_human_count():
with open('Saves.txts/Save_humancount.txt', 'a') as text:
text.truncate(1)
text.write(str(human_count))
def save_to_file_cat_price():
with open('Saves.txts/Save_catprice.txt', 'a') as text:
text.truncate(1)
text.write(str(cat_price))
def save_to_file_dog_price():
with open('Saves.txts/Save_dogprice.txt', 'a') as text:
text.truncate(1)
text.write(str(dog_price))
def save_to_file_eagle_price():
with open('Saves.txts/Save_eagleprice.txt', 'a') as text:
text.truncate(1)
text.write(str(eagle_price))
def save_to_file_human_price():
with open('Saves.txts/Save_humanprice.txt', 'a') as text:
text.truncate(1)
text.write(str(human_price))
#save all
def save_all():
save_to_file_EPC()
save_to_file_number()
save_to_file_UpgradePrice()
save_to_file_cat_count()
save_to_file_dog_count()
save_to_file_eagle_count()
save_to_file_human_count()
save_to_file_dog_price()
save_to_file_cat_price()
save_to_file_eagle_price()
save_to_file_human_price()
#Pygame Initialize / Updating
pygame.init()
with open('Saves.txts/Save_humanprice.txt')as f:
human_price = round(float(f.read()))
with open('Saves.txts/Save_humancount.txt')as f:
human_count = round(float(f.read()))
with open('Saves.txts/Save_eagleprice.txt')as f:
eagle_price = round(float(f.read()))
with open('Saves.txts/Save_eaglecount.txt')as f:
eagle_count = round(float(f.read()))
with open('Saves.txts/Save_dogprice.txt')as f:
dog_price = round(float(f.read()))
with open('Saves.txts/Save_dogcount.txt')as f:
dog_count = round(float(f.read()))
with open('Saves.txts/Save_catprice.txt')as f:
cat_price = str(round(float(f.read())))
if cat_price != 30 or 36 or 33 or 39:
cat_price = cat_price[1:]
with open('Saves.txts/Save_catcount.txt')as f:
cat_count = round(float(f.read()))
with open('Saves.txts/Save_UP.txt')as f:
UP = round(float(f.read()))
with open('Saves.txts/Save_EPC.txt')as f:
EPC = f.read()
EPC = int(EPC[1:])
#Backround
screen = pygame.display.set_mode((image_size))
#Window 1
pygame.display.set_caption('BadClickerGameLOL')
Window1 = pygame.image.load('Pictures/Window1.jpg')
Window1 = pygame.transform.scale(Window1, (windows_one_size))
font = pygame.font.Font('freesansbold.ttf', text_size)
#Window 2
Window2 = pygame.image.load('Pictures/Shop.jpg')
Window2 = pygame.transform.scale(Window2, (250, 200))
#Window 3
Window3 = pygame.image.load('Pictures/Shop.jpg')
Window3 = pygame.transform.scale(Window2, (250, 200))
#Number/Windows/EPC
def PlusOneText():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
PlusOne = font.render('+' + str(EPC), True, (42, 92, 222))
screen.blit(PlusOne, (random.randint(25, 150), random.randint(25, 150)))
pygame.display.update()
def MainFunc():
with open('Saves.txts/Save_humanprice.txt')as f:
human_price = round(float(f.read()))
with open('Saves.txts/Save_humancount.txt')as f:
human_count = round(float(f.read()))
with open('Saves.txts/Save_eagleprice.txt')as f:
eagle_price = round(float(f.read()))
with open('Saves.txts/Save_eaglecount.txt')as f:
eagle_count = round(float(f.read()))
with open('Saves.txts/Save_dogprice.txt')as f:
dog_price = round(float(f.read()))
with open('Saves.txts/Save_dogcount.txt')as f:
dog_count = round(float(f.read()))
with open('Saves.txts/Save_catprice.txt')as f:
cat_price = str(round(float(f.read())))
if cat_price != 30 or 36 or 33 or 39:
cat_price = cat_price[1:]
with open('Saves.txts/Save_catcount.txt')as f:
cat_count = round(float(f.read()))
with open('Saves.txts/Save_UP.txt')as f:
UP = round(float(f.read()))
with open('Saves.txts/Save_EPC.txt')as f:
EPC = f.read()
EPC = int(EPC[1:])
running = True
while running:
with open('Saves.txts/Save_number.txt') as f:
number = int(f.read())
f.close()
CompactNumber = numerize.numerize(number)
schedule.run_pending()
text = font.render(str(CompactNumber), True, (42, 92, 222))
text_rect = text.get_rect(center=(number_position))
screen.blit(text, text_rect)
screen.blit(Window1, (image_place))
screen.blit(text, (text_rect))
screen.blit(Window2, (251, 0))
screen.blit(Window3, (251, 201))
pygame.display.update()
#skeypressing
for event in pygame.event.get():
#Keypressing ifs
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
save_all()
sys.exit()
if event.key == pygame.K_r:
with open('Saves.txts/Save_number.txt', 'a') as text:
text.truncate(1)
text.write(str(0))
with open('Saves.txts/Save_EPC.txt', 'a') as text:
text.truncate(1)
text.write(str(1))
with open('Saves.txts/Save_UP.txt', 'a') as text:
text.truncate(1)
text.write(str(100))
with open('Saves.txts/Save_catcount.txt', 'a') as text:
text.truncate(1)
text.write(str(0))
with open('Saves.txts/Save_dogcount.txt', 'a') as text:
text.truncate(1)
text.write(str(0))
with open('Saves.txts/Save_eaglecount.txt', 'a') as text:
text.truncate(1)
text.write(str(0))
with open('Saves.txts/Save_humancount.txt', 'a') as text:
text.truncate(1)
text.write(str(0))
with open('Saves.txts/Save_catprice.txt', 'a') as text:
text.truncate(1)
text.write(str(60))
with open('Saves.txts/Save_dogprice.txt', 'a') as text:
text.truncate(1)
text.write(str(100))
with open('Saves.txts/Save_eagleprice.txt', 'a') as text:
text.truncate(1)
text.write(str(1000))
with open('Saves.txts/Save_humanprice.txt', 'a') as text:
text.truncate(1)
text.write(str(3500))
sys.exit()
if event.key == pygame.K_c:
if number >= int(cat_price):
number -= int(cat_price)
cat_count += 1
cat_price = float(int(cat_price) * 1.1)
print("The next cat price is " + str(int(cat_price)))
EPS += 1
save_to_file_EPS()
save_to_file_cat_count()
save_to_file_cat_price()
save_to_file_number()
else:
print('Insufficient Energy!')
print("You need " + str(int(cat_price)) + " to buy a cat!")
if event.key == pygame.K_d:
if number >= dog_price:
number -= int(dog_price)
dog_count += 1
dog_price = int(dog_price * 1.1)
print("The next dog price is " + str(dog_price))
EPS += 2
save_to_file_EPS()
save_to_file_dog_count()
save_to_file_dog_price()
save_to_file_number()
else:
print('Insufficient Energy!')
print("You need " + str(dog_price) + " to buy a dog")
if event.key == pygame.K_e:
if number >= eagle_price:
number -= int(eagle_price)
eagle_count += 1
eagle_price = int(eagle_price * 1.1)
print("The next dog price is " + str(eagle_price))
EPS += 25
save_to_file_EPS()
save_to_file_eagle_count()
save_to_file_eagle_price()
save_to_file_number()
else:
print('Insufficient Energy!')
print("You need " + str(eagle_price) + " to buy an eagle")
if event.key == pygame.K_h:
if number >= human_price:
number -= int(human_price)
human_count += 1
human_price = int(human_price * 1.1)
print("The next human price is " + str(human_price))
EPS += 100
save_to_file_EPS()
save_to_file_human_count()
save_to_file_human_price()
save_to_file_number()
else:
print('Insufficient Energy!')
print("You need " + str(human_price) + " to buy a human")
if event.key == pygame.K_SPACE:
number += int(EPC)
save_to_file_number()
if event.key == pygame.K_u:
if number >= UpgradePrice:
number -= int(UpgradePrice)
UpgradePrice = int(UpgradePrice*1.6)
print("The next Clicker Upgrade price is " + str(UpgradePrice))
EPC +=1
if number <= UpgradePrice:
print('You Need ' + str(int(UpgradePrice)) + ' To Buy Another Clicker Upgrade!')
save_to_file_EPC()
save_to_file_number()
save_to_file_UpgradePrice()
else:
print('Insufficient Energy!')
if event.type == pygame.QUIT:
save_all()
sys.exit()
threading.Thread(target=MainFunc).start()
threading.Thread(target=PlusOneText).start()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
