Monday, February 15, 2016

#PythonTuesday #Coding ---- Mini combat with random damage


A mini combat system using class & function, and random damage. Player is "Java", enemy is "Python"...and you can see the final output attached...

  1. import random
  2. from random import randint 
  3.  
  4. class player:
  5.     def __init__(self,name,health):
  6.         self.name = name
  7.         self.health = health
  8.  
  9.     def attack(self,enemy):
  10.          player.damage = randint(1,20) #Range of damage.
  11.          enemy.health -= player.damage #Minus enemy's health points based on player's damage.
  12.          print "{} attacks {} !".format(player.name,enemy.name)
  13.          print "{}'s health is {}!".format(enemy.name,enemy.health)
  14.          return int(enemy.health)
  15.  
  16.     def alive(self): #Define the condition that the player is alive.
  17.     return self.health > 0  

  18.  
  19. class enemy:
  20.     def __init__(self,name,health):
  21.         self.name = name
  22.         self.health = health
  23.  
  24.     def attack(self,player):
  25.         enemy.damage = randit(1,30) #Range of damage.
  26.         player.health -= enemy.damage #Minus player's health points based on enemy's damage.
  27.         print "{} attacks {}!".format(enemy.name,player.name)
  28.         print "{}'s health is {}!".format(player.name,player.health)
  29.         return int(player.health)
  30.  
  31.     def alive(self): #Define the condition that the enemy is alive.
  32.         return self.health > 0 
  33.  
  34.  
  35. def combat(player, enemy):
  36.     print "{} and {} enter into the combat!".format(player.name,enemy.name) 
  37.     while player.alive() and enemy.alive(): #While loop to keep player fighting in the combat.
  38.         player.attack(enemy)
  39.  
  40.     if player.alive():
  41.         print "{} killed {}!".format(player.name,enemy.name)
  42.     elif enemy.alive():
  43.         print "{} was killed by {}!".format(player.name,enemy.name)
  44.  
  45.     return
  46.   
  47. player = player("Java", 80)
  48. enemy = enemy("Python", 100)
  49. combat(player,enemy) #Call the function of combat.
The output:
Java and Python enter into the combat!
Java attacks Python !
Python's health is 96!
Java attacks Python !
Python's health is 83!
Java attacks Python !
Python's health is 79!
Java attacks Python !
Python's health is 62!
Java attacks Python !
Python's health is 61!
Java attacks Python !
Python's health is 48!
Java attacks Python !
Python's health is 31!
Java attacks Python !
Python's health is 14!
Java attacks Python !
Python's health is -3!
Java killed Python!
_______________________________________________________________________


 Back to CodeCombat coding. At the level of "The Final Kithmaze":

Our goals are:
  • Hero must survive.
  • Defeat the ogres(3/3)
  • Navigate the maze.
  • Under 10 statements.
  • Collect the gems.
Coding:

while True:
       self.moveRight()
       self.moveUp()
       self.moveRight()
       enemy = self.findNearestEnemy()
       self.attack(enemy)
       self.attack(enemy)
       self.moveDown(2)
       self.moveUp()
self.moveRight()


Screenshot:


No comments:

Post a Comment