import pygame
import random
FPS = 60
W = 700 # ширина экрана
H = 700 # высота экрана
BLACK = (0, 0, 0) #цвета
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GRAY = (200, 200, 200)
RIGHT = "to the right"
LEFT = "to the left"
STOP = "stop"
class Block(pygame.sprite.Sprite):
def __init__(self, color, size=30, speed=5):
super().__init__()
self.image = pygame.Surface([size, size])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(W)
self.rect.y = random.randrange(H)
self.speed = speed
def update(self):
# Подвинуть блок "навстречу" автомобилю
self.rect.y += self.speed
if self.rect.y > W:
self.rect.y = 0
self.rect.x = random.randrange(W)
pygame.init()
sc = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()
# координаты и радиус круга
'''
x = W // 2
y = H // 2
r = 50
'''
motion = STOP
# Создаем группы спрайтов:
block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
# Создаем блоки - препятствия и добавляем их в группы:
for i in range(10):
block = Block(BLACK)
block_list.add(block)
all_sprites_list.add(block)
# Создаем автомобиль игрока
player = Block(RED, )
player.rect.x = W // 2
player.rect.y = H // 2 + 300
# Добавляем автомобиль в группу all_sprites_list:
all_sprites_list.add(player)
basic_font=pygame.font.SysFont(None,36)
text= basic_font.render('You Crashed!',True,RED)
text_pos =(W//2, H//2)
while 1:
sc.fill(WHITE)
#pygame.draw.circle(sc, BLUE, (x, y), r)
all_sprites_list.draw(sc)
block_list.update()
pygame.display.update()
for i in pygame.event.get():
if i.type == pygame.QUIT:
exit()
elif i.type == pygame.KEYDOWN:
if i.key == pygame.K_LEFT:
motion = LEFT
elif i.key == pygame.K_RIGHT:
motion = RIGHT
elif i.type == pygame.KEYUP:
if i.key in [pygame.K_LEFT, pygame.K_RIGHT]:
motion = STOP
if motion == LEFT:
player.rect.x -= 3
elif motion == RIGHT:
player.rect.x += 3
if pygame.sprite.spritecollideany(player, block_list):
screen.blit(text, text_pos)
clock.tick(FPS)