repl.it
@MATHyou/

Basic Calculator No. 4 (Rest on Sololearn)

Pygame

Ya

fork
loading
Files
  • main.py
  • nohup.out
main.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import pygame
import random
pygame.init()
winheight = 450
winwidth = 380
win = pygame.display.set_mode((winwidth, winheight))
pygame.display.set_caption("Basic Calculator")
FirstNumber = 0
firstnums = True
secondnums = False
SecondNumber = ""
Sign = " "
black = (0,0,0)
white = (255,255,255)
divide = False
dividec = white
dividet = black
multiply = False
multiplyc = white
multiplyt = black
subtract = False
subtractc = white
subtractt = black
addition = False
additionc = white
additiont = black
decimal = False
decimalc = white
decimalt = black
equals = False
equalsc = white
equalst = black
butzero = False
butzeroc = white
butzerot = black
butone = False
butonec = white
butonet = black
buttwo = False
buttwoc = white
buttwot = black
butthree = False
butthreec = white
butthreet = black
butfour = False
butfourc = white
butfourt = black
butfive = False
butfivec = white
butfivet = black
butsix = False
butsixc = white
butsixt = black
butseven = False
butsevenc = white
butsevent = black
buteight = False
buteightc = white
buteightt = black
butnine = False
butninec = white
butninet = black
run = True
def message(msg, color, cords, size):
    fontsize = int(size)
    font = pygame.font.SysFont(None, fontsize)
    screen_text = font.render(msg, True, color)
    win.blit(screen_text, cords)
while run:
    win.fill(black)
    pos = pygame.mouse.get_pos()
    #pygame.time.delay(1)
    pygame.draw.rect(win, white, (25, 10, winwidth-50, 80))
    buttonone = pygame.draw.rect(win, butonec, (25, 100, 75, 75))
    buttontwo = pygame.draw.rect(win, buttwoc, (110, 100, 75, 75))
    buttonthree = pygame.draw.rect(win, butthreec, (195, 100, 75, 75))
    div = pygame.draw.rect(win, dividec, (280, 100, 75, 75))
    buttonfour = pygame.draw.rect(win, butfourc, (25, 185, 75, 75))
    buttonfive = pygame.draw.rect(win, butfivec, (110, 185, 75, 75))
    buttonsix = pygame.draw.rect(win, butsixc, (195, 185, 75, 75))
    mult = pygame.draw.rect(win, multiplyc, (280, 185, 75, 75))
    buttonseven = pygame.draw.rect(win, butsevenc, (25, 270, 75, 75))
    buttoneight = pygame.draw.rect(win, buteightc, (110, 270, 75, 75))
    buttonnine = pygame.draw.rect(win, butninec, (195, 270, 75, 75))
    sub = pygame.draw.rect(win, subtractc, (280, 270, 75, 75))
    buttonzero = pygame.draw.rect(win, butzeroc, (25, 355, 75, 75))
    buttondecimal = pygame.draw.rect(win, decimalc, (110, 355, 75, 75))
    buttonequals = pygame.draw.rect(win, equalsc, (195, 355, 75, 75))
    ad = pygame.draw.rect(win, additionc, (280, 355, 75, 75))
    if buttonone.collidepoint(pos):
        butonec = black
        butonet = white
    else:
        butonec = white
        butonet = black
    if buttontwo.collidepoint(pos):
        buttwoc = black
        buttwot = white
    else:
        buttwoc = white
        buttwot = black
    if buttonthree.collidepoint(pos):
        butthreec = black
        butthreet = white
    else:
        butthreec = white
        butthreet = black
    if div.collidepoint(pos):
        dividec = black
        dividet = white
    else:
        dividec = white
        dividet = black
    if buttonfour.collidepoint(pos):
        butfourc = black
        butfourt = white
    else:
        butfourc = white
        butfourt = black
    if buttonfive.collidepoint(pos):
        butfivec = black
        butfivet = white
    else:
        butfivec = white
        butfivet = black
    if buttonsix.collidepoint(pos):
        butsixc = black
        butsixt = white
    else:
        butsixc = white
        butsixt = black
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            run = False
    pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
    if buttonone.collidepoint(pos) and pressed1:
        if not butone:
            butone = True
            if firstnums:
                FirstNumber *= 10
                FirstNumber += 1
            else:
                SecondNumber *= 10
                SecondNumber += 1
    else:
        butone = False
    if buttontwo.collidepoint(pos) and pressed1:
        if not buttwo:
            buttwo = True
            if firstnums:
                FirstNumber *= 10
                FirstNumber += 2
            else:
                SecondNumber *= 10
                SecondNumber += 2
    else:
        buttwo = False
    if buttonthree.collidepoint(pos) and pressed1:
        if not butthree:
            butthree = True
            if firstnums:
                FirstNumber *= 10
                FirstNumber += 3
            else:
                SecondNumber *= 10
                SecondNumber += 3
    else:
        butthree = False
    if buttonfour.collidepoint(pos) and pressed1:
        if not butfour:
            butfour = True
            if firstnums:
                FirstNumber *= 10
                FirstNumber += 4
            else:
                SecondNumber *= 10
                SecondNumber += 4
    else:
        butfour = False
    if buttonfive.collidepoint(pos) and pressed1:
        if not butfive:
            butfive = True
            if firstnums:
                FirstNumber *= 10
                FirstNumber += 5
            else:
                SecondNumber *= 10
                SecondNumber += 5
    else:
        butfive = False
    if buttonsix.collidepoint(pos) and pressed1:
        if not butsix:
            butsix = True
            if firstnums:
                FirstNumber *= 10
                FirstNumber += 6
            else:
                SecondNumber *= 10
                SecondNumber += 6
    else:
        butsix = False
    message(str(FirstNumber) + str(Sign) + str(SecondNumber), black, [25, 40], 35)
    message("1", butonet, [(25 + 75/2 - 10), (100 + 75/2 - 15)], 50)
    message("2", buttwot, [(110 + 75/2 - 10), (100 + 75/2 - 15)], 50)
    message("3", butthreet, [(195 + 75/2 - 10), (100 + 75/2 - 15)], 50)
    message("÷", dividet, [(280 + 75/2 - 10), (100 + 75/2 - 20)], 50)
    message("4", butfourt, [(25 + 75/2 - 10), (185 + 75/2 - 15)], 50)
    message("5", butfivet, [(110 + 75/2 - 10), (185 + 75/2 - 15)], 50)
    message("6", butsixt, [(195 + 75/2 - 10), (185 + 75/2 - 15)], 50)
    message("×", multiplyt, [(280 + 75/2 - 10), (185 + 75/2 - 20)], 50)
    message("7", butsevent, [(25 + 75/2 - 10), (270 + 75/2 - 15)], 50)
    message("8", buteightt, [(110 + 75/2 - 10), (270 + 75/2 - 15)], 50)
    message("9", butninet, [(195 + 75/2 - 10), (270 + 75/2 - 15)], 50)
    message("-", subtractt, [(280 + 75/2 - 5), (270 + 75/2 - 15)], 50)
    message("0", butzerot, [(25 + 75/2 - 10), (355 + 75/2 - 15)], 50)
    message(".", decimalt, [(110 + 75/2 - 5), (355 + 75/2 - 15)], 50)
    message("=", equalst, [(195 + 75/2 - 10), (355 + 75/2 - 15)], 50)
    message("+", additiont, [(280 + 75/2 - 10), (355 + 75/2 - 15)], 50)
    #pygame.draw.rect(win, (0,0,0), (playerx, playery, playerwidth, playerheight))
    pygame.display.update()
pygame.quit()