Page 1 of 1

pygame and actions by holding the button

Posted: Mon Aug 06, 2018 3:02 pm
by Andredron
Tried to create a function (by type of action,Call...) to hold the button. A friend told how a in PyGame can for example register the color change of the buttons when you hold.

Code: Select all

label main_menu:
    return
 
define _confirm_quit = False
 
init python:
   
    import pygame
   
    class KeyHold(renpy.Displayable):
   
        def __init__(self,**kwargs):
            super(KeyHold, self).__init__(**kwargs)
           
        def render(self,w,h,st,at):
            global foo
            if foo: r = Solid('#0f0').render(100,100,st,at)
            else:   r = Solid('#f00').render(100,100,st,at)
            renpy.redraw(self,0)
            return r
           
        def event(self, event, x, y, st):
            global foo
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    foo = True
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    foo = False
 
image keycatcher = KeyHold()
                   
label start:
   
    $ foo = False
   
    show keycatcher at truecenter
 
    "press a"
 
    return
and change the variable when the button is held

Code: Select all

label main_menu:
    return
define _confirm_quit = False
 
init python:
   
    import pygame
   
    class Indicator(renpy.Displayable):
        '''
       Used in this example only to track the change of a variable in real time.
       Draws a red box if the global variable foo == False,
       draws a green square, if the global variable foo == True.
       '''
        def render(self,w,h,st,at):
            global foo
            if foo: r = Solid('#0f0').render(100,100,st,at)
            else:   r = Solid('#f00').render(100,100,st,at)
            renpy.redraw(self,0)
            return r
 
    class KeyCatcher(renpy.Displayable):
        '''
       When you are on the screen receives events and allows you to respond to them.
       '''
        def render(self,w,h,st,at):
            return Null().render(w,h,st,at)
           
        def event(self, event, x, y, st):
            global foo
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    foo = True
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_a:
                    foo = False
 
screen barsik():
    add KeyCatcher()
    frame:
        align (0.5,0.5)
        vbox:
            add Indicator()
            label "hold a":
                align (0.5,0.5)
                   
label start:
   
    $ foo = False
   
    call screen barsik
   
    "end"
 
    return


And viewtopic.php?f=8&t=26961&p=327013&hilit=button#p327339

viewtopic.php?t=68143

Re: pygame and actions by holding the button

Posted: Mon Jan 20, 2020 12:41 pm
by Andredron