Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Calculator
- # a simple calculator using pygame
- # Version 1.21
- # Bobby Clarke
- # Change log:
- # 1.0 initial relaese with numbers and basic operators, no decimals
- # 1.1 added decimal answers and Error messages
- # 1.2 added decimal input
- # Upcoming:
- # Ability to use keyboard for input
- # 10 digit limit
- # Imports
- import pygame, math, sys
- from pygame.locals import *
- pygame.init()
- # # # # #
- screen = pygame.display.set_mode((180, 300))
- clock = pygame.time.Clock()
- interface = pygame.image.load("calc.png") # the GUI image for the calculator
- errormsg = pygame.image.load("error.png") # The error message
- # All the number and operator images
- #numbers
- img0 = pygame.image.load("Numbers/0.png")
- img1 = pygame.image.load("Numbers/1.png")
- img2 = pygame.image.load("Numbers/2.png")
- img3 = pygame.image.load("Numbers/3.png")
- img4 = pygame.image.load("Numbers/4.png")
- img5 = pygame.image.load("Numbers/5.png")
- img6 = pygame.image.load("Numbers/6.png")
- img7 = pygame.image.load("Numbers/7.png")
- img8 = pygame.image.load("Numbers/8.png")
- img9 = pygame.image.load("Numbers/9.png")
- # operators
- plus = pygame.image.load("Numbers/+.png")
- minus = pygame.image.load("Numbers/-.png")
- multiply = pygame.image.load("Numbers/multiply.png")
- divide = pygame.image.load("Numbers/divide.png")
- dot = pygame.image.load("Numbers/dot.png")
- # # # # #
- # The dictionary showing which images coresponds to which operation
- op2img = {# Numbers
- "0" : img0,
- "1" : img1,
- "2" : img2,
- "3" : img3,
- "4" : img4,
- "5" : img5,
- "6" : img6,
- "7" : img7,
- "8" : img8,
- "9" : img9,
- #Operators
- "+" : plus,
- "-" : minus,
- "*" : multiply,
- "/" : divide,
- "." : dot}
- # # # # #
- num = False # a variable to temporarily hold a number
- op = False # a variable to temporarily hold an operator
- back = False # will clear the last operation when true
- equals = False # when this is true it will find the solution to the question
- answer = False # will become the answer
- mousepress = False # True if mouse button is pressed
- error = False # true if there is an error
- operations = "" # a list of operations
- while True:
- #reset the variables
- num = False
- op = False
- back = False
- equals = False
- if mousepress:
- # Decides what the mouse press means, if anything
- # this is done by determining whether the mouse was on a button when pressed
- # the ranges are the x and y positions of the buttons
- # and are being compared to the position of the mouse at the time of being pressed
- if mouse_x in range(13, 44):
- if mouse_y in range(126, 146):
- num = "1"
- elif mouse_y in range(151, 171):
- num = "4"
- elif mouse_y in range(179, 199):
- num = "7"
- elif mouse_y in range(208, 228):
- num = "0"
- is_number = True
- elif mouse_x in range(47, 79):
- if mouse_y in range(126, 146):
- num = "2"
- elif mouse_y in range(151, 171):
- num = "5"
- elif mouse_y in range(179, 199):
- num = "8"
- is_number = True
- elif mouse_x in range(80, 113):
- if mouse_y in range(126, 146):
- num = "3"
- elif mouse_y in range(151, 171):
- num = "6"
- elif mouse_y in range(179, 199):
- num = "9"
- elif mouse_x in range(132, 164):
- if mouse_y in range(126, 146):
- op = "+"
- elif mouse_y in range(151, 171):
- op = "-"
- elif mouse_y in range(179, 199):
- op = "*"
- elif mouse_y in range(208, 228):
- op = "/"
- elif mouse_y in range(232, 252):
- op = "."
- elif mouse_x in range (15, 70) and mouse_y in range (258, 288):
- back = True
- elif mouse_x in range(89, 167) and mouse_y in range(258, 288):
- equals = True
- else:
- pass
- mousepress = False
- if num:
- # adds a number to the operations
- operations += num
- elif op:
- # adds an operator to the operations
- operations += op
- elif back:
- # This removes the last Item in operations
- if len(operations) > 1:
- operations = operations[0 : len(operations) - 1]
- else:
- operations = ""
- elif equals:
- # find an answer by evaluating the operations
- try:
- if len(operations) > 1:
- answer = str(eval(operations))
- else:
- answer = 0
- except:
- error = True
- if len(operations) > 10:
- operations = operations[0 : 10]
- screen.fill((255, 255, 255))
- if answer:
- count = 0
- for i in answer:
- count += 1
- img = op2img[i]
- screen.blit(img, (0 + (count * 15), 78))
- elif error:
- screen.blit(errormsg, (15, 78))
- else:
- count = 0
- for operation in operations:
- count += 1
- img = op2img[operation]
- screen.blit(img, (0 + (count * 15), 78))
- screen.blit(interface, (0, 0))
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == MOUSEBUTTONDOWN:
- mouse_x, mouse_y = pygame.mouse.get_pos()
- mousepress = True
- if answer:
- answer = False
- operations = ""
- elif error:
- error = False
- operations = ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement