es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿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 &gt; = 900:
victory1 ()

`if health &lt; = 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)’

Tags:  ,

Answer

  1. Avatar for 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 y victory1 de la siguiente manera:

    def game_over1():
        Game_over = True
        while not QuitState.quit:
            clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    QuitState.set_quit()
    
            screen.blit(game_over_jpg, game_over_rect)
            button_5 = Button(235,80)
            print_text_3('К сожалению, вы проиграли! Попробуйте ещё раз!', 135, 400)
            button_5.draw(400, 500, 'Заново', game1)
            button_5.draw(700, 500, 'Назад', beginning)
            pygame.display.update()
    
            # Comprobar si el jugador hizo clic en el botón "Заново"
            if button_5.is_clicked(400, 500):
                Game_over = False
                game1()
    
    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()
    
            screen.blit(victory_jpg, victory_rect)
            button_5 = Button(205,90)
            print_text('Ура! Вы победили! Мои поздравления! Хотите сыграть ещё раз?', 280, 360)
            button_5.draw(450, 500, 'Да', game1)
            button_5.draw(650, 500, 'Нет', beginning)
            pygame.display.update()
    
            # Comprobar si el jugador hizo clic en el botón "Да"
            if button_5.is_clicked(450, 500):
                Victory = False
                game1()
    

    Aquí, primero creamos un objeto Button para los botones “Заново” y “Да”, respectivamente. Luego, los dibujamos usando el método draw. También comprobamos si el jugador hizo clic en el botón usando el método is_clicked, y si es así, establecemos la variable indicadora (Game_over o Victory) en False y llamamos a la función game1 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:

    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() 
    
            mini_button = Button(230, 80)
            screen.blit(important_phone_jpg, important_phone_rect)
            print_text_5('Выживание с Соником', 320, 50)
            mini_button.draw(535, 170, 'Играть', game1)
            mini_button1 = Button(440, 80)
            pygame.display.update()
    
            # Comprobar si el jugador hizo clic en el botón "Играть"
            if mini_button.is_clicked(535, 170):
                Beginning = False
                game1()
    

    De manera similar a las funciones game_over1 y victory1, primero creamos un objeto Button para el botón “Играть”. Luego, lo dibujamos y comprobamos si el jugador hizo clic en él usando el método is_clicked. Si es así, establecemos la variable indicadora Beginning en False y comenzamos el juego usando la función game1.

    Al agregar estas modificaciones al código, el jugador podrá comenzar un nuevo juego haciendo clic en los botones “Заново”, “Да” o “Играть”.

Comments are closed.