Shake your screen, not your bootie

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

Shake your screen, not your bootie

#1 Post by monele »

Playing with movement functions again ^^;... It's been a long time... things have changed... and I'm lost @_@.

What I want to do : the same thing as vpunch/hpunch, except that the movement is not limited to being horizontal or vertical... it would shake all around. Think Phoenix Wright.
If possible, I'd like this to applicable to either the whole screen (explosions) or to a single sprite (someone freezing? :3). I suppose this means I'll need a transition for "with" and a function for "at".

I tried to make my own function by copying Revolve and the Revolver class. My only real problem so far is that I'd like the function to know where the object already is, ie. not having to give it a specific position.
For example :

Code: Select all

show something at MoveUp(50)
could be a function that moves the object up 50 pixels from its previous position... Is this even possible?


EDIT :

It's not perfect yet (it seems to shake mostly from top-left to bottom-right and it doesn't register anchors ;o;...) but if you want to put some "OBJECTION!" in your future games...

Code: Select all

init:

    python:
    
        import math

        class Shaker(object):
        
            anchors = {
                'top' : 0.0,
                'center' : 0.5,
                'bottom' : 1.0,
                'left' : 0.0,
                'right' : 1.0,
                }
        
            def __init__(self, start, child, dist, freq):
                self.start = [ self.anchors.get(i, i) for i in start ]  # start position
                self.dist = dist    # how many pixels away from the start position it should go (maximum distance)
                self.freq = freq    # how many "rebounds" (going away and centering back)
                self.child = child
                
            def __call__(self, t, sizes=(None, None, None, None)):
        
                xpos = self.start[0]
                ypos = self.start[1]
        
                d = 5  # friction
                f = self.freq   # fréquence de rebonds
                
                # tc : valeur courante passée par la fonction "dampened harmonic"
                tc = (d**(-t))*math.sin(t*math.pi*f)

                nx = xpos + ((tc) * self.dist) * renpy.random.random()
                ny = ypos + ((tc) * self.dist) * renpy.random.random()
        
                return (int(nx), int(ny), 0, 0)
        
        
        def Shake(start, time, child=None, dist=100.0, freq=25, **properties):
        
            return Motion(Shaker(start, child, dist=dist, freq=freq),
                          time,
                          child,
                          add_sizes=False,
                          **properties)
    
    #

    $ sshake = Shake((0.5,0.5), 1.0, dist=50, freq=15)
    
#
As for using it...

Code: Select all

    show girl with dissolve
    cor "Didn't you hear something?"
    
    show girl at Shake((0.5, 0.5), 1.0, dist=50)
    with None
    
    $ renpy.pause(2)
    
    h "... Okay, stop shaking now!"
    
    with sshake
    
    h "Gguaah... I'm going to puke!"

Frequency (freq) is what will make it look either like an explosion, a Phoenix Wright shake or a dizzy-spell. 15 is good for a fullscreen explosion shake. 25 or more to emulate Phoenix Wright. Less to make it "woaah, dizzy".

Arg... except the "woah dizzy" doesn't work so well >.>... Will have to fix this ^^;

yummy
Miko-Class Veteran
Posts: 733
Joined: Fri Jul 07, 2006 9:58 pm
Projects: Suna to Majo
Location: France
Contact:

#2 Post by yummy »

Objection !

Haha, it really kicks off. Thanks Monele ! I should really study python one of these days to start doing funny things like that.

derik
Regular
Posts: 57
Joined: Wed Oct 18, 2006 4:56 am
Contact:

Re: Shake your screen, not your bootie

#3 Post by derik »

monele wrote:It's not perfect yet (it seems to shake mostly from top-left to bottom-right and it doesn't register anchors ;o;...) but if you want to put some "OBJECTION!" in your future games...
*puts one of his character sprites into a blank template and tries it out*

Bwehehehe he he he... :~)

Oh man, that's fun!
*does it again*

If you get the return-to-origin bit down, you could combine this with an imagemap for hours of entertainment.

"Stop poking me! Stop poking me! IT GIVES ME SEIZURES!"

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#4 Post by PyTom »

You can use MoveTransition to get at the location of various images on the screen. Apart from that, there isn't really a good way to get the object's position... largely because the position isn't computed until after the Motion runs.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#5 Post by monele »

If you want a more random (*really* going all over the place) shaking, replace the two "nx" and "ny" lines with this :

Code: Select all

nx = xpos + (1.0-t) * self.dist * (renpy.random.random()*2-1)
ny = ypos + (1.0-t) * self.dist * (renpy.random.random()*2-1)
PyTom : any idea why using xanchor and yanchor won't work when calling Shake? I do use Motion here, as I did with my other Ease functions...

And the thing about position is mostly because, while you'd use "show something at left" which is simple, you will have to use "at Shake((0.0, 1.0), xanchor='left', yanchor='bottom')" to have it shake at the same position. I didn't find a way to reuse predefined positions in movement functions :/

EDIT : actually, it seems like it interprets (0.5,0.5) as being "half a pixel" instead of "middle" ô_o;... So maybe xanchor actually works...

User avatar
DaFool
Lemma-Class Veteran
Posts: 4171
Joined: Tue Aug 01, 2006 12:39 pm
Contact:

#6 Post by DaFool »

Sweet! Thanks monele, I may have use for this effect.

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#7 Post by monele »

I'm glad to see this being useful ^.^.

The new Shake is able to deal with floats now, so scratch that question. I still can't get anchors to work though ^^;...

Also, looking for information about MoveTransition and its factory parameter, I realize I had already asked about it but it didn't work at the time (bug fixed in 5.6.2). Hadn't asked again since then, so... PyTom, how does one use the factory parameter? I mean, could here be an example of a factory function?

EDIT : Yatta! My Shaker actually returned the wrong values (it always returned the anchors at 0... >.>...). Now all that's left is to understand this factory thingie and I'll release this in the cookbook ^^

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#8 Post by PyTom »

Basically, MoveTransition takes three factory functions as arguments, one for images that are staying on the screen, one for images that are entering, and one for images that are leaving. Each is called with the image, and with its positions. The factories are expected to return a Displayable that replaces that image... usually a Motion.

It should be documented on the wiki.

You might also be interested in the relatively new add_sizes parameter to Motion, which causes the motion function to be called with various sizes.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#9 Post by monele »

I'm struggling with MoveTransition and the factory right now @_@... I found the "default_move_factory" in the source and tried to make my own, invoking Shake, but I'm getting a Curry error @_@... I'm reminded of the EaseTransition nightmare of months ago.

As for add_sizes... I still don't understand what it does ô_o;... What kind of "various sizes"? *lost*


Ok... my current problem is having me scream each time I launch the test and it fails XD...

(see attached script)


If I comment any of the "move = move()" lines, I get this :

Code: Select all

-- Full Traceback ------------------------------------------------------------

  File "D:\Jeux\renpy-5.6.6\renpy\bootstrap.py", line 165, in bootstrap
  File "D:\Jeux\renpy-5.6.6\renpy\main.py", line 273, in main
  File "D:\Jeux\renpy-5.6.6\renpy\main.py", line 92, in run
  File "D:\Jeux\renpy-5.6.6\renpy\execution.py", line 97, in run
  File "D:\Jeux\renpy-5.6.6\renpy\ast.py", line 616, in execute
  File "D:\Jeux\renpy-5.6.6\renpy\exports.py", line 617, in with
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 970, in with
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 1101, in interact
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 1282, in interact_core
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 185, in visit_all
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 1282, in <lambda>
AttributeError: 'Curry' object has no attribute 'per_interact'

While executing game script on line 128 of warp/game/script.rpy.

Ren'Py Version: Ren'Py 5.6.6a
Otherwise, I get this :

Code: Select all

-- Full Traceback ------------------------------------------------------------

  File "D:\Jeux\renpy-5.6.6\renpy\bootstrap.py", line 165, in bootstrap
  File "D:\Jeux\renpy-5.6.6\renpy\main.py", line 273, in main
  File "D:\Jeux\renpy-5.6.6\renpy\main.py", line 92, in run
  File "D:\Jeux\renpy-5.6.6\renpy\execution.py", line 97, in run
  File "D:\Jeux\renpy-5.6.6\renpy\ast.py", line 616, in execute
  File "D:\Jeux\renpy-5.6.6\renpy\exports.py", line 617, in with
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 970, in with
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 1101, in interact
  File "D:\Jeux\renpy-5.6.6\renpy\display\core.py", line 1260, in interact_core
  File "D:\Jeux\renpy-5.6.6\renpy\curry.py", line 37, in __call__
  File "D:\Jeux\renpy-5.6.6\renpy\display\transition.py", line 882, in MoveTransition
  File "D:\Jeux\renpy-5.6.6\renpy\display\transition.py", line 780, in merge_slide
  File "D:\Jeux\renpy-5.6.6\renpy\display\transition.py", line 852, in merge_slide
  File "warp/game/script.rpy", line 101, in shakemove
  File "D:\Jeux\renpy-5.6.6\renpy\curry.py", line 37, in __call__
  File "warp/game/script.rpy", line 87, in _Shake
TypeError: __call__() takes at least 2 arguments (1 given)
*going crazy* @_@... I'm attaching the current script and asking for help ^^;
Attachments
script.rpy
Script of hell
(5.04 KiB) Downloaded 119 times

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#10 Post by PyTom »

It's because the version of Motion used in renpy.layout is different then the version of motion that's available in a script. You can convert between the two of them by calling the version of motion available in script-land, or by using renpy.display.layout.Motion instead.

(There may be other problems as well.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#11 Post by monele »

I wonder if I'll ever understand that all curry VS non-curry thing @_@...

Anyway, changing Motion to renpy.display.layout.motion and removing the "move=move()" in _Shake prevents the crash but doesn't display things properly ô_o... it looks like the character is moved around but nothing is refresh ô_o... and... not *everything* is drawn anyway...
Attachments
The sprite is supposed to be blue but... what's that patch of color where I should see the character shaking? ^^;...
The sprite is supposed to be blue but... what's that patch of color where I should see the character shaking? ^^;...

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#12 Post by PyTom »

I'd need a runnable game directory to see what's up, since I can't run the script without the images.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#13 Post by monele »

Sorry, I'll know for next time ^^;
Attachments
game.rar
the game directory
(155.82 KiB) Downloaded 83 times

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

#14 Post by PyTom »

Okay, the problem was that you were showing things at fairly random locations on the screen, because you didn't take into account the various anchors.

I've attached a fixed version of the script. What I did was to add in code that takes the xpos, ypos, xanchor, yanchor, and the sizes passed in, and computes an absolute xpos and ypos. When we work with this, everything starts working... well, I did have to turn all of the position pairs into position 4-tuples.

The fixed file is attached.
Attachments
script.rpy
(5.4 KiB) Downloaded 110 times
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

monele
Lemma-Class Veteran
Posts: 4101
Joined: Sat Oct 08, 2005 7:57 am
Location: France
Contact:

#15 Post by monele »

Ohhh... I've seen that "fti" snippet of code during my exploration but didn't know it would serve here ô_o. So... you actually do need to deal with floats yourself. Hmm... And the r and b variables represent the "sizes" here, right? So I take it the sizes are the screen size by default but can also be something smaller if needed? That's pretty cool ^^... but you should definitely have something on this in the documentation :) (well... I suddenly realize it's quite specific to my problem and not many people will have a use for it but eh ^^;...)

Next added snippet : so you actually return "0, 0" for the anchors (something I removed to make it work XD)... but I assume this is because we *already* took care of creating absolute positions, which means the anchors are now "0, 0".

add_sizes=True : goes with the sizes management... and passes the screen size, right?

Finally, one will have to pass x, y, xanchor and yanchor to the Shake function as the "start" parameter. Does this mean there's no point in passing xanchor and yanchor to the function too? (as seen on line 133)

Code: Select all

show girl at Shake((0.5, 1.0, 0.5, 1.0), 1.0, dist=20, xanchor="center", yanchor="bottom")
should really be

Code: Select all

show girl at Shake((0.5, 1.0, 0.5, 1.0), 1.0, dist=20)
... right?


Well I've learned a few things about how this works ^.^ Thanks!


... One last thing... >.>....

Code: Select all

show girl at left with tshake
I expected this to put the sprite on the left (that's ok) and to shake the sprite right away. Instead, it shakes the whole screen. tshake being a MoveTransition, shouldn't it apply only to sprites that have moved?

Post Reply

Who is online

Users browsing this forum: No registered users