一糜颠、pygame基本操作
import pygame——導(dǎo)入pygame模塊
pygame.init()——初始化游戲模塊
新建游戲窗口
set_mode(窗口大小):創(chuàng)建一個窗口并且返回
窗口大卸丝肌:是一個元祖日熬,兩個值分別代表寬度和高度(單位是像素)
window=pygame.display.set_mode((600,400))——創(chuàng)建窗口
給窗口填充顏色
fill(顏色)
顏色:計算機(jī)三原色(紅,綠咱圆,藍(lán))搭幻,每個顏色對應(yīng)點值的范圍為0~255
(255, 0, 0) --> 紅色
(0, 255, 0) --> 綠色
(0, 0, 255) --> 藍(lán)色
(0已艰,0,0)--->黑色
(255,255,255)--->白色
window.fill((0,0,0))
讓游戲一直運(yùn)行唁盏,直到點關(guān)閉按鈕才結(jié)束
flag=True
while flag:
# 獲取游戲過程中產(chǎn)生的所有事件
for event in pygame.event.get():
# type來判斷事件類型
if event.type==pygame.QUIT:
# exit() #退出程序
flag=False
基礎(chǔ)代碼
import pygame
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
二内狸、圖片操作
1.顯示圖片
1.1獲取圖片,創(chuàng)建圖片對象
image.load()——獲取本地的一張圖片厘擂,返回圖片對象
image=pygame.image.load('./files/QQ截圖20180823160531.png')
1.2 get_size()——獲取圖片大小昆淡,返回值是一個元祖,有兩個元素刽严,分別是寬和高
image_width,image_height=image.get_size()
print(image_width,image_height)
1.3 渲染圖片(將圖片畫在紙上)
blit(渲染對象昂灵,位置)
位置(x,y)值的類型為元祖,兩個元素分別對應(yīng)x眨补,y
window.blit(image,(0,0))
1.4展示內(nèi)容將紙貼在畫框上
pygame.display.flip()
2管削,操作圖片
2.1形變:
transform.scale(縮放目標(biāo),目標(biāo)大小)——將指定的對象縮放到指定的大小撑螺,會返回縮放后的對象
new_image=pygame.transform.scale(image,(100,100))
2.2旋轉(zhuǎn)縮放(指定縮放比例)
transform.rotozoom(Surface,angle,scale)
Surface:旋轉(zhuǎn)縮放對象
angle:旋轉(zhuǎn)的對象
scale:縮放比例含思,大于1放大,小于1縮小
new_image1=pygame.transform.rotozoom(image,90,0.5)
2.3僅旋轉(zhuǎn):
transform.rotate(縮放對象,旋轉(zhuǎn)角度)
new_image2=pygame.transform.rotate(image,270)
三甘晤、文字操作(顯示文字)
1,創(chuàng)建字體對象
1.1,創(chuàng)建系統(tǒng)的字體對象
SysFont(name, size, bold=0, italic=0, constructor=None)
name:字體名(系統(tǒng)支持的字體)
size:字體大小
bold=0:是否加粗
italic=0:是否傾斜
font=pygame.font.SysFont('Times',30)
1.2,創(chuàng)建自定義的字體對象
Font(字體文件路徑含潘,字體大小)
font=pygame.font.Font('./files/aa.ttf',50)
2,根據(jù)字體創(chuàng)建文字對象
render(text, antialias, color, background=None)
text: 需要顯示的文本
antialias: 是否平滑(bool)
color:顏色
background=None:背景顏色
text=font.render('hello pygame',True,(100,100,255),(10,10,10))
四、圖形操作
1,畫直線
line(Surface,color,start_pos,end_pos,width=1)
Surface:畫在哪
color:顏色
start_pos:起點
end_pos:終點
width=1:線寬
# 水平線
pygame.draw.line(window,(255,0,0),(50,50),(500,50))
# 垂直線
pygame.draw.line(window,(255,0,0),(50,50),(50,500))
2线婚,畫線段(折線)
lines(Surface,color,closed,pointlist,width=1)
Surface:畫在哪
color:顏色
closed:是否閉合(連接起點和終點)
pointlist:點對應(yīng)的列表
width=1:線寬
pygame.draw.lines(window,(255,0,0),True,[(10,50),(20,100),(50,200)])
3,畫圓
circle(Surface,color,pos,radius,width=0)
Surface:畫在哪
color:顏色
pos:圓心坐標(biāo)
radius:半徑
width=1:線寬调鬓,0,:填充
pygame.draw.circle(window,(255,255,0),(300,300),100,0)
4,畫矩形
rect(Surface,color,Rect,width=0)
Rect:范圍(元祖,四個元素(x,y,width,height))
pygame.draw.rect(window,(0,255,0),(0,0,50,100))
5酌伊,畫多邊形
polygon(Surface,color,pointlist,width=0)
pygame.draw.polygon(window,(0,255,0),[(150,50),(150,100),(100.200),(200,200)],1)
6,畫橢圓
ellipse(Surface,color,Rect,width=0)
pygame.draw.ellipse(window,(0,255,0),(10,10,100,100))
7,畫弧線
arc((Surface,color,Rect,start_angle,stop_angle,width=0)
start_angle:開始角度
stop_angle:結(jié)束角度
pygame.draw.arc(window,(255,0,255),(200,200,300,100),0,pi,20)
五腾窝、事件
1,事件的type(event.type)----決定發(fā)生的什么事件
QUIT:關(guān)閉按鈕被點擊事件
鼠標(biāo)事件
MOUSEBUTTONDOWN:鼠標(biāo)按下
MOUSEBUTTONUP:鼠標(biāo)松開
MOUSEMOTION:鼠標(biāo)移動
鍵盤事件
KEYDOWN:鍵盤按下
KEYUP:鍵盤彈起
2居砖,事件的pos(event.pos)---鼠標(biāo)事件發(fā)生的位置(坐標(biāo))
3虹脯,事件的key---鍵盤事件被按的鍵盤的坐標(biāo)
for event in pygame.event.get():
# 不同的事件發(fā)生后對應(yīng)的type值不一樣
if event.type == pygame.QUIT:-----關(guān)閉按鈕被點擊事件
print('點擊關(guān)閉按鈕')
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
print(event.pos)
print('鼠標(biāo)按下')
elif event.type == pygame.MOUSEBUTTONUP:
print('鼠標(biāo)松開',event.pos)
elif event.type == pygame.MOUSEMOTION:
print('鼠標(biāo)移動')
draw_cirtle()
# 鍵盤
elif event.type == pygame.KEYDOWN:
print('鍵盤按下',chr(event.key))
elif event.type == pygame.KEYUP:
print('鍵盤彈起')
動畫效果
import pygame
# 初始化
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
pygame.display.flip()
# 球的圓心坐標(biāo)
x = 100
y = 100
r = 50
add = 4
y_speed = 2
# 游戲循環(huán)
while True:
# 延遲
# pygame.time.delay(10)
# 將之前紙上的內(nèi)容給覆蓋
window.fill((255, 255, 255))
# 不斷的畫圓
pygame.draw.circle(window, (255, 0, 0), (x, y), r)
pygame.display.update()
# 改變y值讓圓在垂直方向移動
y += y_speed
# r += add
# if r >= 120 or r <= 20:
# add *= -1
# 邊界檢測
if y > 600 - r:
y = 600 - r
y_speed = -2
elif y < 50:
y = 50
y_speed = 2
# 事件檢測
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
按住鼠標(biāo)不放,拖動圖片
import pygame
if __name__ == '__main__':
# 游戲初始化
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
# pygame.display.flip()
# 1.顯示一張圖片
image = pygame.image.load('./files/luffy4.jpg')
# 縮放
image = pygame.transform.rotozoom(image, 0, 0.5)
window.blit(image, (100, 100))
# 獲取圖片的寬度和高度
image_w, image_h = image.get_size()
pygame.display.flip()
# 用來存儲圖片是否移動
flag = False
# 保存圖片的坐標(biāo)
image_x, image_y = 100, 100
# 游戲循環(huán)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 鼠標(biāo)按下
if event.type == pygame.MOUSEBUTTONDOWN:
# 判斷按下的位置是否在圖片上
m_x, m_y = event.pos
if image_x<=m_x<=image_x+image_w and image_y<=m_y<=image_y+image_h:
flag = True
elif event.type == pygame.MOUSEBUTTONUP:
flag = False
# 鼠標(biāo)移動事件
# (鼠標(biāo)在移動并且flag是True)
if event.type == pygame.MOUSEMOTION and flag:
# 填充背景色,覆蓋原來的內(nèi)容
window.fill((255, 255, 255))
# 在鼠標(biāo)移動的位置渲染圖片
# window.blit(image, event.pos)
center_x, center_y = event.pos
image_x, image_y = center_x - image_w/2, center_y-image_h/2
window.blit(image, (image_x, image_y))
# 更新屏幕的顯示
pygame.display.update()