Python ile flappy bird yapıyoruz

"Python ile flappy bird yapıyoruz"

Herkese merhaba. Python ile basit flappy bird kodlarını sizinle paylaşıyorum. ayrıca aşağıdakı linkden dosyayı tam şekilde indire bilirsiniz. 

flappy_bird.py

 

import pygame
import random
import sys

# Pygame'i başlat
pygame.init()

# Renkler
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 100, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
DARK_GREEN = (0, 200, 0)
ORANGE = (255, 165, 0)

# Oyun ayarları
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
GROUND_HEIGHT = 100
BIRD_SIZE = 30
PIPE_WIDTH = 80
PIPE_GAP = 200
GRAVITY = 0.5
JUMP_STRENGTH = -8
PIPE_SPEED = 3

class Bird:
    def __init__(self):
        self.x = SCREEN_WIDTH // 4
        self.y = SCREEN_HEIGHT // 2
        self.velocity = 0
        self.size = BIRD_SIZE
        
    def jump(self):
        self.velocity = JUMP_STRENGTH
        
    def update(self):
        self.velocity += GRAVITY
        self.y += self.velocity
        
    def draw(self, screen):
        # Kuş gövdesi
        pygame.draw.circle(screen, YELLOW, (int(self.x), int(self.y)), self.size)
        # Kuş gözü
        pygame.draw.circle(screen, BLACK, (int(self.x + 8), int(self.y - 8)), 5)
        # Kuş gagası
        pygame.draw.polygon(screen, ORANGE, [
            (self.x + self.size, self.y),
            (self.x + self.size + 15, self.y - 5),
            (self.x + self.size + 15, self.y + 5)
        ])
        
    def get_rect(self):
        return pygame.Rect(self.x - self.size, self.y - self.size, 
                          self.size * 2, self.size * 2)

class Pipe:
    def __init__(self, x):
        self.x = x
        self.gap_y = random.randint(PIPE_GAP, SCREEN_HEIGHT - GROUND_HEIGHT - PIPE_GAP)
        self.passed = False
        
    def update(self):
        self.x -= PIPE_SPEED
        
    def draw(self, screen):
        # Üst boru
        pygame.draw.rect(screen, DARK_GREEN, 
                        (self.x, 0, PIPE_WIDTH, self.gap_y - PIPE_GAP // 2))
        # Alt boru
        pygame.draw.rect(screen, DARK_GREEN,
                        (self.x, self.gap_y + PIPE_GAP // 2, PIPE_WIDTH, 
                         SCREEN_HEIGHT - GROUND_HEIGHT - (self.gap_y + PIPE_GAP // 2)))
        
        # Boru kapakları
        pygame.draw.rect(screen, GREEN,
                        (self.x - 10, self.gap_y - PIPE_GAP // 2 - 20, PIPE_WIDTH + 20, 20))
        pygame.draw.rect(screen, GREEN,
                        (self.x - 10, self.gap_y + PIPE_GAP // 2, PIPE_WIDTH + 20, 20))
        
    def collides_with(self, bird):
        bird_rect = bird.get_rect()
        
        # Üst boru çarpışması
        top_pipe = pygame.Rect(self.x, 0, PIPE_WIDTH, self.gap_y - PIPE_GAP // 2)
        # Alt boru çarpışması
        bottom_pipe = pygame.Rect(self.x, self.gap_y + PIPE_GAP // 2, PIPE_WIDTH,
                                 SCREEN_HEIGHT - GROUND_HEIGHT - (self.gap_y + PIPE_GAP // 2))
        
        return bird_rect.colliderect(top_pipe) or bird_rect.colliderect(bottom_pipe)
    
    def is_off_screen(self):
        return self.x + PIPE_WIDTH < 0

class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("🐦 Flappy Bird - Python")
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(None, 36)
        self.big_font = pygame.font.Font(None, 72)
        self.reset_game()
        
    def reset_game(self):
        self.bird = Bird()
        self.pipes = []
        self.score = 0
        self.game_over = False
        self.game_started = False
        
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if not self.game_started:
                        self.game_started = True
                    elif self.game_over:
                        self.reset_game()
                        self.game_started = True
                    else:
                        self.bird.jump()
                elif event.key == pygame.K_ESCAPE:
                    return False
        return True
        
    def update(self):
        if not self.game_started or self.game_over:
            return
            
        # Kuşu güncelle
        self.bird.update()
        
        # Boruları güncelle
        for pipe in self.pipes[:]:
            pipe.update()
            if pipe.is_off_screen():
                self.pipes.remove(pipe)
            elif not pipe.passed and pipe.x + PIPE_WIDTH < self.bird.x:
                pipe.passed = True
                self.score += 1
        
        # Yeni boru ekle
        if len(self.pipes) == 0 or self.pipes[-1].x < SCREEN_WIDTH - 300:
            self.pipes.append(Pipe(SCREEN_WIDTH))
        
        # Çarpışma kontrolü
        # Zemin veya tavan çarpışması
        if (self.bird.y + self.bird.size >= SCREEN_HEIGHT - GROUND_HEIGHT or 
            self.bird.y - self.bird.size <= 0):
            self.game_over = True
            
        # Boru çarpışması
        for pipe in self.pipes:
            if pipe.collides_with(self.bird):
                self.game_over = True
                break
    
    def draw_background(self):
        # Gökyüzü gradyanı
        for y in range(SCREEN_HEIGHT - GROUND_HEIGHT):
            color_intensity = 255 - (y * 50 // SCREEN_HEIGHT)
            color = (100, 150, color_intensity)
            pygame.draw.line(self.screen, color, (0, y), (SCREEN_WIDTH, y))
        
        # Zemin
        pygame.draw.rect(self.screen, (139, 69, 19), 
                        (0, SCREEN_HEIGHT - GROUND_HEIGHT, SCREEN_WIDTH, GROUND_HEIGHT))
        
        # Zemin detayları
        for x in range(0, SCREEN_WIDTH, 40):
            pygame.draw.rect(self.screen, (101, 67, 33),
                           (x, SCREEN_HEIGHT - GROUND_HEIGHT, 20, GROUND_HEIGHT))
    
    def draw_ui(self):
        if not self.game_started:
            # Başlangıç ekranı
            title_text = self.big_font.render("🐦 FLAPPY BIRD", True, WHITE)
            title_rect = title_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 3))
            self.screen.blit(title_text, title_rect)
            
            start_text = self.font.render("BAŞLAMAK İÇİN SPACE TUŞUNA BASIN", True, WHITE)
            start_rect = start_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
            self.screen.blit(start_text, start_rect)
            
            controls_text = self.font.render("KONTROLLER: SPACE = Zıpla, ESC = Çıkış", True, WHITE)
            controls_rect = controls_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50))
            self.screen.blit(controls_text, controls_rect)
            
        elif self.game_over:
            # Oyun bitti ekranı
            game_over_text = self.big_font.render("OYUN BİTTİ!", True, RED)
            game_over_rect = game_over_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 3))
            self.screen.blit(game_over_text, game_over_rect)
            
            final_score_text = self.font.render(f"FINAL SKOR: {self.score}", True, WHITE)
            final_score_rect = final_score_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
            self.screen.blit(final_score_text, final_score_rect)
            
            restart_text = self.font.render("YENİDEN BAŞLAMAK İÇİN SPACE TUŞUNA BASIN", True, WHITE)
            restart_rect = restart_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50))
            self.screen.blit(restart_text, restart_rect)
        else:
            # Skor
            score_text = self.font.render(f"Skor: {self.score}", True, WHITE)
            self.screen.blit(score_text, (10, 10))
            
            # Yükseklik göstergesi
            height_text = self.font.render(f"Yükseklik: {int((SCREEN_HEIGHT - GROUND_HEIGHT - self.bird.y) / 10)}m", True, WHITE)
            self.screen.blit(height_text, (10, 50))
    
    def draw(self):
        # Arkaplanı çiz
        self.draw_background()
        
        # Oyun nesnelerini çiz
        if self.game_started:
            # Boruları çiz
            for pipe in self.pipes:
                pipe.draw(self.screen)
            
            # Kuşu çiz
            self.bird.draw(self.screen)
        
        # UI'yi çiz
        self.draw_ui()
        
        pygame.display.flip()
    
    def run(self):
        running = True
        while running:
            running = self.handle_events()
            self.update()
            self.draw()
            self.clock.tick(60)  # 60 FPS
        
        pygame.quit()
        sys.exit()

def main():
    """
    Ana oyun fonksiyonu
    """
    print("🐦 Flappy Bird Oyunu Başlatılıyor...")
    print("=" * 40)
    print("KONTROLLER:")
    print("🔹 SPACE: Kuşu zıplat")
    print("🔹 ESC: Oyundan çık")
    print("=" * 40)
    print("Eğlenceli oyunlar! 🎮")
    
    game = Game()
    game.run()

if __name__ == "__main__":
    main()

 

Aykhan Gasimzade Aykhan Gasimzade verified icon
Full Stack Developer

Merhaba! Ben Aykhan, 1999 doğumlu bir Azerbaycanlıyım ve 2017 yılından bu yana programlamayla ilgileniyorum. Teknolojinin büyülü dünyası beni her zaman etkilemiştir ve bu merakımı kendi blogumda paylaşarak daha geniş bir kitleyle buluşturmayı amaçlıyorum.