15.03.2024
статья
Игра Динозавр Chrome на Pygame

Код игры:

import pygame

pygame.init()

screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption('Dinosaur Chrome')

background = pygame.image.load('фон')
background = pygame.transform.scale(background, (1280, 720))

cactus1 = pygame.image.load('кактус')
cactus1 = pygame.transform.scale(cactus1, (50, 75))
cactus1_rect = cactus1.get_rect()
cactus1_rect.x = 1280
cactus1_rect.y = 580

cactus2 = pygame.image.load('кактус')
cactus2 = pygame.transform.scale(cactus2, (50, 75))
cactus2_rect = cactus2.get_rect()
cactus2_rect.x = 800
cactus2_rect.y = 580

cactus3 = pygame.image.load('кактус')
cactus3 = pygame.transform.scale(cactus3, (50, 75))
cactus3_rect = cactus3.get_rect()
cactus3_rect.x = 400
cactus3_rect.y = 580

#by Freepik
dinosaur = pygame.image.load('динозавр')
dinosaur = pygame.transform.scale(dinosaur, (75, 100))
dinosaur_rect = dinosaur.get_rect()
dinosaur_rect.x = 50
dinosaur_rect.y = 555

font1 = pygame.font.Font(None, 64)
font2 = pygame.font.Font(None, 128)

hp = 3
dinosaur_normal = True
speed = 4
y_speed = 0

while True:
    events = pygame.event.get()
    for i in events:
        if i.type == pygame.QUIT:
            exit()

    cactus1_rect.x -= speed
    if cactus1_rect.x < -50:
        cactus1_rect.x = 1280

    cactus2_rect.x -= speed
    if cactus2_rect.x < -50:
        cactus2_rect.x = 1280

    cactus3_rect.x -= speed
    if cactus3_rect.x < -50:
        cactus3_rect.x = 1280

    keys = pygame.key.get_pressed()
    if dinosaur_normal == True:
        if keys[pygame.K_SPACE]:
            dinosaur_normal = False

    if dinosaur_normal == False:
        dinosaur_rect.y += y_speed

        if dinosaur_rect.y > 450:
            y_speed -= 0.2
        else:
            y_speed += 0.2

        if dinosaur_rect.y > 554:
            dinosaur_normal = True

    if dinosaur_rect.colliderect(cactus1_rect) or dinosaur_rect.colliderect(cactus2_rect) or dinosaur_rect.colliderect(cactus3_rect):
        hp -= 0.033

    text1 = font1.render(f'HP: {hp}', True, 'white')
    text2 = font2.render('GAME OVER', True, 'white')

    screen.blit(background, (0, 0))

    if hp > 0:
        screen.blit(dinosaur, dinosaur_rect)
        screen.blit(text1, (0, 0))
    else:
        screen.blit(text2, (320, 240))

    screen.blit(cactus1, cactus1_rect)
    screen.blit(cactus2, cactus2_rect)
    screen.blit(cactus3, cactus3_rect)
    pygame.display.update()
    pygame.time.Clock().tick(60)
Комментарии (1)
можно файлы спрайтов?