Answer the question
In order to leave comments, you need to log in
How to synchronize the display of colors?
You need to write a program that changes colors. When you press 1 on the keyboard, the colors change in 1 and 3 quarters, when you press 2, respectively, in 2 and 4, if you press 0, the program will change colors in each square diagonally. And that's actually the question, if you catch the correct timing, then the program will change colors incorrectly (the entire window is repainted in one color)
import pygame
import sys
# здесь определяются константы,
# классы и функции
WIN_WIDTH = 600
WIN_HEIGHT = 600
WHITE = 255, 255, 255
BLACK = 0, 0, 0
PINK = 255, 0, 255
AQUA = 0, 255, 255
RED = 255, 0, 0
FPS = 1
class ChangeColor():
color_to_change = 1
def __init__(self, surface, first_fourth, second_fourth, color_to_change):
self.first_fourth = first_fourth
self.second_fourth = second_fourth
self.color_to_change = color_to_change
self.surface = surface
def change(self):
if self.color_to_change % 2 == 0:
self.surface.fill(AQUA)
screen.blit(self.surface, self.first_fourth)
screen.blit(self.surface, self.second_fourth)
self.color_to_change += 1
else:
self.surface.fill(RED)
screen.blit(self.surface, self.first_fourth)
screen.blit(self.surface, self.second_fourth)
self.color_to_change += 1
# Создание ректов для каждый четверти
fourth_1 = pygame.Rect((WIN_WIDTH // 2, 0), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
fourth_2 = pygame.Rect((0, 0), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
fourth_3 = pygame.Rect((0, WIN_HEIGHT // 2), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
fourth_4 = pygame.Rect((WIN_WIDTH // 2, WIN_HEIGHT // 2), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
# здесь происходит инициация,
# создание объектов
pygame.init()
screen = pygame.display.set_mode((600, 600))
screen.fill(WHITE)
clock = pygame.time.Clock()
surf_fourth = pygame.Surface((fourth_1.width, fourth_1.height))
# Создание экземпляра класса ChangeColor
first_two_quarters = ChangeColor(surf_fourth, fourth_1, fourth_3, 1)
second_two_quarters = ChangeColor(surf_fourth, fourth_2, fourth_4, 2)
# если надо до цикла отобразить
# какие-то объекты, обновляем экран
pygame.display.update()
active_first_fourth = False
active_second_fourth = False
# главный цикл
while True:
# задержка
clock.tick(FPS)
# цикл обработки событий
for i in pygame.event.get():
if i.type == pygame.QUIT:
sys.exit()
if i.type == pygame.KEYDOWN:
if i.key == pygame.K_1:
active_first_fourth = True
active_second_fourth = False
if i.key == pygame.K_2:
active_first_fourth = False
active_second_fourth = True
if i.key == pygame.K_0:
active_first_fourth = True
active_second_fourth = True
if active_first_fourth:
first_two_quarters.change()
if active_second_fourth:
second_two_quarters.change()
# --------
# изменение объектов
# --------
# обновление экрана
pygame.display.update()
Answer the question
In order to leave comments, you need to log in
Can I make it so that when you click on buttons 1 and 2, unnecessary fields (those that do not change color) are painted white?
And it turns out that if, for example, you press 1, and then, for example, 2, then it does not replace the first squares. During blink 2, it can merge into one color.
The simplest thing I could think of was to add a white color to the entire screen when a button is pressed.
if i.type == pygame.KEYDOWN:
if i.key == pygame.K_1:
screen.fill(WHITE)
active_first_fourth = True
active_second_fourth = False
if i.key == pygame.K_2:
screen.fill(WHITE)
active_first_fourth = False
active_second_fourth = True
if i.key == pygame.K_0:
screen.fill(WHITE)
active_first_fourth = True
active_second_fourth = True
import pygame
import sys
# здесь определяются константы,
# классы и функции
WIN_WIDTH = 600
WIN_HEIGHT = 600
WHITE = 255, 255, 255
BLACK = 0, 0, 0
PINK = 255, 0, 255
AQUA = 0, 255, 255
RED = 255, 0, 0
FPS = 1
class ChangeColor():
color_to_change = 1
def __init__(self, surface, first_fourth, second_fourth, color_to_change):
self.first_fourth = first_fourth
self.second_fourth = second_fourth
self.color_to_change = color_to_change
self.surface = surface
def change(self):
if self.color_to_change % 2 == 0:
self.surface.fill(AQUA)
screen.blit(self.surface, self.first_fourth)
screen.blit(self.surface, self.second_fourth)
self.color_to_change += 1
else:
self.surface.fill(RED)
screen.blit(self.surface, self.first_fourth)
screen.blit(self.surface, self.second_fourth)
self.color_to_change += 1
# Создание ректов для каждый четверти
fourth_1 = pygame.Rect((WIN_WIDTH // 2, 0), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
fourth_2 = pygame.Rect((0, 0), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
fourth_3 = pygame.Rect((0, WIN_HEIGHT // 2), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
fourth_4 = pygame.Rect((WIN_WIDTH // 2, WIN_HEIGHT // 2), (WIN_WIDTH // 2, WIN_HEIGHT // 2))
# здесь происходит инициация,
# создание объектов
pygame.init()
screen = pygame.display.set_mode((600, 600))
screen.fill(WHITE)
clock = pygame.time.Clock()
surf_fourth = pygame.Surface((fourth_1.width, fourth_1.height))
# Создание экземпляра класса ChangeColor
first_two_quarters = ChangeColor(surf_fourth, fourth_1, fourth_3, 1)
second_two_quarters = ChangeColor(surf_fourth, fourth_2, fourth_4, 2)
# если надо до цикла отобразить
# какие-то объекты, обновляем экран
pygame.display.update()
active_first_fourth = False
active_second_fourth = False
# главный цикл
while True:
# задержка
clock.tick(FPS)
# цикл обработки событий
for i in pygame.event.get():
if i.type == pygame.QUIT:
sys.exit()
if i.type == pygame.KEYDOWN:
if i.key == pygame.K_1:
screen.fill(WHITE)
active_first_fourth = True
active_second_fourth = False
if i.key == pygame.K_2:
screen.fill(WHITE)
active_first_fourth = False
active_second_fourth = True
if i.key == pygame.K_0:
screen.fill(WHITE)
active_first_fourth = True
active_second_fourth = True
if active_first_fourth:
first_two_quarters.change()
if active_second_fourth:
second_two_quarters.change()
# --------
# изменение объектов
# --------
# обновление экрана
pygame.display.update()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question