'buttons arent working correctly in pygame, need help figuring out why they dont dynamically change the text

I've created a slots-type game based on a numpy grid but the code below is just a shortened example that demonstrates the problem

The issue I'm running into is with the "cost" feature that I'm trying to add, I have designed two buttons that are supposed to have the function of changing the current cost but I have a few problems that I can't solve;

  1. The amount of winning lines is calculated after the cost button can be clicked making it so increasing cost also increases winnings for the last roll that we already know the result of.

  2. The cost button isn't working properly and clicking either the left or right button just makes a "2" appear on top of the standard "1". It's supposed to increase and decrease the "1" instead.

import sys
import pygame as pg

pg.init()

width = 800
height = 800
lineWidth = 15
winLineWidth = 15
windowName = "Slots"
windowNameInt = 0
cost = 1
costtxt = "Cost: 1"
game_over = False

bgColor = (200, 200, 0)
lineColor = (0, 0, 180)
fontClr = (255,99,71)
triangleColor = (255, 0, 0)
winLineColor = (220, 220, 220)

tri1L = (750, 730)
tri2L = (750, 790)
tri3L = (720, 760)
tri1R = (760, 730)
tri2R = (760, 790)
tri3R = (790, 760)

screen = pg.display.set_mode((width, height))
pg.display.set_caption(windowName)
screen.fill(bgColor)

def drawPointSyst(cost) :

    #(rightest point)(top point)(bottom point)
    pg.draw.polygon(screen, (triangleColor), ((tri3R), (tri1R), (tri2R)))
    #(leftest point)(top point)(bottom point)
    pg.draw.polygon(screen, (triangleColor), ((tri3L), (tri1L), (tri2L)))

    costtxt = "Cost: {}".format(cost)
    myFont = pg.font.SysFont(None, 50)
    textSurface = myFont.render(costtxt, True, (fontClr))
    #(x,y)
    screen.blit(textSurface, (560, 750))

def posCheckLeft(pos) :
    x, y = pos
    return 720 < x < 750 and 730 < y < 790

def posCheckRight(pos) :
    x, y = pos
    return 760 < x < 790 and 730 < y < 790

def game(cost) :
    for event in pg.event.get() :
        if event.type == pg.QUIT :
            sys.exit()

        if event.type == pg.MOUSEBUTTONDOWN:
            pos = pg.mouse.get_pos()
            print(pos)
            if posCheckLeft(pos) :
                print("left")
                cost += 1   
                drawPointSyst(cost)

            elif posCheckRight(pos) :
                print("right")
                cost += 1       
                drawPointSyst(cost)

    pg.display.update()

drawPointSyst(cost)
while True: game(cost)


Solution 1:[1]

I suggest reading Pygame mouse clicking detection. You have to redraw the scene in every frame. Python has no concept of in-out arguments. If you change a value in a function, you must return the new value from the function:

def game(cost):
    for event in pg.event.get() :
        if event.type == pg.QUIT :
            sys.exit()

        if event.type == pg.MOUSEBUTTONDOWN:
            if posCheckLeft(event.pos):
                cost -= 1  
                print(cost) 
            elif posCheckRight(event.pos):
                cost += 1

    screen.fill(bgColor)
    drawPointSyst(cost)
    pg.display.update()
    return cost

while True: cost = game(cost)

Alternatively you can use a variable (see global statement).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1