+ 2
Intermediate Python - 27 Code Project - “Shooting Game”
Please help, thanks. You are creating a shooting game! The game has two types of enemies, aliens and monsters. You shoot the aliens using your laser, and monsters using your gun. Each hit decreases the lives of the enemies by 1. The given code declares a generic Enemy class, as well as the Alien and Monster classes, with their corresponding lives count. It also defines the hit() method for the Enemy class.
8 odpowiedzi
+ 3
hi guys this is the solution.
class Enemy:
name = ""
lives = 0
def __init__(self, name, lives):
self.name = name
self.lives = lives
def hit(self):
self.lives -= 1
if self.lives <= 0:
print(self.name + ' killed')
else:
print(self.name + ' has '+ str(self.lives) + ' lives')
class Monster(Enemy):
def __init__(self):
super().__init__('Monster', 3)
class Alien(Enemy):
def __init__(self):
super().__init__('Alien', 5)
m = Monster()
a = Alien()
while True:
x = input()
if x == 'exit':
break
elif x == 'laser':
a.hit()
elif x == 'gun':
m.hit()
+ 2
This code still not running
elif x == 'laser':
a.hit()
elif x == 'gun':
m.hit()
+ 2
Finally
class Enemy:
name = ""
lives = 0
def __init__(self, name, lives):
self.name = name
self.lives = lives
def hit(self):
self.lives -= 1
if self.lives <= 0:
print(self.name + ' killed')
else:
print(self.name + ' has '+ str(self.lives) + ' lives')
class Monster(Enemy):
def __init__(self):
super().__init__('Monster', 3)
def hit(self):
super().hit()
class Alien(Enemy):
def __init__(self):
super().__init__('Alien', 5)
def hit(self):
super().hit()
m = Monster()
a = Alien()
while True:
x = input()
if x == 'exit':
break
elif x=='laser':
a.hit()
elif x=='gun':
m.hit()
+ 1
You need to do the following to complete the program:
1. Inherit the Alien and Monster classes from the Enemy class.
2. Complete the while loop that continuously takes the weapon of choice from user input and call the corresponding object's hit() method.
Sample Input
laser
laser
gun
exit
Sample Output
Alien has 4 lives
Alien has 3 lives
Monster has 2 lives
0
class Enemy:
name = ""
lives = 0
def __init__(self, name, lives):
self.name = name
self.lives = lives
def hit(self):
self.lives -= 1
if self.lives <= 0:
print(self.name + ' killed')
else:
print(self.name + ' has '+ str(self.lives) + ' lives')
class Monster:
def __init__(self):
super().__init__('Monster', 3)
class Alien:
def __init__(self):
super().__init__('Alien', 5)
m = Monster()
a = Alien()
while True:
x = input()
if x == 'exit':
break
0
i'm stuck here also. waiting for help
0
Stuck also
0
The variable x is is taking input each time the while loop runs. So the first time the while loop runs x = "gun", and not ["gun", "laser", "laser", "laser", "exit"]. x is not an iterable. The second time he while loop runs x = "laser". The while loop will run indefinitely until x has "exit" entered as its input. For anyone who was having trouble with the While loop. I was trying to use a (for i in x) loop after the while loop to get things to work.
This is what worked in the end for me.
while True:
x = input()
if x == "exit":
break
elif x == "gun":
m.hit()
elif x == "laser":
a.hit()