import pygame
import random
WIDTH = 500
HEIGHT = 700
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Найди красные объекты")
f1 = pygame.font.Font(None, 36)
running = True
x = 0
y = 100
x1 = random.randint(0,9)*50
x2 = random.randint(0,9)*50
y1 = random.randint(0,7)*50+100
y2 = random.randint(0,7)*50+100
count_obj = 0
def create_x_and_y():
global x1,y1,x2,y2
while 1:
x1 = random.randint(0,9)*50
y1 = random.randint(0,7)*50+100
x2 = random.randint(0,9)*50
y2 = random.randint(0,7)*50+100
if (x1 != x2 and y1 != y2):
break
create_x_and_y()
print(y2)
print(y1)
while running:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x = x - 50
if event.key == pygame.K_RIGHT:
x = x + 50
if event.key == pygame.K_DOWN:
y = y + 50
if event.key == pygame.K_UP:
y = y - 50
for it_1 in range(0,500,50):
for it_2 in range(100,500,50):
pygame.draw.rect(screen,WHITE, (it_1,it_2,49,49))
if (x < 0): x = 0
if (x > 450): x = 450
if (y > 500): y = 500
if (y < 100): y = 100
if (x == x1 and y == y1 ):
count_obj = count_obj + 1
pygame.draw.rect(screen,WHITE, (x1,y1,49,49))
pygame.draw.rect(screen,WHITE, (x2,y2,49,49))
create_x_and_y()
ticks=pygame.time.get_ticks()
img = f1.render("Прошло секунд "+str(ticks//1000), True, RED)
screen.blit(img, (0, 0))
img2 = f1.render("Найдено красных объектов "+str(count_obj), True, RED)
screen.blit(img2,(0, 50))
pygame.draw.rect(screen,RED, (x1,y1,49,49))
pygame.draw.rect(screen,BLUE, (x2,y2,49,49))
pygame.draw.rect(screen,GREEN, (x,y,49,49))
pygame.display.update()
pygame.quit()
|