import pygame import sys import random pygame.init() SIRKA = 800 VYSKA = 600 okno = pygame.display.set_mode((SIRKA, VYSKA)) pygame.display.set_caption("Vesmírná střílečka - Plná Hra") CERNA = (0, 0, 0) ZELENA = (0, 255, 0) CERVENA = (255, 0, 0) ZLUCA = (255, 255, 0) BILA = (255, 255, 255) hodiny = pygame.time.Clock() font_velky = pygame.font.SysFont("Arial", 50) font_maly = pygame.font.SysFont("Arial", 30) STAV_MENU = 0 STAV_HRA = 1 STAV_KONEC = 2 stav = STAV_MENU hrac_sirka = 40 hrac_vyska = 40 rychlost_hrace = 6 rychlost_projektilu = 10 SPAWN_NEPRITELE = pygame.USEREVENT + 1 # Funkce pro reset def reset_hry(): global hrac_x, hrac_y, projektily, nepratele, skore, rychlost_nepritele, spawn_cas hrac_x = SIRKA // 2 - hrac_sirka // 2 hrac_y = VYSKA - 60 projektily = [] nepratele = [] skore = 0 rychlost_nepritele = 2.0 spawn_cas = 1000 pygame.time.set_timer(SPAWN_NEPRITELE, spawn_cas) reset_hry() 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_SPACE: # Výstřel projektil_x = hrac_x + hrac_sirka // 2 - 5 projektily.append(pygame.Rect(projektil_x, hrac_y, 10, 20)) if udalost.type == SPAWN_NEPRITELE and stav == STAV_HRA: n_x = random.randint(0, SIRKA - 40) nepratele.append(pygame.Rect(n_x, -40, 40, 40)) # Postupné zrychlování hry a rychlejší spawnování rychlost_nepritele += 0.05 if spawn_cas > 300: spawn_cas -= 20 pygame.time.set_timer(SPAWN_NEPRITELE, spawn_cas) if stav == STAV_HRA: klavesy = pygame.key.get_pressed() if klavesy[pygame.K_LEFT] and hrac_x > 0: hrac_x -= rychlost_hrace if klavesy[pygame.K_RIGHT] and hrac_x < SIRKA - hrac_sirka: hrac_x += rychlost_hrace # Pohyb a mazání projektilů for p in projektily[:]: p.y -= rychlost_projektilu if p.y < 0: projektily.remove(p) # Pohyb nepřátel a kolize hrac_rect = pygame.Rect(hrac_x, hrac_y, hrac_sirka, hrac_vyska) for n in nepratele[:]: n.y += int(rychlost_nepritele) # Pokud nepřítel proletí až dolů nebo trefí hráče = GAME OVER if n.y > VYSKA or n.colliderect(hrac_rect): stav = STAV_KONEC # Kolize se střelami for p in projektily[:]: if p.colliderect(n): if p in projektily: projektily.remove(p) if n in nepratele: nepratele.remove(n) skore += 10 break # Vykreslování okno.fill(CERNA) if stav == STAV_MENU: text = font_velky.render("VESMÍRNÁ STŘÍLEČKA", True, ZLUCA) start = font_maly.render("Stiskni MEZERNÍK. Střílíš mezerníkem, pohyb šipkami.", True, BILA) 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, ZELENA, hrac_rect) for p in projektily: pygame.draw.rect(okno, ZLUCA, p) for n in nepratele: pygame.draw.rect(okno, CERVENA, n) skore_text = font_maly.render(f"Skóre: {skore}", True, BILA) okno.blit(skore_text, (10, 10)) elif stav == STAV_KONEC: text = font_velky.render("ZEMŘEL JSI!", True, CERVENA) skore_text = font_velky.render(f"Tvé skóre: {skore}", True, BILA) restart = font_maly.render("Stiskni MEZERNÍK pro novou hru", True, ZLUCA) okno.blit(text, (SIRKA//2 - text.get_width()//2, 150)) okno.blit(skore_text, (SIRKA//2 - skore_text.get_width()//2, 250)) okno.blit(restart, (SIRKA//2 - restart.get_width()//2, 350)) pygame.display.flip() hodiny.tick(60) pygame.quit() sys.exit()