In this Python tutorial, learn to create a soccer shootout game that can be played in Python user interface. This tutorial will review class methods, class objects, defining multiple functions, while loops, for loops. Also, multiple counters will need to be created to track each time a player and virtual player kicks the ball.
Import Python Modules
The Python modules random, sys, and time will need to be imported to create a few of the Python functions.
1 2 3 | import random import sys import time |
Create Python Class Methods and Objects
Within the Python soccerShooutout class, there will need to be two variables for the player team and the virtual team. Later in defining functions, a for loop will pull a random team for both teams by using a for loop and the Python random module. Also, variables for the player and virtual player teams will be created to count the amount of kicks, kicks that are blocked and kicks that are goals. Finally, in the Python class, multiple functions will need to be created to increase the counts of each of the kicks, blocks, and goals for each team.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class soccerShootout: player_team = "" virtual_player_team = "" player_shootout_blocks = 0 player_shootout_goals = 0 count_player = 0 virtual_player_shootout_goals = 0 virtual_player_shootout_blocks = 0 count_virtual_player = 0 event_count = 0 def increaseEventCount(): soccerShootout.event_count += 1 def increaseCountPlayer(): soccerShootout.count_player += 1 def increasePlayerShootoutGoals(): soccerShootout.player_shootout_goals += 1 def increasePlayerShootoutBlocks(): soccerShootout.player_shootout_blocks += 1 def increaseCountVirutalPlayer(): soccerShootout.count_virtual_player += 1 def increaseVirutalPlayerShootoutGoals(): soccerShootout.virtual_player_shootout_goals += 1 def increaseVirtualPlayerShootoutBlocks(): soccerShootout.virtual_player_shootout_blocks += 1 |
Python Random.Sample Teams
The below two functions create a variable for each time with a list of teams. A for loop will loop through the lists and randomly pull a team for the player and the virtual team.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def playerTeamRandom(): player_team = ["Rangers F.C.", "Inter Milan","D.C. United", "Chelsea F.C.", "Portland Timbers", "Real Salt Lake"] for name in random.sample(player_team, 1): player_team = name player_team = player_team.upper() soccerShootout.player_team = player_team def virtualPlayerTeamRandom(): virtual_player_team = ["Sporting Kansas City", "LA Galaxy","Arsenal F.C.", "Manchester United F.C.", "Liverpool F.C.", "Real Madrid"] for name in random.sample(virtual_player_team, 1): virtual_player_team = name virtual_player_team = virtual_player_team.upper() soccerShootout.virtual_player_team = virtual_player_team |
Define the Soccer Shootout Python Functions
The below Python functions define multiple print statements and use variables from the class and/or variables from previous functions to count each teams kick. Each time the player kicks the ball, it will count until the count is greater than 5, and then virtual team will kick and you will play goalie to block their kicks. Each team will have 5 chances to kick consecutively. The last function will print out who won with a score for each team in the soccer shootout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | def soccerEvent(): soccerShootout.increaseEventCount() pt = soccerShootout.player_team vpt = soccerShootout.virtual_player_team ec = soccerShootout.event_count print(f"Event: Soccer Shootout - {ec} ") print("Location: Los Angeles, California") print(f"\n{pt} vs. {vpt} ") print(f"You will represent {pt} and the opponent is {vpt}.") print("Let the soccer shootout begin!") def playerKicker(): pt = soccerShootout.player_team print(f"\nYou will be representing {pt} in the shootout as a kicker.") def playerGoalie(): pt = soccerShootout.player_team print(f"\nYou will be representing {pt} in the shootout as a goalie.") print("goalie") def playerShootoutKicks(): soccerShootout.increaseCountPlayer() cp = soccerShootout.count_player pt = soccerShootout.player_team while (cp >= 1 and cp <= 5): time.sleep(1) print(f"\nSetting up for shootout kick {cp}. Where will you kick the ball? ") ball_kick = input("Please enter left, center, or right: ") ball_direction = ball_kick.upper() time.sleep(1) while (ball_direction != "LEFT" and ball_direction != "CENTER" and ball_direction != "RIGHT"): print("\nInvalid input.") ball_kick = input("Please enter left, center, or right: ") ball_direction = ball_kick.upper() directions = ["LEFT", "CENTER", "RIGHT"] for i in random.sample(directions, 1): kicker_location = i if ball_direction == kicker_location: soccerShootout.increasePlayerShootoutGoals() ipsg = soccerShootout.player_shootout_goals time.sleep(1) print(f"\nYou scored a goal at the {kicker_location} location.") print(f"{pt} has scored {ipsg} goal(s).") playerShootoutKicks() else: soccerShootout.increaseVirtualPlayerShootoutBlocks() time.sleep(1) print(f"\nThe goalie blocked the kick at the {kicker_location} location.") playerShootoutKicks() else: playerGoalie() virtualPlayerShootoutKicks() def virtualPlayerShootoutKicks(): directions = ["LEFT", "CENTER", "RIGHT"] soccerShootout.increaseCountVirutalPlayer() cvp = soccerShootout.count_virtual_player vpt = soccerShootout.virtual_player_team while (cvp >= 1 and cvp <= 5): time.sleep(1) print(f"\nSetting up for shootout kick {cvp} for {vpt}. Where will you try to block the ball? ") ball_kick = input("Please enter left, center, or right: ") ball_direction = ball_kick.upper() time.sleep(1) while (ball_direction != "LEFT" and ball_direction != "CENTER" and ball_direction != "RIGHT"): print("\nInvalid input.") ball_kick = input("Please enter left, center, or right: ") ball_direction = ball_kick.upper() for i in random.sample(directions, 1): kicker_location = i if ball_direction == kicker_location: soccerShootout.increaseVirutalPlayerShootoutGoals() ivpsg = soccerShootout.virtual_player_shootout_goals time.sleep(1) print(f"\n{vpt} scored a goal at the {kicker_location} location.") print(f"{vpt} has scored {ivpsg} goal(s).") virtualPlayerShootoutKicks() else: pt = soccerShootout.player_team soccerShootout.increasePlayerShootoutBlocks() time.sleep(1) print(f"\n{pt} goalie blocked the kick at the {kicker_location} location.") virtualPlayerShootoutKicks() else: shootoutScore() def shootoutScore(): vpt = soccerShootout.virtual_player_team pt = soccerShootout.player_team psg = soccerShootout.player_shootout_goals vpsg = soccerShootout.virtual_player_shootout_goals print(f"\n{pt} {psg} - {vpt} {vpsg} ") if psg == vpsg: print(f"\nThe shootout resulted in a tie.") elif psg > vpsg: print(f"\n{pt} Wins!!!") elif psg < vpsg: print(f"\n{vpt} Wins!!!") confirmPlay() |
Soccer Game Replay and Reset the Python Counter Class Objects
The below functions will prompt the user to replay by selecting 1 or 2. If the user selects 1, the user can play again and the system will reset the counter variables and new teams will be randomly selected. If the user selects 2, the system will exit and game is over.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | def confirmPlay(): time.sleep(3) choice = int(input("\nEnter 1 to keep playing or 2 to quit: ")) while choice < 1 or choice > 2: choice = int(input("\nInvalid Input, please enter 1 to keep playing or 2 to quit: ")) if choice == 1: reset() playSoccerShooutoutAgain() else: sys.exit() def reset(): soccerShootout.player_shootout_blocks = 0 soccerShootout.player_shootout_goals = 0 soccerShootout.count_player = 0 soccerShootout.virtual_player_shootout_goals = 0 soccerShootout.virtual_player_shootout_blocks = 0 soccerShootout.count_virtual_player = 0 def playSoccerShooutoutAgain(): playerTeamRandom() virtualPlayerTeamRandom() soccerEvent() playerKicker() playerShootoutKicks() playerTeamRandom() virtualPlayerTeamRandom() soccerEvent() playerKicker() playerShootoutKicks() |
I hope this Python tutorial was informative and helped build knowledge in Python class methods, class objects, for loops, while loops and reset counter variables in the class.