Flappy Bird dengan Python dan Pygame: Panduan Lengkap
Berikut adalah contoh kode untuk membuat permainan Flappy Bird menggunakan bahasa pemrograman Python dan library Pygame:
import pygame
import random
# Inisialisasi Pygame
pygame.init()
# Ukuran layar
screen_width = 576
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
# Judul dan ikon layar
pygame.display.set_caption('Flappy Bird')
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# Warna
background_color = (0, 0, 0)
font_color = (255, 255, 255)
# Font
font = pygame.font.Font('freesansbold.ttf', 32)
# Variabel permainan
gravity = 0.25
bird_movement = 0
score = 0
# Load gambar
background = pygame.image.load('background.png')
bird = pygame.image.load('bird.png')
bird_rect = bird.get_rect(center=(100, screen_height // 2))
pipe_top = pygame.image.load('pipe_top.png')
pipe_bottom = pygame.image.load('pipe_bottom.png')
# Fungsi untuk menggambar skor
def draw_score():
score_surface = font.render(f'Score: {score}', True, font_color)
score_rect = score_surface.get_rect(center=(screen_width // 2, 100))
screen.blit(score_surface, score_rect)
# Fungsi untuk menggambar burung
def draw_bird():
screen.blit(bird, bird_rect)
# Fungsi untuk menggambar pipa
def draw_pipes(pipes):
for pipe in pipes:
if pipe.bottom >= screen_height:
screen.blit(pipe_bottom, pipe)
else:
screen.blit(pipe_top, pipe)
# Fungsi untuk memperbarui skor tertinggi
def update_highscore(score, highscore):
if score > highscore:
highscore = score
return highscore
# Fungsi untuk memeriksa tabrakan dengan pipa
def check_collision(pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return True
if bird_rect.top <= 0 or bird_rect.bottom >= screen_height:
return True
return False
# Fungsi untuk menghasilkan pipa baru
def generate_pipe():
random_pipe_pos = random.choice(pipe_heights)
bottom_pipe = pipe_bottom.get_rect(midtop=(screen_width + 200, random_pipe_pos))
top_pipe = pipe_top.get_rect(midbottom=(screen_width + 200, random_pipe_pos - 200))
return bottom_pipe, top_pipe
# Mengatur waktu pipa baru muncul
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)
pipe_heights = [400, 600, 800]
# Variabel permainan
running = True
game_over = False
pipes = []
highscore = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not game_over:
bird_movement = 0
bird_movement -= 7
if event.key == pygame.K_SPACE and game_over:
game_over = False
pipes.clear()
bird_rect.center = (100, screen_height // 2)
bird_movement = 0
score = 0
if event.type == SPAWNPIPE:
pipes.extend(generate_pipe())
screen.blit(background, (0, 0))
if not game_over:
# Gerakan burung
bird_movement += gravity
bird_rect.centery += int(bird_movement)
screen.blit(bird, bird_rect)
game_over = check_collision(pipes)
# Gerakan pipa
pipes = [(pipe[0] - 5, pipe[1]) for pipe in pipes]
draw_pipes(pipes)
# Menghapus pipa yang sudah lewat
pipes = [pipe for pipe in pipes if pipe.right > -50]
# Menambah skor jika melewati pipa
for pipe in pipes:
if pipe.centerx == bird_rect.centerx:
score += 1
# Menggambar skor
draw_score()
else:
highscore = update_highscore(score, highscore)
score_surface = font.render(f'Score: {score}', True, font_color)
score_rect = score_surface.get_rect(center=(screen_width // 2, screen_height // 2))
screen.blit(score_surface, score_rect)
highscore_surface = font.render(f'Highscore: {highscore}', True, font_color)
highscore_rect = highscore_surface.get_rect(center=(screen_width // 2, screen_height // 2 + 50))
screen.blit(highscore_surface, highscore_rect)
pygame.display.update()
clock.tick(60)
# Keluar dari Pygame
pygame.quit()
Pastikan Anda telah menyediakan gambar latar belakang ('background.png'), burung ('bird.png'), ikon ('icon.png'), pipa atas ('pipe_top.png'), dan pipa bawah ('pipe_bottom.png') dalam folder yang sama dengan file Python ini.
原文地址: https://www.cveoy.top/t/topic/peZ6 著作权归作者所有。请勿转载和采集!