一、Pygame
import pygame
1.初始化
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():
pass
檢測窗口上的關(guān)閉按鈕是否被點擊
if event.type == pygame.QUIT:
退出游戲
print('關(guān)閉按鈕被點擊')
exit()
其他操作
二双饥、顯示文字
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
- 1.創(chuàng)建字體對象(找一只筆)
創(chuàng)建系統(tǒng)字體
SysFont(name, size, bold=False, italic=False)
name -> 字體名
size -> 字體大小
bold -> 加粗
italic -> 傾斜 """
font = pygame.font.SysFont('宋體', 22)
創(chuàng)建自定義字體
Font(字體文件路徑,字體大小)
font = pygame.font.Font('./font/HYShangWeiShouShuW.ttf', 22)
-
2.根據(jù)字體去創(chuàng)建顯示對象(文字)(找內(nèi)容)
render(self, text, antialias, color, background=None)text -> 要顯示的文字內(nèi)容(str)
antialias -> 是否平滑
color -> 計算機三原色(紅、綠么抗、藍),RGB顏色,值的范圍都是0-255
(255,0,0) -> 紅色
(0,255,0) -> 綠色
(0,0,255) -> 藍色
(0,0,0) -> 黑色
(255,255,255) -> 白色
(X,X,X) -> 灰色
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()
三、顯示圖片
import pygame
screen = pygame.display.set_mode((600, 400))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
1.獲取圖片對象
image = pygame.image.load('./images/luffy.jpeg')
a.獲取圖片大小
get_size()
image_size = image.get_size()
print(image_size)
b.形變:
transform:形變包含縮放措译、旋轉(zhuǎn)和平移
scale(縮放對象,新的大小) --> 返回一個縮放后的新對象
image = pygame.transform.scale(image, (150, 150))
旋轉(zhuǎn)
rotate(旋轉(zhuǎn)對象, 旋轉(zhuǎn)角度) --- 角度是0-360對應(yīng)的度數(shù)
image = pygame.transform.rotate(image, -90)
def rotozoom(旋轉(zhuǎn)對象, 旋轉(zhuǎn)角度, 縮放比例)
image = pygame.transform.rotozoom(image, 90, 0.4)
2.將圖片對象渲染到窗口上
screen.blit(image, (0, 0))
screen.blit(image,(60, 60))
3.展示在屏幕上
pygame.display.flip()
四、顯示圖形
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
screen.fill((255, 255, 255))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
1.畫直線
line(Surface, color, start_pos, end_pos, width=1)
Surface -> 畫在哪個地方
color -> 線的顏色
start_pos -> 起點
end_pos -> 終點
width -> 線的寬度
pygame.draw.line(screen, (255, 0, 0), (78, 59), (100, 100), 2)
pygame.draw.line(screen, (0, 255, 0), (0, 0), (130, 100), 2)
lines(畫線的位置, 顏色, closed, 點的列表, width=1)
pygame.draw.lines(screen, (0, 0, 255), True, [(10, 10), (200, 50), (100, 100)])
畫矩形
rect(位置劈愚,顏色瞳遍,(x,y,width,height))
pygame.draw.rect(screen,(255,255,0),(0,0,200,200),2)
2.畫曲線
arc(Surface, color, Rect, start_angle, stop_angle, width=1)
Rect -> (x, y, width, height)矩形
start_angle
stop_angle
from math import pi
pygame.draw.arc(screen, (0, 0, 0), (0, 0, 100, 200), pi+pi/4, pi*2-pi/4)
3.畫圓
circle(位置, 顏色, 圓心位置, 半徑, width=0)
import random
pygame.draw.circle(screen,\
(random.randint(0,255),random.randint(0,255),random.randint(0,255)),\
(400,200),100)
畫橢圓
ellipse(Surface, color, Rect, width=0)
pygame.draw.ellipse(screen, (0, 100, 0), (100, 300, 200, 80), 1)