Write a program in python for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function until you run out of ammo, at which point you lose. Report both hits and misses (see Sample Outputs). If your shot is a hit as determined by the value returned by shoot, your code should lower the enemy's health. If you are lucky, the health of the enemy will be reduced to zero before you run out of ammo. If this happens, report the enemy's demise and use the break keyword to stop the loop. You have won.Sample Output 1Shot fired. Enemy was hit!Shot fired. Shot missedShot fired. Enemy was hit!Shot fired. Enemy was hit!Shot fired. Enemy was hit!Shot fired. Enemy was hit!Enemy destroyed. You won!GAME OVERSample Output 2Shot fired. Shot missedShot fired. Enemy was hit!Shot fired. Shot missedShot fired. Shot missedShot fired. Shot missedShot fired. Enemy was hit!Shot fired. Shot missedShot fired. Shot missedShot fired. Shot missedShot fired. Enemy was hit!You are out of ammo! You lose!GAME OVER

Question
Asked Nov 8, 2019

Write a program in python for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function until you run out of ammo, at which point you lose. Report both hits and misses (see Sample Outputs). If your shot is a hit as determined by the value returned by shoot, your code should lower the enemy's health. If you are lucky, the health of the enemy will be reduced to zero before you run out of ammo. If this happens, report the enemy's demise and use the break keyword to stop the loop. You have won.
Sample Output 1
Shot fired. Enemy was hit!
Shot fired. Shot missed
Shot fired. Enemy was hit!
Shot fired. Enemy was hit!
Shot fired. Enemy was hit!
Shot fired. Enemy was hit!
Enemy destroyed. You won!
GAME OVER

Sample Output 2
Shot fired. Shot missed
Shot fired. Enemy was hit!
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Enemy was hit!
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Enemy was hit!
You are out of ammo! You lose!
GAME OVER

check_circle

Expert Answer

Step 1

Source Code

# import the required modules

import random

import time

#Definition of the function shoot()

def shoot():

    #Print the following string

    print ("shot fired.", end=" ")

    #Generation of a random number between 0 and 1 for false and true respectively using randint()

    n = random.randint(0,2)%2

    #Check the condition if n=1 return true

    if(n):

        return True

    #Otherwise return false

    else:

        return False

#Definition of the main() function

def main():

    #Initialize the necessary variables

    ammo=10

    health=5

    #Set the starting point to produce pseudo random integer series using seed()

    random.seed(int(time.time()))

    #"while" loop will execute until ammo=0

    while(ammo):

        #Calling of the function shoot()

        result=shoot()

        #Check if the result is equal to "True"

        if(result==True):

            #Print the following string

            print("Enemy was hit!")

            #Decrement the value of health by 1 every time the enemy is hit

            health-=1

            #Check if health is equal to "0"

            if(health==0):

                #Print the following string

                print("Enemy destroyed. You won!")

                # use break to exit from the loop

                break

        #Otherwise

        else:

            #Print "Shoot missed"

            print("Shoot missed")

        #Decrement the value of ammo by 1

        ammo-=1

    #Check if health is greater than "0"

    if(health>0):

        #Print the following string

        print("You are out of ammo! You lose!")

    #Print the following string

    print("GAME OVER")

    #Return "0"

    return 0

#Calling of the main() function

main()

 

Screenshots of the Source Code

Screenshot 1

# import the required modules
import random
import time
#Definition of the function shoot()
def shoot)
#Print the following string
print ("shot fired.", end-" ")
#Generation of a random number between 0 and 1 for false
and true respectively using randint()
random.randint (0, 2)%2
n=
#Check the condition if n=1 return true
if (n):
return True
#0therwise return false
else:
return False
help_outline

Image Transcriptionclose

# import the required modules import random import time #Definition of the function shoot() def shoot) #Print the following string print ("shot fired.", end-" ") #Generation of a random number between 0 and 1 for false and true respectively using randint() random.randint (0, 2)%2 n= #Check the condition if n=1 return true if (n): return True #0therwise return false else: return False

fullscreen
Step 2

Screenshot 2

#Definition of the main ( ) function
def main)
#Initialize the necessary variables
ammo-10
health-5
#Set the starting point to produce pseudo random integer
series using seed ()
random.seed(int (time . time ( ) ))
#while" loop will execute until ammo=e
while (ammo)
#Calling of the function shoot ( )
result-shoot(
#Check if the result is equal to "True"
if (result--True):
#Print the following string
print("Enemy was hit!")
#Decrement the value of health by 1 every time
the enemy is hit
health--1
#Check if health is equal to "e"|
if (health==0):
#Print the following string
print("Enemy destroyed. You won!")
# use break to exit from the loop
break
help_outline

Image Transcriptionclose

#Definition of the main ( ) function def main) #Initialize the necessary variables ammo-10 health-5 #Set the starting point to produce pseudo random integer series using seed () random.seed(int (time . time ( ) )) #while" loop will execute until ammo=e while (ammo) #Calling of the function shoot ( ) result-shoot( #Check if the result is equal to "True" if (result--True): #Print the following string print("Enemy was hit!") #Decrement the value of health by 1 every time the enemy is hit health--1 #Check if health is equal to "e"| if (health==0): #Print the following string print("Enemy destroyed. You won!") # use break to exit from the loop break

fullscreen
Step 3

Screenshot 3

...
#0therwise
else:
#Print "Shoot missed"
print ("Shoot missed")
#Decrement the value of ammo by 1
ammo--1
#Check if health is greater than "e"
if (health>e)
#Print the following string
print ("You are out of ammo! You lose!")
#Print the following string
print ("GAME OVER")
#Return "e"
return
#Calling of the main() function
main()
help_outline

Image Transcriptionclose

#0therwise else: #Print "Shoot missed" print ("Shoot missed") #Decrement the value of ammo by 1 ammo--1 #Check if health is greater than "e" if (health>e) #Print the following string print ("You are out of ammo! You lose!") #Print the following string print ("GAME OVER") #Return "e" return #Calling of the main() function main()

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Python

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: 4. Given the for loop, rewrite the code so that it executes exactly the same but uses a while loop f...

A: For-loop syntax:             for(initialization ; condition ; updation)                             ...

question_answer

Q: EXPLAIN ALL STEPS: Determine the output of the following Pep/9 machine language program if the input...

A: Address   Instruction(in hex)0000         D1FC15Convert the instructions into binary form-D=11011=00...

question_answer

Q: Assume each bank branch can issue loan independently of other branches and assumecustomers may joint...

A: Option “B” is the correct functional dependency for the given table “Loans”.

question_answer

Q: Research and describe one interesting application of data or text mining.

A: Data miningData mining is the process of discovering patterns of data in large data sets using metho...

question_answer

Q: Can someone help me on solving this? Thank you!

A: For loop:A loop is used repeatedly to execute a sequence of statements until a given condition is fu...

question_answer

Q: Why does the order of the operands (tables) matter in an EXCEPT (MINUS) query but not in a UNION que...

A: MINUSMINUS operator is used for getting the result that is present in the first table but not in the...

question_answer

Q: Please Use JAVA to solve the problem

A: Create a java class “ArrayInv” and create a main method inside it.And declare a required variable to...

question_answer

Q: PHP  Think of one of your Math homework. How can you use program to reveal the answer? Write a calcu...

A: Program Instructions:The given program implements the Strassen’s method of multiplication between tw...

question_answer

Q: Using DeMorgan's Law, write an expression for the complement of F ifF(x,y,z) = xz' (xy + xz)+ xy'(wz...

A: Expression: