基于python和pygame庫實(shí)現(xiàn)刮刮樂游戲
用python和pygame庫實(shí)現(xiàn)刮刮樂游戲
首先,確保你已經(jīng)安裝了pygame庫。如果沒有安裝,可以通過以下命令安裝:
pip install pygame
示例有兩個(gè)。
一、簡(jiǎn)單刮刮樂游戲
準(zhǔn)備兩張圖片,一張作為背景bottom_image.png,一張作為刮開的圖片top_image.png:

請(qǐng)將bottom_image.png和top_image.png圖片文件與游戲代碼文件(.py文件)放在在同一目錄下。
以下是簡(jiǎn)單刮刮樂游戲的代碼:
import pygame
import os
# 初始化pygame
pygame.init()
# 設(shè)置游戲窗口
width, height = 356, 358
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('刮刮樂游戲')
# 定義顏色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 確保圖片文件存在
if not os.path.isfile('bottom_image.png') or not os.path.isfile('top_image.png'):
raise Exception("圖片文件未找到,請(qǐng)確保bottom_image.png和top_image.png文件在同一目錄下。")
# 加載圖片
bottom_image = pygame.image.load('bottom_image.png').convert()
top_image = pygame.image.load('top_image.png').convert_alpha()
# 調(diào)整圖片大小以適應(yīng)窗口
bottom_image = pygame.transform.scale(bottom_image, (width, height))
top_image = pygame.transform.scale(top_image, (width, height))
# 創(chuàng)建一個(gè)與頂層圖片相同大小的透明表面
scratch_surface = pygame.Surface((width, height), pygame.SRCALPHA)
# 將頂層圖片繪制到透明表面上
scratch_surface.blit(top_image, (0, 0))
# 游戲主循環(huán)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 獲取鼠標(biāo)位置和狀態(tài)
mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
# 如果按下鼠標(biāo)左鍵,則在透明表面上繪制透明圓形,模擬刮開效果
if mouse_pressed[0]: # 檢測(cè)鼠標(biāo)左鍵是否按下
pygame.draw.circle(scratch_surface, (0, 0, 0, 0), mouse_pos, 20)
# 繪制背景圖片
screen.blit(bottom_image, (0, 0))
# 繪制刮開的透明表面
screen.blit(scratch_surface, (0, 0))
# 更新屏幕
pygame.display.flip()
# 退出游戲
pygame.quit()
運(yùn)行效果:

二、多對(duì)圖片的刮刮樂游戲
使用多對(duì)圖片,準(zhǔn)備了好了多對(duì)圖片,如bottom1.png和top1.png 、 bottom2.png和top2.png 、 bottom2.png和top3.png,并將它們放到了img文件夾中。用戶可以選擇圖對(duì)游戲,游戲過程中可按下ESC 鍵返回到菜單頁開始重玩。

項(xiàng)目的目錄(project_directory)結(jié)構(gòu)如下:

源碼如下:
import pygame
import os
import sys
# 初始化pygame
pygame.init()
# 設(shè)置游戲窗口
width, height = 356, 358
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('刮刮樂游戲(可選擇圖片對(duì))')
# 定義顏色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# 圖片對(duì)列表
image_pairs = [
("img/bottom1.png", "img/top1.png"),
("img/bottom2.png", "img/top2.png"),
("img/bottom3.png", "img/top3.png")
]
# 加載圖片
def load_images(pair_index):
bottom_image_path, top_image_path = image_pairs[pair_index]
bottom_image = pygame.image.load(bottom_image_path).convert()
top_image = pygame.image.load(top_image_path).convert_alpha()
bottom_image = pygame.transform.scale(bottom_image, (width, height))
top_image = pygame.transform.scale(top_image, (width, height))
return bottom_image, top_image
# 游戲主函數(shù)
def run_game(bottom_image, top_image):
scratch_surface = pygame.Surface((width, height), pygame.SRCALPHA)
scratch_surface.blit(top_image, (0, 0))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 檢測(cè)鍵盤事件以返回菜單
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: # 按下ESC鍵
return # 返回到菜單,而不是退出游戲
mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
if mouse_pressed[0]:
pygame.draw.circle(scratch_surface, (0, 0, 0, 0), mouse_pos, 20)
screen.blit(bottom_image, (0, 0))
screen.blit(scratch_surface, (0, 0))
pygame.display.flip()
# 菜單函數(shù)
def menu():
font = pygame.font.Font(None, 26)
menu_running = True
text_surfaces = []
text_rects = []
for i, pair in enumerate(image_pairs):
text = font.render(f"[ Image {i+1} ]", True, RED)
text_rect = text.get_rect(topleft=(10, 40 + i * 30))
text_surfaces.append(text)
text_rects.append(text_rect)
while menu_running:
screen.fill(WHITE)
text = font.render(f"Press Esc to return to the menu:", True, BLACK)
text_rect = text.get_rect(topleft=(10, 5))
screen.blit(text, text_rect)
for i, text in enumerate(text_surfaces):
screen.blit(text, text_rects[i])
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # Left click
for i, rect in enumerate(text_rects):
if rect.collidepoint(event.pos):
bottom_image, top_image = load_images(i)
run_game(bottom_image, top_image)
# 在這里不需要設(shè)置menu_running = False,因?yàn)槲覀兿M谟螒蚪Y(jié)束后自動(dòng)返回菜單
# 運(yùn)行菜單
menu()運(yùn)行效果如下圖所示:

用戶可以單擊菜單項(xiàng)選擇圖對(duì)游戲,游戲過程中可按下ESC 鍵返回到菜單頁開始重玩。
以上就是基于python和pygame庫實(shí)現(xiàn)刮刮樂游戲的詳細(xì)內(nèi)容,更多關(guān)于python pygame刮刮樂的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python多線程爬蟲實(shí)戰(zhàn)_爬取糗事百科段子的實(shí)例
下面小編就為大家分享一篇Python多線程爬蟲實(shí)戰(zhàn)_爬取糗事百科段子的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
使用Python將Markdown格式轉(zhuǎn)為EPUB電子書格式的代碼實(shí)現(xiàn)
我們每天都會(huì)接觸到大量的文本內(nèi)容,無論是收藏的技術(shù)文檔、自己撰寫的筆記,還是網(wǎng)絡(luò)上的優(yōu)質(zhì)文章,都可能面臨閱讀體驗(yàn)不佳的問題,所以本文給大家介紹了使用Python將Markdown格式轉(zhuǎn)為EPUB電子書格式的實(shí)現(xiàn)方法,需要的朋友可以參考下2025-04-04
python常用的各種排序算法原理與實(shí)現(xiàn)方法小結(jié)
這篇文章主要介紹了python常用的各種排序算法原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了冒泡排序、插入排序、選擇排序、快速排序等排序算法的相關(guān)原理與實(shí)現(xiàn)方法,需要的朋友可以參考下2023-04-04
python爬取免費(fèi)代理并驗(yàn)證代理是否可用
這篇文章主要介紹了python爬取免費(fèi)代理并驗(yàn)證是否可用,通過本文給大家介紹了在什么情況下會(huì)用到代理并分享腳本的完整代碼,需要的朋友可以參考下2022-01-01
Python cx_freeze打包工具處理問題思路及解決辦法
這篇文章主要介紹了Python cx_freeze打包工具處理問題思路及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-02-02
Django 通過JS實(shí)現(xiàn)ajax過程詳解
這篇文章主要介紹了Django 通過JS實(shí)現(xiàn)ajax過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

