I am trying to make a basic calculator but the problem I am having is how do I output the text? How do I make it so when I click plus it allows me to add or if I click divide it allows me to divide and shows the output on the yellow part on my screen
This is what I have right now. You could run it; there is nothing special. My question is how could I make the calculator make it so when I click plus and then go and add up numbers it allows me to add or when I click divide it allows me to divide my numbers and show the outputs on the screen?
import pygame,math
pygame.init()
window_height = 500
window_width = 500
window = pygame.display.set_mode((window_height,window_width))
# the buttons for the shop MENU
class button():
def __init__(self, color, x,y,width,height, text=''):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.over = False
def draw(self,window,outline=None):
#Call this method to draw the button on the screen
if outline:
pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
if self.text != '':
font = pygame.font.SysFont('comicsans', 60)
text = font.render(self.text, 1, (0,0,0))
window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
#Pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
def playSoundIfMouseIsOver(self, pos, sound):
if self.isOver(pos):
if not self.over:
beepsound.play()
self.over = True
else:
self.over = False
white = (255,255,255)
# the numbers for the calcaltor
s_1s = button((0,255,0),40,450,30,30, '1')
s_2s = button((0,255,0),40,400,30,30, '2')
s_3s = button((0,255,0),40,350,30,30, '3')
s_4s = button((0,255,0),100,450,30,30, '4')
s_5s = button((0,255,0),100,400,30,30, '5')
s_6s = button((0,255,0),100,350,30,30, '6')
s_7s = button((0,255,0),150,450,30,30, '7')
s_8s = button((0,255,0),150,400,30,30, '8')
s_9s = button((0,255,0),150,350,30,30, '9')
s_0s = button((0,255,0),200,450,30,30, '0')
numbers = [s_1s,s_2s,s_3s,s_4s,s_5s,s_6s,s_7s,s_8s,s_9s,s_0s]
# the symbols!
d_1s = button((0,255,0),260,450,30,30, '+')
d_2s = button((0,255,0),260,400,30,30, '-')
d_3s = button((0,255,0),260,350,30,30, 'x')
d_4s = button((0,255,0),200,400,30,30, '÷')
symbols = [d_1s,d_2s,d_3s,d_4s]
# input tap
inputtap = button((253,100,32),10,280,450,50,"")
# redraw window
def redraw():
# draw all the numbers
for button in numbers:
button.draw(window)
# the symbols
for button in symbols:
button.draw(window)
inputtap.draw(window)
def Symbols():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if d_1s.isOver(pos):
print("+")
if d_2s.isOver(pos):
print("-")
if d_3s.isOver(pos):
print("x")
if d_4s.isOver(pos):
print("÷")
def MOUSEOVERnumbers():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if s_1s.isOver(pos):
print("1")
if s_2s.isOver(pos):
print("2")
if s_3s.isOver(pos):
print("3")
if s_4s.isOver(pos):
print("4")
if s_5s.isOver(pos):
print("5")
if s_6s.isOver(pos):
print("6")
if s_7s.isOver(pos):
print("7")
if s_8s.isOver(pos):
print("8")
if s_9s.isOver(pos):
print("9")
if s_0s.isOver(pos):
print("0")
# the main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
MOUSEOVERnumbers()
Symbols()
redraw()
pygame.display.update()
pygmae.quit()

