+ 2
Help me explain this code
I can't understand how this code works, pls help me The code: 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 = Enemy('Monster', 3) a = Enemy('Alien', 5) while True: x = input() if x == 'exit': break if x == 'laser': a.hit() if x == 'gun': m.hit()
3 Answers
+ 4
(m) object has an enemy named "Monster" With 3 lives.
(a) object has an enemy named "Alien" with 5 lives.
When input is 'laser' (a) hits the enemy using method a.hit() else if input is 'gun' (m) hits the enemy using method m.hit().
When the hit method is called the following happens,
self.lives which has value of 3 and 5 (depending on which object called it) the value is reduced by 1,so suppose (a) called hit method for "laser" then self.value-=1 sets the new value for (a) as 4.
And the next if else statements checks if the lives left are 0 , if yes output self.name("alien" here) killed , otherwise it outputs self.name has self.lives left which is "alien" has 4 lives left.
0
class Game:
def __init__(self):
self.player = Player()
self.enemies = [Enemy(), Enemy(), Enemy()]
self.score = 0
def play(self):
while True:
# Draw the game state
self.draw()
# Get player input
action = self.get_input()
# Update game state based on player input
self.update(action)
# Check if player is dead
if self.player.is_dead():
print("Game over!")
break
def draw(self):
# Draw the player
self.player.draw()
# Draw the enemies
for enemy in self.enemies:
enemy.draw()
# Draw the score
print(f"Score: {self.score}")
def get_input(self):
# Get player input (e.g. left, right, up, down)
return input("Enter action: ")
def update(self, action):
# Move player
self.player.move(action)
# Move enemies
for enemy in self.enemies:
enemy.move()
# Check if player collided with any enemies
for enemy in self.enemies:
if self.player.collided_with(enemy):
self.player.hit()
self.score -= 1
# Check if player has reached the goal
if self.player.reached_goal():
self.score += 1
self.player.reset()
self.enemies = [Enemy(), Enemy(), Enemy()]
class Player:
def __init__(self):
self.x = 0
self.y = 0
self.health = 100
def draw(self):
# Draw the player at their current position
pass
def move(self, action):
# Move the player based on the action
pass
def collided_with(self, enemy):
# Check if the player has collided with the given enemy
pass
def hit(self):
# Reduce the player's health
self.health -= 10
def is_dead(self):
# Return True if the player is dead, False otherwise
return self.health <= 0
0
class Game:
def __init__(self):
self.player = Player()
self.enemies = [Enemy(), Enemy(), Enemy()]
self.score = 0
def play(self):
while True:
# Draw the game state
self.draw()
# Get player input
action = self.get_input()
# Update game state based on player input
self.update(action)
# Check if player is dead
if self.player.is_dead():
print("Game over!")
break
def draw(self):
# Draw the player
self.player.draw()
# Draw the enemies
for enemy in self.enemies:
enemy.draw()
# Draw the score
print(f"Score: {self.score}")
def get_input(self):
# Get player input (e.g. left, right, up, down)
return input("Enter action: ")
def update(self, action):
# Move player
self.player.move(action)
# Move enemies
for enemy in self.enemies:
enemy.move()
# Check if player collided with any enemies
for enemy in self.enemies:
if self.player.collided_with(enemy):
self.player.hit()
self.score -= 1
# Check if player has reached the goal
if self.player.reached_goal():
self.score += 1
self.player.reset()
self.enemies = [Enemy(), Enemy(), Enemy()]
class Player:
def __init__(self):
self.x = 0
self.y = 0
self.health = 100
def draw(self):
# Draw the player at their current position
pass
def move(self, action):
# Move the player based on the action
pass
def collided_with(self, enemy):
# Check if the player has collided with the given enemy
pass
def hit(self):
# Reduce the player's health
self.health -= 10
def is_dead(self):
# Return True if the player is dead, False otherwise
return self.health <= 0