pygame and actions by holding the button

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 700
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

pygame and actions by holding the button

#1 Post 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


Post Reply

Who is online

Users browsing this forum: No registered users