Repository navigation
Expand file tree
/
Copy pathcalculator.py
More file actions
97 lines (83 loc) · 3.33 KB
/
Copy pathcalculator.py
File metadata and controls
97 lines (83 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pyxel
from button import Button
from operators import Operator
from display import Display
buttonsList = ["(", ")", "CE", 7, 8, 9, 4, 5, 6, 1, 2, 3, "-", 0, ".", "⌫"]
# List of operators. The objects are just used for the construction of Button objects
operators = [
Operator("basic", "+", [pyxel.KEY_PLUS, pyxel.KEY_KP_PLUS]),
Operator("basic", "-", [pyxel.KEY_MINUS, pyxel.KEY_KP_MINUS]),
Operator("basic", "*", pyxel.KEY_KP_MULTIPLY, altKey=(pyxel.KEY_SHIFT, pyxel.KEY_PLUS)),
Operator("basic", "/", pyxel.KEY_KP_DIVIDE, altKey=(pyxel.KEY_SHIFT, pyxel.KEY_7)),
Operator("basic", "=", [pyxel.KEY_RETURN, pyxel.KEY_KP_ENTER]),
]
class Calculator:
def __init__(self, width, height, w, h):
self.width = width
self.height = height
self.buttonW = w
self.buttonH = h
self.buttons = self.__createButtons()
# Shows the chain of operations, the typed numbers & symbols, and stores previous ones
self.display = Display()
def __createButtons(self) -> list:
buttons = []
x_0 = int((self.width - 4 * self.buttonW) / 2)
y_0 = int(self.height - 5 * self.buttonH) - 2
# --- Number buttons ---
x, y = x_0, y_0
for i in range(len(buttonsList)):
elt = buttonsList[i]
if isinstance(elt, int):
# if elt != 0:
button = Button("num", elt, x, y, self.buttonW, self.buttonH)
else:
# Special characters '(', ')', 'CE', '-', '.', '⌫'
button = Button("special", elt, x, y, self.buttonW, self.buttonH)
x += self.buttonW
# We leave space for the 'basic operator' buttons
if (i + 1) % 3 == 0:
x = x_0
y += self.buttonH
buttons.append(button)
# --- Basic operator buttons ---
x, y = x_0 + self.buttonW * 3, y_0
for op in operators:
altKey = op.altKey if op.altKey else None
button = Button("operator", op.value, x, y, self.buttonW, self.buttonH, op.key, altKey=altKey)
y += self.buttonH
buttons.append(button)
#
return buttons
def checkButtons(self):
for button in self.buttons:
if button.mouseInside():
for but in self.buttons:
but.selected = False
button.selected = True
if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
self.display.addOperation(button)
def checkKeyboard(self):
for button in self.buttons:
self.__checkKey(button, button.key)
if button.altKey is not None:
self.__checkKey(button, button.altKey)
def __checkKey(self, button, key):
if button.keyPressed(key):
if button.value == "CE":
self.display.clear()
elif button.value == "=":
self.display.calc()
elif button.value == "⌫":
self.display.delete()
else:
self.display.addOperation(button)
def draw(self):
self.display.draw()
for button in self.buttons:
button.draw()
def __str__(self):
txt = f"Calculator:\n {self.display}"
for but in self.buttons:
txt += str(but) + ", "
return txt[:-2]