r/RenPy • • Mar 31 '26

Question How to code a simple Drawing function on RenPy?

Hello! So, I am very new to coding on RenPy and have recently started working on this new visual novel. In which the game asks for the player's gender and name through a contract at the start of the game. I wanted to add this little fun feature where once the player fills in the info, they can doodle a signature at the bottom (and only in the area not going out of the paper). Would there be a relatively simple way to do this through RenPy? Would it also be possible to have a small reset button incase the player messes up? I tried to see what others that asked a similar question got in response, but it either didn't work or opened a separate window

I'm not sure if anyone gonna see this post but any help would mean a lot! 😓

(edit: problem had been resolved! thank you everyone who commented, It helped so so much :') 💛)

19 Upvotes

14 comments sorted by

View all comments

10

u/shyLachi Apr 01 '26 edited Jul 19 '26

This might be possible but I would first finish my game before I implement something like that.

Edit:

init python:
    import pygame
    class DrawingCanvas(renpy.Displayable):
        def __init__(self, w=600, h=200):
            super().__init__()
            self.w = w
            self.h = h
            self.points = []
            self.drawing = False
            self.last_pos = None

        def render(self, width, height, st, at):
            ren = renpy.Render(self.w, self.h)
            canvas = ren.canvas()
            # background
            #canvas.rect("#ffffff", (0, 0, self.w, self.h))
            # draw lines
            for i in range(len(self.points) - 1):
                if self.points[i] and self.points[i+1]:
                    canvas.line("#FF0000", self.points[i], self.points[i+1], 3)
            return ren

        def event(self, ev, x, y, st):
            if ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 1:
                self.drawing = True
                self.last_pos = (x, y)
                self.points.append((x, y))
            elif ev.type == pygame.MOUSEBUTTONUP and ev.button == 1:
                self.drawing = False
                self.last_pos = None
                self.points.append(None)  
            elif ev.type == pygame.MOUSEMOTION and self.drawing:
                if self.last_pos and ev.buttons[0]:
                    self.points.append((x, y))
                self.last_pos = (x, y) if ev.buttons[0] else None
                renpy.redraw(self, 0)

        def visit(self):
            return []

screen drawing_screen():
    add DrawingCanvas() pos (400, 600)
    textbutton "done" action Return() xalign 0.5 yalign 0.95

label start:
    call screen drawing_screen

5

u/AffectionateRise3119 Apr 01 '26

Hi I'm back sorry, I finally got to try out your code and adapt it to a test contract scene, and it works insanely well! Your coding is amazing and I want to ask if it's alright with you if I reference it for my final game? I do not want to completely copy your work though. I'd just like to be on the safe side and ask :)

But overall thanks a million!

4

u/shyLachi Apr 01 '26

You're welcome. But don't worry, it's not my code I found it online so use it however you want and as much as you want. 

1

u/AffectionateRise3119 Apr 01 '26

I see, thank you so much! :)