¿Cómo crear la función “Again”?
Creé mi propio juego, pero aún no sé ni entiendo cómo crear la función “Again” cuando haga clic en el botón. ¿Cómo crearlo? Mi código parcial:
“ `import pygame“`
“ def beginning ():
Beginning = True
while Beginning and not QuitState.quit:
clock.tick (FPS)
for event in pygame.event.get ():
#[…]
if event.type == pygame.QUIT:
QuitState.set_quit ()
`<pre> <code> mini_button = Button (230, 80)`
`screen.blit (important_phone_jpg, important_phone_rect)`
`print_text_5 ('Survival with Sonic', 320, 50)`
`mini_button.draw (535, 170, 'Play', game1)`
`mini_button1 = Button (440, 80)`
`pygame.display.update ()`
“ def game1 ():
global health
Game = True
while Game and not QuitState.quit:
clock.tick (FPS)
for event in pygame.event.get ():
#[…]
if event.type == pygame.QUIT:
QuitState.set_quit ()
“ <pre> <code> if player.score > = 900:
victory1 ()
`if health < = 0:`
`game_over1 ()`
`show_health ()`
`button = Button (195,80)`
`button.draw (20, 80, 'Pause', pause1)`
`print_text ('Points & # 39; + str (player.score), 725, 25)`
`print_text (':', 840, 25)`
`pygame.display.update ()`
`all_sprites.update ()`
`screen.blit (morning_png, morning_rect)`
`all_sprites.draw (screen)`
“
“ def pause1 ():
Pause = True
while Pause and not QuitState.quit:
clock.tick (FPS)
for event in pygame.event.get ():
if event.type == pygame.QUIT:
QuitState.set_quit ()
`<pre> <code> screen.blit (phone_jpg, phone_rect)`
`buttonq = Button (120, 80)`
`print_text_2 ('Do you want to continue playing?', 400, 270)`
`buttonq.draw (300, 340, 'Yes!', game1)`
`buttonq_1 = Button (140, 80)`
`buttonq_1.draw (870, 340, 'No!', beginning)`
`pygame.display.update ()`
“ def game<em>over1 ():
Game </em> over = True
while not QuitState.quit:
clock.tick (FPS)
for event in pygame.event.get ():
if event.type == pygame.QUIT:
QuitState.set_quit ()
`<pre> <code> screen.blit (game_over_jpg, game_over_rect)`
`button_5 = Button (235,80)`
`print_text_3 ('Unfortunately, you lost! Try again!', 135, 400)`
`button_5.draw (400, 500, 'Again', game1)`
`button_5.draw (700, 500, 'Back', beginning)`
`pygame.display.update ()`
“ def victory1 ():
Victory = True
while Victory and not QuitState.quit:
clock.tick (FPS)
for event in pygame.event.get ():
if event.type == pygame.QUIT:
QuitState.set_quit ()
`<pre> <code> screen.blit (victory_jpg, victory_rect)`
`button_5 = Button (205,90)`
`print_text ('Hurray! You won! My congratulations! Want to play again?', 280, 360)`
`button_5.draw (450, 500, 'Yes', game1)`
`button_5.draw (650, 500, 'No', beginning)`
`pygame.display.update ()`
`beginning ()`
pygame.quit ()
*Quiero que el jugador tenga la capacidad de volver a jugar cuando haga clic en los botones:
‘button5.draw (450, 500, ‘Yes’, game1) ‘
‘button 5.draw (400, 500, ‘Again’, game1) ‘
o cuando el jugador abandona el juego1 y vuelve a jugar:
‘mini_button.draw (535, 170, ‘Play’, game1)’
davy.ai
Para crear una función que permita al jugador volver a jugar cuando haga clic en un botón, se pueden modificar las funciones existentes
game_over1
yvictory1
de la siguiente manera:Aquí, primero creamos un objeto
Button
para los botones “Заново” y “Да”, respectivamente. Luego, los dibujamos usando el métododraw
. También comprobamos si el jugador hizo clic en el botón usando el métodois_clicked
, y si es así, establecemos la variable indicadora (Game_over
oVictory
) enFalse
y llamamos a la funcióngame1
para iniciar un nuevo juego.Tenga en cuenta que en la función
beginning
, también se puede agregar el mismo botón “Играть” para reiniciar el juego después de que el jugador haya terminado de jugar la primera vez:De manera similar a las funciones
game_over1
yvictory1
, primero creamos un objetoButton
para el botón “Играть”. Luego, lo dibujamos y comprobamos si el jugador hizo clic en él usando el métodois_clicked
. Si es así, establecemos la variable indicadoraBeginning
enFalse
y comenzamos el juego usando la funcióngame1
.Al agregar estas modificaciones al código, el jugador podrá comenzar un nuevo juego haciendo clic en los botones “Заново”, “Да” o “Играть”.