import pygame

pygame.init()

from src.sprite.background import Background
from src.sprite.player import Player

if __name__ == "__main__":
    screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    pygame.display.toggle_fullscreen()

    width, height = screen.get_size()

    player = Player()
    background = Background(screen)
    to_display = [background, player]

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            if event.type == pygame.KEYDOWN:
                match event.key:
                    case pygame.K_p:
                        running = False
                    case pygame.K_UP:
                        player.move(0, -1)
                    case pygame.K_DOWN:
                        player.move(0, 1)
                    case pygame.K_LEFT:
                        player.move(-1, 0)
                    case pygame.K_RIGHT:
                        player.move(1, 0)
                    case _:
                        pass

        for sprite in to_display:
            sprite.blit(screen)
        # pygame.display.update()
        pygame.display.flip()

    pygame.quit()

main.py
Guillaume-Crand/FDC-project
import pygame


class Background(pygame.sprite.Sprite):
    FONT = pygame.font.Font("font/Pixelcastle-Regular.otf", 90)

    def __init__(self, screen: pygame.Surface):
        super(Background, self).__init__()

        width, height = screen.get_size()

        self.bg_img = pygame.image.load("img/moutains.png")
        self.bg_img = pygame.transform.scale(self.bg_img, (width, height))

        self.text = self.FONT.render("Press p to leave the game", True, "red")
        self.textRect = self.text.get_rect()
        self.textRect.center = (width // 2, height // 2)

    def blit(self, screen: pygame.Surface):
        screen.blit(self.bg_img, (0, 0))
        screen.blit(self.text, self.textRect)

src/sprite/background.py
Guillaume-Crand/FDC-project
import pygame


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        self.SIZE = 200
        self.STEP = 50
        self.COLOR = pygame.Color((255, 0, 0))

        self.surf = pygame.Surface((self.SIZE, self.SIZE))
        self.surf.fill(self.COLOR)
        self.rect = self.surf.get_rect()

    def blit(self, screen: pygame.Surface):
        screen.blit(self.surf, self.rect)

    def move(self, x: int, y: int):
        self.rect.move_ip(x * self.STEP, y * self.STEP)

src/sprite/player.py
Guillaume-Crand/FDC-project