How to make a button repeat its action?

0

As an entertainment I'm doing a "mini-game" based on Stargate, the platform is pygame, the function of the program is to get random addresses based on a dictionary, the theme is that the program works only once, when you give it button to make the call, you get a single address, and what I want is that every time you hit the button a new address comes out.

The program I have separated in 2 files for now, one is the main program and the other is where I store the variables (in this case are the chevrons), and the random function that takes the chevrons from the dictionary, and does not how to do to restart the variables and return to take another chevron roll, I would appreciate guidance in this matter, since I do not like to be leaving things half by ignorance, I leave the code for you to take a look, thank you very much in advance and greetings.

from cheurones import *


class Cursor(pygame.Rect):
    def __init__(self):
    pygame.Rect.__init__(self, 0, 0, 1, 1)

def update(self):
    self.left, self.top = pygame.mouse.get_pos()


class Boton(pygame.sprite.Sprite):
    def __init__(self, imagen1, imagen2, x=50, y=250):
        self.imagen_normal = imagen1
        self.imagen_seleccion = imagen2
        self.imagen_actual = self.imagen_normal
        self.rect = self.imagen_actual.get_rect()
        self.rect.left, self.rect.top = (x, y)

def update(self, pantalla, cursor):
    if cursor.colliderect(self.rect):
        self.imagen_actual = self.imagen_seleccion
    else:
        self.imagen_actual = self.imagen_normal

    pantalla.blit(self.imagen_actual, self.rect)


def main():
    pygame.init()
    pantalla = pygame.display.set_mode((600, 338))
    background_image = pygame.image.load('stargate-fondo.jpg')
    pantalla.blit(background_image, (0, 0))
    pygame.display.set_caption("Llamadas Stargate")
    reloj = pygame.time.Clock()
    bot1 = pygame.image.load('boton3.png')
    bot2 = pygame.image.load('boton4.png')
    boton1 = Boton(bot1, bot2)
    cursor1 = Cursor()
    shape_color = (40, 210, 250)
    salir = False
    presionado = False

while not salir:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            salir = True

    reloj.tick(15)
    pantalla.fill((0, 0, 0))
    pantalla.blit(background_image, (0, 0))

    cursor1.update()
    boton1.update(pantalla, cursor1)

    cuadrados_azules(pantalla, shape_color)

    if event.type == pygame.MOUSEBUTTONUP:
        if cursor1.colliderect(boton1.rect):
            presionado = True

    if presionado:
        pon_cheurones(pantalla)

    pygame.display.update()

pygame.quit()


def cuadrados_azules(pantalla, shape_color):
    pygame.draw.rect(pantalla, shape_color, (18, 18, 52, 52), 0)
    pygame.draw.rect(pantalla, shape_color, (78, 18, 52, 52), 0)
    pygame.draw.rect(pantalla, shape_color, (138, 18, 52, 52), 0)
    pygame.draw.rect(pantalla, shape_color, (198, 18, 52, 52), 0)
    pygame.draw.rect(pantalla, shape_color, (258, 18, 52, 52), 0)
    pygame.draw.rect(pantalla, shape_color, (318, 18, 52, 52), 0)
    pygame.draw.rect(pantalla, shape_color, (378, 18, 52, 52), 0)


def pon_cheurones(pantalla):
    pantalla.blit(cheuron1, (20, 20))
    pantalla.blit(cheuron2, (80, 20))
    pantalla.blit(cheuron3, (140, 20))
    pantalla.blit(cheuron4, (200, 20))
    pantalla.blit(cheuron5, (260, 20))
    pantalla.blit(cheuron6, (320, 20))
    pantalla.blit(origen, (380, 20))
    pygame.display.flip()


main()
    
asked by Juan Tavio 04.03.2018 в 10:42
source

0 answers