- Python基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python控制語句
- Python - 控制流
- Python - 決策
- Python - if語句
- Python - if else語句
- Python - 巢狀if語句
- Python - match-case語句
- Python - 迴圈
- Python - for迴圈
- Python - for-else迴圈
- Python - while迴圈
- Python - break語句
- Python - continue語句
- Python - pass語句
- Python - 巢狀迴圈
- Python函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS檔案/目錄方法
- Python - OS路徑方法
- 面向物件程式設計
- Python - OOPs概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except塊
- Python - try-finally塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀try塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 加入執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python網路程式設計
- Python - 網路程式設計
- Python - 套接字程式設計
- Python - URL處理
- Python - 泛型
- Python庫
- NumPy教程
- Pandas教程
- SciPy教程
- Matplotlib教程
- Django教程
- OpenCV教程
- Python雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI程式設計
- Python - XML處理
- Python - GUI程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python編譯器
- NumPy編譯器
- Matplotlib編譯器
- SciPy編譯器
Python - 使用pygame模組製作貪吃蛇遊戲
貪吃蛇遊戲是最古老的遊戲之一,最初出現在街機上,並被幾代喜歡玩遊戲的人們所接受。在本教程中,我們將學習如何開發一個簡單的貪吃蛇遊戲,包括開始介面、帶邊框的遊戲區域和可移動的蛇。
安裝
首先,您需要安裝pygame模組。 pygame是一個用於開發遊戲的Python庫。您可以使用pip安裝它:
pip install pygame
設計貪吃蛇遊戲的Python程式碼
以下是使用pygame模組設計貪吃蛇遊戲的完整程式碼:
import pygame
import time
import random
# Initialize Pygame
pygame.init()
# Define colors
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# Define display dimensions
dis_width = 800
dis_height = 600
# Create the display
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.setCaption('Snake Game by Souvik')
# Set the clock for controlling the game speed
clock = pygame.time.Clock()
# Define snake block size and speed
snake_block = 10
snake_speed = 15
# Define font style and size
font_style = pygame.font.SysFont(None, 50)
score_font = pygame.font.SysFont(None, 35)
# Function to display the score
def Your_score(score):
value = score_font.render("Your Score: " + str(score), True, yellow)
dis.blit(value, [0, 0])
# Function to draw the snake
def our_snake(snake_block, snake_List):
for x in snake_List:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
# Function to display message
def message(msg, color, position):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, position)
# Start screen
def start_screen():
start = True
while start:
dis.fill(blue)
message("Welcome to Snake Game", white, [dis_width / 6, dis_height / 3])
message("Press S to Start or Q to Quit", white, [dis_width / 6, dis_height / 2])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
quit()
if event.key == pygame.K_s:
start = False
if event.type == pygame.QUIT:
pygame.quit()
quit()
# The game loop
def gameLoop():
start_screen()
game_over = False
game_close = False
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
# Generate food coordinates
foodx = round(random.randrange(10, dis_width - snake_block - 10) / 10.0) * 10.0
foody = round(random.randrange(10, dis_height - snake_block - 10) / 10.0) * 10.0
while not game_over:
while game_close == True:
dis.fill(blue)
message("You Lost! Press C-Play Again or Q-Quit", red, [dis_width / 6, dis_height / 3])
Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
if x1 >= dis_width - 10 or x1 < 10 or y1 >= dis_height - 10 or y1 < 10:
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(blue)
# Draw the border
pygame.draw.rect(dis, white, [10, 10, dis_width - 20, dis_height - 20], 5)
# Draw the food
pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
# Move the snake
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(10, dis_width - snake_block - 10) / 10.0) * 10.0
foody = round(random.randrange(10, dis_height - snake_block - 10) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
# Run the game
gameLoop()
程式碼解釋
開發遊戲過程中使用的一些步驟如下:
- 初始化pygame - 首先,匯入pygame,然後使用**init()**啟動它。
- 設定顯示 - 清除螢幕大小並獲取視窗大小,用於顯示內容。
- 定義顏色和字型 - 包含遊戲中將使用的所有顏色和文字部分將使用的所有字型。
- 建立開始螢幕 - start_screen()函式向控制檯列印一條訊息,告知玩家遊戲即將開始,並等待使用者輸入以開始遊戲。
- 遊戲迴圈 - **gameLoop()**函式處理運動、碰撞檢測和計分。它包含控制蛇、建立食物以及確定遊戲何時結束的邏輯。
- 帶邊框的遊戲區域 - 由於這裡使用**pygame**來設計遊戲區域,所以在區域周圍建立了一個白色邊框。也畫出邊框,這樣蛇就無法逃出被包圍的區域。
- 結束遊戲 - 遊戲持續進行,直到蛇撞到邊界或自己的尾巴;遊戲結束。遊戲結束後,玩家可以選擇重新開始遊戲或退出。
輸出
運行遊戲時,您將看到一個歡迎螢幕,提示您按“S”開始遊戲或按“Q”退出。
遊戲開始後,蛇將在帶邊框的區域內移動。目標是吃掉食物(綠色方塊)並讓蛇變長,同時避免與牆壁或蛇自身的身體碰撞。
遊戲結束後,您可以選擇重新開始或退出。
python_projects_from_basic_to_advanced.htm
廣告