Ниже прикладываю скрин и код.
import random
import pygame
import pygame.freetype
pygame.init()
Obstacle_spawn_time = 1000
red = (213, 50, 80)
screen = pygame.display.set_mode((900, 500))
pygame.display.set_caption('CatRun')
clock = pygame.time.Clock()
font = pygame.freetype.Font(None, 40)
Barier_image = pygame.image.load('Barier.JPEG')
Barier_image = pygame.transform.scale(Barier_image, (50, 80))
CatRun_image = pygame.image.load('CatRun.JPG')
CatRun_image= pygame.transform.scale(CatRun_image, (100, 100))
CatLie_image= pygame.image.load('CatLie.JPG')
CatLie_image = pygame.transform.scale(CatLie_image, (100, 100))
CatStay_image = pygame.image.load('CatStay.JPG')
CatStay_image = pygame.transform.scale(CatStay_image, (100, 100))
Ground_image = pygame.image.load('Ground.jpg')
Ground_image= pygame.transform.scale(Ground_image, (800, 142))
Coin_image = pygame.image.load('Coin.JPG')
Coin_image= pygame.transform.scale(Coin_image, (100, 100))
# Загружаем спрайты персонажа, монетки, «барьера»
ground_group = pygame.sprite.Group()
cactus_group = pygame.sprite.Group()
Coin_group = pygame.sprite.Group()
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
# Выбор шрифта и установка размера
ground_event = pygame.USEREVENT
cactus_event = pygame.USEREVENT + 1
Coin_event = pygame.USEREVENT + 1
pygame.time.set_timer(ground_event, 2000)
pygame.time.set_timer(cactus_event, Obstacle_spawn_time)
pygame.time.set_timer(Coin_event, Obstacle_spawn_time)
# Присваем значение объектам
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [300 / 6, 300 / 3])
# Работа с игровым «сообщением»
class Ground(pygame.sprite.Sprite):
def __init__ (self, image, position):
super() .__init__ ()
self.image = image
self.rect = self.image.get_rect()
self.rect.center = position
def update(self):
self.rect.x -= 3
if self.rect.right < 0:
self.kill()
class Cactus(pygame.sprite.Sprite):
def __init__ (self, image, position):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.center = position
def update(self):
self.rect.x -= 3
if self.rect.right < 0:
self.kill()
Cat.score += 1
if self.rect.colliderect(Cat.rect):
Cat.game_status = 'Menu'
# Функция обновления экрана
class Cat:
def __init__ (self, image, position):
self.image = image
self.rect = self.image.get_rect()
self.rect.center = position
self.y = 0
self.step = 6
self.max_jump = 10
self.in_jump = False
self.score = 0
self.game_status = 'Game'
def jump(self):
if self.in_jump:
if self.y < self.max_jump:
self.y += 1
self.rect.y -= self.step
elif self.y < self.max_jump * 2:
self.y += 1
self.rect.y += self.step
else:
self.in_jump = False
self.y = False
def draw(self):
screen.blit(self.image, self.rect)
Barier_event =int(input())
GameCat = Cat(CatStay_image, (100,400))
g = Ground(Ground_image, (300, 450))
ground_group.add(g)
g = Ground(Ground_image, (900, 450))
ground_group.add(g)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
GameCat.in_jump = True
if event.type == ground_event:
g = Ground(Ground_image, (900, 450))
Ground_group.add(g)
if event.type == Barier_event:
pygame.time.set_timer(Barier_event, random.randint(6000, 10000))
c = Barier(Barier_image, (910, 400))
Barier_group.add(c)
screen.fill((255, 255, 255))
if GameCat.game_status == 'Game':
Ground_group.update()
Ground_group.draw(screen)
Barier_group.update()
Barier_group.draw(screen)
Coin_group.update()
Coin_group.draw(screen)
GameCat.jump()
GameCat.draw()
font.render_to(screen, (850, 50), str(GameCat.score), (0, 0, 0))
else:
message("You lose!!!", red)
pygame.display.flip()
clock.tick(60)
# Обновление всех элементов и работа с «игровым» сообщением в случае проигрыша