My reset() function is supposed to clear everything and redraw the fresh ones, but it do not work. Can somehone give me some advice?
import pygame
import string
import random
import time
import sys
pygame.init ()
size = (1600, 900)
win = pygame.display.set_mode(size)
pygame.display.set_caption('Gerador de encontros para Tomb of Annihilation')
win.fill((255,255,255))
pygame.display.update()
red = (200,0,0)
green = (0,200,0)
blue = (0,0,200)
black = (0,0,0)
white = (255,255,255)
encontro1 = "Aguardando"
encontro = 0
MONSTRO_T = "Arakrocra.jpg"
texto = "Monstro Puto"
item = "NONE"
image = pygame.image.load(MONSTRO_T)
imagePos = (1100,190)
win.blit(image, imagePos)
def text_objects(text, font, color):
textSurface = font.render(text, True, color)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,it,at,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",18)
textSurf, textRect = text_objects(msg, smallText,at)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
win.blit(textSurf, textRect)
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(win, ic,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText,it)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
win.blit(textSurf, textRect)
## x = eixoX, y = eixoY, w = Altura, h = Largura, ic = Cor inativa ( do botão ), ac = cor ativa, it = Inative text color, at = active text color
def M_generator():
return (random.randint(1,100))
def ef():
global encontro
global encontro1
encontro = 1
encontro1 = "Characters Level 1 to 4"
def en():
global encontro
global encontro1
encontro = 2
encontro1 = "Characters Level 4 to 8"
def em():
global encontro
global encontro1
encontro = 3
encontro1 = "Characters Level 8 or above"
def warning():
largeText = pygame.font.Font('freesansbold.ttf',50)
TextSurf, TextRect = text_objects('Selecione a dificuldade primeiro', largeText, red)
TextRect.center = ((700),(300))
win.blit(TextSurf, TextRect)
pygame.display.update()
def lore(tf,color,ta,tl,texto):
largeText = pygame.font.Font('freesansbold.ttf',tf)
TextSurf, TextRect = text_objects(texto, largeText, color)
TextRect.center = ((tl),(ta))
win.blit(TextSurf, TextRect)
pygame.display.update()
def quitgame():
pygame.quit()
exit()
def CACHES():
global texto
global item
roll = (random.randint(1,20))
if roll == 1:
texto = "Rain catcher (see chapter l) and mess kit "
item1 = (random.randint(1,100))
item = "1"
elif roll == 2:
texto = "10-day supply of preserved rations"
item1 = (random.randint(1,10))
item = str(item1)
elif roll == 3:
texto = "20-day supply of preserved rations"
item1 = (random.randint(1,20))
item = str(item1)
elif roll == 4:
texto = "50-day supply of preserved rations"
item1 = (random.randint(1,50))
item = str(item1)
elif roll == 5:
texto = "ld4 casks of water holding 5 gallons each"
item1 = (random.randint(1,4))
item = str(item1)
elif roll == 6:
texto = "ld4 casks oftej (see chapter l)"
item1 = (random.randint(1,4))
item = str(item1)
elif roll == 7:
texto = "ld4 climber's kits"
item1 = (random.randint(1,4))
item = str(item1)
elif roll == 8:
texto = "20-day supply of insect repellent salve (see chapter l) in a leather tube"
item1 = (random.randint(1,20))
item = str(item1)
elif roll == 9:
texto = "ld4 quivers, each containing ld20 arrows"
item1 = (random.randint(1,4))
item = str(item1)
elif roll == 10:
texto = "Canoe with six paddles"
item1 = (random.randint(1,100))
item = "1"
elif roll == 11:
texto = "2 hooded lanterns and 10 flasks of lamp oil"
item1 = (random.randint(1,10))
item = str(item1)
elif roll == 12:
texto = "Two-person tent and ld4 explorer's packs"
item1 = (random.randint(1,4))
item = str(item1)
elif roll == 13:
texto = "Wooden box containing 2dl0 daggers (low quality,of the type used as trade goods)"
item1 = (random.randint(2,20))
item = str(item1)
elif roll == 14:
texto = "20-day supply of insect repellent salve (see chapter l) in a leather tube"
item1 = (random.randint(1,20))
item = str(item1)
elif roll == 15:
texto = "Set of navigator's tools"
item1 = (random.randint(1,100))
item = "1"
elif roll == 16:
texto = "ldlO changes of woolen clothing"
item1 = (random.randint(1,10))
item = str(item1)
elif roll == 17:
texto = "Set of cartographer's tools"
item1 = (random.randint(1,100))
item = "1"
elif roll == 18:
texto = "Two-person tent and 1d4 healer's kits"
item1 = (random.randint(1,4))
item = str(item1)
elif roll == 19:
texto = "2 two-person tents, folding camp table, and four folding stools"
item1 = (random.randint(1,100))
item = "1"
else :
texto = "Wooden box containing 2d4 po lions of healing"
item1 = (random.randint(2,8))
item = str(item1)
def beach():
global MONSTRO_T
MONSTRO_N = (M_generator())
if (MONSTRO_N < 7):
if encontro == 1:
MONSTRO_T = "Arakocra.jpg"
imagePos = (900,190)
win.blit(image, imagePos)
lore(30,black,250,500,"Any character with a passive Wisdom (Perception) score ")
lore(30,black,300,500,"of 15 or higher spots ld4 + 1 aarakocra flying overhead. ")
lore(30,black,350,500,"These creatures are scouts from Kir Sabal or another ")
lore(30,black,400,500,"aerie. They observe the party from a safe distance")
lore(30,black,450,500,"but don't approach unless the characters demonstrate")
lore(30,black,500,500,"peaceful intentions. The bird folk are friendly and can")
lore(30,black,550,500,"point characters in the direction of nearby landmarks.")
grupo1 = (random.randint(1,4)+1)
print(grupo1)
grupo = str(grupo1)
lore(35,black,600,500,"Grupo de ")
lore(35,black,600,650,grupo)
if encontro == 2:
MONSTRO_T = "Arakocra.jpg"
imagePos = (900,190)
win.blit(image, imagePos)
lore(30,black,250,500,"Any character with a passive Wisdom (Perception) score ")
lore(30,black,300,500,"of 15 or higher spots 2d4 + 2 aarakocra flying overhead. ")
lore(30,black,350,500,"These creatures are scouts from Kir Sabal or another ")
lore(30,black,400,500,"aerie. They observe the party from a safe distance")
lore(30,black,450,500,"but don't approach unless the characters demonstrate")
lore(30,black,500,500,"peaceful intentions. The bird folk are friendly and can")
lore(30,black,550,500,"point characters in the direction of nearby landmarks.")
grupo1 = (random.randint(2,8)+2)
print(grupo1)
grupo = str(grupo1)
lore(35,black,600,500,"Grupo de ")
lore(35,black,600,650,grupo)
if encontro == 3:
MONSTRO_T = "Arakocra.jpg"
imagePos = (900,190)
win.blit(image, imagePos)
lore(30,black,250,500,"Any character with a passive Wisdom (Perception) score ")
lore(30,black,300,500,"of 15 or higher spots 3d4 + 3 aarakocra flying overhead. ")
lore(30,black,350,500,"These creatures are scouts from Kir Sabal or another ")
lore(30,black,400,500,"aerie. They observe the party from a safe distance")
lore(30,black,450,500,"but don't approach unless the characters demonstrate")
lore(30,black,500,500,"peaceful intentions. The bird folk are friendly and can")
lore(30,black,550,500,"point characters in the direction of nearby landmarks.")
grupo1 = (random.randint(3,12)+3)
print(grupo1)
grupo = str(grupo1)
lore(35,black,600,500,"Grupo de ")
lore(35,black,600,650,grupo)
elif ((MONSTRO_N > 7) and (MONSTRO_N < 10)):
MONSTRO_T = "Cache.jpg"
imagePos = (900,190)
win.blit(image, imagePos)
lore(30,black,250,500,"The party finds a cache of supplies left behind by other")
lore(30,black,300,500,"explorers. There's no way to ten from the cache whether")
lore(30,black,350,500,"those who left it are still alive and coming back for it, or")
lore(30,black,400,500,"dead. Roll a d20 and consult the Caches table to determine what")
lore(30,black,450,500,"the characters find. ")
lore(30,black,500,500,texto)
lore(30,black,500,700,item)
elif ((MONSTRO_N > 10) and (MONSTRO_N < 12)):
MONSTRO_T = "Chwinga.jpg"
elif ((MONSTRO_N > 12) and (MONSTRO_N < 14)):
MONSTRO_T = "Dinossauro, Allosaurus.jpg"
elif ((MONSTRO_N > 14) and (MONSTRO_N < 16)):
MONSTRO_T = "Dinossauro, Dimetrodon.jpg"
elif ((MONSTRO_N > 16) and (MONSTRO_N < 21)):
MONSTRO_T = "Dinossauro, Plesiosaurus.jpg"
elif ((MONSTRO_N > 21) and (MONSTRO_N < 28)):
MONSTRO_T = "Dinossauro, Pteranodon.jpg"
elif ((MONSTRO_N > 28) and (MONSTRO_N < 31)):
MONSTRO_T = "Dinossauro, Quetzalcoatlus.jpg"
elif ((MONSTRO_N > 31) and (MONSTRO_N < 37)):
MONSTRO_T = "Dinossauro, Velociraptor.jpg"
elif ((MONSTRO_N > 37) and (MONSTRO_N < 40)):
MONSTRO_T = "Dragon, Red.jpg"
elif ((MONSTRO_N > 40) and (MONSTRO_N < 42)):
MONSTRO_T = "Emerald Enclave.jpg"
elif ((MONSTRO_N > 42) and (MONSTRO_N < 46)):
MONSTRO_T = "Explorers.jpg"
elif ((MONSTRO_N > 46) and (MONSTRO_N < 49)):
MONSTRO_T = "Flaming Fist.jpg"
elif ((MONSTRO_N > 49) and (MONSTRO_N < 52)):
MONSTRO_T = "Flying Monkeys"
elif ((MONSTRO_N > 52) and (MONSTRO_N < 55)):
MONSTRO_T = "Flying Snakes.jpg"
elif ((MONSTRO_N > 55) and (MONSTRO_N < 57)):
MONSTRO_T = "Frost Giants"
elif ((MONSTRO_N > 57) and (MONSTRO_N < 63)):
MONSTRO_T = "Giant Lizard"
elif ((MONSTRO_N > 63) and (MONSTRO_N < 67)):
MONSTRO_T = "Giant Snapping Turtle"
elif ((MONSTRO_N > 67) and (MONSTRO_N < 71)):
MONSTRO_T = "LizardFolk"
elif ((MONSTRO_N > 71) and (MONSTRO_N < 74)):
MONSTRO_T = "Red Wizard"
elif ((MONSTRO_N > 74) and (MONSTRO_N < 84)):
MONSTRO_T = "Sea Hags"
elif ((MONSTRO_N > 84) and (MONSTRO_N < 87)):
MONSTRO_T = "Stirges"
elif ((MONSTRO_N > 87) and (MONSTRO_N < 89)):
MONSTRO_T = "Swarm of bats"
elif ((MONSTRO_N > 89) and (MONSTRO_N < 94)):
MONSTRO_T = "Tabaxy Hunter"
elif ((MONSTRO_N > 94) and (MONSTRO_N < 100)):
MONSTRO_T = "Tri-Flower Frond"
def noundead():
print("oioioi")
def Lundead():
print("oioioi")
def Gundead():
print("oioioi")
def mountains():
print("oioioi")
def rivers():
print("oioioi")
def ruins():
print("oioioi")
def swamp():
print("oioioi")
def wasteland():
print("oioioi")
def reset():
win.fill(white)
button("Facil",20,10,110,50,green,blue,black,white,ef)
button("Normal",150,10,110,50,green,blue,black,white,en)
button("Mortal",280,10,110,50,green,blue,black,white,em)
button("resete",410,10,110,50,green,blue,black,white,reset)
main()
def main():
while True:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
pygame.quit()
exit()
button("Facil",20,10,110,50,green,blue,black,white,ef)
button("Normal",150,10,110,50,green,blue,black,white,en)
button("Mortal",280,10,110,50,green,blue,black,white,em)
button("reset",410,10,110,50,green,blue,black,white,reset)
if encontro == 0:
button("Aguardando",930,10,300,50,green,blue,black,white)
elif encontro == 1:
button("Characters Level 1 to 4",930,10,300,50,green,blue,black,white)
elif encontro == 2:
button("Characters Level 4 to 8",930,10,300,50,green,blue,black,white)
elif encontro == 3:
button("Characters Level 8 or above",930,10,300,50,green,blue,black,white)
if encontro == 0:
button("Beach",20,100,110,50,green,blue,black,white,warning)
button("NoUndead",150,100,110,50,green,blue,black,white,warning)
button("- Undead",280,100,110,50,green,blue,black,white,warning)
button("+ undead",410,100,110,50,green,blue,black,white,warning)
button("Mountains",540,100,110,50,green,blue,black,white,warning)
button("Rivers",670,100,110,50,green,blue,black,white,warning)
button("Ruins",800,100,110,50,green,blue,black,white,warning)
button("Swamp",930,100,110,50,green,blue,black,white,warning)
button("Wasteland",1060,100,110,50,green,blue,black,white,warning)
button("Sair",1060,100,110,50,green,blue,black,white,quitgame)
elif encontro > 0:
win.fill(white)
if encontro == 1:
button("Characters Level 1 to 4",930,10,300,50,green,blue,black,white)
elif encontro == 2:
button("Characters Level 4 to 8",930,10,300,50,green,blue,black,white)
elif encontro == 3:
button("Characters Level 8 or above",930,10,300,50,green,blue,black,white)
button("Facil",20,10,110,50,green,blue,black,white,ef)
button("Normal",150,10,110,50,green,blue,black,white,en)
button("Mortal",280,10,110,50,green,blue,black,white,em)
button("Beach",20,100,110,50,green,blue,black,white,beach)
button("NoUndead",150,100,110,50,green,blue,black,white,noundead)
button("- Undead",280,100,110,50,green,blue,black,white,Lundead)
button("+ undead",410,100,110,50,green,blue,black,white,Gundead)
button("Mountains",540,100,110,50,green,blue,black,white,mountains)
button("Rivers",670,100,110,50,green,blue,black,white,rivers)
button("Ruins",800,100,110,50,green,blue,black,white,ruins)
button("Swamp",930,100,110,50,green,blue,black,white,swamp)
button("Wasteland",1060,100,110,50,green,blue,black,white,wasteland)
button("Sair",1060,100,110,50,green,blue,black,white,quitgame)
pygame.display.update()
main()
But when I use the reset() function, a loop crash the app.
What is wrong with my function?
I try use a lot of things, but I think my logic is wrong.
how I make a a reset() function to clear the app, and redraw everthing?
or, how to run from the start again?