Making an imagebutton move from within python function

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.
Post Reply
Message
Author
Lacha
Newbie
Posts: 9
Joined: Wed Mar 06, 2024 12:23 pm
Contact:

Making an imagebutton move from within python function

#1 Post by Lacha »

Hello everyone,

I'm working on a minigame where you can move cars by clicking them. It all works pretty fine, but instead of moving around, the cars just change position.
I am basically done with this game, but I want to make it look better.
I defined the cars to move as imagebuttons facing the correct direction by a Transform with rotate
Here is the code I use:

Code: Select all


init python:
    def move_testcar():
        global testcar_x
        global testcar_y
        global path
        for p in path:
            obstacle = False #checking for obstacle is done in another function
            if not obstacle:
                testcar_x = p[0]
                testcar_y = p[1]
            else:
                break

label testlane:
    scene bg trafficgame
    call screen testlane

screen testlane:
    image "minigames/trafficgame/townmap.png"
    imagebutton:
        idle Transform("minigames/trafficgame/othercar.png", rotate = 90, rotate_pad = False)
        xpos testcar_x
        ypos testcar_y
        action Function(move_testcar)

default testcar_x = 602
default testcar_y = 803

default path =[
    [602,803],
    [702,803],
    [802,803],
    [902,803],
    [1002,803],
    [1102,803],
    [1202,803],
    [1302,803],
    [1402,803]
]
I attached an image of my testlane (car1 = before click, car2 = after click).

What I'd like to have is that the car moves from the first to the second coordinates in the path list. And from there on to the third and so on.

I am totally confused about how to make this happen.

I already read about Transformations and that I can use them to move displayables. But I cannot imagine where to call that transformation and how.

Help would be appreciated...

Thanks in advance!
Attachments
car2.png
(79.17 KiB) Not downloaded yet
car1.png
(77.8 KiB) Not downloaded yet

User avatar
Kia
Eileen-Class Veteran
Posts: 1050
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Making an imagebutton move from within python function

#2 Post by Kia »

Transforms will give you lots of grief when it comes to complex animation, your best bet is using CDD for those cars: https://www.renpy.org/doc/html/cdd.html

Lacha
Newbie
Posts: 9
Joined: Wed Mar 06, 2024 12:23 pm
Contact:

Re: Making an imagebutton move from within python function

#3 Post by Lacha »

Wow... I don't know if I understand CDD's right at this point, but it looks very complex. Would mean I have to restructure the game completely.
Do you have suggestions on how to start with CDD's to achieve my goal?

User avatar
Kia
Eileen-Class Veteran
Posts: 1050
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Making an imagebutton move from within python function

#4 Post by Kia »

Lacha wrote: Sun Apr 28, 2024 1:58 pm Do you have suggestions on how to start with CDD's to achieve my goal?
Study classes first if you are not familiar with classes in python. A CDD is a couple of stone throws away from a class.

Lacha
Newbie
Posts: 9
Joined: Wed Mar 06, 2024 12:23 pm
Contact:

Re: Making an imagebutton move from within python function

#5 Post by Lacha »

I'm pretty familiar with classes. A tutorial how to implement a CDD would be helpful for the renpy docs are a pain in the #ss

philat
Eileen-Class Veteran
Posts: 1925
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Making an imagebutton move from within python function

#6 Post by philat »

While you CAN use a CDD for finer control, you also don't need it for simple linear movement. (Sidenote: I don't understand how your move_testcar function is supposed to work in the context of the game, but I assume that's due to it being edited for posting here.)

Code: Select all

init python:
    def move_testcar():
        global testcar_x
        global testcar_y
        global path
        global move_time 
        for p in path:
            obstacle = False #checking for obstacle is done in another function
            if not obstacle:
                move_time = 0.2 # moves in 0.2 seconds, set this to 0 to turn linear movement off
                testcar_x = p[0]
                testcar_y = p[1]
            else:
                break

screen testlane:
    imagebutton:
        at move_pos(testcar_x, testcar_y, move_time) # replaced the xpos ypos with the transform 
        idle Transform("minigames/trafficgame/othercar.png", rotate = 90, rotate_pad = False)
        action Function(move_testcar)

transform move_pos(x, y, t):
    linear t xpos x ypos y

default move_time = 0 

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2414
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Making an imagebutton move from within python function

#7 Post by Ocelot »

philat wrote: Mon Apr 29, 2024 12:35 am (Sidenote: I don't understand how your move_testcar function is supposed to work in the context of the game, but I assume that's due to it being edited for posting here.)
I am pretty sure that the intention was to move through all points from path in order. Like animating moving through the city map by using roads.
< < insert Rick Cook quote here > >

philat
Eileen-Class Veteran
Posts: 1925
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Making an imagebutton move from within python function

#8 Post by philat »

Ah, yes, I had skimmed the part of the OP that said that. Well, the principle remains the same, I guess, just adjusted to move one "step" at a time and wait before moving to the next step. I would probably use a timer to repeatedly call the function on a small delay.

Code: Select all

screen repeating_function():
    default counter = 0
    timer 0.5 action If(counter < len(path), [Function(move_testcar, counter), IncrementScreenVariable("counter")], Hide()) # pass the counter to the move_testcar function to determine which step to move to (e.g., testcar_x = path[counter][0], etc.)

Lacha
Newbie
Posts: 9
Joined: Wed Mar 06, 2024 12:23 pm
Contact:

Re: Making an imagebutton move from within python function

#9 Post by Lacha »

First of all: thank you for your efford.

@Ocelot: That is exactly how I want it to work. Several cars on the screen get clicked and drive along the screen to the outside. Example:
TrafficMinigame.png
(197.24 KiB) Not downloaded yet
@philat: Thanks for explaining the timer function to me. As I understand it, it is similar to the JS

Code: Select all

for (counter=0;counter < path.length;counter++)

Unfortunately my car doesn't to anything when clicking it. I must have missed something.

Code: Select all

init python:
    def move_testcar(counter):
        global testcar_x
        global testcar_y
        global path
        p = path[counter]
        #obstacle = False #checking for obstacle is done in another function
        #if not obstacle:
        testcar_x = p[0]
        testcar_y = p[1]


label testlane:
    scene bg trafficgame
    call screen testlane
screen repeating_function():
    default counter = 0
    timer 0.5 action If(counter < len(path), [Function(move_testcar, counter), IncrementScreenVariable("counter")], Hide()) 
    # pass the counter to the move_testcar function to determine which step to move to (e.g., testcar_x = path[counter][0], etc.)
screen testlane:
    image "minigames/trafficgame/townmap.png"
    textbutton "{color=#000}SKIP >>{/color}":
        xpos 150
        ypos 850
        action [Hide("testlane"), Jump("tardis_games3")]
    imagebutton:
        idle Transform("minigames/trafficgame/othercar.png", rotate = 90, rotate_pad = False)
        xpos testcar_x
        ypos testcar_y
        action Show("repeating_function")

default testcar_x = 602
default testcar_y = 803

default path =[
    [602,803],
    [702,803],
    [802,803],
    [902,803],
    [1002,803],
    [1102,803],
    [1202,803],
    [1302,803],
    [1402,803]
]

philat
Eileen-Class Veteran
Posts: 1925
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Making an imagebutton move from within python function

#10 Post by philat »

My bad, that should be a repeating timer.

Code: Select all

screen repeating_function():
    default counter = 0
    timer 0.5 repeat True action If(counter < len(path), [Function(move_testcar, counter), IncrementScreenVariable("counter")], Hide()) 

Lacha
Newbie
Posts: 9
Joined: Wed Mar 06, 2024 12:23 pm
Contact:

Re: Making an imagebutton move from within python function

#11 Post by Lacha »

Oh my Gosh! It's moving! Thank you, that is mostly what I was looking for!!!

Post Reply

Who is online

Users browsing this forum: thexerox123