Всем привет: есть код
import pygame
WIDTH = 300
HEIGHT = 250
FPS = 30
BLUE = (0,0,255)
WHITE = (255,255,255)
pygame.init()
x = WIDTH / 2
y = HEIGHT / 2
r = 10
motion = ''
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.draw.circle(screen, BLUE, (x, y), r)
pygame.display.update()
clock = pygame.time.Clock()
motion = 'stop'
running = True
while running:
events = pygame.event.get()
for i in events:
if i.type == pygame.QUIT:
running = False
if i.type == pygame.KEYDOWN:
if i.key == pygame.K_LEFT:
motion = "LEFT"
if i.key == pygame.K_RIGHT:
motion = "RIGHT"
if i.key == pygame.K_DOWN:
motion = "DOWN"
if i.type == pygame.KEYUP:
if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT or i.key == pygame.K_UP or i.key == pygame.K_DOWN:
motion = "stop"
if motion == "LEFT":
x -= 1
if motion == "RIGHT":
x += 1
if motion == "UP":
y -= 1
if motion == "DOWN":
y += 1
screen.fill(WHITE)
pygame.draw.circle(screen, BLUE, (x, y), r)
pygame.display.update()
clock.tick(FPS)
в него надо добавить строки что бы шар при нажатии становился красным
Тебе нужно добавить переменную color и проверять нажатие кнопки мыши, когда нажмут кнопку меняешь color
Объяснение:
import pygame
WIDTH = 300
HEIGHT = 250
FPS = 30
BLUE = (0,0,255)
WHITE = (255,255,255)
pygame.init()
x = WIDTH / 2
y = HEIGHT / 2
r = 10
color = "BLUE"
motion = ''
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.draw.circle(screen, color, (x, y), r)
pygame.display.update()
clock = pygame.time.Clock()
motion = 'stop'
running = True
while running:
events = pygame.event.get()
for i in events:
if i.type == pygame.QUIT:
running = False
if i.type == pygame.KEYDOWN:
if i.key == pygame.K_LEFT:
motion = "LEFT"
if i.key == pygame.K_RIGHT:
motion = "RIGHT"
if i.key == pygame.K_DOWN:
motion = "DOWN"
if i.type == pygame.KEYUP:
print("Up")
if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT or i.key == pygame.K_UP or i.key == pygame.K_DOWN:
motion = "stop"
if motion == "LEFT":
x -= 1
if motion == "RIGHT":
x += 1
if motion == "UP":
y -= 1
if motion == "DOWN":
y += 1
if i.type == pygame.MOUSEBUTTONDOWN:
if i.button == 1:
if color == "BLUE":
color = "RED"
else:
color = "BLUE"
screen.fill(WHITE)
pygame.draw.circle(screen, color, (x, y), r)
pygame.display.update()
clock.tick(FPS)