Advertisement
xgeovanni

Calculator

Jan 8th, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.94 KB | None | 0 0
  1. # Calculator
  2. # a simple calculator using pygame
  3. # Version 1.21
  4. # Bobby Clarke
  5.  
  6. # Change log:
  7.  
  8. # 1.0 initial relaese with numbers and basic operators, no decimals
  9. # 1.1 added decimal answers and Error messages
  10. # 1.2 added decimal input
  11.  
  12. # Upcoming:
  13.  
  14. # Ability to use keyboard for input
  15. # 10 digit limit
  16.  
  17. # Imports
  18. import pygame, math, sys
  19. from pygame.locals import *
  20. pygame.init()
  21.  
  22. #   #   #   #   #
  23.  
  24. screen = pygame.display.set_mode((180, 300))
  25.  
  26. clock = pygame.time.Clock()
  27.  
  28. interface = pygame.image.load("calc.png") # the GUI image for the calculator
  29. errormsg = pygame.image.load("error.png") # The error message
  30.  
  31. # All the number and operator images
  32.  
  33. #numbers
  34. img0 = pygame.image.load("Numbers/0.png")
  35. img1 = pygame.image.load("Numbers/1.png")
  36. img2 = pygame.image.load("Numbers/2.png")
  37. img3 = pygame.image.load("Numbers/3.png")
  38. img4 = pygame.image.load("Numbers/4.png")
  39. img5 = pygame.image.load("Numbers/5.png")
  40. img6 = pygame.image.load("Numbers/6.png")
  41. img7 = pygame.image.load("Numbers/7.png")
  42. img8 = pygame.image.load("Numbers/8.png")
  43. img9 = pygame.image.load("Numbers/9.png")
  44.  
  45. # operators
  46. plus = pygame.image.load("Numbers/+.png")
  47. minus = pygame.image.load("Numbers/-.png")
  48. multiply = pygame.image.load("Numbers/multiply.png")
  49. divide = pygame.image.load("Numbers/divide.png")
  50. dot = pygame.image.load("Numbers/dot.png")
  51.  
  52. #   #   #   #   #
  53.  
  54. # The dictionary showing which images coresponds to which operation
  55.  
  56. op2img = {# Numbers
  57.           "0" : img0,
  58.           "1" : img1,
  59.           "2" : img2,
  60.           "3" : img3,
  61.           "4" : img4,
  62.           "5" : img5,
  63.           "6" : img6,
  64.           "7" : img7,
  65.           "8" : img8,
  66.           "9" : img9,
  67.  
  68.           #Operators
  69.           "+" : plus,
  70.           "-" : minus,
  71.           "*" : multiply,
  72.           "/" : divide,
  73.           "." : dot}
  74.  
  75. #   #   #   #   #
  76.          
  77. num = False # a variable to temporarily hold a number
  78. op = False # a variable to temporarily hold an operator
  79. back = False # will clear the last operation when true
  80. equals = False # when this is true it will find the solution to the question
  81. answer = False # will become the answer
  82. mousepress = False # True if mouse button is pressed
  83. error = False # true if there is an error
  84.  
  85. operations = "" # a list of operations
  86.  
  87. while True:
  88.    
  89.     #reset the variables
  90.     num = False
  91.     op = False
  92.     back = False
  93.     equals = False
  94.    
  95.     if mousepress:
  96.         # Decides what the mouse press means, if anything
  97.         # this is done by determining whether the mouse was on a button when pressed
  98.         # the ranges are the x and y positions of the buttons
  99.         # and are being compared to the position of the mouse at the time of being pressed
  100.        
  101.         if mouse_x in range(13, 44):
  102.             if mouse_y in range(126, 146):
  103.                 num = "1"
  104.             elif mouse_y in range(151, 171):
  105.                 num = "4"
  106.             elif mouse_y in range(179, 199):
  107.                 num = "7"
  108.             elif mouse_y in range(208, 228):
  109.                 num = "0"
  110.  
  111.             is_number = True
  112.  
  113.         elif mouse_x in range(47, 79):
  114.             if mouse_y in range(126, 146):
  115.                 num = "2"
  116.             elif mouse_y in range(151, 171):
  117.                 num = "5"
  118.             elif mouse_y in range(179, 199):
  119.                 num = "8"
  120.             is_number = True
  121.  
  122.         elif mouse_x in range(80, 113):
  123.             if mouse_y in range(126, 146):
  124.                 num = "3"
  125.             elif mouse_y in range(151, 171):
  126.                 num = "6"
  127.             elif mouse_y in range(179, 199):
  128.                 num = "9"
  129.  
  130.         elif mouse_x in range(132, 164):
  131.             if mouse_y in range(126, 146):
  132.                 op = "+"
  133.             elif mouse_y in range(151, 171):
  134.                 op = "-"
  135.             elif mouse_y in range(179, 199):
  136.                 op = "*"
  137.             elif mouse_y in range(208, 228):
  138.                 op = "/"
  139.             elif mouse_y in range(232, 252):
  140.                 op = "."
  141.  
  142.         elif mouse_x in range (15, 70) and mouse_y in range (258, 288):
  143.             back = True
  144.  
  145.         elif mouse_x in range(89, 167) and mouse_y in range(258, 288):
  146.             equals = True
  147.  
  148.         else:
  149.             pass
  150.  
  151.         mousepress = False
  152.  
  153.  
  154.     if num:
  155.         # adds a number to the operations
  156.         operations += num
  157.     elif op:
  158.         # adds an operator to the operations
  159.         operations += op
  160.     elif back:
  161.         # This removes the last Item in operations
  162.         if len(operations) > 1:
  163.             operations = operations[0 : len(operations) - 1]
  164.         else:
  165.             operations = ""
  166.            
  167.     elif equals:
  168.         # find an answer by evaluating the operations
  169.         try:
  170.             if len(operations) > 1:
  171.                 answer = str(eval(operations))
  172.             else:
  173.                 answer = 0
  174.  
  175.         except:
  176.             error = True
  177.  
  178.     if len(operations) > 10:
  179.         operations = operations[0 : 10]
  180.            
  181.        
  182.            
  183.     screen.fill((255, 255, 255))
  184.  
  185.     if answer:
  186.         count = 0
  187.         for i in answer:
  188.             count += 1
  189.             img = op2img[i]
  190.             screen.blit(img, (0 + (count * 15), 78))
  191.  
  192.     elif error:
  193.         screen.blit(errormsg, (15, 78))
  194.  
  195.     else:
  196.         count = 0
  197.         for operation in operations:
  198.             count += 1
  199.             img = op2img[operation]
  200.             screen.blit(img, (0 + (count * 15), 78))
  201.  
  202.     screen.blit(interface, (0, 0))
  203.  
  204.     pygame.display.update()
  205.    
  206.     for event in pygame.event.get():
  207.         if event.type == pygame.QUIT:
  208.             sys.exit()
  209.  
  210.         elif event.type == MOUSEBUTTONDOWN:
  211.             mouse_x, mouse_y = pygame.mouse.get_pos()
  212.             mousepress = True
  213.  
  214.             if answer:
  215.                 answer = False
  216.                 operations = ""
  217.  
  218.             elif error:
  219.                 error = False
  220.                 operations = ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement