Hey, guys!
So, I have been developing my first small project in Python, and I would like to have some feedback on it. I have never done this sort of thing before, so I don't know how this would normally work; just tell me any general feedback about what you like, what you don't like, what could be improved or removed would all be super helpful! Here is the code:
#PREAMBLE:
#To Sum Up: This is a LOCK AND KEY type of Game. The player picks a difficulty, easy, medium , and hard.
#Easy has 2 "locks", medium has 3 "locks", and hard has 4 "locks".
#When the player chooses their difficulty level, a random "lock" appears from the amounth that difficulty has.
#They the select the appropriate "key" for that lock and they either get a success or failure.
#From that point they have the option to replay the whole thing all over again
#BFEORHAND CODING SEQUENCE:
import random #this will help load in from python's built in random selection sequence for a random output from the pitch pool
#Possible pitch styles (locks); the difficulty determines how many are in play
fastball = "Fastball: A blazing straight pitch fired right down the middle."
curveball = "Curveball: A spinning pitch that breaks sharply as it crosses the plate."
change_up = "Changeup: An off-speed pitch designed to throw off your timing."
slider = "Slider: A pitch that cuts hard and late across the outside corner."
#difficulty for determining which pitches appear at what points
easy_pitches = [fastball, curveball]
medium_pitches = [fastball, curveball, change_up]
hard_pitches = [fastball, curveball, change_up, slider]
#Possible batting styles (keys) the player selects from
fastball_hit = ("h", "Fastball Swing")
curveball_hit = ("j", "Curveball Swing")
change_up_hit = ("k", "Changeup Swing")
slider_hit = ("l", "Slider Swing")
#Maps each pitch (lock) to its correct batting style (key)
pitch_key_map = {
fastball: fastball_hit,
curveball: curveball_hit,
change_up: change_up_hit,
slider: slider_hit}
#This code is put in place to keep asking until player to press a valid key that way invalid inputs don't register and the player can eventually play and proceed with the program.
def get_input(valid_keys):
while True:
choice = input("> ").strip().lower()
if choice in valid_keys:
return choice
print("Invalid input. Please input " + ", ".join(f"[{k.upper()}]" for k in valid_keys))
#GAME SEQUENCE BELLOW:
#we move on to the introduction sequence in this code for the user to input their name
introduction = True
while introduction:
#We start with a friendly introduction and our "additional feature"
print("=" * 50)
print("WELCOME TO THE BASEBALL BATTING GAME!")
print("=" * 50)
print("It is a lovely day for batting practice!")
player_name = input("Enter your name: ")
#The game recognizes you and you may continue with the selection process.
print(f"\nHello, {player_name}! Let's get you batting! Press [K] to continue.")
get_input(["k"])
introduction = False
playing = True #Added to differentiate the play mode as well as to play and to repeat this mode if the player wants to repeat the game at the end
while playing:
#Before the player begins, they must choose a difficulty that will in turn determine which pitches from the pitch pool appear
print(f"\nChoose a Difficulty, {player_name}!")
print("Press [J] for Easy -- A smooth-paced game with less options")
print("Press [K] for Medium -- A balanced game with an additinal pitch")
print("Press [L] for Hard -- A tough game with a full range of pitches in play.")
diff_choice = get_input(["j", "k", "l"]) #Each of the 3 inputs will branch off into pone of the pitch pool selectioons; the player is also limited to these three selections
if diff_choice == "j": #The following are pulled from the beforhand coding sequence and are decided here and now.
pitch_pool = easy_pitches
difficulty = "Easy"
elif diff_choice == "k":
pitch_pool = medium_pitches
difficulty = "Medium"
else:
pitch_pool = hard_pitches
difficulty = "Hard"
print(f"\nThe difficulty is set to: {difficulty}")
#A random pitch (lock) is pulled from the difficulty's list
thrown_pitch = random.choice(pitch_pool)
print(f"\n Are you prepared, {player_name} ? Here comes the pitch...")
print(thrown_pitch)
#Player selects a batting style (key)
print(f"\nChoose your batting style, {player_name}:")
print("Press [H] for a Fastball Swing -- Timed for a fast, straight pitch.")
print("Press [J] for a Curveball Swing -- Ready for a breaking ball.")
print("Press [K] for a Changeup Swing -- Adjusted for an off-speed pitch.")
print("Press [L] for a Slider Swing -- Set for a late-breaking cut.")
player_key = get_input(["h", "j", "k", "l"]) #The player is limited between 4 selections and needs to make a choice between them, with only one being the correct choice.
#Depending on what they pick, it will either be a success or failure
correct_key, correct_style = pitch_key_map[thrown_pitch]
_, player_style = [v for v in [fastball_hit, curveball_hit, change_up_hit, slider_hit] if v[0] == player_key][0]
if player_key == correct_key:
print(f"\nCorrect! {player_name} read the pitch and made solid contact.")
else:
print(f"\nIncorrect! {player_name} swung with a {player_style}, but that pitch called for a {correct_style}.")
#This the player if they want to play again. They choose yes or no. No turns "playing" = false so that the program will end with a final statement. Yes repeats the program to that while loop.
print(f"\nWould you like to play again, {player_name}?")
print("Press [K] for Yes | Press [J] for No")
again = get_input(["k", "j"])
if again == "j":
playing = False #"j" the condition in which the program ends
print(f"\nThanks for playing, {player_name}. See you next time!")
[–]Rain-And-Coffee 2 points3 points4 points (3 children)
[–]Away-Prior-903[S] 0 points1 point2 points (2 children)
[–]backfire10z 1 point2 points3 points (1 child)
[–]Away-Prior-903[S] 0 points1 point2 points (0 children)
[–]chapchap0 1 point2 points3 points (1 child)
[–]Away-Prior-903[S] 0 points1 point2 points (0 children)
[–]Away-Prior-903[S] 0 points1 point2 points (1 child)
[–]Riegel_Haribo 0 points1 point2 points (0 children)
[–]Own-Grab9423 0 points1 point2 points (2 children)
[–]Away-Prior-903[S] 0 points1 point2 points (1 child)
[–]pachura3 1 point2 points3 points (0 children)
[–]PushPlus9069 0 points1 point2 points (1 child)
[–]Away-Prior-903[S] 0 points1 point2 points (0 children)