как уменьшить номер баланса на моем экране при нажатии кнопки - PullRequest
1 голос
/ 15 апреля 2020

У меня отображается текст со значением 100, я хочу, чтобы значение на экране уменьшилось на 10 при нажатии кнопки ставки. Не уверен, как go об этом. Добавлен тик часов, чтобы спам не менял значение при щелчке один раз. Я попытался создать новую переменную с именем balance_value_change и установить ее равной 0. Заменить - = 10 на balance_value_change = 10 , а затем balance_value - = balance_value_change . Мне удалось заставить его распечатать то, что я хотел, но я все еще застрял в том, как сделать экран, чтобы обновить sh новый номер при нажатии кнопки ставки. Извините за длинное объяснение!

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