Taschenrechner Pygame
Verfasst: Donnerstag 14. April 2022, 16:39
Hallo,
ich versuche gerade ganz einfach und unkompliziert mit Pygame einen Taschenrechner zu programmieren. Mein Problem ist in der rechner() Methode. Wenn z.b nur einmal ein Button geklickt wird, so werden Strings mehrfach und hintereinander hinzugefügt. Kann man das irgendwie umgehen, dass durch einen Klick auch nur ein String hinzugefügt wird?
Und sry ich weiß gerade nicht wie ich meinen Code Übersichtlich im forum darstellen kann. Kann mir das vielleicht wer erklären?
Danke im VORAUS.
Hier mein Code:
import pygame
pygame.init()
screen = pygame.display.set_mode((580,320))
Zahl = [[50,150],[150,150],[250,150],[50,200],[150,200],[250,200],[50,250],[150,250],[250,250]]
Farbe1, Farbe2, Farbe3, Farbe4, Farbe5, Farbe6, Farbe7, Farbe8, Farbe9, Farbe10, Farbe11, Farbe12, Farbe13, Farbe14, Farbe15 = (240,248,245),(240,248,245),(240,248,245),(240,248,245),(240,248,245),(240,248,245),(240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245)
Feld_leiste = ""
click = True
def rechner():
global Farbe1, Farbe2, Farbe3, Farbe4, Farbe5, Farbe6, Farbe7, Farbe8, Farbe9, Farbe10, Farbe11, Farbe12, Farbe13, Farbe14, Farbe15, Feld_leiste, leer, click, a
#
pos = pygame.mouse.get_pos()
Feld = pygame.draw.rect(screen, (240, 248, 245), (50, 50, 480, 80), 5)
zahlen1 = pygame.draw.rect(screen, (Farbe1), (50, 150, 80, 35))
zahlen2 = pygame.draw.rect(screen, (Farbe2), (150, 150, 80, 35))
zahlen3 = pygame.draw.rect(screen, (Farbe3), (250, 150, 80, 35))
zahlen4 = pygame.draw.rect(screen, (Farbe4), (50, 200, 80, 35))
zahlen5 = pygame.draw.rect(screen, (Farbe5), (150, 200, 80, 35))
zahlen6 = pygame.draw.rect(screen, (Farbe6), (250, 200, 80, 35))
zahlen7 = pygame.draw.rect(screen, (Farbe7), (50, 250, 80, 35))
zahlen8 = pygame.draw.rect(screen, (Farbe8), (150, 250, 80, 35))
zahlen9 = pygame.draw.rect(screen, (Farbe9), (250, 250, 80, 35))
Mal = pygame.draw.rect(screen, (Farbe10), (350, 150, 80, 35))
geteilt = pygame.draw.rect(screen, (Farbe11), (350, 200, 80, 35))
Plus = pygame.draw.rect(screen, (Farbe12), (350, 250, 80, 35))
Minus = pygame.draw.rect(screen, (Farbe13), (450, 150, 80, 35))
Gleich = pygame.draw.rect(screen, (Farbe14), (450, 200, 80, 35))
Löschen = pygame.draw.rect(screen, (Farbe15), (450, 250, 80, 35))
#
if event.type == pygame.MOUSEBUTTONDOWN:
if click == True:
if pygame.mouse.get_pressed()[0] and zahlen1.collidepoint(pos) == True:
Farbe1 = (176, 196, 222)
Feld_leiste += "1"
if pygame.mouse.get_pressed()[0] and zahlen2.collidepoint(pos) == True:
Farbe2 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen3.collidepoint(pos) == True:
Farbe3 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen4.collidepoint(pos) == True:
Farbe4 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen5.collidepoint(pos) == True:
Farbe5 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen6.collidepoint(pos) == True:
Farbe6 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen7.collidepoint(pos) == True:
Farbe7 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen8.collidepoint(pos) == True:
Farbe8 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen9.collidepoint(pos) == True:
Farbe9 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Mal.collidepoint(pos) == True:
Farbe10 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and geteilt.collidepoint(pos) == True:
Farbe11 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Plus.collidepoint(pos) == True:
Farbe12 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Minus.collidepoint(pos) == True:
Farbe13 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Gleich.collidepoint(pos) == True:
Farbe14 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Löschen.collidepoint(pos) == True:
Farbe15 = (176, 196, 222)
else:
Farbe1 = (240, 248, 245)
Farbe2 = (240, 248, 245)
Farbe3 = (240, 248, 245)
Farbe4 = (240, 248, 245)
Farbe5 = (240, 248, 245)
Farbe6 = (240, 248, 245)
Farbe7 = (240, 248, 245)
Farbe8 = (240, 248, 245)
Farbe9 = (240, 248, 245)
Farbe10 = (240, 248, 245)
Farbe11 = (240, 248, 245)
Farbe12 = (240, 248, 245)
Farbe13 = (240, 248, 245)
Farbe14 = (240, 248, 245)
Farbe15 = (240, 248, 245)
font = pygame.font.SysFont("OCR A Extended", 90)
img = font.render(Feld_leiste, True, pygame.Color(0, 0, 0))
screen.blit(img, (60, 43))
def text():
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("1", True, pygame.Color(0, 0, 0))
screen.blit(img, (82, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("2", True, pygame.Color(0, 0, 0))
screen.blit(img, (182, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("3", True, pygame.Color(0, 0, 0))
screen.blit(img, (282, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("4", True, pygame.Color(0, 0, 0))
screen.blit(img, (82, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("5", True, pygame.Color(0, 0, 0))
screen.blit(img, (182, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("6", True, pygame.Color(0, 0, 0))
screen.blit(img, (282, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("7", True, pygame.Color(0, 0, 0))
screen.blit(img, (82, 255))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("8", True, pygame.Color(0, 0, 0))
screen.blit(img, (182, 255))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("9", True, pygame.Color(0, 0, 0))
screen.blit(img, (282, 255))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("*", True, pygame.Color(0, 0, 0))
screen.blit(img, (382, 160))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("/", True, pygame.Color(0, 0, 0))
screen.blit(img, (383, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("+", True, pygame.Color(0, 0, 0))
screen.blit(img, (482, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("-", True, pygame.Color(0, 0, 0))
screen.blit(img, (485, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("=", True, pygame.Color(0, 0, 0))
screen.blit(img, (382, 252))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("C", True, pygame.Color(0, 0, 0))
screen.blit(img, (482, 255))
while True:
screen.fill((100,149,237))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
rechner()
text()
pygame.display.flip()
ich versuche gerade ganz einfach und unkompliziert mit Pygame einen Taschenrechner zu programmieren. Mein Problem ist in der rechner() Methode. Wenn z.b nur einmal ein Button geklickt wird, so werden Strings mehrfach und hintereinander hinzugefügt. Kann man das irgendwie umgehen, dass durch einen Klick auch nur ein String hinzugefügt wird?
Und sry ich weiß gerade nicht wie ich meinen Code Übersichtlich im forum darstellen kann. Kann mir das vielleicht wer erklären?
Danke im VORAUS.
Hier mein Code:
import pygame
pygame.init()
screen = pygame.display.set_mode((580,320))
Zahl = [[50,150],[150,150],[250,150],[50,200],[150,200],[250,200],[50,250],[150,250],[250,250]]
Farbe1, Farbe2, Farbe3, Farbe4, Farbe5, Farbe6, Farbe7, Farbe8, Farbe9, Farbe10, Farbe11, Farbe12, Farbe13, Farbe14, Farbe15 = (240,248,245),(240,248,245),(240,248,245),(240,248,245),(240,248,245),(240,248,245),(240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245), (240,248,245)
Feld_leiste = ""
click = True
def rechner():
global Farbe1, Farbe2, Farbe3, Farbe4, Farbe5, Farbe6, Farbe7, Farbe8, Farbe9, Farbe10, Farbe11, Farbe12, Farbe13, Farbe14, Farbe15, Feld_leiste, leer, click, a
#
pos = pygame.mouse.get_pos()
Feld = pygame.draw.rect(screen, (240, 248, 245), (50, 50, 480, 80), 5)
zahlen1 = pygame.draw.rect(screen, (Farbe1), (50, 150, 80, 35))
zahlen2 = pygame.draw.rect(screen, (Farbe2), (150, 150, 80, 35))
zahlen3 = pygame.draw.rect(screen, (Farbe3), (250, 150, 80, 35))
zahlen4 = pygame.draw.rect(screen, (Farbe4), (50, 200, 80, 35))
zahlen5 = pygame.draw.rect(screen, (Farbe5), (150, 200, 80, 35))
zahlen6 = pygame.draw.rect(screen, (Farbe6), (250, 200, 80, 35))
zahlen7 = pygame.draw.rect(screen, (Farbe7), (50, 250, 80, 35))
zahlen8 = pygame.draw.rect(screen, (Farbe8), (150, 250, 80, 35))
zahlen9 = pygame.draw.rect(screen, (Farbe9), (250, 250, 80, 35))
Mal = pygame.draw.rect(screen, (Farbe10), (350, 150, 80, 35))
geteilt = pygame.draw.rect(screen, (Farbe11), (350, 200, 80, 35))
Plus = pygame.draw.rect(screen, (Farbe12), (350, 250, 80, 35))
Minus = pygame.draw.rect(screen, (Farbe13), (450, 150, 80, 35))
Gleich = pygame.draw.rect(screen, (Farbe14), (450, 200, 80, 35))
Löschen = pygame.draw.rect(screen, (Farbe15), (450, 250, 80, 35))
#
if event.type == pygame.MOUSEBUTTONDOWN:
if click == True:
if pygame.mouse.get_pressed()[0] and zahlen1.collidepoint(pos) == True:
Farbe1 = (176, 196, 222)
Feld_leiste += "1"
if pygame.mouse.get_pressed()[0] and zahlen2.collidepoint(pos) == True:
Farbe2 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen3.collidepoint(pos) == True:
Farbe3 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen4.collidepoint(pos) == True:
Farbe4 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen5.collidepoint(pos) == True:
Farbe5 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen6.collidepoint(pos) == True:
Farbe6 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen7.collidepoint(pos) == True:
Farbe7 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen8.collidepoint(pos) == True:
Farbe8 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and zahlen9.collidepoint(pos) == True:
Farbe9 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Mal.collidepoint(pos) == True:
Farbe10 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and geteilt.collidepoint(pos) == True:
Farbe11 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Plus.collidepoint(pos) == True:
Farbe12 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Minus.collidepoint(pos) == True:
Farbe13 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Gleich.collidepoint(pos) == True:
Farbe14 = (176, 196, 222)
if pygame.mouse.get_pressed()[0] and Löschen.collidepoint(pos) == True:
Farbe15 = (176, 196, 222)
else:
Farbe1 = (240, 248, 245)
Farbe2 = (240, 248, 245)
Farbe3 = (240, 248, 245)
Farbe4 = (240, 248, 245)
Farbe5 = (240, 248, 245)
Farbe6 = (240, 248, 245)
Farbe7 = (240, 248, 245)
Farbe8 = (240, 248, 245)
Farbe9 = (240, 248, 245)
Farbe10 = (240, 248, 245)
Farbe11 = (240, 248, 245)
Farbe12 = (240, 248, 245)
Farbe13 = (240, 248, 245)
Farbe14 = (240, 248, 245)
Farbe15 = (240, 248, 245)
font = pygame.font.SysFont("OCR A Extended", 90)
img = font.render(Feld_leiste, True, pygame.Color(0, 0, 0))
screen.blit(img, (60, 43))
def text():
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("1", True, pygame.Color(0, 0, 0))
screen.blit(img, (82, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("2", True, pygame.Color(0, 0, 0))
screen.blit(img, (182, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("3", True, pygame.Color(0, 0, 0))
screen.blit(img, (282, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("4", True, pygame.Color(0, 0, 0))
screen.blit(img, (82, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("5", True, pygame.Color(0, 0, 0))
screen.blit(img, (182, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("6", True, pygame.Color(0, 0, 0))
screen.blit(img, (282, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("7", True, pygame.Color(0, 0, 0))
screen.blit(img, (82, 255))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("8", True, pygame.Color(0, 0, 0))
screen.blit(img, (182, 255))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("9", True, pygame.Color(0, 0, 0))
screen.blit(img, (282, 255))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("*", True, pygame.Color(0, 0, 0))
screen.blit(img, (382, 160))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("/", True, pygame.Color(0, 0, 0))
screen.blit(img, (383, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("+", True, pygame.Color(0, 0, 0))
screen.blit(img, (482, 205))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("-", True, pygame.Color(0, 0, 0))
screen.blit(img, (485, 155))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("=", True, pygame.Color(0, 0, 0))
screen.blit(img, (382, 252))
font = pygame.font.SysFont("Arial Rounded MT Bold", 40)
img = font.render("C", True, pygame.Color(0, 0, 0))
screen.blit(img, (482, 255))
while True:
screen.fill((100,149,237))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
rechner()
text()
pygame.display.flip()