#translating cli input to button input

1 messages · Page 1 of 1 (latest)

viral shell
#

im making my first game which is literally just blackjack. i first cooked up a quick text-based thing in python before attempting to recreate it in godot, but i hit a stump when converting all the input("y/n") stuff to clicking buttons instead

so here's my python code

# player draw loop
standing = False
while not (standing or isGameEndForPlayer(playerHand)):
    hitOrStand = input("Hit or stand? h/s")
    if hitOrStand == "h":
        playerHand.append(getRandomCard(cards))
        print(f"You drew a {playerHand[len(playerHand) - 1]['name']}, now totaling at {calcTotal(playerHand)}")
    else:
        standing = True

in godot, button presses are signals and in their own functions. this is my stump as idk how to remake this logic using signals. how is this usually done? my current (well, empty but) code is in the image

brisk rivet
polar chasm
#

well, you have two options so you need two buttons. The first button should be for hitting and the other one should be for stand

you connect both buttons to their respective functions in editor

func _on_hit_button_pressed() -> void:
# HIT LOGIC HERE

func _on_stand_button_pressed() -> void:
# STAND LOGIC HERE

basically all you have to do is split the logic since youre using buttons

viral shell
polar chasm
#

I dont think youd need a while loop. in your _ready() function, youd spawn cards and give it to the player. The player would then have two options, hit or stand

if they hit, you draw a card and check to see if its under 21. if it isnt, its the dealer's turn to drawn
if they stand, the enemy would do whatever they need to,

When the cards are dealt, you could calculate the max card value that the player would be allowed to have without busting

this would be the equivalent of smth like this (pseudocode ahead)

var max_player_card_value: int = 21
var max_dealer_card_value: int = 21

var player_cards: Array
var dealer_cards: Array

func _ready() -> void:
  deal_cards()
  max_player_card_value -= player_cards[0]
  max_dealer_card_value -= dealer_cards[0]
  play_player_round()

func _on_hit_button_pressed() -> void:
  new_card = spawn_new_card
  if new_card > max_player_card_value:
    game_over()
  player_cards.append(new_card)
  max_player_card_value -= new_card value
  play_dealer_round()

func _on_stand_button_pressed() -> void:
  play_dealer_round()

func deal_cards() -> void:
  new_card = spawn_new_card()
  player_cards.append(new_card)

# Spawn a new card, i reuse the new card variable cus im lazy but also i dont think there needs to be a good reason to have two seperate values

  new_card = spawn_new_card()
  dealer_cards.append(new_card)

func spawn_new_card() -> void:
# Idk what your spawn logic is

func play_dealer_round() -> void:
# Idk what your dealer logic is, you could just have them keep spawning new cards, subtract it from the max_dealer_card_value then transition to the player round

func play_player_round() -> void:
# Show the player buttons

the reason you dont need a while loop is because the game is progressed by the user pressing a max of 2 buttons. The game state only changes when they press these buttons so adding a while loop to wait for it would be unnecesary.

#

@viral shell

#

(pinging cus of ur name)

#

i'd recommend having a card class to make spawning cards easier. if you go this route, i'd strongly type the player_cards array var player_cards: Array[Card]

#

but its up to u

viral shell
polar chasm
#

honestly, im pretty sure it would

Because blackjack is so simple, they dont need any big loops only state changes. Even something like balatro basically goes from one state to another without having to use smth like a process function. The closest thing might be the drag and drop but that could be handled in the input function.

viral shell
#

ok, thank you!