Question: class ShootingGame: def _ _ init _ _ ( self , target _ distance, target _ height ) : self.target _ distance = target _

class ShootingGame:
def __init__(self, target_distance, target_height):
self.target_distance = target_distance
self.target_height = target_height
def calculate_trajectory(self, x, angle, velocity):
# Calculate the trajectory of the shot
# Given x-coordinate, Return y-coordinate
# Step 1: Calculate the time of flight to reach x-coordinate
# Equation: t = x /(velocity * cos(angle))
# Step 2: Calculate the y-coordinate
# Equation: y = velocity * sin(angle)* time_of_flight -0.5* g * t^2
return y
def check_hit_target(self, angle, velocity, tolerance):
# Check if the shot hits the target
# Given tolerance, Return True or False
# Step 1: Calculate the y-coordinate of the shot at the target distance
# Equation: y = self.calculate_trajectory(self.target_distance, angle,
velocity)
# use 'self' to access other attributes and methods inside the class
# Step 2: Check if the y-coordinate is within the tolerance of the target
height
# Equation: abs(y - target_height)< tolerance
return bool
def check_score(self):
# Assign score based on whether the shot hits the target
# and the game-specific scoring rules
pass
class Basketball(ShootingGame): # Basketball inherits from ShootingGame
def __init__(self, target_distance, target_height):
super().__init__(target_distance, target_height)
def check_score(self, angle, velocity, distance, tolerance=0.1): # Override
the check_score method
'''
Assign score based on whether the shot hits the target and the basketball
scoring rules
Input arguments:
angle - shooting angle
velocity - shooting velocity
distance - shooting distance from the basket, a player can shoot from any
distance
tolerance- tolerance for hitting the target, default value is 0.1
scoring rules:
- If the shot hits the target within the tolerance and the distance is
less than 7.24 meters, score =2
- If the shot hits the target within the tolerance and the distance is
greater than or equal to 7.24 meters, score =3
- Otherwise, score =0
'''
# update self.target_distance to distance (self.target_distance =
distance)
# if check_hit_target(tolerance)== True and distance <7.24: score =2
# if check_hit_target(tolerance)== True and distance >=7.24: score =3
# else: score =0
return score
class Archery(ShootingGame):
def __init__(self, target_distance, target_height):
super().__init__(target_distance, target_height)
def check_score(self, angle, velocity, tolerance):
'''
Assign score based on whether the shot hits the target and the archery
scoring rules
Input arguments:
angle - shooting angle
velocity - shooting velocity
tolerance - radius of the physical target
scoring rules:
- the target is a circle with radius 0.3 meter and is divided into 4
scoring zones from center to outer ring
- If the shot hits center ring (radius =0.1), score =10
- If the shot hits the second ring (radius =0.2), score =8
- If the shot hits the third ring (radius =0.3), score =5
- Otherwise, score =0
'''
# if check_hit_target(0.1)== True: score =10
# if check_hit_target(0.2)== True: score =8
# if check_hit_target(0.3)== True: score =5
# else: score =0
return score
# Create objects for the games
# basketball game: target_distance =7.24, target_relative_height =1.25
# archery game: target_distance =60, target_relative_height =0.0
basketball_game = Basketball(...)
archery_game = Archery(...)
# Check the score of the shots
'''
For basketball game: check the score of the following shots:
(angle, velocity, distance)
(36,10,7.5)
(30,10,5.0)
(45,10,5.5)
basketball_game.check_score(angle, velocity, distance)
'''
'''
For archery game: check the score of the following shots:
(angle, velocity)
(1.6,100)
(1.5,100)
(1.2,110)
(1.2,90)
archery_game.check_score(angle, velocity)
'''
# Create another class for players to interact with the game and keep track of
their scores
class Player:
def __init__(self, name):
self.name = name
self.score =0
def play_game(self, game, *args):
'''
Play the game and update the player's score
Input arguments:
game - the game object (e.g., basketball_game, archery_game)
*args - passing multiple arguments based on the game
for basketball game: (angle, velocity, distance)
for archery game: (angle, velocity)
'''
# Step 1: Check the score of the shot
# score = game.check_score(*args)
# Step 2: Update the player's score
# self.score += score
def show_score(self):
# Display the player's score
return 'Total Score: '+ str(self.score)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!