【问题标题】:Creating and reference multiple Pygame board elements (buttons)创建和引用多个 Pygame 板元素(按钮)
【发布时间】:2018-04-06 11:03:35
【问题描述】:

我是 pygame 新手,这是我在 stackoverflow 上的第一篇文章(你好!)。我希望您能够帮助我解决我在为简单的益智游戏创建棋盘时遇到的问题。该板由一个 3x3 的按钮网格组成,按下时会更改网格中其他按钮的颜色。我遇到的问题是如何最好地引用每个按钮,以便可以识别鼠标点击,并且我可以根据这次点击控制其他按钮的颜色变化。

在没有代码的情况下,我可以提供以下信息,希望可以让我的问题更加清晰(如果有帮助,我可以尝试整理并提供我目前编写的测试代码)。

我已经导入了一个按钮图像,并通过循环 9 次 blitting 创建了一个网格,并引用了一个坐标列表:

For i in range(9):
  gamedisp.blit(button,coordList[i])

所以我已经有了网格,但我如何指定哪个按钮是哪个?我的第一个想法是将它们分配给变量 button0 – button8 但是在这里和其他地方的简短搜索清楚地表明这不是一个好习惯。那么有什么选择呢?如何以可以执行以下操作的方式存储和引用信息?

例如左上角的按钮已被单击,因此更改了中间行中所有按钮的颜色。我很确定我可以自己处理颜色变化,它只是我正在努力解决的对象引用。

提前感谢您的帮助。如果我可以提供更多详细信息,请告诉我。

【问题讨论】:

  • 欢迎来到 Stack Overflow!请将您的代码添加到问题中(minimal, complete example)。您是否已经有过类/面向对象编程的经验?
  • 嗨。谢谢您的答复。稍后我将在可用时添加代码。我目前正在学习课程和 OOP,经过一番疯狂的搜索后,我想我自己已经接近解决这个问题了。我想我需要创建一个包含 x、y、图像等的 Button 类。我可以启动这 9 次来填充列表,buttons[]。一旦有了这个,我就可以根据每个元素的索引来引用它,并使用与 get_rect() 方法的冲突来识别点击。我变热了吗?细节仍然有点不完整,但我稍后会发布代码(或解决方案)。再次感谢。
  • 是的,听起来你走在正确的轨道上。

标签: button pygame


【解决方案1】:

我设法找到了解决我自己问题的方法,并包含了我的代码,希望它可以在未来帮助其他人。我创建了一个 Button 类并循环了 9 次以创建网格。该类有一个方法'controls',它是一个按钮索引列表,可以通过按下按钮来更改。我可以使用按钮[i].controls 访问这些按钮。

如原帖中所述,我是 python 和 pygame 新手,所以如果我的代码中有任何错误或效率低下,请随时发布改进。在我的学习过程中,我一直在使用 Michael Dawson 的 Python Programming for the Absolute Beginner (Third Edition) 和 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.

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    相关资源
    最近更新 更多