Use Python core programming to crack difficulties in game exercises

Source: Internet
Author: User

If you encounter problems that cause you pain in game exercises, the following articles will solve the problems encountered in the actual operations on game exercises in Python core programming, and detailed analysis of the relevant code, the following is a detailed introduction of the article.

A game exercise in Python core programming

According to the questions in the book, I expanded it and wrote a game robot automatic game competition plan description: first define a base class: AutoPeople, which contains a global member, valid GameDic = {'stone ': 'scissors', 'scissors ': 'bus', 'bus ': 'stone '} refers to stone> scissors> Cloth> stone, and a Score value must be defined.

The Code is as follows:

 
 
  1. PythonCode: #-*-coding: gbk -*-
  2. Import time, random
  3. Class BaseAuto:
  4. GameDic = {'stone ': 'scissors', 'scissors ': 'cloth', 'cloth ': 'stone '}
  5. Def _ init _ (self ):
  6. Self. score = 0
  7. Def OutHandle (self ):
  8. Return None
  9. Class AutoPeopl1 (BaseAuto ):
  10. Def OutHandle (self ):
  11. Return self. GameDic. keys () [0]
  12. Class AutoPeople2 (BaseAuto ):
  13. Def OutHandle (self ):
  14. Return self. GameDic. keys () [random. randint (0, 2)]
  15. Class DoStart:
  16. Def _ init _ (self, Pa, Pb ):
  17. Self. pa = Pa
  18. Self. pb = Pb
  19. Def Play (self, PlayCount ):
  20. Self. pa. score = 0
  21. Self. pb. score = 0
  22. PjCount = 0
  23. For I in range (0, PlayCount ):
  24. PaT = time. clock ()
  25. PaValue = self. pa. OutHandle ()
  26. PaT = time. clock ()-paT
  27. PbT = time. clock ()
  28. PbValue = self. pb. OutHandle ()
  29. PbT = time. clock ()-pbT
  30. If (paT> 100) or (pbT> 100 ):
  31. If paT> 100:
  32. Self. pa. score-= 5
  33. If pbT> 100:
  34. Self. pb. score-= 5
  35. Continue
  36. If (paT> 10) or (pbT> 10 ):
  37. If paT> 10:
  38. Self. pa. score-= 1
  39. If pbT> 10:
  40. Self. pb. score-= 1
  41. Print '-* bureau % d -*-*-*-*-*-*-*-* -* \ n
    Robot 1 Output: % s \ n robot 2 output: % s' % (I + 1, paValue, pbValue)
  42. If self. pa. GameDic [paValue] = pbValue:
  43. Self. pa. score + = 5
  44. Print 'Robot 1 Shengli'
  45. Elif self. pb. GameDic [pbValue] = paValue:
  46. Self. pb. score + = 5
  47. Print 'Robot 2 Shengli'
  48. Else:
  49. PjCount + = 1
  50. Print 'final score: \ n robot 1 score: % d \ n robot 2 score: % d \ n
    Draw % d' % (self. pa. score, self. pb. score, PjCount)
  51. Def GameTest ():
  52. P1 = AutoPeopl1 ()
  53. P2 = AutoPeople2 ()
  54. Playgame = DoStart (p1, p2)
  55. InputI = 'sdf'
  56. Count = 0
  57. While not inputI. isdigit ():
  58. InputI = raw_input ('enter a number ')
  59. Print 'test'
  60. If inputI. isdigit ():
  61. Count = int (inputI)
  62. Break
  63. Else: print ('enter a number ')
  64. Playgame. Play (count)
  65. If _ name _ = "_ main __":
  66. GameTest ()

The above article introduces the actual operation scheme and related code of a game exercise in Python core programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.