python - 单击按钮时如何减少屏幕上的余额数
问题描述
我在屏幕上呈现了值为 100 的文本,当我单击下注按钮时,我希望屏幕上的值减少 10。不知道该怎么做。添加了时钟滴答,因此单击一次时它不会垃圾邮件值更改。我尝试创建一个名为balance_value_change的新变量并将其设置为 0。将 -= 10 替换为balance_value_change = 10然后balance_value -= balance_value_change。我能够让它打印出我想要的东西,但我仍然坚持如何在点击下注按钮时渲染屏幕以刷新新号码。抱歉,解释太长了!
import pygame as pg
pg.init()
screen = pg.display.set_mode((800, 600))
background = pg.image.load('images/sideBar.jpg')
bj_icon = pg.image.load('images/blackjack_aces.png')
clock = pg.time.Clock()
#Sidebar fonts/buttons
font = pg.font.Font('freesansbold.ttf', 18)
blackjack = font.render('BlackJack', True, (255,255,255))
balance_value = 100
balance_value_change = 0
money = font.render('Balance: ' + str(balance_value), True, (255,255,255))
#Button Class
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
def draw(self, screen, outline=None):
if outline:
pg.draw.rect(screen, outline, (self.x-2, self.y-2, self.width+4,
self.height+4), 0)
pg.draw.rect(screen, self.color, (self.x, self.y, self.width,
self.height), 0)
if self.text != '':
font = pg.font.Font('freesansbold.ttf', 18)
text = font.render(self.text, 1, (190,0,0))
screen.blit(text, (self.x + (self.width/2 -text.get_width()/2),
self.y + (self.height/2 - text.get_height()/2)))
def isOver(self, pos):
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 redrawWindow():
betButton.draw(screen, (0,0,0))
hitButton.draw(screen, (0,0,0))
stayButton.draw(screen, (0,0,0))
betButton = button((255,0,0), 625,300,120,25, 'B E T' )
hitButton = button((255,0,0), 625,350,120,25, 'H I T' )
stayButton = button((255,0,0), 625,400,120,25, 'S T A Y' )
running = True
while running:
screen.fill((0,100,0))
screen.blit(background, (550,50))
screen.blit(bj_icon, (625, 50))
for event in pg.event.get():
pos = pg.mouse.get_pos()
if event.type == pg.QUIT:
running = False
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
if betButton.isOver(pos):
balance_value -= 10
#Mouse Button Hover
if event.type == pg.MOUSEMOTION:
if betButton.isOver(pos):
betButton.color = (125,125,125)
else:
betButton.color = (255,255,255)
if hitButton.isOver(pos):
hitButton.color = (125,125,125)
else:
hitButton.color = (255,255,255)
if stayButton.isOver(pos):
stayButton.color = (125,125,125)
else:
stayButton.color = (255,255,255)
redrawWindow()
screen.blit(money, (625,270))
screen.blit(blackjack, (640,180))
clock.tick(15)
pg.display.update()
解决方案
减少值是不够的。必须呈现新文本:
if betButton.isOver(pos):
balance_value -= 10
money = font.render('Balance: ' + str(balance_value), True, (255,255,255))
此外,它是Indentation的问题。您必须MOUSEBUTTONDOWN
在事件循环中而不是在应用程序循环中处理事件:
running = True
while running:
# [...]
for event in pg.event.get():
pos = pg.mouse.get_pos()
if event.type == pg.QUIT:
running = False
#-->| INDENTATION
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
if betButton.isOver(pos):
balance_value -= 10
money = font.render('Balance: ' + str(balance_value), True, (255,255,255))
if event.type == pg.MOUSEMOTION:
if betButton.isOver(pos):
betButton.color = (125,125,125)
else:
betButton.color = (255,255,255)
if hitButton.isOver(pos):
hitButton.color = (125,125,125)
else:
hitButton.color = (255,255,255)
if stayButton.isOver(pos):
stayButton.color = (125,125,125)
else:
stayButton.color = (255,255,255)
推荐阅读
- docker - docker-compose up vs docker-compose up service1 service2
- c# - 数据解密
- laravel - 未定义的变量:Laravel 5.6 中的错误
- go - 使用 CGO 动态获取正确的操作系统和处理器架构
- c# - 一键调用 c# 和 javascript asp.net mvc
- c# - C# 应用程序。用于向 LABVIEW 应用程序发送可视键“Enter”
- docker - 组织想要登上 Docker 列车。这值得么?
- spring-boot - Junit - 测试使用 Foreach 的方法
- lua - 如何按值对表进行排序,然后按顺序打印索引
- dji-sdk - mobile-sdk 和 onboard-sdk 使用时都需要联网吗?