什么是pygame?
Pygame 是一個專門用來開發(fā)游戲的 Python 模塊,主要為開發(fā)、設(shè)計 2D 電子游戲而生,具有免費(fèi)、開源,支持多種操作系統(tǒng),具有良好的跨平臺性等優(yōu)點。它提供了諸多操作模塊,比如圖像模塊(image)、聲音模塊(mixer)、輸入/輸出(鼠標(biāo)、鍵盤、顯示屏)模塊等。簡單來說,如果你使用pygame,理論上可以開發(fā)設(shè)計市面上所有的2d類型游戲(僅僅是理論上)。
一.pygame模塊的安裝
使用pip接口進(jìn)行安裝
pip install pygame
pip接口詳細(xì)說明可以看:https://blog.csdn.net/pengneng123/article/details/129556320
二.pygame庫基本函數(shù)使用
pygame.init() 初始化,調(diào)用pygame的任何函數(shù)之前都要調(diào)用這個函數(shù)。
pygame.init()
pygame.display.set_mode((400,300)) 創(chuàng)建窗口,傳入寬和高
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("xxxx") 設(shè)置窗口頂部標(biāo)題名稱
pygame.display.set_caption("豬了個豬")
運(yùn)行上述代碼:
import pygame
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("豬了個豬")
運(yùn)行上面代碼你會發(fā)現(xiàn)左上角出現(xiàn)一個窗口,然后一閃即過,這是正常的。這里就要思考python代碼的運(yùn)行,通過執(zhí)行上述代碼,你創(chuàng)建了一個黑色窗口,但是當(dāng)代碼執(zhí)行完會發(fā)生什么,當(dāng)然是python自動退出了。所以你要使用一定的方法不讓代碼結(jié)束運(yùn)行,這就要添加“死循環(huán)”了,這很好理解,while true 會一遍又一遍的運(yùn)行里面的代碼而不退出。
5.while True讓游戲無限循環(huán)
while True: # 死循環(huán)確保窗口一直顯示
for event in pygame.event.get(): # 遍歷所有事件
if event.type == pygame.QUIT: # 如果單擊關(guān)閉窗口,則退出
sys.exit() #按X 退出
6.完整代碼示例:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((400,300))
pygame.display.set_caption("豬了個豬")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
輸出:

這樣,一個游戲的最基礎(chǔ)框架就出來了。
三.操作pygame
顏色與繪制,screen.fill() 對顏色進(jìn)行填充
#設(shè)置顏色
Black = (0,0,0)
White = (255,255,255)
Red = (255,0,0)
Green = (0,255,0)
Blue = (0,0,255)
screen.fill(White)
2.繪制圖形
###繪制多邊形,pylygon(surface,color,pointlist,width)pointlist參數(shù)是一個元組或者點的
pygame.draw.polygon(screen,Green,((146,0),(291,106),(236,277),(56,277),(0,106)))
####畫線,參數(shù)為 surface,color,起始位置,終止位置,粗細(xì)
pygame.draw.line(screen,Blue,(60,120),(120,120),4)
#畫圓,參數(shù)為surface,color,圓心,半徑,邊粗細(xì)(0填充)
pygame.draw.circle(screen,Blue,(300,50),20,0)
#畫橢圓,參數(shù)為surface,color,邊界矩形(以矩形的位置來畫橢圓),邊粗細(xì)
pygame.draw.ellipse(screen,Red,(300,250,40,80),1)
#畫矩形,參數(shù)為surface,color,矩形(左上角x坐標(biāo),左上角y坐標(biāo),寬,高),邊粗細(xì)
pygame.draw.rect(screen,Red,(200,150,100,50))
3.pygame.display.flip() 更新全部顯示
pygame.display.flip()
4.pygame.quit() #退出pygame
pygame.quit()
輸出上述代碼:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((400,300))
color = (0, 0, 0) # 設(shè)置顏色
pygame.display.set_caption("豬了個豬")
Black = (0,0,0)
White = (255,255,255)
Red = (255,0,0)
Green = (0,255,0)
Blue = (0,0,255)
screen.fill(White)
###繪制多邊形,pylygon(surface,color,pointlist,width)pointlist參數(shù)是一個元組或者點的
pygame.draw.polygon(screen,Green,((146,0),(291,106),(236,277),(56,277),(0,106)))
####畫線,參數(shù)為 surface,color,起始位置,終止位置,粗細(xì)
pygame.draw.line(screen,Blue,(60,120),(120,120),4)
#畫圓,參數(shù)為surface,color,圓心,半徑,邊粗細(xì)(0填充)
pygame.draw.circle(screen,Blue,(300,50),20,0)
#畫橢圓,參數(shù)為surface,color,邊界矩形(以矩形的位置來畫橢圓),邊粗細(xì)
pygame.draw.ellipse(screen,Red,(300,250,40,80),1)
#畫矩形,參數(shù)為surface,color,矩形(左上角x坐標(biāo),左上角y坐標(biāo),寬,高),邊粗細(xì)
pygame.draw.rect(screen,Red,(200,150,100,50))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.flip() # 更新全部顯示
pygame.quit()
輸出:

5.圖片插入pygame.image.load('xxx.png')
pygame.image.load('1158.png')
6.screen.blit(xx,(0,0))##寫入到背景
screen.blit(xx,(0,0))##寫入到背景
輸出:

7.pygame.time.Clock()設(shè)置時鐘
clock = pygame.time.Clock() # 設(shè)置時鐘
8.看一段完整的代碼,更好的理解pygame
import pygame
import time
pygame.init() # 初始化pygame
size = width, height = 480, 700 # 設(shè)置窗口大小
screen = pygame.display.set_mode(size) # 顯示窗口
pygame.display.set_caption("豬了個豬")
color = (0, 0, 0) # 設(shè)置顏色
pig = pygame.image.load('1158.png') # 加載圖片
beijing121 = pygame.image.load("gr/147.png")##加載背景圖片
beijing131 = pygame.image.load("gr/189.png")##加載背景圖片
beijing141 = pygame.image.load("gr/199.png")##加載背景圖片
beijing151 = pygame.image.load("gr/200.png")##加載背景圖片
pigrect = pig.get_rect() # 獲取矩形區(qū)域
pigrect1 = beijing121.get_rect() # 獲取矩形區(qū)域
speed = [1, 1] # 設(shè)置移動的X軸、Y軸
speed1 = [80, 80]
speed2 = [160, 160]
speed3 = [200, 120]
speed4 = [120, 260] # 設(shè)置移動的X軸、Y軸
clock = pygame.time.Clock() # 設(shè)置時鐘
def Map():
screen.fill((255,255,255))##填充顏色
screen.blit(background,(0,0))##寫入到背景
while True: # 死循環(huán)確保窗口一直顯示
clock.tick(10) # 每秒執(zhí)行10次
for event in pygame.event.get(): # 遍歷所有事件
if event.type == pygame.QUIT: # 如果單擊關(guān)閉窗口,則退出
sys.exit()
background = pygame.image.load("beijing1.png")##加載背景圖片
Map()
pigrect = pigrect.move(speed) # 移動小豬
pigrect1 = pigrect.move(speed1) # 移動小豬
pigrect2 = pigrect.move(speed2)
pigrect3 = pigrect.move(speed3)
pigrect4 = pigrect.move(speed4)
# 碰到左右邊緣
if pigrect.left < 0 or pigrect.right > width:
speed[0] = -speed[0]
# 碰到上下邊緣
if pigrectt.top < 0 or pigrect.bottom > height:
speed[1] = -speed[1]
if pigrect1.left < 0 or pigrect1.right > width:
speed[0] = -speed[0]
# 碰到上下邊緣
if pigrect1.top < 0 or pigrect1.bottom > height:
speed[1] = -speed[1]
if pigrect2.left < 0 or pigrect2.right > width:
speed[0] = -speed[0]
# 碰到上下邊緣
if pigrect2.top < 0 or pigrect2.bottom > height:
speed[1] = -speed[1]
if pigrect3.left < 0 or pigrect3.right > width:
speed[0] = -speed[0]
# 碰到上下邊緣
if pigrect3.top < 0 or pigrect3.bottom > height:
speed[1] = -speed[1]
if pigrect4.left < 0 or pigrect4.right > width:
speed[0] = -speed[0]
# 碰到上下邊緣
if pigrect4.top < 0 or pigrect4.bottom > height:
speed[1] = -speed[1]
screen.blit(ball, ballrect) # 將圖片畫到窗口上
screen.blit(beijing121, ballrect1) # 將圖片畫到窗口上
screen.blit(beijing131, ballrect2) # 將圖片畫到窗口上
screen.blit(beijing141, ballrect3) # 將圖片畫到窗口上
screen.blit(beijing151, ballrect4) # 將圖片畫到窗口上
pygame.display.flip() # 更新全部顯示
pygame.quit() # 退出pygame
最后輸出:
最后可以根據(jù)自己想做的東西,慢慢嘗試去實現(xiàn)啦。文章來源:http://www.zghlxwxcb.cn/news/detail-718365.html
@Neng文章來源地址http://www.zghlxwxcb.cn/news/detail-718365.html
到了這里,關(guān)于【python】之pygame模塊,游戲開發(fā)【基礎(chǔ)篇】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!