Python / Pygame - 用类让正方形改变颜色

3
我正在为一款战舰游戏制作一个小项目,但是我在让代码理解当我点击对象时要改变颜色方面遇到了一些问题。
到目前为止,我有这个类:
class Tile:
def __init__(self, xstart, ystart, game_display, colour):
    self.tile = pygame.Rect(xstart, ystart, 50, 50)
    pygame.draw.rect(game_display, colour,  self.tile, 1)
    print(self.tile)

def ChangeColour(self, game_display, colour):
    pygame.draw.rect(game_display, colour,  self.tile, 0)

我使用我编写的这个函数创建了这个板子:
def new_board():
global board
xstart = 70
ystart = 50
#pygame.draw.rect(game_display, BLACK, (70, 50, 500, 500), 3)
for square in range(PLAYER_NUMBER_SQUARES):
    board.append(Tile(xstart, ystart, game_display, BLACK))
    xstart += 50
    if xstart == 570:
        ystart += 50
        xstart = 70
return board

到目前为止,我已经将正方形/矩形对象添加到列表“board”中。当玩家点击时,我获取像素并使用此方法来确定被点击的是哪个正方形:
def get_x_number(x):
index = None
if x >= 70 and x < 120:
    index = 70
if x >= 120 and x < 170:
    index = 120
if x >= 170 and x < 220:
    index = 170
if x >= 220 and x < 270:
    index = 220
if x >= 270 and x < 320:
    index = 270
if x >= 320 and x < 370:
    index = 320
if x >= 370 and x < 420:
    index = 370
if x >= 420 and x < 470:
    index = 420
if x >= 470 and x < 520:
    index = 470
return index

def get_y_number(y):
index = None
if y >= 50 and y < 100:
    index = 70
if y >= 100 and y < 150:
    index = 120
if y >= 150 and y < 200:
    index = 170
if y >= 200 and y < 250:
    index = 220
if y >= 250 and y < 300:
    index = 270
if y >= 300 and y < 350:
    index = 320
if y >= 350 and y < 400:
    index = 370
if y >= 400 and y < 450:
    index = 420
if y >= 450 and y < 500:
    index = 470
return index

(我知道这是一种略微冗长的方法,但如果有更好的方法,请告诉我,尝试了循环,但无法使其正常工作。)

最后,我到达了获取点击方块的位置,但它并没有改变方块,这是因为当我从列表中索引方块时,它返回了这个:<__main__.Tile object at 0x068A5590>

如何让它返回pygame.Rect而不是这个?我已经研究了类,尝试了__repr____str__,但没有成功,如果有人能帮助我,那就太好了!

谢谢!

2个回答

3
我建议将你的 Tile 类作为一个 pygame.sprite.Sprite 的子类,并将所有的瓦片添加到一个 sprite 组 中。这样,你就可以通过调用 sprite_group.update()sprite_group.draw(screen) 来更新和绘制所有精灵。Pygame 精灵需要一个 image(必须是一个 pygame.Surface 实例)和一个 rect 属性。
要检查碰撞,您可以首先迭代 sprite 组中的精灵,然后使用精灵的 rectcollidepoint 方法来查看它是否与鼠标位置发生碰撞,例如:sprite.rect.collidepoint(event.pos)pygame.mouse.get_pos()。如果它与鼠标发生碰撞,则调用 change_color 方法并将精灵的 image 填充新颜色(或者也可以替换图像)。
import pygame as pg


GREEN = pg.Color(0, 200, 90)
BLUE = pg.Color(0, 90, 200)


class Tile(pg.sprite.Sprite):

    def __init__(self, position):
        super().__init__()
        self.image = pg.Surface((50, 50))
        self.color = GREEN
        self.image.fill(self.color)
        self.rect = self.image.get_rect(topleft=position)

    def change_color(self, color):
        self.color = color
        self.image.fill(self.color)


def main():
    screen = pg.display.set_mode((800, 600))
    clock = pg.time.Clock()
    all_sprites = pg.sprite.Group()
    # Create the tiles and add them to the all_sprites group.
    for y in range(10):
        for x in range(12):
            all_sprites.add(Tile((x*51, y*51)))

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            # If a mouse button was pressed.
            elif event.type == pg.MOUSEBUTTONDOWN:
                # Iterate over the sprites in the group.
                for sprite in all_sprites:
                    # Check if the sprite's rect collides with the mouse pos.
                    if sprite.rect.collidepoint(event.pos):
                        # Finally change the color.
                        sprite.change_color(BLUE)

        all_sprites.update()
        screen.fill((30, 30, 30))
        all_sprites.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

这里有一个pygame精灵教程。当然,你现在也可以继续使用普通的类和列表,但是精灵和组通常更加方便使用。 - skrx

0

所以,我认为使用循环会更容易(就像你要求的建议一样)。在这种方法中,您可以访问所有已按下的瓦片,然后可以执行changeColor

import pygame

class Tile:
    def __init__(self, xstart, ystart, game_display, colour):
        self.tile = pygame.Rect(xstart, ystart, 50, 50)
        self.game_display = game_display
        self.colour = colour
        self.draw()

    def draw(self):
        pygame.draw.rect(game_display, colour,  self.tile, 1)

    def changeColour(self, colour):
        self.colour = colour
        self.draw()

    def doesHit(self,point):
        return self.tile[0] <= point[0] and self.tile[2] > point[0] and self.tile[1] < point[1] and self.tile[4] >= point[1]

def getTile(x,y):
    global board
    point = (x,y)
    for tile in board:
        if tile.doesHit(point):
            print("hit detected!")
            #now we have direct access to all tile information via tile!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接