fork(2) download
  1. #!/usr/bin/python
  2.  
  3. #try exception checking imported modules are loaded
  4. try:
  5. import pygame
  6. from pygame.locals import *
  7. import random
  8. import time
  9. except ImportError, err:
  10. print " The module %s failed to load" %(err)
  11. exit(1)
  12.  
  13. #pygame.init()
  14.  
  15. #create object to track time
  16. #timer = pygame.time.Clock()
  17.  
  18. #set screen
  19. #screen = pygame.display.set_mode((500,300))
  20. #pygame.display.set_caption('The Game of Pig (2 dice)')
  21.  
  22. #background = pygame.Surface(screen.get_size())
  23. #background = background.convert()
  24. #background.fill((250,250,250))
  25. #clock = pygame.time.Clock()
  26.  
  27. #load images of dice faces (1-6)
  28. DIE1 = [pygame.image.load('Face-1.png'),pygame.image.load('Face-2.png'),
  29. pygame.image.load('Face-3.png'),pygame.image.load('Face-4.png'),
  30. pygame.image.load('Face-5.png'),pygame.image.load('Face-6.png')]
  31.  
  32. DIE2 = [pygame.image.load('Face-1.png'),pygame.image.load('Face-2.png'),
  33. pygame.image.load('Face-3.png'),pygame.image.load('Face-4.png'),
  34. pygame.image.load('Face-5.png'),pygame.image.load('Face-6.png')]
  35.  
  36. set_score = 20
  37.  
  38. class game():
  39. #Initialize variables to 0 and true. Fresh (new) player
  40. def __init__(self):
  41. self.round_score = 0
  42. self.total_score = 0
  43. self.turn = True
  44.  
  45. #function to roll the 2 dice. set turn to true.
  46. #return dice as a tuple
  47. def roll_dice(self):
  48. self.turn = True
  49. return (random.randint(1,6), random.randint(1,6))
  50.  
  51. #function to hold. Add player's current round score to total score
  52. #set turn to false. indicating next player's turn
  53. def hold_dice(self):
  54. self.total_score += self.round_score
  55. self.round_score = 0
  56. self.turn = False
  57. print 'hold:\ntotal_score = %s' %(self.total_score)
  58.  
  59. #function to determine:
  60. #1.either die is 1 if so round score set to 0 and turn over
  61. #2.both dice are 1 total score set to 0 and turn over
  62. def check_dice_status(self, dice):
  63. if( dice[0] == 1 and dice[1] == 1):
  64. self.round_score, self.total_score = 0,0
  65. self.turn = False
  66. print '2total_score set to 0'
  67.  
  68. elif( dice[0] == 1 or dice[1] ==1):
  69. self.total_score -= self.round_score
  70. self.round_score = 0
  71. self.turn = False
  72. print 'round_score set to 0'
  73. else:
  74. if(self.total_score >= set_score):
  75. self.turn = False
  76. #BELOW ADDED
  77. else:
  78. self.round_score += (dice[0]+dice[1])
  79. self.total_score += self.round_score
  80. self.turn = True
  81. print 'round_score = %s' %(self.round_score)
  82. print 'total_score = %s' %(self.total_score)
  83.  
  84. #function to return status on turn.
  85. #return turn (true/false)
  86. def check_turn(self):
  87. return self.turn
  88.  
  89. #function that returns the true if player is winner else false
  90. def winner(self):
  91. if(self.total_score >= set_score):
  92. return True
  93. else:
  94. return False
  95.  
  96. #create objects of the class game, which is the rules
  97. player1, player2 = game(), game()
  98.  
  99. #pick a player to go first randomly
  100. player_turn =random.randint(1,2)
  101. print 'Player %s goes first' %(player_turn)
  102.  
  103. #way to stop loop
  104. game_state = True
  105.  
  106. while(game_state):#game loop
  107. #BELOW ADDED
  108. if(player1.winner() or player2.winner()):
  109. player_turn = None
  110. game_state = False
  111. break
  112. else:
  113. #print which player's turn it is
  114. print '\nPlayer%s :' %(player_turn)
  115. option = raw_input('Roll or Hold? ')
  116.  
  117. #if player 1 do below commands, else player 2
  118. if(player_turn == 1):
  119.  
  120. #1 of 2 commands, ROLL. Check dice status to determine if their score has diminished or added onto
  121. if(option.lower() == 'roll'):
  122.  
  123. #roll contains a tuple of the random number(dice roll)
  124. roll = player1.roll_dice()
  125.  
  126. #display their rolls
  127. print 'roll[0], roll[1] =', roll[0], ' ', roll[1]
  128.  
  129. #check status of dice, adjust score accordingly
  130. player1.check_dice_status(roll)
  131.  
  132. #BELOW ADDED
  133. #determines when the player's next turn will be
  134. if (player1.check_turn() and player1.winner()):
  135. break
  136. elif (player1.check_turn()):
  137. player_turn = 1
  138. else:
  139. player_turn = 2
  140.  
  141. #2 of 2 commands, HOLD. Player holds, meaning round score is added to total score.
  142. #next player's turn
  143. elif(option.lower() == 'hold'):
  144. player1.hold_dice()
  145. player_turn = 2
  146.  
  147. #player 2 does below commands. Iterate commands from top
  148. elif(player_turn == 2):
  149.  
  150. if(option.lower() == 'roll'):
  151.  
  152. roll = player2.roll_dice()
  153. print 'roll[0], roll[1] =', roll[0], ' ', roll[1]
  154. player2.check_dice_status(roll)
  155.  
  156. #BELOW ADDED
  157. #check status of current player's turn
  158. if (player2.check_turn() and player2.winner()):
  159. break
  160. elif (player1.check_turn()):
  161. player_turn = 2
  162. else:
  163. player_turn = 1
  164.  
  165. elif(option.lower() == 'hold'):
  166. player2.hold_dice()
  167. player_turn = 1
  168.  
  169. #declare which one is the winner
  170. if(player1.winner()):
  171. print '\nplayer1 is the WINNER\n'
  172. else:
  173. print '\nplayer2 is the WINNER\n'
Runtime error #stdin #stdout 0.02s 4676KB
stdin
Standard input is empty
stdout
 The module No module named pygame failed to load