目錄
Python實例題
題目
pygame-aircraft-game使用 Pygame 開發的打飛機游戲腳本
代碼解釋
初始化部分:
游戲主循環:
退出部分:
運行思路
注意事項
Python實例題
題目
pygame開發打飛機游戲
pygame-aircraft-game使用 Pygame 開發的打飛機游戲腳本
import pygame
import random# 初始化 Pygame
pygame.init()# 定義屏幕尺寸
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 650# 創建屏幕對象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("打飛機游戲")# 定義顏色
WHITE = (255, 255, 255)# 加載玩家飛機圖片
player_img = pygame.image.load("player.png") # 請確保該圖片存在
player_rect = player_img.get_rect()
player_rect.centerx = SCREEN_WIDTH // 2
player_rect.bottom = SCREEN_HEIGHT - 10# 玩家飛機移動速度
player_speed = 5# 加載敵機圖片
enemy_img = pygame.image.load("enemy.png") # 請確保該圖片存在
enemies = []# 敵機生成間隔和計時器
ENEMY_SPAWN_INTERVAL = 1000
enemy_spawn_timer = 0# 加載子彈圖片
bullet_img = pygame.image.load("bullet.png") # 請確保該圖片存在
bullets = []# 子彈移動速度
bullet_speed = 8# 游戲主循環
running = True
clock = pygame.time.Clock()while running:# 控制游戲幀率clock.tick(60)# 處理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:# 發射子彈bullet_rect = bullet_img.get_rect()bullet_rect.centerx = player_rect.centerxbullet_rect.top = player_rect.topbullets.append(bullet_rect)# 獲取按鍵狀態keys = pygame.key.get_pressed()if keys[pygame.K_LEFT] and player_rect.left > 0:player_rect.x -= player_speedif keys[pygame.K_RIGHT] and player_rect.right < SCREEN_WIDTH:player_rect.x += player_speedif keys[pygame.K_UP] and player_rect.top > 0:player_rect.y -= player_speedif keys[pygame.K_DOWN] and player_rect.bottom < SCREEN_HEIGHT:player_rect.y += player_speed# 生成敵機enemy_spawn_timer += clock.get_time()if enemy_spawn_timer > ENEMY_SPAWN_INTERVAL:enemy_rect = enemy_img.get_rect()enemy_rect.x = random.randint(0, SCREEN_WIDTH - enemy_rect.width)enemy_rect.y = -enemy_rect.heightenemies.append(enemy_rect)enemy_spawn_timer = 0# 移動敵機for enemy in enemies[:]:enemy.y += 3if enemy.top > SCREEN_HEIGHT:enemies.remove(enemy)# 移動子彈for bullet in bullets[:]:bullet.y -= bullet_speedif bullet.bottom < 0:bullets.remove(bullet)# 檢測子彈和敵機的碰撞for bullet in bullets[:]:for enemy in enemies[:]:if bullet.colliderect(enemy):bullets.remove(bullet)enemies.remove(enemy)# 繪制背景screen.fill(WHITE)# 繪制玩家飛機screen.blit(player_img, player_rect)# 繪制敵機for enemy in enemies:screen.blit(enemy_img, enemy)# 繪制子彈for bullet in bullets:screen.blit(bullet_img, bullet)# 更新顯示pygame.display.flip()# 退出 Pygame
pygame.quit()
代碼解釋
-
初始化部分:
- 初始化
pygame
庫,設置屏幕尺寸和標題。 - 加載玩家飛機、敵機和子彈的圖片,并設置玩家飛機的初始位置。
- 定義敵機生成間隔和計時器,以及子彈的移動速度。
- 初始化
-
游戲主循環:
- 控制游戲幀率為 60 幀每秒。
- 處理事件,包括關閉窗口事件和發射子彈事件。
- 根據按鍵狀態移動玩家飛機。
- 按一定間隔生成敵機,并移動敵機和子彈。
- 檢測子彈和敵機的碰撞,若碰撞則移除對應的子彈和敵機。
- 繪制背景、玩家飛機、敵機和子彈,并更新顯示。
-
退出部分:
- 當用戶關閉窗口時,退出
pygame
。
- 當用戶關閉窗口時,退出
運行思路
- 安裝依賴庫:確保已經安裝了
pygame
庫,若未安裝,可使用以下命令進行安裝:
pip install pygame
- 準備圖片:準備好
player.png
、enemy.png
和bullet.png
三張圖片,并將其放在與代碼文件相同的目錄下。 - 運行腳本:將上述代碼保存為
aircraft_game.py
文件,在終端中運行:
python aircraft_game.py
- 開始游戲:使用上下左右鍵移動玩家飛機,按空格鍵發射子彈,嘗試擊落敵機。
注意事項
- 請確保圖片文件的路徑和名稱正確,否則會出現加載圖片失敗的錯誤。
- 此代碼只是一個簡單的示例,你可以根據需求對游戲進行擴展,如添加音效、計分系統、關卡設計等。