Загрузка данных
import pygame as pg
import pytmx
from pygame.sprite import spritecollide
pg.init()
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 600
FPS = 80
TILE_SCALE = 3
class Platform(pg.sprite.Sprite):
def __init__(self, image, x, y, width, height):
super(Platform, self).__init__()
self.image = pg.transform.scale(image, (width * TILE_SCALE, height * TILE_SCALE))
self.rect = self.image.get_rect()
self.rect.x = x * TILE_SCALE
self.rect.y = y * TILE_SCALE
class Player(pg.sprite.Sprite):
def __init__(self, map_width, map_height):
super(Player, self).__init__()
self.load_animations()
self.current_animation = self.idle_animation_right
self.image = self.current_animation[0]
self.current_image = 0
self.rect = self.image.get_rect()
self.rect.center = (200, 100) # Начальное положение персонажа
# Начальная скорость и гравитация
self.velocity_x = 0
self.velocity_y = 0
self.gravity = 2
self.is_jumping = False
self.map_width = map_width * TILE_SCALE
self.map_height = map_height * TILE_SCALE
def load_animations(self):
tail_size = 16
tail_scale = 4
self.idle_animation_right = []
num_images = 2
spritesheet = pg.image.load("taileset/7 - Rocket Cherry/Hurt (16 x 32).png")
for i in range(num_images):
x = i * tail_size
y = 0
reck = pg.Rect(x, y, tail_size, tail_size)
image = spritesheet.subsurface(reck)
image = pg.transform.scale(image, (tail_size * tail_scale, tail_size * tail_scale))
self.idle_animation_right.append(image)
self.idle_animation_left = [pg.transform.flip(image, True, False) for image in self.idle_animation_right]
def update(self, platforms):
keys = pg.key.get_pressed()
if keys[pg.K_SPACE] and not self.is_jumping:
self.jump()
if keys[pg.K_a]:
self.velocity_x=-5
elif keys[pg.K_d]:
self.velocity_x=5
else:
self.velocity_x=0
new_x = self.rect.x + self.velocity_x
if 0 <= new_x <= self.map_width - self.rect.width:
self.rect.x = new_x
self.velocity_y += self.gravity
self.rect.y += self.velocity_y
for platform in platforms:
if platform.rect.collidepoint(self.rect.midbottom):
self.rect.bottom = platform.rect.top
self.velocity_y = 0
self.is_jumping = False
if platform.rect.collidepoint(self.rect.midtop):
self.rect.top = platform.rect.bottom
self.velocity_y = 0
if platform.rect.collidepoint(self.rect.midleft):
self.rect.left = platform.rect.right
self.velocity_x = 0
if platform.rect.collidepoint(self.rect.midright):
self.rect.right = platform.rect.left
self.velocity_x = 0
def jump(self):
self.velocity_y = -35
self.is_jumping = True
class Game:
def __init__(self):
self.screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pg.display.set_caption("Платформер")
self.clock = pg.time.Clock()
self.is_running = False
self.camera_x = 0
self.camera_y = 0
self.camera_speed = 4
self.all_sprites = pg.sprite.Group()
self.platforms = pg.sprite.Group()
self.tmx_map = pytmx.load_pygame("mape.tmx")
self.map_pixel_width = self.tmx_map.width * self.tmx_map.tilewidth * TILE_SCALE
self.map_pixel_height = self.tmx_map.height * self.tmx_map.tilewidth * TILE_SCALE
self.player = Player(self.map_pixel_width, self.map_pixel_height)
self.all_sprites.add(elf.player)
for layer in self.tmx_map:
for x, y, gid in layer:
tile = self.tmx_map.get_tile_image_by_gid(gid)
if tile:
platform = Platform(tile, x * self.tmx_map.tilewidth, y * self.tmx_map.tilewidth,
self.tmx_map.tilewidth,
self.tmx_map.tilewidth)
self.all_sprites.add(platform)
self.platforms.add(platform)
self.run()
def run(self):
self.is_running = True
while self.is_running:
self.event()
self.update()
self.draw()
self.clock.tick(FPS)
pg.quit()
quit()
def event(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.is_running = False
keys = pg.key.get_pressed()
# if keys[pg.K_LEFT]:
# self.camera_x += self.camera_speed
# if keys[pg.K_RIGHT]:
# self.camera_x -= self.camera_speed
# if keys[pg.K_UP]:
# self.camera_y += self.camera_speed
# if keys[pg.K_DOWN]:
# self.camera_y -= self.camera_speed
def update(self):
self.plaer.update(self.platforms)
self.camera_x = self.plaer.rect.x - SCREEN_WIDTH // 2
self.camera_y = self.plaer.rect.y - SCREEN_WIDTH // 2
def draw(self):
self.screen.fill("grey")
self.camera_x = min(self.camera_x )
for sprite in self.all_sprites:
self.screen.blit(sprite.image, sprite.rect.move( -self.camera_x, -self.camera_y))
pg.display.flip()
if __name__ == "__main__":
game = Game()