bird.py:
"""
Klasa Bird – ptak wystrzeliwany z procy.
"""
import pygame
import pymunk
import os
class Bird:
"""
Klasa reprezentująca ptaka.
"""
def init(self, x, y, space):
self.x = x
self.y = y
self.radius = 20
self.launched = False
self.destroyed = False
self.body = pymunk.Body(1, pymunk.moment_for_circle(1, 0, self.radius))
self.body.position = x, y
self.shape = pymunk.Circle(self.body, self.radius)
self.shape.elasticity = 0.6
self.shape.friction = 0.5
space.add(self.body, self.shape)
self.rect = pygame.Rect(x-self.radius, y-self.radius, self.radius2, self.radius2)
self.color = (255, 0, 0)
self.image = pygame.image.load(os.path.join('assets', 'red_bird.png')).convert_alpha()
self.image = pygame.transform.smoothscale(self.image, (self.radius2, self.radius2))
def set_position(self, pos):
self.body.position = pos
self.rect.center = pos
def launch(self, pos):
if not self.launched:
dx = self.x - pos[0]
dy = self.y - pos[1]
# Ogranicz długość wektora (maksymalna siła)
max_dist = 90
dist = (dx**2 + dy**2)**0.5
if dist > max_dist:
scale = max_dist / dist
dx *= scale
dy *= scale
self.body.apply_impulse_at_local_point((dx*12, dy*12))
self.launched = True
def update(self):
self.rect.center = self.body.position
if self.body.position.y > 1000:
self.destroyed = True
def draw(self, screen):
pos = (int(self.body.position.x), int(self.body.position.y))
rect = self.image.get_rect(center=pos)
screen.blit(self.image, rect)
class PlayerPig(Bird):
"""
Wystrzeliwana świnka gracza – zachowuje się jak ptak, ale wygląda jak świnka i nie koliduje z innymi świnkami gracza.
"""
def init(self, x, y, space, is_first=False):
super().init(x, y, space)
self.radius = 18
self.color = (0, 200, 0)
self.body = pymunk.Body(1, pymunk.moment_for_circle(1, 0, self.radius))
self.body.position = x, y
self.shape = pymunk.Circle(self.body, self.radius)
self.shape.elasticity = 0.6
self.shape.friction = 0.5
self.shape.collision_type = 3 # osobny collision_type dla świnek gracza
self.shape.filter = pymunk.ShapeFilter(categories=0b100, mask=0b01|0b10) # nie koliduje z innymi świnkami gracza
space.add(self.body, self.shape)
self.rect = pygame.Rect(x-self.radius, y-self.radius, self.radius2, self.radius2)
self.launched = False
self.destroyed = False
self.is_first = is_first
self.image = pygame.image.load(os.path.join('assets', 'pig.png')).convert_alpha()
self.image = pygame.transform.smoothscale(self.image, (self.radius2, self.radius2))
def draw(self, screen):
# Rysuj katapultę jeśli to pierwszy ptak/świnka gracza
if hasattr(self, 'is_first') and self.is_first:
# Podstawa katapulty
base_rect = pygame.Rect(self.x-30, self.y+18, 60, 18)
pygame.draw.rect(screen, (120, 80, 40), base_rect, border_radius=8)
# Ramię katapulty
pygame.draw.line(screen, (100, 60, 30), (self.x, self.y+18), (self.x, self.y-30), 10)
# Koszyk katapulty
pygame.draw.ellipse(screen, (160, 120, 60), (self.x-22, self.y-38, 44, 28))
screen.blit(self.image, self.image.get_rect(center=(self.x, self.y-24)))
pos = (int(self.body.position.x), int(self.body.position.y))
rect = self.image.get_rect(center=pos)
screen.blit(self.image, rect)
class FastBird(Bird):
"""Szybki ptak – mniejszy, lżejszy, leci szybciej, mniejsza siła kolizji."""
def init(self, x, y, space):
super().init(x, y, space)
self.radius = 16
self.color = (0, 180, 255)
self.body = pymunk.Body(0.5, pymunk.moment_for_circle(0.5, 0, self.radius))
self.body.position = x, y
self.shape = pymunk.Circle(self.body, self.radius)
self.shape.elasticity = 0.7
self.shape.friction = 0.4
space.add(self.body, self.shape)
self.rect = pygame.Rect(x-self.radius, y-self.radius, self.radius2, self.radius2)
def launch(self, pos):
if not self.launched:
dx = self.x - pos[0]
dy = self.y - pos[1]
max_dist = 90
dist = (dx2 + dy2)**0.5
if dist > max_dist:
scale = max_dist / dist
dx = scale
dy = scale
self.body.apply_impulse_at_local_point((dx18, dy18))
self.launched = True
class BombBird(Bird):
"""Wybuchowy ptak – po uderzeniu wybucha, zadaje duże obrażenia w promieniu."""
def init(self, x, y, space):
super().init(x, y, space)
self.radius = 22
self.color = (80, 80, 80)
self.body = pymunk.Body(2, pymunk.moment_for_circle(2, 0, self.radius))
self.body.position = x, y
self.shape = pymunk.Circle(self.body, self.radius)
self.shape.elasticity = 0.5
self.shape.friction = 0.6
space.add(self.body, self.shape)
self.rect = pygame.Rect(x-self.radius, y-self.radius, self.radius2, self.radius2)
self.exploded = False
def update(self):
super().update()
if not self.exploded and self.launched and self.body.velocity.length < 10 and not self.destroyed:
self.explode()
def explode(self):
self.exploded = True
self.destroyed = True
# Efekt wybuchu i obrażenia w promieniu (obsłużone w Level.update)
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.body.position.x), int(self.body.position.y)), self.radius)
pygame.draw.circle(screen, (255, 80, 0), (int(self.body.position.x), int(self.body.position.y)), self.radius-8, 2)
level.py:
"""
Moduł obsługujący poziomy gry AngryFlip.
"""
import pygame
import pymunk
import json
import os
from bird import Bird, PlayerPig, FastBird, BombBird
from pig import Pig, HeavyPig, ExplodingPig
from block import Block, ExplodingBlock, SpringBlock, IceBlock, MetalBlock
from effects import ParticleManager
class Level:
"""
Klasa reprezentująca pojedynczy poziom gry.
"""
def init(self, filename, game):
self.game = game
self.filename = filename
self.space = pymunk.Space()
self.space.gravity = (0, 900)
self.objects = []
self.birds = []
self.pigs = []
self.blocks = []
self.wind = 0
self.ground_y = 500
self.load_level()
# Dodaj ziemię (statyczna platforma)
static_body = self.space.static_body
ground_shape = pymunk.Segment(static_body, (0, self.ground_y), (1024, self.ground_y), 8)
ground_shape.elasticity = 0.8
ground_shape.friction = 1.0
self.space.add(ground_shape)
self.ground_shape = ground_shape
self.selected_bird = None
self.dragging = False
self.score = 0
self.font = pygame.font.SysFont('arial', 24)
self.paused = False
self.particle_manager = ParticleManager()
def load_level(self):
with open(self.filename, 'r', encoding='utf-8') as f:
data = json.load(f)
self.wind = data.get('wind', 0)
self.star_thresholds = data.get('star_thresholds', [1000, 2000, 3000])
birds_count = data.get('birds', 3)
pig_bird_count = 0
for i in range(birds_count):
bird_type = None
bird_skin = None
for obj in data['objects']:
if obj['type'] == 'bird' and obj.get('order', 0) == i:
bird_type = obj.get('bird_type')
bird_skin = obj.get('skin')
break
if bird_type == 'fast':
bird = FastBird(120+40*i, 420, self.space)
elif bird_type == 'bomb':
bird = BombBird(120+40*i, 420, self.space)
elif bird_skin == 'pig':
bird = PlayerPig(120+40*i, 420, self.space, is_first=(pig_bird_count==0))
pig_bird_count += 1
else:
bird = Bird(120+40*i, 420, self.space)
self.birds.append(bird)
for obj in data['objects']:
if obj['type'] == 'pig':
pig_type = obj.get('pig_type')
if pig_type == 'heavy':
pig = HeavyPig(obj['x'], obj['y'], obj.get('health', 100), self.space)
elif pig_type == 'exploding':
pig = ExplodingPig(obj['x'], obj['y'], obj.get('health', 100), self.space)
else:
pig = Pig(obj['x'], obj['y'], obj.get('health', 100), self.space)
self.pigs.append(pig)
elif obj['type'] == 'block':
block_type = obj.get('block_type')
if block_type == 'exploding':
block = ExplodingBlock(
obj['x'], obj['y'], obj.get('angle', 0), obj.get('health', 50), self.space, obj.get('shape', 'rect_h'))
elif block_type == 'spring':
block = SpringBlock(
obj['x'], obj['y'], obj.get('angle', 0), obj.get('health', 50), self.space, obj.get('shape', 'rect_h'))
elif block_type == 'ice':
block = IceBlock(
obj['x'], obj['y'], obj.get('angle', 0), obj.get('health', 50), self.space, obj.get('shape', 'rect_h'))
elif block_type == 'metal':
block = MetalBlock(
obj['x'], obj['y'], obj.get('angle', 0), obj.get('health', 50), self.space, obj.get('shape', 'rect_h'))
else:
block = Block(
obj['x'], obj['y'], obj.get('angle', 0), obj.get('health', 50), self.space, obj.get('shape', 'rect_h'))
self.blocks.append(block)
self.objects = self.birds + self.pigs + self.blocks
def run(self):
running = True
paused = False
while running and self.game.state == "game":
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
paused = True
if event.key == pygame.K_F5:
paused = True
elif not paused and event.type == pygame.MOUSEBUTTONDOWN:
self.handle_mouse_down(event.pos)
elif not paused and event.type == pygame.MOUSEBUTTONUP:
self.handle_mouse_up(event.pos)
elif not paused and event.type == pygame.MOUSEMOTION:
self.handle_mouse_motion(event.pos)
if paused:
self.draw()
self.draw_pause_menu()
pygame.display.flip()
paused = not self.handle_pause_menu_events()
self.game.clock.tick(60)
continue
self.update()
self.draw()
pygame.display.flip()
self.game.clock.tick(60)
if all(pig.destroyed for pig in self.pigs):
pygame.time.wait(1000)
self.save_stars()
self.game.next_level()
running = False
def handle_mouse_down(self, pos):
for bird in self.birds:
if not bird.launched and bird.rect.collidepoint(pos):
self.selected_bird = bird
self.dragging = True
break
def handle_mouse_up(self, pos):
if self.selected_bird and self.dragging:
self.selected_bird.launch(pos)
self.dragging = False
self.selected_bird = None
def handle_mouse_motion(self, pos):
if self.selected_bird and self.dragging:
self.selected_bird.set_position(pos)
def update(self):
self.space.step(1/60)
for obj in self.objects:
obj.update()
# Efekty wybuchów i obrażenia w promieniu
for block in self.blocks:
if hasattr(block, 'exploded') and block.exploded:
self.explode_at(block.body.position, radius=70, damage=40)
block.exploded = False
for pig in self.pigs:
if hasattr(pig, 'exploded') and pig.exploded:
self.explode_at(pig.body.position, radius=70, damage=40)
pig.exploded = False
for bird in self.birds:
if hasattr(bird, 'exploded') and bird.exploded:
self.explode_at(bird.body.position, radius=70, damage=60)
bird.exploded = False
# Kolizje: uszkodzenia bloków i świnek
for block in self.blocks:
for pig in self.pigs:
if not block.destroyed and not pig.destroyed:
dist = ((block.body.position.x - pig.body.position.x)**2 + (block.body.position.y - pig.body.position.y)**2)**0.5
if dist < 60:
block.take_damage(10)
pig.take_damage(10)
# Prosty system uszkodzeń bloków przy dużej prędkości
for block in self.blocks:
if not block.destroyed:
v = block.body.velocity.length
if v > 200:
block.take_damage(int(v/10))
# Usuwanie zniszczonych bloków i świnek
self.blocks = [b for b in self.blocks if not b.destroyed]
self.pigs = [p for p in self.pigs if not p.destroyed]
self.objects = self.birds + self.pigs + self.blocks
self.particle_manager.update()
def draw(self):
self.game.screen.fill((135, 206, 235))
# Rysuj ziemię
pygame.draw.rect(self.game.screen, (60, 180, 60), (0, self.ground_y, 1024, 600-self.ground_y))
# Podgląd trajektorii ptaka
if self.selected_bird and self.dragging:
self.draw_trajectory_preview(self.selected_bird)
for obj in self.objects:
obj.draw(self.game.screen)
# HUD: liczba ptaków i świnek
birds_left = len([b for b in self.birds if not b.launched and not b.destroyed])
pigs_left = len([p for p in self.pigs if not p.destroyed])
birds_text = self.font.render(f"Ptaki: {birds_left}", True, (0,0,0))
pigs_text = self.font.render(f"Świnki: {pigs_left}", True, (0,0,0))
self.game.screen.blit(birds_text, (10, 40))
self.game.screen.blit(pigs_text, (10, 70))
# Podświetlenie aktywnego ptaka
if self.selected_bird and not self.selected_bird.destroyed:
pygame.draw.circle(self.game.screen, (255,255,0), (int(self.selected_bird.body.position.x), int(self.selected_bird.body.position.y)), self.selected_bird.radius+6, 4)
score_text = self.font.render(f"Wynik: {self.score}", True, (0,0,0))
self.game.screen.blit(score_text, (10, 10))
self.particle_manager.draw(self.game.screen)
def draw_trajectory_preview(self, bird):
# Oblicz wektor siły na podstawie pozycji przeciągnięcia
start = (bird.x, bird.y)
mouse = pygame.mouse.get_pos()
dx = bird.x - mouse[0]
dy = bird.y - mouse[1]
max_dist = 90
dist = (dx**2 + dy**2)**0.5
if dist > max_dist:
scale = max_dist / dist
dx *= scale
dy *= scale
v0 = pymunk.Vec2d(dx*12, dy*12) / 1.0 # zgodnie z launch
points = []
pos = pymunk.Vec2d(bird.x, bird.y)
vel = v0 / bird.body.mass
g = 900 # grawitacja
for t in range(0, 90, 6):
t_sec = t / 60.0
x = pos.x + vel.x * t_sec
y = pos.y + vel.y * t_sec + 0.5 * g * t_sec * t_sec
if y > self.ground_y:
break
points.append((int(x), int(y)))
if len(points) > 1:
pygame.draw.lines(self.game.screen, (0,0,0), False, points, 2)
def draw_pause_menu(self):
surf = self.game.screen
w, h = surf.get_size()
overlay = pygame.Surface((w, h), pygame.SRCALPHA)
overlay.fill((0,0,0,160))
surf.blit(overlay, (0,0))
font = pygame.font.SysFont('arialblack', 38)
# Stylowe okno pauzy
pause_rect = pygame.Rect(w//2-220, h//2-180, 440, 360)
pygame.draw.rect(surf, (255,255,255), pause_rect, border_radius=32)
pygame.draw.rect(surf, (255,220,60), pause_rect, 6, border_radius=32)
label = font.render('PAUZA', True, (0,0,0))
surf.blit(label, (pause_rect.x+pause_rect.width//2-label.get_width()//2, pause_rect.y+32))
# Przycisk Kontynuuj
cont_rect = pygame.Rect(pause_rect.x+60, pause_rect.y+100, 320, 54)
pygame.draw.rect(surf, (100,220,100), cont_rect, border_radius=18)
pygame.draw.rect(surf, (0,0,0), cont_rect, 3, border_radius=18)
cont_label = font.render('KONTYNUUJ', True, (0,0,0))
surf.blit(cont_label, (cont_rect.x+cont_rect.width//2-cont_label.get_width()//2, cont_rect.y+cont_rect.height//2-cont_label.get_height()//2))
# Przycisk Osiągnięcia
ach_rect = pygame.Rect(pause_rect.x+60, pause_rect.y+170, 320, 54)
pygame.draw.rect(surf, (255,220,60), ach_rect, border_radius=18)
pygame.draw.rect(surf, (0,0,0), ach_rect, 3, border_radius=18)
ach_label = font.render('OSIĄGNIĘCIA', True, (0,0,0))
surf.blit(ach_label, (ach_rect.x+ach_rect.width//2-ach_label.get_width()//2, ach_rect.y+ach_rect.height//2-ach_label.get_height()//2))
# Przycisk Ustawienia
opt_rect = pygame.Rect(pause_rect.x+60, pause_rect.y+240, 320, 54)
pygame.draw.rect(surf, (80,180,255), opt_rect, border_radius=18)
pygame.draw.rect(surf, (0,0,0), opt_rect, 3, border_radius=18)
opt_label = font.render('USTAWIENIA', True, (0,0,0))
surf.blit(opt_label, (opt_rect.x+opt_rect.width//2-opt_label.get_width()//2, opt_rect.y+opt_rect.height//2-opt_label.get_height()//2))
# Przycisk Menu
menu_rect = pygame.Rect(pause_rect.x+60, pause_rect.y+310, 320, 54)
pygame.draw.rect(surf, (220,100,100), menu_rect, border_radius=18)
pygame.draw.rect(surf, (0,0,0), menu_rect, 3, border_radius=18)
menu_label = font.render('WYJŚCIE DO MENU', True, (0,0,0))
surf.blit(menu_label, (menu_rect.x+menu_rect.width//2-menu_label.get_width()//2, menu_rect.y+menu_rect.height//2-menu_label.get_height()//2))
self._pause_cont_rect = cont_rect
self._pause_ach_rect = ach_rect
self._pause_opt_rect = opt_rect
self._pause_menu_rect = menu_rect
def handle_pause_menu_events(self):
mouse = pygame.mouse.get_pos()
click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
return True
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return True
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
click = True
if click:
if self._pause_cont_rect.collidepoint(mouse):
return True # Kontynuuj
if self._pause_ach_rect.collidepoint(mouse):
self.show_achievements()
if self._pause_opt_rect.collidepoint(mouse):
self.show_options()
if self._pause_menu_rect.collidepoint(mouse):
self.game.back_to_menu()
return True
return False
def get_stars(self):
stars = 0
for threshold in self.star_thresholds:
if self.score >= threshold:
stars += 1
return stars
def save_stars(self):
profile_path = os.path.join("users", "profile.json")
try:
with open(profile_path, "r", encoding="utf-8") as f:
profile = json.load(f)
except Exception:
profile = {"levels": {}}
level_name = os.path.basename(self.filename)
stars = self.get_stars()
prev = profile["levels"].get(level_name, 0)
if stars > prev:
profile["levels"][level_name] = stars
with open(profile_path, "w", encoding="utf-8") as f:
json.dump(profile, f, indent=2)
def explode_at(self, pos, radius=70, damage=40):
# Efekt cząsteczek
self.particle_manager.emit(pos.x, pos.y, (255, 120, 0), amount=40)
# Obrażenia w promieniu
for pig in self.pigs:
if not pig.destroyed:
dist = ((pig.body.position.x - pos.x)**2 + (pig.body.position.y - pos.y)**2)**0.5
if dist < radius:
pig.take_damage(damage)
for block in self.blocks:
if not block.destroyed:
dist = ((block.body.position.x - pos.x)**2 + (block.body.position.y - pos.y)**2)**0.5
if dist < radius:
block.take_damage(damage)
def show_achievements(self):
surf = self.game.screen
w, h = surf.get_size()
overlay = pygame.Surface((w, h), pygame.SRCALPHA)
overlay.fill((0,0,0,180))
surf.blit(overlay, (0,0))
font = pygame.font.SysFont('arialblack', 36)
title = font.render('OSIĄGNIĘCIA', True, (0,0,0))
surf.blit(title, (w//2-title.get_width()//2, 60))
# Przykładowe osiągnięcia
achs = [
('Pierwszy poziom', True),
('Wszystkie gwiazdki', False),
('Wybuchowy mistrz', False),
('Kreator poziomów', False)
]
y = 140
for name, unlocked in achs:
color = (255,220,60) if unlocked else (180,180,180)
pygame.draw.rect(surf, color, (w//2-200, y, 400, 54), border_radius=16)
label = font.render(name, True, (0,0,0))
surf.blit(label, (w//2-200+200-label.get_width()//2, y+27-label.get_height()//2))
y += 70
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
return
if event.type == pygame.KEYDOWN or (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
waiting = False
def show_options(self):
surf = self.game.screen
w, h = surf.get_size()
overlay = pygame.Surface((w, h), pygame.SRCALPHA)
overlay.fill((0,0,0,180))
surf.blit(overlay, (0,0))
font = pygame.font.SysFont('arialblack', 36)
title = font.render('USTAWIENIA', True, (0,0,0))
surf.blit(title, (w//2-title.get_width()//2, 60))
# Głośność
vol = self.game.volume
vol_rect = pygame.Rect(w//2-180, 180, 360, 54)
pygame.draw.rect(surf, (255,255,255), vol_rect, border_radius=16)
pygame.draw.rect(surf, (0,0,0), vol_rect, 2, border_radius=16)
vol_label = font.render(f'Głośność: {vol:.2f}', True, (0,0,0))
surf.blit(vol_label, (vol_rect.x+20, vol_rect.y+10))
plus_rect = pygame.Rect(vol_rect.right-90, vol_rect.y+8, 40, 38)
minus_rect = pygame.Rect(vol_rect.right-45, vol_rect.y+8, 40, 38)
pygame.draw.rect(surf, (100,220,100), plus_rect, border_radius=10)
pygame.draw.rect(surf, (220,100,100), minus_rect, border_radius=10)
plus_label = font.render('+', True, (0,0,0))
minus_label = font.render('-', True, (0,0,0))
surf.blit(plus_label, (plus_rect.x+12, plus_rect.y+2))
surf.blit(minus_label, (minus_rect.x+12, minus_rect.y+2))
# Muzyka
music_rect = pygame.Rect(w//2-180, 260, 360, 54)
pygame.draw.rect(surf, (255,255,255), music_rect, border_radius=16)
pygame.draw.rect(surf, (0,0,0), music_rect, 2, border_radius=16)
music_label = font.render('Muzyka:', True, (0,0,0))
surf.blit(music_label, (music_rect.x+20, music_rect.y+10))
toggle_rect = pygame.Rect(music_rect.right-120, music_rect.y+8, 100, 38)
toggle_color = (100,220,100) if self.game.music_on else (220,100,100)
pygame.draw.rect(surf, toggle_color, toggle_rect, border_radius=12)
toggle_text = 'WŁ.' if self.game.music_on else 'WYŁ.'
toggle_label = font.render(toggle_text, True, (0,0,0))
surf.blit(toggle_label, (toggle_rect.x+18, toggle_rect.y+2))
# Przycisk Zamknij
close_rect = pygame.Rect(w//2-90, 350, 180, 54)
pygame.draw.rect(surf, (255,220,60), close_rect, border_radius=16)
pygame.draw.rect(surf, (0,0,0), close_rect, 2, border_radius=16)
close_label = font.render('Zamknij', True, (0,0,0))
surf.blit(close_label, (close_rect.x+close_rect.width//2-close_label.get_width()//2, close_rect.y+close_rect.height//2-close_label.get_height()//2))
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
return
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mx, my = event.pos
if plus_rect.collidepoint((mx, my)):
self.game.volume = min(1.0, self.game.volume+0.05)
pygame.mixer.music.set_volume(self.game.volume)
return self.show_options()
if minus_rect.collidepoint((mx, my)):
self.game.volume = max(0.0, self.game.volume-0.05)
pygame.mixer.music.set_volume(self.game.volume)
return self.show_options()
if toggle_rect.collidepoint((mx, my)):
self.game.music_on = not self.game.music_on
if self.game.music_on:
pygame.mixer.music.unpause()
else:
pygame.mixer.music.pause()
return self.show_options()
if close_rect.collidepoint((mx, my)):
waiting = False
if event.type == pygame.KEYDOWN:
waiting = False
popraw mi system strzelania z katapulty gdzie gracz strzela ptakami aby poprawnie leciał ptak i był pokazany podgląd toru lotu i daj cały kod