Guessing game: the best way to approach it ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Beureuuu

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!

:bust_in_silhouette: Reply From: Wakatta

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

Thanks, pretty clear and understandable! Works great

Beureuuu | 2021-06-27 06:49