Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.24 KB | None | 0 0
  1. __author__ = '1011923269'
  2. import pygame, sys, random, time
  3. from pygame.locals import *
  4.  
  5. pygame.init()
  6.  
  7. clear = False
  8. clicked = False
  9. box_is_blue = True
  10. hidden = False
  11. endgame = False
  12.  
  13. FPS = 60
  14. fpsClock = pygame.time.Clock()
  15.  
  16. dice1 = random.randint(1,6)
  17. dice2 = random.randint(1,6)
  18. dice3 = random.randint(1,6)
  19. dice4 = random.randint(1,6)
  20. dice5 = random.randint(1,6)
  21.  
  22. comp1 = random.randint(1,6)
  23. comp2 = random.randint(1,6)
  24. comp3 = random.randint(1,6)
  25. comp4 = random.randint(1,6)
  26. comp5 = random.randint(1,6)
  27.  
  28. _screen = pygame.display.set_mode((550, 400))
  29. _blue = (0, 100, 255)
  30. _yellow = (255, 255, 0)
  31. _red = (255, 0, 0)
  32. _screen.fill((0, 0, 0))
  33. _white = (255, 255, 255)
  34. _black = (0, 0, 0)
  35.  
  36. fontObj = pygame.font.Font('freesansbold.ttf', 32)
  37. endFont = pygame.font.Font('freesansbold.ttf', 72)
  38.  
  39. ######################################
  40.  
  41. # Dice nr. 1
  42. _dice1SurfaceObj = fontObj.render(str(dice1), True, _yellow, _blue)
  43. _dice1RectObj = _dice1SurfaceObj.get_rect()
  44. _dice1RectObj.center = (60,95)
  45.  
  46. # Dice nr. 2
  47. _dice2SurfaceObj = fontObj.render(str(dice2), True, _yellow, _blue)
  48. _dice2RectObj = _dice2SurfaceObj.get_rect()
  49. _dice2RectObj.center = (160,95)
  50.  
  51. # Dice nr. 3
  52. _dice3SurfaceObj = fontObj.render(str(dice3), True, _yellow, _blue)
  53. _dice3RectObj = _dice3SurfaceObj.get_rect()
  54. _dice3RectObj.center = (260,95)
  55.  
  56. # Dice nr. 4
  57. _dice4SurfaceObj = fontObj.render(str(dice4), True, _yellow, _blue)
  58. _dice4RectObj = _dice4SurfaceObj.get_rect()
  59. _dice4RectObj.center = (360,95)
  60.  
  61. # Dice nr. 5
  62. _dice5SurfaceObj = fontObj.render(str(dice5), True, _yellow, _blue)
  63. _dice5RectObj = _dice5SurfaceObj.get_rect()
  64. _dice5RectObj.center = (460,95)
  65.  
  66. ##################
  67.  
  68. # Comp Dice nr. 1
  69. _comp1SurfaceObj = fontObj.render(str(comp1), True, _yellow, _red)
  70. _comp1RectObj = _comp1SurfaceObj.get_rect()
  71. _comp1RectObj.center = (60,195)
  72.  
  73. # Comp Dice nr. 2
  74. _comp2SurfaceObj = fontObj.render(str(comp2), True, _yellow, _red)
  75. _comp2RectObj = _comp2SurfaceObj.get_rect()
  76. _comp2RectObj.center = (160,195)
  77.  
  78. # Comp Dice nr. 3
  79. _comp3SurfaceObj = fontObj.render(str(comp3), True, _yellow, _red)
  80. _comp3RectObj = _comp3SurfaceObj.get_rect()
  81. _comp3RectObj.center = (260,195)
  82.  
  83. # Comp Dice nr. 4
  84. _comp4SurfaceObj = fontObj.render(str(comp4), True, _yellow, _red)
  85. _comp4RectObj = _comp4SurfaceObj.get_rect()
  86. _comp4RectObj.center = (360,195)
  87.  
  88. # Comp Dice nr. 5
  89. _comp5SurfaceObj = fontObj.render(str(comp5), True, _yellow, _red)
  90. _comp5RectObj = _comp5SurfaceObj.get_rect()
  91. _comp5RectObj.center = (460,195)
  92.  
  93. ######################################
  94.  
  95. # reroll all
  96. _rerollSurfaceObj = fontObj.render('Reroll all', True, _yellow, _blue)
  97. _rerollRectObj = _rerollSurfaceObj.get_rect()
  98. _rerollRectObj.center = (115,295)
  99.  
  100. # Show hidden
  101. _hiddenSurfaceObj = fontObj.render('Show hidden', True, _yellow, _blue)
  102. _hiddenRectObj = _hiddenSurfaceObj.get_rect()
  103. _hiddenRectObj.center = (345,295)
  104.  
  105.  
  106. ######################################
  107.  
  108. # Reroll all
  109. def reroll_all():
  110.     global _dice1SurfaceObj
  111.     global _dice2SurfaceObj
  112.     global _dice3SurfaceObj
  113.     global _dice4SurfaceObj
  114.     global _dice5SurfaceObj
  115.     dice1 = random.randint(1,6)
  116.     dice2 = random.randint(1,6)
  117.     dice3 = random.randint(1,6)
  118.     dice4 = random.randint(1,6)
  119.     dice5 = random.randint(1,6)
  120.     _dice1SurfaceObj = fontObj.render(str(dice1), True, _yellow, _blue)
  121.     _dice2SurfaceObj = fontObj.render(str(dice2), True, _yellow, _blue)
  122.     _dice3SurfaceObj = fontObj.render(str(dice3), True, _yellow, _blue)
  123.     _dice4SurfaceObj = fontObj.render(str(dice4), True, _yellow, _blue)
  124.     _dice5SurfaceObj = fontObj.render(str(dice5), True, _yellow, _blue)
  125.  
  126. def show_hidden():
  127.     global _hiddenSurfaceObj
  128.     global _dice5SurfaceObj
  129.     dice5 = random.randint(1,6)
  130.     _dice5SurfaceObj = fontObj.render(str(dice5), True, _yellow, _blue)
  131.  
  132.  
  133.  
  134. def _drawNumbers():
  135.     _screen.blit(_dice1SurfaceObj, _dice1RectObj)
  136.     _screen.blit(_dice2SurfaceObj, _dice2RectObj)
  137.     _screen.blit(_dice3SurfaceObj, _dice3RectObj)
  138.     _screen.blit(_dice4SurfaceObj, _dice4RectObj)
  139.     if hidden == True:
  140.         _screen.blit(_dice5SurfaceObj, _dice5RectObj)
  141.  
  142.     _screen.blit(_comp1SurfaceObj, _comp1RectObj)
  143.     _screen.blit(_comp2SurfaceObj, _comp2RectObj)
  144.     _screen.blit(_comp3SurfaceObj, _comp3RectObj)
  145.     _screen.blit(_comp4SurfaceObj, _comp4RectObj)
  146.     _screen.blit(_comp5SurfaceObj, _comp5RectObj)
  147.  
  148.     if clear == False:
  149.         _screen.blit(_rerollSurfaceObj, _rerollRectObj)
  150.         _screen.blit(_hiddenSurfaceObj, _hiddenRectObj)
  151.  
  152. def _endGame():
  153.     userSum = dice1+dice2+dice3+dice4+dice5
  154.     compSum = comp1+comp2+comp3+comp4+comp5
  155.     if userSum > compSum:
  156.         _screen.fill((0,0,0))
  157.         _text1 = endFont.render("You Win!!!", True, _white, _black)
  158.         _text1RectObj = _text1.get_rect()
  159.         _text1RectObj.center = (260,300)
  160.         _screen.blit(_text1, _text1RectObj)
  161.  
  162.     if userSum == compSum:
  163.         _screen.fill((0,0,0))
  164.         _text2 = endFont.render("It's a tie", True, _white, _black)
  165.         _text2RectObj = _text2.get_rect()
  166.         _text2RectObj.center = (260,300)
  167.         _screen.blit(_text2, _text2RectObj)
  168.  
  169.     if userSum < compSum:
  170.         _screen.fill((0,0,0))
  171.         _text3 = endFont.render("You Lose!!", True, _white, _black)
  172.         _text3RectObj = _text3.get_rect()
  173.         _text3RectObj.center = (260,300)
  174.         _screen.blit(_text3, _text3RectObj)
  175.  
  176. def _drawAll():
  177.     pygame.draw.rect(_screen, _blue, pygame.Rect(35, 69, 50, 50))
  178.     pygame.draw.rect(_screen, _blue, pygame.Rect(135, 69, 50, 50))
  179.     pygame.draw.rect(_screen, _blue, pygame.Rect(235, 69, 50, 50))
  180.     pygame.draw.rect(_screen, _blue, pygame.Rect(335, 69, 50, 50))
  181.     pygame.draw.rect(_screen, _blue, pygame.Rect(435, 69, 50, 50))
  182.  
  183.     pygame.draw.rect(_screen, _red, pygame.Rect(35, 169, 50, 50))
  184.     pygame.draw.rect(_screen, _red, pygame.Rect(135, 169, 50, 50))
  185.     pygame.draw.rect(_screen, _red, pygame.Rect(235, 169, 50, 50))
  186.     pygame.draw.rect(_screen, _red, pygame.Rect(335, 169, 50, 50))
  187.     pygame.draw.rect(_screen, _red, pygame.Rect(435, 169, 50, 50))
  188.  
  189.  
  190.  
  191.  
  192. pygame.display.set_caption('Hello World!')
  193. while True: # main game loop
  194.  
  195.     _drawAll()
  196.  
  197.  
  198.     if endgame == False:
  199.         pygame.draw.rect(_screen, _blue, pygame.Rect(35, 269, 160, 50))
  200.         pygame.draw.rect(_screen, _blue, pygame.Rect(235, 269, 220, 50))
  201.  
  202.     _rerollRect = pygame.Rect(35, 269, 160, 50)
  203.     _hiddenRect = pygame.Rect(235, 269, 220, 50)
  204.  
  205.     _drawNumbers()
  206.  
  207.     for event in pygame.event.get():
  208.  
  209.  
  210.         if event.type == QUIT:
  211.             pygame.quit()
  212.             sys.exit
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.         if event.type == pygame.MOUSEBUTTONUP and clicked == False:
  220.             _mousePos = pygame.mouse.get_pos()
  221.  
  222.             if _rerollRect.collidepoint(_mousePos):
  223.                 hidden = True
  224.                 reroll_all()
  225.                 _endGame()
  226.                 clicked = True
  227.                 endgame = True
  228.                 clear = True
  229.                 break
  230.  
  231.             if _hiddenRect.collidepoint(_mousePos):
  232.                 hidden = True
  233.                 show_hidden()
  234.                 _endGame()
  235.                 clicked = True
  236.                 endgame = True
  237.                 clear = True
  238.                 break
  239.  
  240.  
  241.  
  242.  
  243.     pygame.draw
  244.  
  245.     pygame.display.flip()
  246.     fpsClock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement