gpt4 book ai didi

python - Pygame 基本计算器

转载 作者:行者123 更新时间:2023-12-03 13:59:34 24 4
gpt4 key购买 nike

我正在尝试制作一个基本的计算器,但我遇到的问题是如何输出文本?我如何做到这一点,当我点击加它允许我添加或者如果我点击分割它允许我划分并在我的屏幕上的黄色部分显示输出
enter image description here
这就是我现在所拥有的。你可以运行它;没有什么特别的。 我的问题是我怎样才能让计算器做到这样,当我点击加号然后去添加数字时,它允许我添加或当我点击除法时,它允许我划分我的数字并在屏幕上显示输出?

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()

最佳答案

您可以添加 =按钮,所以每次用户点击它时,用 python eval() 计算用户输入功能。
至于用户输入,首先需要全局记录。然后你可以将用户输入传递给 inputtap = button((253,100,32),10,280,450,50,"") 的字符串字段将其显示在窗口上。

import pygame, math

pygame.init()

window_height = 500
window_width = 600
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, '÷')
d_5s = button((0,255,0),200,350,30,30, '=')
d_6s = button((0,255,0),260,500,30,30, 'C')

symbols = [d_1s,d_2s,d_3s,d_4s,d_5s,d_6s]


# redraw window
def redraw(inputtap):
# 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():
global user_input
global python_input
global is_finished

if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()

try:
if is_finished or user_input[-1] in ["+", "-", "x", "÷", "="]:
# User shouldn't type two symbols continuously
# User shouldn't input any symbols when game finished because there is no number
return
except IndexError:
# User shouldn't input any symbols if there is no number
return


if d_1s.isOver(pos):
print("+")
user_input += "+"
python_input += "+"

if d_2s.isOver(pos):
print("-")
user_input += "-"
python_input += "-"

if d_3s.isOver(pos):
print("x")
user_input += "x"
python_input += "*"

if d_4s.isOver(pos):
print("÷")
user_input += "÷"
python_input += "/"

if d_5s.isOver(pos):
print("=")
result = eval(python_input)
python_input = ""
user_input += f"={result:.2f}"
is_finished = True

if d_6s.isOver(pos):
print("C")
python_input = ""
user_input = ""

def MOUSEOVERnumbers():
global user_input
global python_input
global is_finished

if event.type == pygame.MOUSEBUTTONDOWN:
if is_finished:
user_input = ""
python_input = ""
is_finished = False
pos = pygame.mouse.get_pos()
if s_1s.isOver(pos):
print("1")
user_input += "1"
python_input += "1"
if s_2s.isOver(pos):
print("2")
user_input += "2"
python_input += "2"
if s_3s.isOver(pos):
print("3")
user_input += "3"
python_input += "3"
if s_4s.isOver(pos):
print("4")
user_input += "4"
python_input += "4"
if s_5s.isOver(pos):
print("5")
user_input += "5"
python_input += "5"
if s_6s.isOver(pos):
print("6")
user_input += "6"
python_input += "6"
if s_7s.isOver(pos):
print("7")
user_input += "7"
python_input += "7"
if s_8s.isOver(pos):
print("8")
user_input += "8"
python_input += "8"
if s_9s.isOver(pos):
print("9")
user_input += "9"
python_input += "9"
if s_0s.isOver(pos):
print("0")
user_input += "0"
python_input += "0"

# the main loop
run = True
user_input = ""
python_input = ""
is_finished = True

while run:
# input tap
inputtap = button((253,100,32),10,280,450,50,f"{user_input}")

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

MOUSEOVERnumbers()

Symbols()

redraw(inputtap)
pygame.display.update()

pygame.quit()
然后您可以添加 reset按钮以重置用户输入。同样在用户点击后 =按钮,开始一个新的用户输入,而不是连接旧的。 reset按钮标有 C在这个例子中。每次用户点击它时,清空用户输入字符串和 python 输入字符串。
我也使用全局 is_finished用于检查用户是否点击的 bool 变量 =按钮。如果用户单击它,则表示用户已完成计算,以便下次用户单击任何符号按钮时,用户输入的字符串将被清除。
同时,用户不应输入除 C 之外的两个符号。按钮的同时。我通过比较最后一个字符用户输入和当前字符用户输入来判断它。
此外,用户在输入任何数字之前不应输入任何符号。我用全局变量 is_finished来判断.如 is_finished是真的,这意味着用户没有开始输入,所以用户输入字符串中没有值。我也用了 IndexError以防万一,因为空的用户输入字符串不能使用负索引。
为了区分整数和浮点结果,可以判断结果中是否有点:
>>> '.' in '45.3'
True
>>> '.' in '453'
False
最后,您还可以简化那些 if逻辑与 button.text属性如什么 Rabbid76做:
        for number_button in numbers:
if number_button.isOver(pos):
print(number_button.text)
user_input += number_button.text
python_input += number_button.text

关于python - Pygame 基本计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63200489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com