Hack Forums
[Python] Dice roll game: What do do to only make it roll when I press a key? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Dice roll game: What do do to only make it roll when I press a key? (/showthread.php?tid=6062216)



Dice roll game: What do do to only make it roll when I press a key? - ICUP - Apr 1, 2020

Hi.

Very beginner here. Just finished the basics yesterday and still watching Automate the Boring Stuff with Python.

I'm trying to write my first dice rolling game but I'd like to add more stuff into it since the tut that I followed was very basic.

I'd like it to roll only when the user is ready / presses a key .
1st roll for Player1 only when player 1 presses a key
1st roll for Player 2 only when player 2 presses a key
2nd roll for Player1 only when player 1 presses a key
and so on
loop until they reach round 3 and finds a winner.

PHP Code

<?php 
import random

player1_name 
= input("Hi, player 1. Insert your name: ")
player2_name = input("Hi, player 2. Insert your name: ")
ready_to_roll = True

print("Hello " + player1_name + " and " + player2_name + "!")

print(
"Are you ready to roll, " + player1_name + "?")

input("Press enter to continue...")

def main():
 
   player1 = 0
    player1wins 
= 0
    player2 
= 0
    player2wins 
= 0
    rounds 
= 1
    
    while rounds 
!= 4:
 
       print("Round " + str(rounds))
 
       player1 = dice_roll()
 
       player2 = dice_roll()
 
       print(player1_name + " Roll: " + str(player1))
 
       print(player2_name + " Roll: " + str(player2))
 
       
        if player1 
== player2:
 
           print("Draw!")
 
       elif player1 > player2:
 
           player1wins = player1wins + 1
            print
(player1_name + " wins!\n")
 
       else:
 
           player2wins = player2wins + 1
            print
(player2_name + " wins!\n")
 
       
        rounds 
= rounds + 1
        
    if player1wins 
== player2wins:
 
       print("Draw")
 
   elif player1wins > player2wins:
 
       print("Player 1 wins the game.")
 
   else:
 
       print("Player 2 wins the game.") 

def dice_roll():
 
   diceRoll = random.randint(1, 6)
 
   return diceRoll

main
() 


I feel like the main function needs to be changed by I don't know how and I'm stuck.

Thank you!


RE: Dice roll game: What do do to only make it roll when I press a key? - ihatevim - Apr 1, 2020

Code
import random

def getNames():
    global player1_name
    global player2_name
    player1_name = input("Hi, player 1. Insert your name: ")
    player2_name = input("Hi, player 2. Insert your name: ")
    
def main():
    getNames()
    print("Hello " + player1_name + " and " + player2_name + "!")
    player1 = 0
    player1wins = 0
    player2 = 0
    player2wins = 0
    rounds = 1
    
    while rounds != 4:
        print("Round " + str(rounds))
        input(player1_name + ", are you ready to roll? Press any key to continue.")
        player1 = dice_roll()
        print(player1_name + " Roll: " + str(player1))
        input(player2_name + ", are you ready to roll? Press any key to continue.")
        player2 = dice_roll()
        print(player2_name + " Roll: " + str(player2))
        
        if player1 == player2:
            print("Draw!")
        elif player1 > player2:
            player1wins = player1wins + 1
            print(player1_name + " wins!\n")
        else:
            player2wins = player2wins + 1
            print(player2_name + " wins!\n")
        
        rounds = rounds + 1
        
    if player1wins == player2wins:
        print("Draw")
    elif player1wins > player2wins:
        print(player1_name + " wins the game.")
    else:
        print(player2_name + " wins the game.")

def dice_roll():
    diceRoll = random.randint(1, 6)
    return diceRoll

main()

It's just a matter of using input again without actually caring about input. I got rid of a useless variable (ready_to_roll) and put your names into a get name function to help you with what needs to be done next. Your next focus should be to check if the user gave a valid response for the name (a null response will break the program). Following that, you should add a conditional for the counters of the wins of each player so that the game ends early if a player wins twice in a row (e.g. if x wins twice there is no reason for them to roll a third time since it is best 2/3).

Another thing to consider is clean code practices. Get used to following one naming scheme, if you used snake_case, then keep snake_case. If you use camelCase, then keep camelCase. Avoid mix and matching.


RE: Dice roll game: What do do to only make it roll when I press a key? - ICUP - Apr 1, 2020

(Apr 1, 2020 12:38 PM)ihatevim Wrote:  
Code
import random

def getNames():
   global player1_name
   global player2_name
   player1_name = input("Hi, player 1. Insert your name: ")
   player2_name = input("Hi, player 2. Insert your name: ")
   
def main():
   getNames()
   print("Hello " + player1_name + " and " + player2_name + "!")
   player1 = 0
   player1wins = 0
   player2 = 0
   player2wins = 0
   rounds = 1
   
   while rounds != 4:
       print("Round " + str(rounds))
       input(player1_name + ", are you ready to roll? Press any key to continue.")
       player1 = dice_roll()
       print(player1_name + " Roll: " + str(player1))
       input(player2_name + ", are you ready to roll? Press any key to continue.")
       player2 = dice_roll()
       print(player2_name + " Roll: " + str(player2))
       
       if player1 == player2:
           print("Draw!")
       elif player1 > player2:
           player1wins = player1wins + 1
           print(player1_name + " wins!\n")
       else:
           player2wins = player2wins + 1
           print(player2_name + " wins!\n")
       
       rounds = rounds + 1
       
   if player1wins == player2wins:
       print("Draw")
   elif player1wins > player2wins:
       print(player1_name + " wins the game.")
   else:
       print(player2_name + " wins the game.")

def dice_roll():
   diceRoll = random.randint(1, 6)
   return diceRoll

main()

It's just a matter of using input again without actually caring about input. I got rid of a useless variable (ready_to_roll) and put your names into a get name function to help you with what needs to be done next. Your next focus should be to check if the user gave a valid response for the name (a null response will break the program). Following that, you should add a conditional for the counters of the wins of each player so that the game ends early if a player wins twice in a row (e.g. if x wins twice there is no reason for them to roll a third time since it is best 2/3).

Another thing to consider is clean code practices. Get used to following one naming scheme, if you used snake_case, then keep snake_case. If you use camelCase, then keep camelCase. Avoid mix and matching.

Huh.

That was very easy. My codes will be very messy for a while, I'm sure.

Thanks for the help!


RE: Dice roll game: What do do to only make it roll when I press a key? - lockhead - Apr 3, 2020

(Apr 1, 2020 02:03 PM)๖ۣۜƝeil™ Wrote:  Huh.

That was very easy. My codes will be very messy for a while, I'm sure.

Thanks for the help!

lmao. always my reaction after hunting bugs for 8 hours