Buongiorno, sto facendo un videogioco simile a flappy bird per un compito di informatica insieme a dei miei compagni(che non stanno facendo un emerita mazza) e ho creato un pop up che esce fuori quando muori con yes e no, e quando premi su no il videogioco si chiude, ma, quando premi su yes per continuare a giocare il programma non si riavvia e il pop up non scompare. Ringrazio anticipamente per le risposte.
Python:
#importo tutte le librerie che mi servono
import pygame
import time
from random import randint,randrange
from tkinter import *
from tkinter import messagebox
black= (0,0,0)
white= (255,255,255)
sunset = (253,72,47)
greenyellow=(184,255,0)
brightblue=(47,228,253)
orange=(255,236,0)
yellow=(255,236,0)
purple=(252,67,255)
colorChoices = [greenyellow,brightblue,orange,yellow,purple]
#assegno dei colori come tuple in modo da poterli ricavare, e creo una lista con tutti i colori
pygame.init()#inizializzo python
surfaceWidth = 800#do la lunghezza della finestra
surfaceHeight = 500#do l'altezza della finestra
imageHeight = 100#mi salvo l'altezza dell'immagine
imageWidth= 100#mi salvo lo spessore dell'immagine
surface = pygame.display.set_mode((surfaceWidth,surfaceHeight))#inizializzo la finestra usando le variabili surfaceWidth e surfaceHeight
pygame.display.set_caption('Jupiter Escape by me')#do il nome alla finestra
clock=pygame.time.Clock()#creo un "orologio" che calcola il tempo
img=pygame.image.load('Helicopter.jpg')#inizializzo la navicella
def score(count):#creo la funzione che stamperà i punti
font = pygame.font.Font('freesansbold.ttf',20)#inizializzo font e grandezza della scritta
text = font.render("Score: "+str(count), True, white)#creo una "lavagna" su cui stampo score + il punteggio, gli do il true per creare la scritta con angoli smussati e gli do il colore bianco
surface.blit(text, [0,0])#incollo la lavagna scritta prima in alto a destra
def blocks(x_block,y_block,block_width,block_height,gap,colorChoice):
pygame.draw.rect(surface,colorChoice,[x_block,y_block,block_width,block_height])
pygame.draw.rect(surface,colorChoice,[x_block,y_block+block_height+gap,block_width,surfaceHeight])
def replay_or_quit():
for event in pygame.event.get([pygame.KEYDOWN,pygame.KEYUP, pygame.QUIT]):
if event.type==pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
continue
return event.key
return None
def gameOver():
Tk().wm_withdraw() #nascondo la finestra del gioco
if messagebox.askyesno('Hai perso!','Vuoi riprovare?') == False:
pygame.quit()
quit()
def helicopter(x,y,image):
surface.blit(img, (x,y))
def main():
x= 150
y= 200
y_move = 0
x_block = surfaceWidth
y_block = 0
block_width = 75
block_height = randint(0, (surfaceHeight/2))
gap = imageHeight * 3
block_move = 4
current_score=0
blockColor= colorChoices[randrange(0,len(colorChoices))]
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over=True
if event.type== pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_move = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_move = 5
y += y_move
surface.fill(black)
helicopter(x,y,img)
blocks(x_block, y_block, block_width, block_height, gap,blockColor)
score(current_score)
x_block -= block_move
if y > surfaceHeight or y < 0:
gameOver()
if x_block < (-1*block_width):
x_block = surfaceWidth
block_height = randint(0, surfaceHeight/2)
blockColor= colorChoices[randrange(0,len(colorChoices))]
if x+imageWidth > x_block:
if x<x_block+block_width:
if y<block_height:
if x - imageWidth < block_width + x_block:
gameOver()
if x + imageWidth > x_block:
if y + imageHeight > block_height + gap:
if x < block_width + x_block:
gameOver()
if x < x_block and x>x_block - block_move:
current_score +=1
pydef gameOver():
Tk().wm_withdraw() #nascondo la finestra del gioco
if messagebox.askyesno('Hai perso!','Vuoi riprovare?') == False:
pygame.quit()
quit()
elif messagebox.askyesno('Hai perso!','Vuoi riprovare?') == True:
game_over = False
main()game.display.update()
clock.tick(60)
main()
pygame.quit()
quit()