Skip to content

XynixDev/jj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

import pygame import sys import random

pygame.init()

------------------ Inicjalizacja ------------------

WIDTH, HEIGHT = 800, 600 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Geometry Dash Clone") clock = pygame.time.Clock() FPS = 60

------------------ Kolory ------------------

WHITE = (255, 255, 255) BLACK = (0, 0, 0) GRAY = (100, 100, 100) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255)

------------------ Ustawienia gry ------------------

game_settings = { "white_style": False, # False: ciemny styl, True: jasny styl "player_skin": "blue", # Dostępne skórki: "blue", "red", "green" }

------------------ Inicjalizacja czcionki ------------------

font = pygame.font.SysFont("Arial", 32)

def draw_text(surface, text, color, rect, font, aa=True, bkg=None): """Rysuje wyśrodkowany tekst w podanym prostokącie.""" text_surface = font.render(text, aa, color, bkg) text_rect = text_surface.get_rect(center=rect.center) surface.blit(text_surface, text_rect)

------------------ Klasa Menu ------------------

class Menu: def init(self): self.options = ["Start Game", "Settings", "Quit"] self.selected = 0

def draw(self, surface):
    surface.fill(WHITE if game_settings["white_style"] else BLACK)
    title = font.render("Geometry Dash Clone", True, BLACK if game_settings["white_style"] else WHITE)
    surface.blit(title, (WIDTH//2 - title.get_width()//2, 100))
    for i, option in enumerate(self.options):
        color = RED if i == self.selected else (BLACK if game_settings["white_style"] else WHITE)
        option_text = font.render(option, True, color)
        surface.blit(option_text, (WIDTH//2 - option_text.get_width()//2, 250 + i * 50))

def update(self, event):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            self.selected = (self.selected - 1) % len(self.options)
        elif event.key == pygame.K_DOWN:
            self.selected = (self.selected + 1) % len(self.options)
        elif event.key == pygame.K_RETURN:
            return self.options[self.selected]
    return None

------------------ Klasa Ustawień ------------------

class Settings: def init(self): self.options = ["Toggle White Style", "Change Skin", "Back"] self.selected = 0 self.skins = ["blue", "red", "green"] self.skin_index = self.skins.index(game_settings["player_skin"])

def draw(self, surface):
    surface.fill(WHITE if game_settings["white_style"] else BLACK)
    title = font.render("Settings", True, BLACK if game_settings["white_style"] else WHITE)
    surface.blit(title, (WIDTH//2 - title.get_width()//2, 100))
    for i, option in enumerate(self.options):
        color = RED if i == self.selected else (BLACK if game_settings["white_style"] else WHITE)
        option_text = font.render(option, True, color)
        surface.blit(option_text, (WIDTH//2 - option_text.get_width()//2, 250 + i * 50))
    skin_text = font.render("Current Skin: " + game_settings["player_skin"], True, GREEN)
    surface.blit(skin_text, (WIDTH//2 - skin_text.get_width()//2, 400))

def update(self, event):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            self.selected = (self.selected - 1) % len(self.options)
        elif event.key == pygame.K_DOWN:
            self.selected = (self.selected + 1) % len(self.options)
        elif event.key == pygame.K_RETURN:
            option = self.options[self.selected]
            if option == "Toggle White Style":
                game_settings["white_style"] = not game_settings["white_style"]
            elif option == "Change Skin":
                self.skin_index = (self.skin_index + 1) % len(self.skins)
                game_settings["player_skin"] = self.skins[self.skin_index]
            elif option == "Back":
                return "back"
    return None

------------------ Klasa Pause Menu ------------------

class PauseMenu: def init(self): self.options = ["Resume", "Restart", "Settings", "Main Menu"] self.selected = 0

def draw(self, surface):
    # Rysujemy półprzezroczyste tło
    overlay = pygame.Surface((WIDTH, HEIGHT))
    overlay.set_alpha(150)
    overlay.fill(BLACK if not game_settings["white_style"] else WHITE)
    surface.blit(overlay, (0, 0))
    
    title = font.render("PAUSED", True, RED)
    surface.blit(title, (WIDTH//2 - title.get_width()//2, 100))
    for i, option in enumerate(self.options):
        color = RED if i == self.selected else (BLACK if game_settings["white_style"] else WHITE)
        option_text = font.render(option, True, color)
        surface.blit(option_text, (WIDTH//2 - option_text.get_width()//2, 200 + i * 50))

def update(self, event):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            self.selected = (self.selected - 1) % len(self.options)
        elif event.key == pygame.K_DOWN:
            self.selected = (self.selected + 1) % len(self.options)
        elif event.key == pygame.K_RETURN:
            return self.options[self.selected]
    return None

------------------ Klasa Gracza ------------------

class Player: def init(self): self.width = 50 self.height = 50 self.x = 100 self.y = HEIGHT - self.height - 100 self.vel_y = 0 self.jump_power = -15 self.gravity = 0.8 self.on_ground = True # Miejsce na załadowanie tekstury gracza self.texture = None # Możesz użyć pygame.image.load("sciezka_do_pliku.png")

def update(self):
    self.vel_y += self.gravity
    self.y += self.vel_y
    if self.y >= HEIGHT - self.height - 100:
        self.y = HEIGHT - self.height - 100
        self.vel_y = 0
        self.on_ground = True

def jump(self):
    if self.on_ground:
        self.vel_y = self.jump_power
        self.on_ground = False

def draw(self, surface):
    # Wybór koloru/skórki
    color = BLUE
    if game_settings["player_skin"] == "red":
        color = RED
    elif game_settings["player_skin"] == "green":
        color = GREEN
    pygame.draw.rect(surface, color, (self.x, self.y, self.width, self.height))
    # Jeśli posiadasz teksturę, możesz ją wyświetlić:
    # if self.texture:
    #     surface.blit(self.texture, (self.x, self.y))

------------------ Klasa Przeszkody ------------------

class Obstacle: def init(self, x): self.width = random.randint(20, 70) self.height = random.randint(20, 70) self.x = x self.y = HEIGHT - self.height - 100 self.speed = 5 # Miejsce na załadowanie tekstury przeszkody self.texture = None # Możesz użyć pygame.image.load("sciezka_do_pliku.png")

def update(self):
    self.x -= self.speed

def draw(self, surface):
    pygame.draw.rect(surface, GRAY, (self.x, self.y, self.width, self.height))
    # Jeśli posiadasz teksturę, możesz ją wyświetlić:
    # if self.texture:
    #     surface.blit(self.texture, (self.x, self.y))

------------------ Klasa Gry ------------------

class Game: def init(self): self.player = Player() self.obstacles = [] self.spawn_timer = 0 self.score = 0 self.paused = False # Inicjalizacja przykładowych dźwięków # self.jump_sound = pygame.mixer.Sound("jump.wav") # self.collision_sound = pygame.mixer.Sound("collision.wav")

def update(self):
    if not self.paused:
        self.player.update()
        for obs in self.obstacles:
            obs.update()
        self.obstacles = [obs for obs in self.obstacles if obs.x + obs.width > 0]
        self.spawn_timer += 1
        if self.spawn_timer > FPS * 2:
            self.obstacles.append(Obstacle(WIDTH))
            self.spawn_timer = 0
        # Wykrywanie kolizji
        player_rect = pygame.Rect(self.player.x, self.player.y, self.player.width, self.player.height)
        for obs in self.obstacles:
            obs_rect = pygame.Rect(obs.x, obs.y, obs.width, obs.height)
            if player_rect.colliderect(obs_rect):
                # pygame.mixer.Sound.play(self.collision_sound)
                self.reset()
        self.score += 1

def reset(self):
    self.player = Player()
    self.obstacles = []
    self.spawn_timer = 0
    self.score = 0

def draw(self, surface):
    surface.fill(WHITE if game_settings["white_style"] else BLACK)
    self.player.draw(surface)
    for obs in self.obstacles:
        obs.draw(surface)
    score_text = font.render("Score: " + str(self.score), True, BLACK if game_settings["white_style"] else WHITE)
    surface.blit(score_text, (10, 10))
    if self.paused:
        pause_overlay = PauseMenu()
        pause_overlay.draw(surface)

def handle_event(self, event):
    if not self.paused:
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                self.player.jump()
                # pygame.mixer.Sound.play(self.jump_sound)
    # Naciśnięcie ESC zawsze przełącza pauzę
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
        self.paused = not self.paused

------------------ Główna Pętla Gry ------------------

def main(): state = "menu" # możliwe stany: menu, settings, game, pause menu = Menu() settings = Settings() game = Game() pause_menu = PauseMenu() # Używany, gdy gra jest w pauzie

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        
        if state == "menu":
            result = menu.update(event)
            if result == "Start Game":
                state = "game"
            elif result == "Settings":
                state = "settings"
            elif result == "Quit":
                pygame.quit()
                sys.exit()
        
        elif state == "settings":
            result = settings.update(event)
            if result == "back":
                state = "menu"
        
        elif state == "game":
            # Przekazujemy zdarzenia do gry
            game.handle_event(event)
            # Jeśli gra jest w pauzie, obsługujemy menu pauzy
            if game.paused:
                result = pause_menu.update(event)
                if result == "Resume":
                    game.paused = False
                elif result == "Restart":
                    game.reset()
                    game.paused = False
                elif result == "Settings":
                    state = "settings"
                    game.paused = False
                elif result == "Main Menu":
                    state = "menu"
                    game.reset()
                    game.paused = False

    if state == "menu":
        menu.draw(WIN)
    elif state == "settings":
        settings.draw(WIN)
    elif state == "game":
        if not game.paused:
            game.update()
        game.draw(WIN)
    
    pygame.display.flip()
    clock.tick(FPS)

if name == "main": main()

Releases

No releases published

Packages

No packages published