Browse Source

extra hry

master
Jakub Škrabánek 7 days ago
parent
commit
b975689810
  1. 94
      36_pygame_intro/extra_01_chytani_jablek.py
  2. 122
      36_pygame_intro/extra_02_pong_dva_hraci.py
  3. 119
      36_pygame_intro/extra_03_skakacka_gravitace.py
  4. 133
      36_pygame_intro/extra_04_vesmirna_strilecka.py

94
36_pygame_intro/extra_01_chytani_jablek.py

@ -0,0 +1,94 @@
import pygame
import sys
import random
pygame.init()
SIRKA = 800
VYSKA = 600
okno = pygame.display.set_mode((SIRKA, VYSKA))
pygame.display.set_caption("Chytání jablek")
# Barvy
CERNA = (0, 0, 0)
ZELENA = (0, 255, 0) # Hráč
CERVENA = (255, 0, 0) # Jablko
BILA = (255, 255, 255) # Text
hodiny = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 36)
# Hráč (košík)
hrac_sirka = 100
hrac_vyska = 20
hrac_x = SIRKA // 2 - hrac_sirka // 2
hrac_y = VYSKA - 40
rychlost_hrace = 8
# Jablko
jablko_velikost = 30
jablko_x = random.randint(0, SIRKA - jablko_velikost)
jablko_y = -jablko_velikost
rychlost_jablka = 5
skore = 0
zivoty = 3
bezime = True
while bezime:
for udalost in pygame.event.get():
if udalost.type == pygame.QUIT:
bezime = False
if zivoty > 0:
# Pohyb hráče
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 jablka padajícího dolů
jablko_y += rychlost_jablka
# Kolize (chycení jablka)
# Jednoduchá kontrola obdélníků (AABB kolize)
hrac_rect = pygame.Rect(hrac_x, hrac_y, hrac_sirka, hrac_vyska)
jablko_rect = pygame.Rect(jablko_x, jablko_y, jablko_velikost, jablko_velikost)
if hrac_rect.colliderect(jablko_rect):
skore += 1
rychlost_jablka += 0.2 # Mírné zrychlování hry
jablko_y = -jablko_velikost
jablko_x = random.randint(0, SIRKA - jablko_velikost)
# Jablko spadlo na zem
elif jablko_y > VYSKA:
zivoty -= 1
jablko_y = -jablko_velikost
jablko_x = random.randint(0, SIRKA - jablko_velikost)
# Vykreslování
okno.fill(CERNA)
if zivoty > 0:
# Vykreslení hráče
pygame.draw.rect(okno, ZELENA, (hrac_x, hrac_y, hrac_sirka, hrac_vyska))
# Vykreslení jablka
pygame.draw.rect(okno, CERVENA, (jablko_x, jablko_y, jablko_velikost, jablko_velikost))
# Texty - Skóre a Životy
text_skore = font.render(f"Skóre: {skore}", True, BILA)
text_zivoty = font.render(f"Životy: {zivoty}", True, BILA)
okno.blit(text_skore, (10, 10))
okno.blit(text_zivoty, (SIRKA - 150, 10))
else:
# Konec hry
text_konec = font.render(f"KONEC HRY! Tvé skóre: {skore}", True, CERVENA)
okno.blit(text_konec, (SIRKA//2 - 180, VYSKA//2))
pygame.display.flip()
hodiny.tick(60)
pygame.quit()
sys.exit()

122
36_pygame_intro/extra_02_pong_dva_hraci.py

@ -0,0 +1,122 @@
import pygame
import sys
pygame.init()
SIRKA = 800
VYSKA = 600
okno = pygame.display.set_mode((SIRKA, VYSKA))
pygame.display.set_caption("Pong - Plná Hra")
BILA = (255, 255, 255)
CERNA = (0, 0, 0)
ZELENA = (0, 255, 0)
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
palka_sirka = 15
palka_vyska = 100
rychlost_palky = 7
micek_velikost = 15
# Funkce pro reset hry
def reset_hry():
global hrac1_y, hrac2_y, skore1, skore2, micek_x, micek_y, micek_rychlost_x, micek_rychlost_y
hrac1_y = VYSKA // 2 - palka_vyska // 2
hrac2_y = VYSKA // 2 - palka_vyska // 2
skore1 = 0
skore2 = 0
micek_x = SIRKA // 2 - micek_velikost // 2
micek_y = VYSKA // 2 - micek_velikost // 2
micek_rychlost_x = 5
micek_rychlost_y = 5
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
klavesy = pygame.key.get_pressed()
if stav == STAV_HRA:
if klavesy[pygame.K_w] and hrac1_y > 0:
hrac1_y -= rychlost_palky
if klavesy[pygame.K_s] and hrac1_y < VYSKA - palka_vyska:
hrac1_y += rychlost_palky
if klavesy[pygame.K_UP] and hrac2_y > 0:
hrac2_y -= rychlost_palky
if klavesy[pygame.K_DOWN] and hrac2_y < VYSKA - palka_vyska:
hrac2_y += rychlost_palky
micek_x += micek_rychlost_x
micek_y += micek_rychlost_y
if micek_y <= 0 or micek_y >= VYSKA - micek_velikost:
micek_rychlost_y *= -1
rect_micek = pygame.Rect(micek_x, micek_y, micek_velikost, micek_velikost)
rect_hrac1 = pygame.Rect(30, hrac1_y, palka_sirka, palka_vyska)
rect_hrac2 = pygame.Rect(SIRKA - 30 - palka_sirka, hrac2_y, palka_sirka, palka_vyska)
if rect_micek.colliderect(rect_hrac1) or rect_micek.colliderect(rect_hrac2):
micek_rychlost_x *= -1.1 # Zrychlení po odrazu
if micek_x < 0:
skore2 += 1
micek_x, micek_y = SIRKA // 2, VYSKA // 2
micek_rychlost_x = 5
elif micek_x > SIRKA:
skore1 += 1
micek_x, micek_y = SIRKA // 2, VYSKA // 2
micek_rychlost_x = -5
# Podmínka výhry
if skore1 >= 5 or skore2 >= 5:
stav = STAV_KONEC
# Vykreslování
okno.fill(CERNA)
if stav == STAV_MENU:
nadpis = font_velky.render("PONG", True, BILA)
navod = font_maly.render("Stiskni MEZERNÍK pro start", True, ZELENA)
okno.blit(nadpis, (SIRKA//2 - nadpis.get_width()//2, 200))
okno.blit(navod, (SIRKA//2 - navod.get_width()//2, 300))
elif stav == STAV_HRA:
pygame.draw.aaline(okno, BILA, (SIRKA // 2, 0), (SIRKA // 2, VYSKA))
pygame.draw.rect(okno, BILA, rect_hrac1)
pygame.draw.rect(okno, BILA, rect_hrac2)
pygame.draw.ellipse(okno, BILA, rect_micek)
text_skore = font_velky.render(f"{skore1} {skore2}", True, BILA)
okno.blit(text_skore, (SIRKA // 2 - text_skore.get_width() // 2, 20))
elif stav == STAV_KONEC:
vitez = "Hráč 1" if skore1 >= 5 else "Hráč 2"
text_vitez = font_velky.render(f"{vitez} vyhrál!", True, ZELENA)
text_restart = font_maly.render("Stiskni MEZERNÍK pro novou hru", True, BILA)
okno.blit(text_vitez, (SIRKA//2 - text_vitez.get_width()//2, 200))
okno.blit(text_restart, (SIRKA//2 - text_restart.get_width()//2, 300))
pygame.display.flip()
hodiny.tick(60)
pygame.quit()
sys.exit()

119
36_pygame_intro/extra_03_skakacka_gravitace.py

@ -0,0 +1,119 @@
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()

133
36_pygame_intro/extra_04_vesmirna_strilecka.py

@ -0,0 +1,133 @@
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()
Loading…
Cancel
Save