python - Mastermind Game,Pygame,添加猜测指标
问题描述
我已经让我的游戏运行起来了,我对它的外观很满意,我需要在游戏中的每一行旁边添加 4 个指示器,以表明用户的猜测是否接近计算机的猜测
游戏的快速解释
如果用户猜到了一种颜色,但它在错误的位置,那么它应该将指示器更改为橙色,如果它在正确的位置,它应该变为红色。然后用户使用它来继续猜测
我想我可以在每一行旁边画 4 个圆圈,然后根据用户的猜测改变它们的颜色。但是我不确定如何做到这一点
也可能有一种更有效的方法来做到这一点,所以有任何建议都会很棒
代码看起来像这样我添加了一些代码,用于计算机猜测,如果你猜对了
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0 , 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, 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,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,
color,
[(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
为True
或False
时,相应圆圈的颜色会发生变化。
推荐阅读
- c++ - for循环中的多线程c ++
- python - 我在哪里可以找到 k-means 的距离度量?
- javascript - 与 for 和回调挑战异步:
- python - 访问函数外部变量我使用另一种方式但不工作
- android-intent - startService() 中的 Intent 应用程序或活动应使用哪个上下文?
- python - Python Tesseract 西里尔字符问题
- python - ValueError:密文长度必须等于密钥大小
- django - Django模型:在一个查询中获取最后一个ForeignKey
- javascript - ECharts:改变线条颜色。我选了 2 种颜色。两种颜色都显示奇偶风格
- r - 在闪亮的仪表板中嵌入图片