Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Button 创建和引用多个Pygame板元素(按钮)_Button_Pygame - Fatal编程技术网

Button 创建和引用多个Pygame板元素(按钮)

Button 创建和引用多个Pygame板元素(按钮),button,pygame,Button,Pygame,我是pygame新手,这是我第一篇关于stackoverflow的帖子(你好!)。我希望你能帮我解决一个问题,我正在为一个简单的益智游戏制作一个棋盘。该板由一个3x3的按钮网格组成,按下该网格时,可改变网格中其他按钮的颜色。我遇到的问题是如何最好地引用每个按钮,以便可以识别鼠标单击,并且我可以基于此单击控制其他按钮的颜色变化 在没有代码的情况下,我可以提供以下信息,我希望这些信息能够澄清我的问题(如果有帮助的话,我可以尝试整理并提供我目前编写的测试代码) 我导入了一个按钮图像,并通过在一个循环中

我是pygame新手,这是我第一篇关于stackoverflow的帖子(你好!)。我希望你能帮我解决一个问题,我正在为一个简单的益智游戏制作一个棋盘。该板由一个3x3的按钮网格组成,按下该网格时,可改变网格中其他按钮的颜色。我遇到的问题是如何最好地引用每个按钮,以便可以识别鼠标单击,并且我可以基于此单击控制其他按钮的颜色变化

在没有代码的情况下,我可以提供以下信息,我希望这些信息能够澄清我的问题(如果有帮助的话,我可以尝试整理并提供我目前编写的测试代码)

我导入了一个按钮图像,并通过在一个循环中对其进行9次闪电扫描,参考坐标列表,成功创建了一个网格:

For i in range(9):
  gamedisp.blit(button,coordList[i])
所以我有网格,但是我如何指定哪个按钮是哪个?我的第一个想法是将它们分配给变量button0–button8,但这里和其他地方的简短搜索表明,这不是一个好的做法。那么,还有什么选择呢?如何存储和引用信息,以便执行以下操作

例如,左上角按钮已被点击,因此改变中间行中所有按钮的颜色。我很确定我能自己处理颜色的变化,这正是我正在努力解决的对象引用问题


提前感谢你的帮助。请告诉我是否可以提供进一步的详细信息。

我已经设法找到了解决我自己问题的方法,并且包含了我的代码,希望它能在将来帮助其他人。我创建了一个Button类,并在其中循环了9次以创建网格。该类有一个方法“controls”,它是一个按钮索引列表,可通过按下按钮进行更改。我可以使用按钮[I]控件访问这些按钮

正如在最初的帖子中提到的,我是python和pygame的新手,因此如果我的代码中有任何错误或低效,请随时发布改进。我一直在使用迈克尔·道森(Michael Dawson)的《绝对初学者Python编程》(第三版)和pythonprogramming.net以及我学习的其他资料

##import and initiate pygame
import pygame as pg

pg.init()

##Set display dimensions and the button size as a function of these dimensions.
display_width = 400
display_height = 600
buttonSize = (display_width/4)

##Create game display surface.
gameDisplay = pg.display.set_mode((display_width,display_height))

white = (255,255,255)

##posList holds nine pairs of X,Y multipliers. These are multiplied by buttonSize to draw the grid. 
posList = ((0.25,0.25), (0.25,1.5), (0.25,2.75), (1.5,0.25), (1.5,1.5), (1.5,2.75), (2.75,0.25), (2.75,1.5), (2.75,2.75))

##board is a list of nine lists. Each nested list holds the butttons that switched by each button press.
board = ((0,1,3),(1,0,2,4),(2,1,5),(3,4),(4,1,7),(5,4),(6,3,7),(7,4,6,8),(8,5,7))

##I import the button image
redSquare = pg.image.load('Button Image.png').convert()
redSquare = pg.transform.scale(redSquare, (int(buttonSize), int(buttonSize)))

##The Button class holds coordinate, image and controls attributes
class Button:
    def __init__(self, x, y, image, controls, green = False):
        self.x = x
        self.y = y
        self.image = image
        self.isgreen = green
        self.controls = controls

##It has a blit method.
    def buttonBlit(self):
        gameDisplay.blit(self.image, (self.x, self.y))

##I create an empty list called buttons.
buttons = []

##I populate buttons by looping through the Button class 9 times.
for i in range(9):   
    buttons.append(Button(posList[i][0] * buttonSize, posList[i][1] * buttonSize, redSquare, board[i]))

##The blit method draws the buttons.
gameDisplay.fill(white)
for button in buttons:
    Button.buttonBlit(button)

pg.display.update()

##I can reference each button by using the index in the buttons list. Furthermore, I can use buttons[i].controls to perform the actions of a button press.

欢迎来到堆栈溢出!请在问题(a)中添加您的代码。你已经有过类/面向对象编程的经验吗?嗨。谢谢你的回复。我将在以后有代码可用时添加代码。目前我正在学习类和面向对象编程,经过疯狂的搜索,我想我自己也快要解决这个问题了。我想我需要创建一个Button类,包括x、y、image等。我可以启动这9次来填充一个列表buttons[]。一旦有了它,我就可以根据每个元素的索引来引用它,并通过与get_rect()方法的冲突来识别单击。我暖和了吗?细节仍然有点不完整,但我稍后会发布代码(或解决方案)。再次感谢。是的,听起来你走对了。