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(180, 370, (0,255,0), (40,20)) # пушка внизу
zet = Object(180, 30, (255,0,0), (20,20)) # цель вверху
strela = Object(180, 360, (255,255,0), (5,10))
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 + 17, hero.y - 5, True
if e.type == pygame.MOUSEMOTION:
hero.x = max(0, min(360, pygame.mouse.get_pos()[0] - 20))
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.y -= 8 # летит вверх
if strela.y < 0: strela.push, strela.y = False, hero.y - 5
if (strela.x+2 > zet.x and strela.x+2 < zet.x+20 and
strela.y > zet.y and strela.y < zet.y+20):
score += 1
strela.push, strela.y = False, hero.y - 5
zet.x, zet.y = random.randint(0, 380), random.randint(20, 100)
screen.fill((0,0,0))
zet.render()
if strela.push: 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()