0 votes

Hello everyone,

I'm pretty much a newbie when it comes to Godot so I've been doing little classic game sto get the hand on it.
As the title state, I'm going for the guessing game where you need to find and click on the 2 same pattern card until there's no more pair anymore. here's a rough preview:
Board game

(Yeah, there's actually more than 2 cards pulled off, but that just for test purpose)

All those cards are instance of the Card.tscn displayed below
Card scene
ValueCard being the actual card and HidingCard the texture to cover the value TextureRect.

I have a whole folder full of card sprite (From ace to king, and the classic shape like heart, diamond, spade and club), and I'd love to randomly pick up cards to fill the board and set the according texture to each card pair. Should I create a scene per card (like, a scene for the 2 of heart, 3 of heart, 4 of heart, etc...) or is there a way to use an array to manage that with texture and all ?

The general idea

Thanks in advance!

Godot version 3.2
in Engine by (51 points)

1 Answer

0 votes
Best answer

Create placeholder Sprites / TextureRects in a scene called card

  • for where the diamond/hearts/clubs/spades go
  • for where the numbers go
  • for where the face down / hidden card Sprite goes

Assign each card pair a unique code for example

var card_codes = Array()

func _ready():
    for face in ["C", "D", "H", "S"]:
        for number in range(1, 13):
            cards_codes.append(face+str(number))

In your board scene which should be a Grid node, randomize which cell gets what code
from the card_codes array

When a card is shown populate the TextureRects Nodes / Sprite Node based on the cell's code
Let's say you have

  • $top_sprite
  • $mid_sprite
  • $end_sprite
  • $top_number_sprite
  • $end_number_sprite

Example.

func show_card(code): #e.g "C12" = Queen of clubs
    if code.has("C"): #clubs
         var clubs_texture = load("clubs.PNG")
         $top_sprite.texture = clubs_texture
         $mid_sprite.texture = clubs_texture
         $end_sprite.texture = clubs_texture

    #get number from code and load Sprite
    var number_sprite = load(str(code.to_int())+".PNG")
    $top_number_sprite.texture = number_sprite
    $end_number_sprite.texture = number_sprite

When two cards are shown compare their codes

by (4,982 points)
selected by

Thanks, pretty clear and understandable! Works great

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.