按钮在 pygame 中无法正常工作,需要帮助弄清楚为什么它们不能动态更改文本
buttons arent working correctly in pygame, need help figuring out why they dont dynamically change the text
我已经创建了一个基于 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)
我建议阅读 Pygame mouse clicking detection。您必须在每一帧中重新绘制场景。 Python 没有 in-out 参数的概念。如果您更改函数中的值,则必须 return 来自函数的新值:
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
statement)。
我已经创建了一个基于 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)
我建议阅读 Pygame mouse clicking detection。您必须在每一帧中重新绘制场景。 Python 没有 in-out 参数的概念。如果您更改函数中的值,则必须 return 来自函数的新值:
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
statement)。