import pygame, random
pygame.init()
w, win = 400, pygame.display.set_mode((400, 430))
pygame.display.set_caption('Стрелок')
screen, clock = pygame.Surface((400, 400)), pygame.time.Clock()
class Object:
def __init__(self, x, y, color, size):
self.x, self.y, self.color, self.size = x, y, color, size
def render(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.size[0], self.size[1]))
hero = Object(350, 350, (0,255,0), (20,20))
zet = Object(10, 10, (255,0,0), (20,20))
strela = Object(-10, 350, (255,255,0), (10,5))
strela.push, zet.right, score = False, True, 0
while True:
clock.tick(60)
for e in pygame.event.get():
if e.type == pygame.QUIT: exit()
if e.type == pygame.KEYDOWN and e.key == pygame.K_SPACE and not strela.push:
strela.x, strela.y, strela.push = hero.x + 15, hero.y, True
if e.type == pygame.MOUSEMOTION:
hero.x = max(0, min(380, pygame.mouse.get_pos()[0] - 10))
zet.x += 3 if zet.right else -3
if zet.x <= 0 or zet.x >= 380: zet.right = not zet.right
if strela.push:
strela.x += 8
if strela.x > 400: strela.push, strela.x = False, -10
if (strela.x+5 > zet.x and strela.x+5 < zet.x+20 and
strela.y+2 > zet.y and strela.y+2 < zet.y+20):
score, strela.push, strela.x = score+1, False, -10
zet.x, zet.y = random.randint(0,380), random.randint(0,380)
screen.fill((0,0,0))
zet.render(); strela.render(); hero.render()
win.blit(screen, (0,0))
font = pygame.font.Font(None, 24)
win.blit(font.render(f'Счёт: {score}', True, (255,255,255)), (10, 405))
pygame.display.update()