Mastermind Game,Pygame,添加猜测指标

问题描述

我的游戏运行正常,我对它的外观感到满意,我需要在游戏的每一行旁边添加 4 个指示器,以表明用户的猜测是否接近计算机的猜测

>

游戏简介

如果用户猜到了颜色但位置错误,则应将指示器更改为橙色 如果它在正确的位置,它应该变成红色。然后用户用这个继续猜测

我想我可以在每一行旁边画 4 个圆圈,然后根据情况改变它们的颜色 来自用户的猜测。但是我不确定如何做到这一点

也可能有更有效的方法来做到这一点,所以有任何建议都会很棒

代码看起来像这样 我已经添加了一些代码,用于计算机猜测,如果你猜对了

import pygame

BLACK = (0,0)
WHITE = (255,255,255)
GREEN = (0,0)
RED = (255,0)
BLUE = (0,255)

WIDTH = 20
HEIGHT = 20
WIDTH1 = 30
HEIGHT1 = 30

MARGIN = 5
MARGIN1 = 10

array_width = 4
array_height = 8

grid = []
for row in range(10):
    grid.append([])
    for column in range(10):
        grid[row].append(0)

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

pygame.init()

# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [300,300]
screen = pygame.display.set_mode(WINDOW_SIZE)

# Set title of screen
pygame.display.set_caption("MASTERMIND GAME")
sair = True
done = False

clock = pygame.time.Clock()
# -------- Main Program Loop -----------
grid[row][column] = 1

#TEXT Box VARIABLES AND DATA
base_font = pygame.font.Font(None,32)
user_text = ""
input_rect = pygame.Rect (10,250,100,50)
color_active = pygame.Color('lightskyblue3')
color_passive = pygame.Color('gray15')
color=color_passive
active = False

current_color = "white"
grid = [[current_color for column in range(4)] for row in range(8)]

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

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

            column = pos[0] // (WIDTH + MARGIN)
            row = pos[1] // (HEIGHT + MARGIN)
            print(row)
            print(column)
            # TEXT Box CREATION AND USAGE

            # check if column and row are valid grid indices
            if 0 <= row < array_height and 0 <= column < array_width:
                try:
                    grid[row][column] = current_color
                except:
                    print("invalid color")

        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 user_text != "":
                    current_color = user_text
                user_text = ""

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

    if active:
        color = color_active
    else:
        color=color_passive

    screen.fill(0)
    pygame.draw.rect(screen,color,input_rect,2)
    text_surface= base_font.render(user_text,True,(255,255))
    screen.blit(text_surface,(input_rect.x +5,input_rect.y + 5))

    for row,gridrow in enumerate(grid):
        for column,color in enumerate(gridrow):
            pygame.draw.rect(screen,[(MARGIN + WIDTH) * column + MARGIN,(MARGIN + HEIGHT) * row + MARGIN,WIDTH,HEIGHT])

    clock.tick(60)

    pygame.display.flip()

pygame.quit

谢谢

解决方法

这个问题有几个子问题,我只回答了一个。因此,有很多机会从其他贡献者那里获得更多答案。


为指标创建一个新网格。所有字段的初始值为 None:

indicator_grid = [[None for column in range(4)] for row in range(8)]

在嵌套循环中绘制指标:

while not done:
    # [...]

    for row,gridrow in enumerate(indicator_grid):
        for column,status in enumerate(gridrow):
            x = 100 + (MARGIN + WIDTH) * column + MARGIN
            y = (MARGIN + HEIGHT) * row + MARGIN
            color = "grey"
            if status != None:
                color = "red" if status else "orange"
            pygame.draw.circle(screen,color,(x+WIDTH//2,y+WIDTH//2),WIDTH//2)

当您想将 indicator_grid 中的状态设置为 TrueFalse 时,相应圆圈的颜色会发生变化。