一、pygame導(dǎo)入及基本工作框架
import pygame
if __name__ == '__main__':
# 1.初始化pygame
pygame.init()
# 2.創(chuàng)建游戲窗口
# set.mode((寬度,高度))
screen = pygame.display.set_mode((600, 400))
#3.游戲循環(huán)
while True:
# 檢測事件
for event in pygame.event.get():
# 檢測窗口上的關(guān)閉按鈕是否被點(diǎn)擊
if event.type == pygame.QUIT:
# 退出游戲
print('關(guān)閉按鈕被點(diǎn)擊')
exit()
這是一個(gè)最簡單的pygame的工作框架,在pygame中實(shí)現(xiàn)各種事件都需要這個(gè)工作框架,也就是說每一個(gè).py文件都需要此工作框架
二、pygame顯示文字
import pygame
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 400))
# 設(shè)置背景顏色
screen.fill((255, 255, 255))
# 1.創(chuàng)建字體
# SysFont(name, size, bold=False, italic=False, constructor=None) 系統(tǒng)字體
# name 字體名字
# size 字體大小
# blod 加粗
# italic 傾斜
font = pygame.font.SysFont('宋體', 80)
"""
創(chuàng)建自定義字體
Font(字體文件路徑,字體大小)
"""
font = pygame.font.Font('./font/aa.ttf', 50)
# 2.根據(jù)字體去創(chuàng)建顯示對象(文字)
# render(self, text, antialias, color, background=None)
# text 要顯示的文字內(nèi)容(str)
# color 計(jì)算機(jī)三原色(紅、綠、藍(lán))RGB顏色,值的范圍都是0~255
# (255,0,0)紅色(0,255,0)綠色(0,0,255)藍(lán)色(0,0,0)黑色(255,255,255)白色(X,X,X)灰色
# antialias 是否平滑
surface = font.render('你好 Python', True, (0, 255, 0))
"""
3.將內(nèi)容添加到窗口上(畫到紙上)
blit(需要顯示的對象,顯示位置)
需要顯示的對象 --> Surface類型的數(shù)據(jù)
顯示位置 --> 坐標(biāo)(x,y)
"""
screen.blit(surface, (100, 100))
# 4.將窗口上的內(nèi)容展示出來(將畫有文字的紙貼出來)
pygame.display.flip()
# 游戲循環(huán)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
三、pygame中顯示圖片
import pygame
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((600, 1000))
screen.fill((255, 255, 255))
# 1.獲取圖片對象
image = pygame.image.load('./image/12.jpg')
"""
a.獲取圖片大小
"""
image_size = image.get_size()
print(image_size)
"""
b.圖片形變
transform:形變包括縮放,旋轉(zhuǎn),平移
scale(縮放對象,新的大小) --> 返回一個(gè)縮放后的新對象
rotate(旋轉(zhuǎn)對象,旋轉(zhuǎn)角度)-- 角度是0-360對應(yīng)的度數(shù)
rotozoom(旋轉(zhuǎn)對象,角度旋轉(zhuǎn),縮放比例)
"""
pygame.transform.scale(image, (500, 600))
pygame.transform.rotate(image, 90)
image = pygame.transform.rotozoom(image, 45, 1)
# 2.將圖片對象渲染到窗口上
screen.blit(image, (0, 0))
# 3.展示在屏幕上
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
四、pygame中顯示圖形
import pygame
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((1000, 1000))
screen.fill((255, 255, 255))
"""
1.畫直線
line(Surface, color, start_pos, end_pos, width=1)
Surface 畫在哪個(gè)地方
color 線的顏色
start_pos 起點(diǎn)
end_pos 終點(diǎn)
width 線的寬度
"""
# pygame.draw.line(screen, (255, 0, 0), (77, 77), (777, 777), 3)
"""
lines(Surface, color, closed, pointlist, width=1)
畫的位置,顏色,是否連接起點(diǎn)和終點(diǎn),點(diǎn)的列表,線的寬度
"""
# pygame.draw.lines(screen, (255, 255, 0), True, [(77, 77), (233, 421), (789, 963), (555, 555), 10])
"""
2.畫矩形
Rect(位置,顏色,坐標(biāo),寬度)
"""
pygame.draw.rect(screen, (0, 0, 255), (77, 77, 600, 700), 0)
"""
3.畫曲線
arc(Surface, color, Rect, start_angle, stop_angle, width=1)
Rect --> (x, y, width, height)矩形
start_angle
"""
from math import pi
pygame.draw.arc(screen, (0, 0, 0), (77, 77, 600, 700), pi/2, pi, 5)
"""
4.畫圓
circle(Surface, color, pos, radius, width=0)
位置,顏色,圓心,半徑,寬度
"""
import random
pygame.draw.circle(screen, (random.randint(0, 255), random.randint(0, 255),\
random.randint(0, 255)), (500, 500), 300, 5)
# 將內(nèi)容展示在屏幕上
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()