Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update main.py
  • Loading branch information
shs19026 committed Mar 3, 2022
1 parent 6e7909d commit 602cbdf
Showing 1 changed file with 169 additions and 14 deletions.
183 changes: 169 additions & 14 deletions main.py
@@ -1,10 +1,16 @@
from subprocess import list2cmdline
import pygame

pygame.init()
win = pygame.display.set_mode((500, 500))
win.fill((235, 235, 235))

pygame.display.set_caption("Calculator")
list_of_ops = []
current_num = ''
temp_num1 = ''
temp_num2 = ''
temp_res = 0


class button():
Expand All @@ -19,12 +25,14 @@ class button():
def draw(self, win, outline=None):
# Call this method to draw the button on the screen
if outline:
pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
pygame.draw.circle(win, outline, (self.x + self.width/2, self.y + self.height/2), 30.0, self.width, True, True, True, True)
#pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
pygame.draw.circle(win, self.color, (self.x + self.width/2, self.y + self.height/2), 30.0, self.width, True, True, True, True)
#pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)

if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
font = pygame.font.SysFont('Segoe UI', 35)
text = font.render(self.text, 1, (0, 0, 0))
win.blit(text, (
self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
Expand All @@ -37,22 +45,36 @@ class button():

return False

def eval_L(L):
pass








run = True
one_button = button((235, 0, 0), 25, 100, 75, 75, "1")
two_button = button((235, 0, 0), 125, 100, 75, 75, "2")
three_button = button((235, 0, 0), 225, 100, 75, 75, "3")
four_button = button((235, 0, 0), 25, 200, 75, 75, "4")
five_button = button((235, 0, 0), 125, 200, 75, 75, "5")
six_button = button((235, 0, 0), 225, 200, 75, 75, "6")
seven_button = button((235, 0, 0), 25, 300, 75, 75, "7")
eight_button = button((235, 0, 0), 125, 300, 75, 75, "8")
nine_button = button((235, 0, 0), 225, 300, 75, 75, "9")
seven_button = button((173, 216, 230), 25, 100, 75, 75, "7")
eight_button = button((173, 216, 230), 125, 100, 75, 75, "8")
nine_button = button((173, 216, 230), 225, 100, 75, 75, "9")
four_button = button((173, 216, 230), 25, 200, 75, 75, "4")
five_button = button((173, 216, 230), 125, 200, 75, 75, "5")
six_button = button((173, 216, 230), 225, 200, 75, 75, "6")
one_button = button((173, 216, 230), 25, 300, 75, 75, "1")
two_button = button((173, 216, 230), 125, 300, 75, 75, "2")
three_button = button((173, 216, 230), 225, 300, 75, 75, "3")
zero_button = button((173, 216, 230), 125, 400, 75, 75, "0")
plus_button = button((255, 229, 180), 325, 300, 75, 75, "+")
minus_button = button((255, 229, 180), 325, 200, 75, 75, "-")
multiply_button = button((255, 229, 180), 425, 300, 75, 75, "*")
divide_button = button((255, 229, 180), 425, 200, 75, 75, "/")
enter_button = button((180, 217, 202), 325, 400, 75, 75, "=")


while run:
clicked_enter = False
one_button.draw(win)
two_button.draw(win)
three_button.draw(win)
Expand All @@ -62,11 +84,17 @@ while run:
seven_button.draw(win)
eight_button.draw(win)
nine_button.draw(win)
zero_button.draw(win)
plus_button.draw(win)
minus_button.draw(win)
multiply_button.draw(win)
divide_button.draw(win)
enter_button.draw(win)


pygame.display.update()
# event, anything user do check for event
list_of_ops = []

for event in pygame.event.get():
pos = pygame.mouse.get_pos()
# pygame.event.get() like a queue of commands
Expand All @@ -75,7 +103,133 @@ while run:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if one_button.isOver(pos):
list_of_ops.append(1)
current_num += '1'
elif two_button.isOver(pos):
current_num += '2'
elif three_button.isOver(pos):
current_num += '3'
elif four_button.isOver(pos):
current_num += '4'
elif five_button.isOver(pos):
current_num += '5'
elif six_button.isOver(pos):
current_num += '6'
elif seven_button.isOver(pos):
current_num += '7'
elif eight_button.isOver(pos):
current_num += '8'
elif nine_button.isOver(pos):
current_num += '9'
elif zero_button.isOver(pos):
current_num += '0'
elif plus_button.isOver(pos):
list_of_ops.append(current_num)
current_num = ''
list_of_ops.append('+')

elif minus_button.isOver(pos):
list_of_ops.append(current_num)
current_num = ''
list_of_ops.append('-')

elif multiply_button.isOver(pos):
list_of_ops.append(current_num)
current_num = ''
list_of_ops.append('*')
elif divide_button.isOver(pos):
list_of_ops.append(current_num)
current_num = ''
list_of_ops.append('/')

elif enter_button.isOver(pos):
print(list_of_ops)



if event.type == pygame.MOUSEMOTION:
if one_button.isOver(pos):
one_button.color = (193, 226, 250)
else:
one_button.color = (173, 216, 230)

if two_button.isOver(pos):
two_button.color = (193, 226, 250)
else:
two_button.color = (173, 216, 230)

if three_button.isOver(pos):
three_button.color = (193, 226, 250)
else:
three_button.color = (173, 216, 230)

if four_button.isOver(pos):
four_button.color = (193, 226, 250)
else:
four_button.color = (173, 216, 230)

if five_button.isOver(pos):
five_button.color = (193, 226, 250)
else:
five_button.color = (173, 216, 230)

if six_button.isOver(pos):
six_button.color = (193, 226, 250)
else:
six_button.color = (173, 216, 230)

if seven_button.isOver(pos):
seven_button.color = (193, 226, 250)
else:
seven_button.color = (173, 216, 230)

if eight_button.isOver(pos):
eight_button.color = (193, 226, 250)
else:
eight_button.color = (173, 216, 230)

if nine_button.isOver(pos):
nine_button.color = (193, 226, 250)
else:
nine_button.color = (173, 216, 230)

if zero_button.isOver(pos):
zero_button.color = (193, 226, 250)
else:
zero_button.color = (173, 216, 230)

if plus_button.isOver(pos):
plus_button.color = (255, 249, 200)
else:
plus_button.color = (255, 229, 180)

if minus_button.isOver(pos):
minus_button.color = (255, 249, 200)
else:
minus_button.color = (255, 229, 180)

if multiply_button.isOver(pos):
multiply_button.color = (255, 249, 200)
else:
multiply_button.color = (255, 229, 180)

if divide_button.isOver(pos):
divide_button.color = (255, 249, 200)
else:
divide_button.color = (255, 229, 180)

if enter_button.isOver(pos):
enter_button.color = (200, 237, 222)
else:
enter_button.color = (180, 217, 202)



if clicked_enter:
result = eval_L(list_of_ops)
print(result)





# keys is something that gets pressed
Expand All @@ -84,3 +238,4 @@ while run:


pygame.quit()

0 comments on commit 602cbdf

Please sign in to comment.