pygame 套件筆記
基本模板
import pygame as pg
pg.init()
width, height = 640, 480
screen = pg.display.set_mode((width, height))
pg.display.set_caption("Sean's game")
bg = pg.Surface(screen.get_size())
bg = bg.convert()
bg.fill((255,255,255))
screen.blit(bg, (0,0))
pg.display.update()
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
pg.quit()
編成步驟
引入資料庫
pygame初始化
依設定顯示視窗
screen = pg.display.set_mode((weight,height))
遊戲標題
pg.display.set_caption("這邊打標題就好")
關閉函式
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
pg.quit()
常用函式
建立畫布
背景變數 = pg.Surface(screen.get_size())
背景變數 = 背景變數.convert()
背景變數.fill((0,0,0))
視窗變數.blit(背景變數, 繪製位置)
pg.display.update()
繪圖函式
畫一個矩形的形狀
pygame.draw.rect(畫布, 顏色, [x坐標, y坐標, 寬度, 高度], 線寬)
畫出任意數量的形狀
圍繞一個點畫一個圓圈
pygame.draw.circle(畫布, 顏色, (x坐標, y坐標), 半徑, 線寬)
在矩形內繪製圓形
pygame.draw.ellipse(畫布, 顏色, [x坐標, y坐標, x直徑, y直徑], 線寬)
繪製一條直線段
pygame.draw.line(畫布, 顏色, (x坐標1, y坐標1), (x坐標2, y坐標2), 線寬)
繪製精細的抗鋸齒線
繪製圓弧形
pygame.draw.arc(畫布, 顏色, [x坐標, y坐標, x直徑, y直徑], 起始角, 結束角, 線寬)
加入文字
字體變數 = pg.font.SysFont(字體名稱, 字體尺寸)
文字變數 = 字體變數.render(文字, 平滑值, 文字顏色, 背景顏色)
視窗變數.blit(文字變數, (320,240))
載入img
圖片變數 = pygame.image.load(圖片檔案路徑)
圖片變數.convert()
讀取img