import pygame import sys import random pygame.init() SIRKA = 800 VYSKA = 600 okno = pygame.display.set_mode((SIRKA, VYSKA)) pygame.display.set_caption("Skákačka - Plná Hra") BILA = (255, 255, 255) CERNA = (0, 0, 0) MODRA = (50, 150, 255) ZELENA = (50, 200, 50) CERVENA = (255, 0, 0) hodiny = pygame.time.Clock() font = pygame.font.SysFont("Arial", 40) font_maly = pygame.font.SysFont("Arial", 25) STAV_MENU = 0 STAV_HRA = 1 STAV_KONEC = 2 stav = STAV_MENU # Nastavení hráče a fyziky podlaha_y = VYSKA - 50 hrac_sirka = 40 hrac_vyska = 40 gravitace = 0.6 sila_skoku = -12 # Funkce pro reset def reset_hry(): global hrac_x, hrac_y, rychlost_y, na_zemi, prekazky, skore, rychlost_hry hrac_x = 100 hrac_y = podlaha_y - hrac_vyska rychlost_y = 0 na_zemi = True prekazky = [] skore = 0 rychlost_hry = 6 reset_hry() SPAWN_PREKAZKY = pygame.USEREVENT + 1 pygame.time.set_timer(SPAWN_PREKAZKY, 1500) bezime = True while bezime: for udalost in pygame.event.get(): if udalost.type == pygame.QUIT: bezime = False if udalost.type == pygame.KEYDOWN: if stav == STAV_MENU and udalost.key == pygame.K_SPACE: stav = STAV_HRA elif stav == STAV_KONEC and udalost.key == pygame.K_SPACE: reset_hry() stav = STAV_HRA elif stav == STAV_HRA and udalost.key == pygame.K_UP and na_zemi: rychlost_y = sila_skoku na_zemi = False if udalost.type == SPAWN_PREKAZKY and stav == STAV_HRA: prekazky.append(pygame.Rect(SIRKA, podlaha_y - 40, 30, 40)) rychlost_hry += 0.1 # Hra se pomalu zrychluje if stav == STAV_HRA: # Fyzika rychlost_y += gravitace hrac_y += rychlost_y if hrac_y + hrac_vyska >= podlaha_y: hrac_y = podlaha_y - hrac_vyska rychlost_y = 0 na_zemi = True hrac_rect = pygame.Rect(hrac_x, hrac_y, hrac_sirka, hrac_vyska) # Prekazky for p in prekazky[:]: p.x -= int(rychlost_hry) if p.colliderect(hrac_rect): stav = STAV_KONEC if p.x < -30: prekazky.remove(p) skore += 1 # Vykreslování okno.fill(MODRA) pygame.draw.rect(okno, ZELENA, (0, podlaha_y, SIRKA, VYSKA - podlaha_y)) if stav == STAV_MENU: text = font.render("SKÁKAČKA", True, BILA) start = font_maly.render("Stiskni MEZERNÍK. Skáče se šipkou NAHORU.", True, CERNA) okno.blit(text, (SIRKA//2 - text.get_width()//2, 200)) okno.blit(start, (SIRKA//2 - start.get_width()//2, 300)) elif stav == STAV_HRA: pygame.draw.rect(okno, CERNA, hrac_rect) for p in prekazky: pygame.draw.rect(okno, CERVENA, p) skore_text = font.render(f"Skóre: {skore}", True, CERNA) okno.blit(skore_text, (10, 10)) elif stav == STAV_KONEC: text = font.render("KONEC HRY", True, CERVENA) skore_text = font.render(f"Dosažené skóre: {skore}", True, BILA) restart = font_maly.render("Stiskni MEZERNÍK pro novou hru", True, CERNA) okno.blit(text, (SIRKA//2 - text.get_width()//2, 150)) okno.blit(skore_text, (SIRKA//2 - skore_text.get_width()//2, 220)) okno.blit(restart, (SIRKA//2 - restart.get_width()//2, 300)) pygame.display.flip() hodiny.tick(60) pygame.quit() sys.exit()