Я хотел бы отобразить 4 цвета, выбранные пользователем из цветовой сетки 3x3. Тем не менее, он появляется, но только на короткое время. Я хотел бы, чтобы цветной дисплей обновлялся, если бы. оно достигло более 4 цветов (т.е. начало нового списка). Мне удалось получить правильный вывод на консоли, но не в окне Pygame. Кроме того, как вы отображаете обновление черно-белого счетчика?
def main():
import pygame
import random
pygame.init() # initialize modules
#pygame.font.init()
# window dimensions
screen_width = 560
screen_height = 630
# colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
orange = (255, 165, 0)
purple = (128, 0, 128)
maroon = (128, 0, 0)
pink = (255, 105, 80)
brown = (139, 69, 19)
# color dictionary
color_dict = {
1: red,
2: green,
3: blue,
4: yellow,
5: orange,
6: purple,
7: maroon,
8: pink,
9: brown,
}
# initialization block
i = 1
guess_list = []
black_count = 0
white_count = 0
no_of_guess = 0
keys = pygame.key.get_pressed() # list of all keyboard keys
display_list = []
display_lower_row = False
# randomization block
ans_list = []
for x in range(1, 5): # loops from 1 to no_of_colors
ans_list.append(color_dict[random.randint(1, 9)]) # 1 to 9
print(ans_list)
# UI block
game_screen = pygame.display.set_mode((355, screen_height))
pygame.display.set_caption("MasterMind")
game_font = pygame.font.SysFont('Comic Sans MS', 40)
display_text = game_font.render('MasterMind', True, white)
clock = pygame.time.Clock()
# main game loop
execute = True
while execute:
# display block
game_screen.fill(black) # black background
# title
game_screen.blit(display_text, (screen_width*(0.885/7), screen_height*(1/7)))
# display the rectangles. args: ((screen where the object is to be displayed), (color), (position(x,y)), (dimensions))
pygame.draw.rect(game_screen, red,
((1 / 7) * screen_width, (screen_height // 2) - 80, 40, 40)
)
pygame.draw.rect(game_screen, green,
((3 / 7) * screen_width - 80, (screen_height // 2) - 80, 40, 40),
)
pygame.draw.rect(game_screen, blue,
((5 / 7) * screen_width - 160, (screen_height // 2) - 80, 40, 40),
)
pygame.draw.rect(game_screen, yellow,
((1 / 7) * screen_width, (screen_height // 2), 40, 40)
)
pygame.draw.rect(game_screen, orange,
((3 / 7) * screen_width - 80, (screen_height // 2), 40, 40)
)
pygame.draw.rect(game_screen, purple,
((5 / 7) * screen_width - 160, (screen_height // 2), 40, 40),
)
pygame.draw.rect(game_screen, maroon,
((1 / 7) * screen_width, (screen_height // 2) + 80, 40, 40)
)
pygame.draw.rect(game_screen,pink,
((3 / 7) * screen_width - 80, (screen_height // 2) + 80, 40, 40),
)
pygame.draw.rect(game_screen,brown,
((5 / 7) * screen_width - 160, (screen_height // 2) + 80, 40, 40),
)
# event block
for event in pygame.event.get(): # loop through user events i.e. keyboard, mouse
if event.type == pygame.QUIT: # exit button
execute = False
if event.type == pygame.MOUSEBUTTONDOWN:
i += 1
if no_of_guess > 12:
print("YOU LOSE!!")
print("Play Again? (Y)-yes | (any keys)-no")
# try again block via recursion
if keys[pygame.K_Y]:
main()
else:
pygame.quit()
else:
x_mouse, y_mouse = pygame.mouse.get_pos() # mouse coordinates
if 80 <= x_mouse <= 120 and 235 <= y_mouse <= 275:
guess_list.append(red)
elif 160 <= x_mouse <= 200 and 235 <= y_mouse <= 275:
guess_list.append(green)
elif 240 <= x_mouse <= 280 and 235 <= y_mouse <= 275:
guess_list.append(blue)
elif 80 <= x_mouse <= 120 and 315 <= y_mouse <= 365:
guess_list.append(yellow)
elif 160 <= x_mouse <= 200 and 315 <= y_mouse <= 365:
guess_list.append(orange)
elif 240 <= x_mouse <= 280 and 315 <= y_mouse <= 365:
guess_list.append(purple)
elif 80 <= x_mouse <= 120 and 395 <= y_mouse <= 435:
guess_list.append(maroon)
elif 160 <= x_mouse <= 200 and 395 <= y_mouse <= 435:
guess_list.append(pink)
elif 240 <= x_mouse <= 280 and 395 <= y_mouse <= 435:
guess_list.append(brown)
else: # clicks outside the squares
print("Only click on the colored squares!!") # this loops 48 times (fix ASAP)
pass # essentially does nothing (prevents extra terms to be printed)
if len(guess_list) == 4:
display_lower_row = True
print(guess_list)
no_of_guess += 1
# scoring block
display_list = guess_list
final_guess_list = guess_list
for index in range(len(ans_list)):
if ans_list[index] == final_guess_list[index]:
black_count += 1
if final_guess_list[index] in ans_list:
white_count += 1
if black_count == 4 and white_count == 4:
print("Guess #{}:".format(no_of_guess))
print("4B - 4W")
print("YOU WIN!!")
print("Play Again? (Y)-yes | (any keys)-no")
# try again block via recursion
if keys[pygame.K_Y]:
main()
else:
pygame.quit()
else:
print("Guess #{}:".format(no_of_guess))
print("{black}B - {white}W".format(black = black_count, white = white_count))
if display_lower_row:
# output display block
pygame.draw.rect(game_screen,guess_list[0],
((0.5 / 7) * screen_width, (screen_height // 2) + 160, 30, 30)
)
pygame.draw.rect(game_screen, guess_list[1],
((2.5 / 7) * screen_width - 100, (screen_height // 2) + 160, 30, 30),
)
pygame.draw.rect(game_screen, guess_list[2],
((4.5 / 7) * screen_width - 200, (screen_height // 2) + 160, 30, 30),
)
pygame.draw.rect(game_screen, guess_list[3],
((6.5 / 7) * screen_width - 300, (screen_height // 2) + 160, 30, 30),
)
pygame.display.update()
# reset counter
black_count = 0
white_count = 0
guess_list = []
pygame.display.update()
clock.tick(40) # sets fps
pygame.quit()
main()