按钮在pygame中正确工作,需要帮​​助弄清楚为什么他们不动态更改文本

发布于 2025-01-28 11:52:30 字数 2039 浏览 2 评论 0原文

我已经基于Numpy网格创建了一个插槽类型游戏,但是下面的代码只是一个缩短的示例,证明了

我遇到的问题的问题是我要添加的“成本”功能,我已经设计了两个按钮,这些按钮应该具有改变当前成本的功能,但是我有一些无法解决的问题。

  1. 可以单击成本按钮后计算获奖线的数量,因此成本增加也增加了我们已经知道的结果。

    的结果。

  2. “成本”按钮无法正常工作,单击左或右键的按钮只会使“ 2”出现在标准“ 1”的顶部。它应该增加并减少“ 1”。

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2025-02-04 11:52:30

我建议阅读 pygame鼠标鼠标单击检测。您必须在每个帧中重新绘制场景。 python没有关于论点的概念。如果您更改函数中的值,则必须从函数返回新值:

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)

或者您可以使用变量(请参阅 global语句)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文