Simple card drawing framework

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
goldo
Regular
Posts: 124
Joined: Mon Jan 23, 2017 8:23 am
Contact:

Simple card drawing framework

#1 Post by goldo »

Hi guys, I was tinkering with Renpy to create a simple minigame where you draw cards and choose any number of them to keep.

It's pretty simple with just about 80 lines of code, but I'm happy with the way it turned out so I am sharing it.
My mastery of screen language is pretty shaky, so I would love some feedback on how to improve it.

Here is the script.rpy file, which you can just copy in a new project:

Code: Select all

# The script of the game goes in this file.
# This is a simple script that creates a deck of cards, lets you draw random cards from it then choose some to keep.
# The cards are objects that you can edit to add more useful properties.

init python:

    class Card(): # This card object is very simple, but you can make it as complex as you'd like
        """ This class represents an individual card in the card set."""
        def __init__(self, value):
            self.value = value

    class CardSet():
        """ This class represents a set of cards."""
        def __init__(self, nb): # Creates a set of cards numbered 1 to nb
            self.cards = [Card(x+1) for x in xrange(nb)]

        def pick_cards(self, nb): # Will return nb cards selected randomly (or all cards if nb > total cards in the card set)
            return renpy.random.sample(self.cards, min(nb, len(self.cards)))

# Declare characters used by this game. The color argument colorizes the
# name of the character.

define e = Character("Eileen")

# This transform moves the cards to a new position when they are drawn
transform moveto(start_pos=(0, 0), new_pos=(0,0), duration=0.6):
        pos start_pos
        ease duration pos new_pos xanchor 0.5 yanchor 1.0

# This screen is the game board, showing the deck of cards, a button to quit and a button to confirm card choice.
screen card_game:
    frame xfill True yfill True:
        use stack

        textbutton "QUIT" xanchor 1.0 xalign 0.7 action (SetVariable("chosen_cards", []), Return())
        if chosen_cards:
            textbutton "CONFIRM (%s)" % str(len(chosen_cards)) xanchor 1.0 xalign 0.7 yalign 0.6 action Return()

# This screen is the card stack button - Click on it to draw 5 cards
screen stack:
    button background Frame("stack.webp") hover_xsize 200 hover_ysize 260 idle_xsize 180 idle_ysize 240:
        action (Hide("hand"), Show("hand", None, 5), SetVariable("chosen_cards", []))

# This screen is the card hand
screen hand(nb): # This screen is recreated every time you click on the card stack
    default draw_it = True # I couldn't figure out a more elegant way to make the drawing animation play only once.
    default new_hand = card_deck.pick_cards(5)

    fixed:
        fit_first True
        $ x = 300
        for c in new_hand:
            use card(c, x=x, draw_it=draw_it)
            $ x += 200
        $ draw_it = False

# Each card is an individual button that can be selected/unselected
screen card(c, x, y=560, draw_it=True):
    textbutton "Card [c.value]" background Frame("front.webp") selected_foreground Frame("selected.webp") text_xalign 0.5 hover_xsize 200 hover_ysize 260 idle_xsize 180 idle_ysize 240 action ToggleSetMembership(chosen_cards, c):
        if draw_it:
            at moveto(new_pos = (x, y))
        else:
            xpos x ypos y xanchor 0.5 yanchor 1.0

label start:

    # The game starts here.

    $ card_deck = CardSet(10) # Creates a set of cards numbered 1 to 10

    e "Click on the card deck to draw some cards."

    $ chosen_cards = []

    # Calls the card screen
    call screen card_game with dissolve
    hide screen hand with dissolve

    if chosen_cards:
        $ card_list = sorted([c.value for c in chosen_cards]) # Sorts chosen cards by value and gets the list ready to print
        e "You have chosen the following cards: [card_list]."
    else:
        e "No card has been chosen."

    # This ends the game.

    return

To make it work properly, you will need three images placed in the image folder (replace the file names in the script if you want to use other formats than .webp):
- deck.webp: An image of your deck
- front.webp: An image of your card's front side (in this example they are all the same, but it wouldn't be hard to give each card object its own picture)
- selected.webp: An image that highlights selected cards (I just used a simple yellow outline for my game)


That's it. I hope it can be useful to some of you. I'm sure there are many ways to improve it, so please let me know. :)

Post Reply

Who is online

Users browsing this forum: No registered users