13 подписчиков
Игра "Ловец яиц" на языке Python от Надила Вильдановича А.
# Ввод пайджейм
pygame.init()
# Рабочее пространство
screen_width = 400
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Ловец яичек")
# Настройка часов
clock = pygame.time.Clock()
# Ввод цвета
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# Ввод игрока
player_width = 50
player_height = 50
player_x = screen_width / 2 - player_width / 2
player_y = screen_height - player_height
player_speed = 5
# Рандомно падающее яйцо и его размеры
egg_width = 30
egg_height = 30
egg_x = random.randint(0, screen_width - egg_width)
egg_y = 0
egg_speed = 3
# Счет
score = 0
font = pygame.font.SysFont(None, 30)
# Основное игровой цикл
running = True
while running:
# Обработка события
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# Движение игрока
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < screen_width - player_width:
player_x += player_speed
# Движение яйца
egg_y += egg_speed
# Поймано ли яйцо?
if egg_y + egg_height > player_y and egg_x + egg_width > player_x and egg_x < player_x + player_width:
egg_x = random.randint(0, screen_width - egg_width)
egg_y = 0
score += 1
egg_speed += 0.1
# Не пропущено ли яйцо?
if egg_y > screen_height:
egg_x = random.randint(0, screen_width - egg_width)
egg_y = 0
score -= 1
egg_speed += 0.1
# Изображение
screen.fill(white)
pygame.draw.rect(screen, black, (player_x, player_y, player_width, player_height))
pygame.draw.ellipse(screen, red, (egg_x, egg_y, egg_width, egg_height))
score_text = font.render("Score: " + str(score), True, black)
screen.blit(score_text, (10, 10))
pygame.display.update()
# Limit the frame rate
clock.tick(60)
# Выход
pygame.quit()
Ниже прилагаю скрин работы компилированного файла. Более подробно можно прочитать в моём паблике по ссылке: vk.com/...138
PS: если Вами найдены грамматические, лексические или даже физические и математические ошибки, то, прошу Вас, сообщайте о них на почту: akhmerov.nadil@gmail.com
1 минута
21 марта 2023