Simulated shooting word games (Python)

  Before using java conduct a relatively simple test - fire a gun simulation game, specific content here much to say, the following is python version for python on the syntax requires more loosely than other languages, so having some pit at the time of writing .

  Simply because in order to achieve the purpose, the following code is not written in simple, here to talk about the main pit encountered: when dealing with variables, because variables in python does not need to declare a data type in use, but if you take a direct use will be given: TypeError: Unsupported the operand type (S) for -: 'Method' and 'int'

  Baidu a bit and found a grammar problem.

  Above an error: because without C like in Python, you need int sum (of the type specified sum), but it does not mean that can be placed directly in an expression to calculate, we still need to be defined (these are small details, usually should be avoided, do collect similar problems)

Code:

 

1 class BULLET_BOX:
2     def __init__(self):
3         self.bullet_count = 0
4 
5     def Add_Bullet(self, count):
6         self.bullet_count = count
7 
8     def See_Bullet(self):
9         return self.bullet_count

 

 1 class GUN:
 2     def __init__(self, BULLET_BOX):
 3         self.BULLET_BOX = BULLET_BOX
 4 
 5     def Shoot(self):
 6         self.count = self.BULLET_BOX.See_Bullet()
 7         if (self.count > 0):
 8             self.count = self.BULLET_BOX.See_Bullet()
 9             self.count = self.count - 1
10             self.BULLET_BOX.Add_Bullet(self.count)
11             print("砰——")
12             print(" The current number of bullets: " , self.count)
 13          the else :
 14              Print ( " lack of bullets! " )

 

1 class BULLET_BOX:
2     def __init__(self):
3         self.bullet_count = 0
4 
5     def Add_Bullet(self, count):
6         self.bullet_count = count
7 
8     def See_Bullet(self):
9         return self.bullet_count

 

 1 import time, PEOPLE, GUN, BULLET_BOX
 2 
 3 from PEOPLE import *
 4 from GUN import *
 5 from BULLET_BOX import *
 6 
 7 a = BULLET_BOX()
 8 a.Add_Bullet(5)
 9 b = GUN(a)
10 c = PEOPLE(b)
11 
12 while True:
13     c.Fire()
14     time.sleep(1)

 

Tag performance:

 

 

Guess you like

Origin www.cnblogs.com/moegarn/p/12355113.html