# Flappy Bird --- ## 遊戲介紹 - **遊戲名稱**: Flappy Bird - **遊戲目標**: 控制小鳥飛行,避免撞擊水管 - **開發工具**: Python, Pygame 庫 --- ## 程式碼結構概述 - **主要模組** ```markdown - `random` 和 `sys`: 內建模組,用於生成隨機數和系統操作 - `pygame`: 遊戲開發主要模組 --- ## 初始化設定 - **視窗設定** ```python window_width = 600 window_height = 499 window = pygame.display.set_mode((window_width, window_height)) ``` - **圖像載入** ```python pipeimage = 'Flappy Bird Images/pipe.png' background_image = 'Flappy Bird Images/background.jpg' birdplayer_image = 'Flappy Bird Images/bird.png' sealevel_image = 'Flappy Bird Images/base.jfif' ``` --- ## 主遊戲循環 - **初始化變量** ```python your_score = 0 horizontal = int(window_width/5) vertical = int(window_width/2) ``` - **事件處理** ```python for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP): if vertical > 0: bird_velocity_y = bird_flap_velocity bird_flapped = True ``` --- ## 遊戲功能實現 - **碰撞測試** ```python def isGameOver(horizontal, vertical, up_pipes, down_pipes): if vertical > elevation - 25 or vertical < 0: return True ... ``` - **水管生成** ```python def createPipe(): offset = window_height/3 pipeHeight = game_images['pipeimage'][0].get_height() ... return pipe ``` --- ## 遊戲邏輯 - **小鳥的運動** ```python if bird_velocity_y < bird_Max_Vel_Y and not bird_flapped: bird_velocity_y += birdAccY if bird_flapped: bird_flapped = False playerHeight = game_images['flappybird'].get_height() vertical = vertical + min(bird_velocity_y, elevation - vertical - playerHeight) ``` - **水管的運動** ```python for upperPipe, lowerPipe in zip(up_pipes, down_pipes): upperPipe['x'] += pipeVelX lowerPipe['x'] += pipeVelX ``` --- ## 分數計算 - **分數顯示** ```python numbers = [int(x) for x in list(str(your_score))] ... for num in numbers: window.blit(game_images['scoreimages'][num], (Xoffset, window_width*0.02)) Xoffset += game_images['scoreimages'][num].get_width() ``` ---
{"title":"無標題","description":"當然可以,下面是基於上述結構的簡報內容,使用 Markdown 語法進行分頁產出:","contributors":"[{\"id\":\"93646446-226a-4795-ae78-1bff808c6fd4\",\"add\":4422,\"del\":2211}]"}
    91 views