Tuesday, January 19, 2016

#PythonTuesday #Coding --- Enemy Class…Enemy Objects (Cheese, Garlic, Tomato, Egg)

My example of creating Class & Objects in Python ---- create a class called "Enemy" and its objects, etc.

Also, just found out this awesome online Python interpreter and debugging tool http://ideone.com/ (Sphere Engine). You can write your own code and test / debug it right away in more than 60 programming languages! It is much better than the original Python IDLE interpreter downloaded from https://www.python.org/ (no offensive...), the original IDLE is much like the notepad editor...

Today I am able to present my code in a clean way ^ o ^ !  OR, you can view my code on http://ideone.com/r092a9 directly.


1.  #create the "Enemy" class.
2.  class Enemy:
3.   
4.      def _init_(self,name,color,hp,speed):
5.          self.name = name
6.          self.color = color
7.          self.hp = 100.00
8.          self.speed = 100.00
9.   
10.    def summary(self):
11.        sum_str = "%s %s has %d hp and %d speed points." % (self.color, self.name, self.hp,  self.speed)
12.        return sum_str
13.        #print ("EnemyName: ", self.name, ", Color: ", self.color, ", HP: ", self.hp, ", ", Speed: ", self.speed)
14.        #OR you can use the 'print' command directly...
15. 
16.# assign the class “Enemy” to the object “ene1” and “ene2” ,etc.,
17.ene1 = Enemy()
18.ene1.name = "Cheese"
19.ene1.color = "Blue"
20.ene1.hp = 50.00
21.ene1.speed = 25.00
22. 
23.ene2 = Enemy()
24.ene2.name = "Tomato"
25.ene2.color = "Red"
26.ene2.hp = 120.00
27.ene2.speed = 35.00
28. 
29.ene3 = Enemy()
30.ene3.name = "Garlic"
31.ene3.color = "White"
32.ene3.hp = 170.00
33.ene3.speed = 40.00
34. 
35.ene4 = Enemy()
36.ene4.name = "Egg"
37.ene4.color = "Yellow"
38.ene4.hp = 300.00
39.ene4.speed = 60.00
40. 
41.# call the function
42.print ene1.summary()
43.print ene2.summary()
44.print ene3.summary()

45.print ene4.summary()


Success

 The output:




Blue Cheese has 50 hp and 25 speed points.
Red Tomato has 120 hp and 35 speed points.
White Garlic has 170 hp and 40 speed points.
Yellow Egg has 300 hp and 60 speed points.

----------------------------------------------------------------------------------------


Back to CodeCombat! 


Level --- Breakout

Goals:
  • Free your friend
  • Escape the level
  • Under 7 Statements
Code:
self.moveRight()
self.attack("Weak Door")
self.moveRight()
self.moveDown()
while True:
self.attack("Door")

No comments:

Post a Comment