策划游戏,选择要猜的颜色

时间:2021-01-18 10:04:29

标签: python pygame pycharm

我正在尝试在 python 中创建一个策划游戏

我尝试使用文本框输入来选择颜色,并使用数组支持的网格作为我的网格。 当我输入一种颜色时,例如“蓝色”,整个网格变成蓝色,而我希望它存储输入并让用户选择网格中的哪些方块变成蓝色

感谢任何帮助

while not done:
for event in pygame.event.get():

    if event.type == pygame.QUIT:
        done = True

    elif event.type == pygame.MOUSEBUTTONDOWN:


        pos = pygame.mouse.get_pos()


        column = pos[0] // (WIDTH + MARGIN)
        row = pos[1] // (HEIGHT + MARGIN)



        print("Click ", pos, "Grid coordinates: ", row, column)

    if event.type == pygame.MOUSEBUTTONDOWN:
        if input_rect.collidepoint(event.pos):
            active = True
        else:
            active = False

    if event.type == pygame.KEYDOWN:
        if active == True:
            if event.key == pygame.K_BACKSPACE:
                user_text = user_text[0:-1]
            else:
                user_text += event.unicode
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RETURN:
            user_text = user_text
            print(user_text)

if active:
    color = color_active
else:
    color=color_passive
pygame.draw.rect(screen,color,input_rect,2)
text_surface= base_font.render(user_text,True,(255,255,255))
screen.blit(text_surface, (input_rect.x +5, input_rect.y + 5))

#input_rect.w = max(20, text_surface.get_width() + 10)

for row in range(8):
    for column in range(4):
        color = WHITE
        if user_text == 'blue':
            print (grid)
            grid[row][column] = 2
            color = BLUE
            pygame.draw.rect(screen,
                             color,
                             [(MARGIN + WIDTH) * column + MARGIN,
                              (MARGIN + HEIGHT) * row + MARGIN,
                              WIDTH,
                              HEIGHT])

        if user_text == 'green':
            grid[row][column] = 1
            color = GREEN
        pygame.draw.rect(screen,
                         color,
                         [(MARGIN + WIDTH) * column + MARGIN,
                          (MARGIN + HEIGHT) * row + MARGIN,
                          WIDTH,
                          HEIGHT])

The result after typing 'blue'

谢谢

1 个答案:

答案 0 :(得分:2)

改变方块颜色

从您的代码来看,您没有单独存储每个块的信息。您需要这样做,因为每个块都有不同的特征(颜色)。拥有某种数据结构来存储每个块信息真的很有帮助。但现在只需将颜色存储在一个数组中。

  colors = [(0, 0, 0) for i in range(array_width * array_height)]

选择块

存储一堆布尔值以指示所选块是否处于活动状态。

  blocks = [False for i in range(array_width * array_height)]

然后你可以在每一帧循环遍历这个数组,并将被点击的那些设置为 True(或者切换它,在我个人看来这是更好的)。

pos = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEBUTTONDOWN:

    #iterate through the 2d array
    for i in range(array_width):
        for j in range(array_height):

            column = pos[0] // (WIDTH + MARGIN)
            row = pos[1] // (HEIGHT + MARGIN)

            blocks[row * array_width + column] = not blocks[row * array_width + column] #toggle
            #convert 2d array index to 1d array at mouse position and set the bool at that index to True
            #blocks[row * array_width + column] = True
            

然后遍历块并设置颜色。

array_width = 4
array_height = 8
for row in range(array_width):
    for column in range(array_height):

        #CHECK IF BLOCK SHOULD BE CHANGED
        if blocks[column * array_width + row]:
            if user_text == 'blue':
                colors[column * array_height + row] = (0, 0, 255)

绘图块

现在这也意味着您必须在不同的循环中进行绘图(即使在问题中的代码中,也无需编写 draw rect 调用两次,只需取消缩进第二次即可)。

for row in range(array_width):
    for column in range(array_height):
        pygame.draw.rect(screen,
                                     #GET THE COLOR AT THAT INDEX
                                     colors[column * array_width + row],
                                     [(MARGIN + WIDTH) * column + MARGIN,
                                      (MARGIN + HEIGHT) * row + MARGIN,
                                      WIDTH,
                                      HEIGHT])

然后您也可以在用户输入蓝色按回车键后将所有块设置回 false,只是为了方便。

相关问题