按钮在pygame中正确工作,需要帮助弄清楚为什么他们不动态更改文本
我已经基于Numpy网格创建了一个插槽类型游戏,但是下面的代码只是一个缩短的示例,证明了
我遇到的问题的问题是我要添加的“成本”功能,我已经设计了两个按钮,这些按钮应该具有改变当前成本的功能,但是我有一些无法解决的问题。
可以单击成本按钮后计算获奖线的数量,因此成本增加也增加了我们已经知道的结果。
的结果。“成本”按钮无法正常工作,单击左或右键的按钮只会使“ 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;
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议阅读 pygame鼠标鼠标单击检测。您必须在每个帧中重新绘制场景。 python没有关于论点的概念。如果您更改函数中的值,则必须从函数返回新值:
或者您可以使用变量(请参阅
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:
Alternatively you can use a variable (see
global
statement).