01.Pygame基本操作
1.初始化游戲模塊
pygame.init()
2.創(chuàng)建游戲窗口
"""
set_mode(窗口大小)
窗口大猩盅帧:是一個元祖,并且元祖中需要兩個值分別表示寬度和高度
(單位是像素)
"""
window=pygame.display.set_mode((400,600))
3.讓游戲一直運行谴咸,直到點關(guān)閉按鈕才結(jié)束
while flag:
# 獲取當(dāng)前游戲的所有時間
for event in pygame.event.get():
# type來判斷事件的類型
if event.type==pygame.QUIT:
exit()#退出程序
02.顯示圖片
1.初始化游戲模塊
pygame.init()
2.創(chuàng)建窗口
window = pygame.display.set_mode((600,400))
# 給窗口填充顏色
3.fill(顏色)
顏色:計算機三顏色:(紅綠藍(lán)),每個顏色對應(yīng)的值的范圍是0-255.可以通過改變?nèi)闹悼梢哉{(diào)配出不同顏色岭佳。
顏色值:是一個元祖,元祖中三個元素珊随,分別代表紅綠藍(lán)(rgb)
(255,0,0) ---> 紅色
(0,255,0) ---> 綠色
(0,0,255) ---> 藍(lán)色
(0,0,0) ---> 黑色
(255,255,255) ---> 白色
4.顯示圖片
image.load(圖片路徑):獲取本地的一張圖片述寡,返回圖片對象
a.獲取圖片,創(chuàng)建圖片對象
image=pygame.image.load('./files/123.png')
1).get_size():獲取大小鲫凶,返回值是一個元祖,有兩個元素螟炫,分別是高和寬
image_width, image_height = image.get_size()
b.渲染圖片(將圖片畫在紙上)
blit(渲染對象,位置)
位置:坐標(biāo)(x,y)昼钻,值的類型是元祖掸屡,元祖有兩個元素分別對應(yīng)x坐標(biāo)y坐標(biāo)
window.blit(image,(0,100))
window.blit(image,(600-image_width,400-image_width))
c.展示內(nèi)容(展示圖片)
pygame.display.flip()
03.形變
圖片相關(guān)
1.加載圖片(選圖)
image = pygame.image.load('./files/123.png')
形變:
a.縮放(指定大小)
transform.scale(縮放對象仅财,目標(biāo)大小):將指定的對象縮放到指定的大小,會返回縮放后的對象
new_image=pygame.transform.scale(image,(400,600))
b.縮放(指定縮放比例)
rotozoom(Surface,angle,scale)
-Surface:旋轉(zhuǎn)縮放對象
-angle:旋轉(zhuǎn)角度(0-360)
-scale:縮放比例(0,-x,x),大于1放大沾瓦,小于1縮小
new_image=pygame.transform.rotozoom(image,99,
rotate(Surface,angle)
-Surface:旋轉(zhuǎn)對象
-angle:旋轉(zhuǎn)角度
new_image=pygame.transform.rotate(image,270)
2.渲染圖片
window.blit(new_image,(9,9)) #圖片放置位置設(shè)定
3.展示內(nèi)容
pygame.display.flip()
04.文字
顯示文字
1.創(chuàng)建字體對象
a.創(chuàng)建系統(tǒng)的字體對象
SysFont(name,size,bold=Folse,italic=False)
-name:字體名(系統(tǒng)支持的字體名)
-size:字體大小
-bold:是否加粗
-italic:是否傾斜
b.創(chuàng)建自定義的文字對象
-Font(字體文件路勁谦炒,字體大小)
字體文件路徑:ttf文件
1).創(chuàng)建系統(tǒng)文件
font = pygame.font.SysFont('Times',30)
2).創(chuàng)建自定義文件
font = pygame.font.Font('./files/aa.ttf',30)
2.根據(jù)字體創(chuàng)建文字對象
render(text,antialias,color,backgrand=None)
-text:需要顯示的文字(字符串)
-antialias:是否平滑(布爾)
-color:顏色
-backgrand:背景顏色
text=font.render('Ftr_2027',True,(255,255,255),(0,0,0))
3.渲染文字
window.blit(text,(139,250))
05.顯示圖形
1.畫線段
line(Surface,color,start_pos,end_pos,width=1)
-Surface:畫在哪
-color:線的顏色
-start_pos:起點
-end_pos:終點
-width:線寬
畫一條水平線
pygame.draw.line(window,(255,0,0),(50,100),(200,100))
畫一條垂直線
pygame.draw.line(window,(255,255,0),(50,100),(50,200),9)
2.畫線段(折線)
lines(Surface,color,closed,pointlist,width=1)
-Surface:畫在哪
-color:線的顏色
-closed:是否閉合(終點起點的連接)
-pointlist:點對應(yīng)的列表
pygame.draw.lines(window,(0,0,0),True,[(100,200),(150,120),(99,99)])
3.畫圓
circle(Surface,color, pos ,radius,width=0)
位置 顏色 圓心坐標(biāo) 半徑 線寬 0 ->填充
pygame.draw.circle(window,(0,0,0),(200,200),100,9)
4.畫矩形
rect(Surface,color,Rect,width=0)
位置 顏色
范圍(元祖宁改,元祖中有四個元素,分別是x,y,width,height)
pygame.draw.rect(window,(0,255,0),(0,100,50,100))
5.畫多邊形
polygn(Surface,color,pointlist,width=0)
#pointlist: 多邊形坐標(biāo)點
pygame.draw.polygon(window,(0,255,255),[(350,0),(350,400),(200,200)])
6.畫橢圓
ellipse(Surface,color,Rect,width=0)
pygame.draw.ellipse(window,(123,200,210),(99,150,150,100))
7.畫弧線
arc(Surface,color,Rect,start_angle,stop_angele)
-start_angle:開始
-stop_angle:結(jié)束
"""
pi --- 180° 1°--- pi/180°
59° = pi/180° * 59°
"""
pygame.draw.arc(window,(200,120,10),(200,400,100,100),pi/4+pi,pi*3/4+pi,3)
06.事件
1.所有事件的處理入口就是這個for循環(huán)
2.for循環(huán)中的代碼只有游戲事件發(fā)生后才會執(zhí)行
3.事件的type:---決定發(fā)生的是什么事件
4.QUIT:關(guān)閉按鈕被點擊事件
a.鼠標(biāo)事件:
MOUSEBUTTONDOWN:鼠標(biāo)按下事件
MOUSEBUTTONUP:鼠標(biāo)彈起事件
MOUSEMOTION:鼠標(biāo)移動事件
b.鍵盤事件:
KEYDOWN:鍵盤按下
KEYUP:鍵盤彈起
b.事件的pos --- 鼠標(biāo)事件發(fā)生的位置(坐標(biāo))
c.事件的key --- 鍵盤事件被按的鍵對應(yīng)的編碼值
for event in pygame.event.get():
#不同的事件發(fā)生后爹耗,對應(yīng)type值不一樣
if event.type == pygame.QUIT:
print('點擊關(guān)閉按鈕')
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠標(biāo)按下要做的事情就寫在這兒
print(event.pos)
print('鼠標(biāo)按下')
pygame.display.flip()
elif event.type == pygame.MOUSEBUTTONUP:
print("鼠標(biāo)彈起",event.pos)
elif event.type == pygame.MOUSEMOTION:
# print("鼠標(biāo)移動")
pygame.draw.circle(window, (randint(0, 255), randint(0, 255), \
randint(0, 255)), event.pos, randint(20, 50))
pygame.display.flip()
pass
elif event.type == pygame.KEYDOWN:
print("鍵盤按下",event.key,chr(event.key))
elif event.type == pygame.KEYUP:
print("鍵盤彈起")
07.動圖原理
import pygame
from random import randint
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
pygame.display.flip()
# 球的圓心坐標(biāo)
x = 50
y = 50
r = 50
y_speed=2
x_speed=1
# 游戲循環(huán)
while True:
pygame.time.delay(9)
# 將之前紙上的內(nèi)容覆蓋
window.fill((255,255,255))
# 不斷的畫圓
pygame.draw.circle(window,(0,0,0),(x,y),r)
pygame.display.update()#刷新
# 改變Y值讓圓在垂直方向移動
y+=y_speed
if y>600-r:
y=600-r
y_speed=-2
elif y<50:
y=50
y_speed=2
x += x_speed
if x > 400 - r:
x = 400 - r
x_speed = -2
elif x < 50:
x = 50
x_speed = 2
# 事件循環(huán)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
08.按住不放原理
import pygame
# 游戲初始化
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
# pygame.display.flip()
# 1.顯示一張圖片
image = pygame.image.load('./files/123.png')
# 縮放
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()