一、pygame基本操作
1.導入pygame模塊
import pygame
2.初始化游戲模塊
pygame.init()
3.創建游戲窗口
- display.set_mode(窗口大小):創建一個窗口并且返回
- 窗口大?。菏且粋€元祖,并且元祖中需要兩個值分別表示寬度和高度(單位是像素)
pygame.display.set_mode((600,400))
4.讓游戲一直運行,知道點擊關閉按鈕才結束
game = True
while game:
#獲取游戲過程中產生的所有事件
for event in pygame.event.get():
# type來判斷事件的類型
if event.type == pygame.QUIT:
#exit() 退出程序
game = False
import pygame
if __name__ == '__main__':
#1.初始化游戲模塊
pygame.init()
#2.創建游戲窗口
'''
display.set_mode(窗口大小):創建一個窗口并且返回
窗口大?。菏且粋€元祖,并且元祖中需要兩個值分別表示寬度和高度(單位是像素)
'''
pygame.display.set_mode((600,400))
#3.讓游戲一直運行,直到點擊關閉按鈕才結束
game = True
while game:
#獲取游戲過程中產生的所有事件
for event in pygame.event.get():
# type來判斷事件的類型
if event.type == pygame.QUIT:
#exit() 退出程序
game = False
二、顯示圖片
創建窗口
1. 給窗口填色
window.fill((255,255,255))
a.顏色:計算機三原色(紅,綠,藍),每個顏色對應的值范圍是0~255,通過改變三原色的值可以調配出不同的顏色
b.顏色值:是一個元祖,元祖中三個元素,分別代表紅綠藍(RGB)
顏色 | 值 |
---|---|
紅色 | (255,0,0) |
l綠色 | (0,255,0) |
藍色 | (0,0,255) |
黑色 | (0,0,0) |
白色 | (255,255,255) |
2.顯示圖片
- image.load(圖片路徑):獲取本地的一種圖片,返回圖片對象
- 獲取圖片,創建圖片對象
image = pygame.image.load('./picture/kobe.jpg')
- get_size():獲取大小,返回值是一個元祖,有兩個元素,分別是寬和高
image_width,image_height = image.get_size()
- b.渲染圖片(將圖片畫在紙上)
'''
blit(渲染對象,位置)
位置:坐標(x,y),值的類型是元祖,元祖有兩個元素分別是對應X坐標和Y坐標
window.blit(image,(75,40))
3.展示內容
pygame.display.flip()
import pygame
if __name__ == '__main__':
#1.初始化游戲模塊
pygame.init()
#2.創建窗口
window = pygame.display.set_mode((660,400))
# 給窗口填充顏色
'''
fill(顏色)
顏色:計算機三原色(紅、綠、藍),每個顏色對應的值的范圍是0~255.可以通過改變三原色的值調配出不同的顏色
顏色值:是一個元祖,元祖中三個元素,分別代表紅綠藍(RGB)
(255,0,0) ---> 紅色
(0,255,0) ---> 綠色
(0,0,255) ---> 藍色
(0,0,0) ----> 黑色
(255,255,255) ---> 白色
'''
window.fill((255,255,255))
'''
顯示圖片
image.load(圖片路徑):獲取本地的一種圖片,返回圖片對象
'''
# a.獲取圖片,創建圖片對象
image = pygame.image.load('./picture/kobe.jpg')
'''
get_size():獲取大小,返回值是一個元祖,有兩個元素,分別是寬和高
'''
image_width,image_height = image.get_size()
# b.渲染圖片(將圖片畫在紙上)
'''
blit(渲染對象,位置)
位置:坐標(x,y),值的類型是元祖,元祖有兩個元素分別是對應X坐標和Y坐標
'''
window.blit(image,(75,40))
# c.展示內容(將紙貼在畫框上)
pygame.display.flip()
#3.游戲循環
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
三、形變
a.縮放為指定大小
transform.scale(縮放對象,目標大小):將指定的對象縮放到指定的大小,會返回縮放后的對象
new_image = pygame.transform.scale(image,(500,350))
b.縮放指定比例
transform.rotozoom(Surface,angle,scale),rotate(Surface,angle)
- Surface:旋轉縮放對象
- angle:旋轉的角度(0~360)
- scale:縮放比例(大于1放大,小于1縮?。?/li>
new_image = pygame.transform.rotozoom(image,30,1)
new_image1 = pygame.transform.rotate(image,45)
import pygame
if __name__ == '__main__':
#初始化和創建窗口
pygame.init()
window = pygame.display.set_mode((600,400))
window.fill((255,255,255))
#圖片相關
#1.加載圖片(選圖)
image = pygame.image.load('./picture/kobe.jpg')
'''
形變:
a.縮放(指定大小)
transform.scale(縮放對象,目標大小):將指定的對象縮放到指定的大小,會返回縮放后的對象
'''
# new_image = pygame.transform.scale(image,(500,350))
'''
b.縮放(指定縮放比例)
rotozoom(Surface,angle,scale),rotate(Surface,angle)
Surface:旋轉縮放對象
angle:旋轉的角度(0-360)
scale:縮放比例(大于1放大,小于1縮小)
'''
new_image = pygame.transform.rotozoom(image,30,1)
new_image1 = pygame.transform.rotate(image,45)
#2.渲染圖片
window.blit(new_image1,(0,0))
#3.展示內容
pygame.display.flip()
# 游戲循環
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
四、顯示文字
1.創建字體對象
a.創建系統的字體對象
- SysFont(name, size, bold=False, italic=False)
- name:字體名(系統支持的字體名)
- size:字體大小
- bold:是否加粗
- italic:是否傾斜
font = pygame.font.SysFont('Times',30)
b.創建自定義的字體對象
Font(字體文件路徑,字體大?。?/p>
font = pygame.font.Font('./picture/aa.ttf',30)
2.根據字體去創建文字對象
- render(self, text, antialias, color,background=None)
- text:需要顯示的文字(字符串)
- antialias:是否平滑(布爾)
- color:顏色
- background:(背景色)
text = font.render('你好,李涵',True,(0,0,255),(200,200,200))
import pygame
if __name__ == '__main__':
# 初始化游戲
pygame.init()
window = pygame.display.set_mode((600,400))
window.fill((255,255,255))
# 顯示文字
# 1.創建字體對象
'''
a.創建系統的字體對象
SysFont(name, size, bold=False, italic=False)
name:字體名(系統支持的字體名)
size:字體大小
bold:是否加粗
italic:是否傾斜
b.創建自定義的字體對象
Font(字體文件路徑,字體大小)
'''
#a.創建系統字體
# font = pygame.font.SysFont('Times',30)
#b.創建自定義字體
font = pygame.font.Font('./picture/aa.ttf',30)
#2.根據字體去創建文字對象
'''
render(self, text, antialias, color,background=None)
text:需要顯示的文字(字符串)
antialias:是否平滑(布爾)
color:顏色
background:
'''
text = font.render('你好,李涵',True,(0,0,255),(200,200,200))
#3.渲染文字
window.blit(text,(100,100))
#4.展示內容
pygame.display.flip()
# 游戲循環
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
五、顯示圖形
1.畫直線
- def 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, (0, 255, 0), (50, 100), (50, 200),20)
2.畫線段(折線)
- def lines(Surface,color,closed,pointlist,width=1)
- Surface:畫在哪里
- color:線的顏色
- closed:是否閉合(是否連接起點和終點)
- pointlist:點對于的列表
pygame.draw.lines(window,(0,255,0),True,[(100,200),(150,200),(200,300),200])
3.畫圓
- def circle(Surface, color, pos, radius, width=0)
- Surface:畫在哪里
- color:線的顏色
- pos:圓心坐標
- radius:半徑
- width:線寬 0 -->填充
pygame.draw.circle(window,(225,215,21),(400,200),100,20)
4.矩形
- def rect(Surface, color, Rect, width=0)
- Surface:畫在哪里
- color:線的顏色
- Rect:范圍(元祖,元祖中有四個元素,分別是x,y,width,height)
pygame.draw.rect(window,(255, 0, 0),(500, 300, 50, 100),1)
5.畫多邊形
- def polygon(Surface, color, pointlist, width=0)
- Surface:畫在哪里
- color:線的顏色
- pointlist:點對于的列表
pygame.draw.polygon(window,(0,255,0),[(321,50),(143,100),(240,150),(210,200),(330,250),4])
import pygame
if __name__ == '__main__':
pygame.init()
window = pygame.display.set_mode((600,400))
window.fill((255,255,255))
'''
1.畫直線
def 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, (0, 255, 0), (50, 100), (50, 200),20)
'''
2.畫線段(折線)
def lines(Surface,color,closed,pointlist,width=1)
Surface:畫在哪里
color:線的顏色
closed:是否閉合(是否連接起點和終點)
pointlist:點對于的列表
'''
pygame.draw.lines(window,(0,255,0),True,[(100,200),(150,200),(200,300),200])
#3.畫圓
'''
def circle(Surface, color, pos, radius, width=0)
Surface:畫在哪里
color:線的顏色
pos:圓心坐標
radius:半徑
width:線寬 0 -->填充
'''
pygame.draw.circle(window,(225,215,21),(400,200),100,20)
'''
4.矩形
def rect(Surface, color, Rect, width=0)
Surface:畫在哪里
color:線的顏色
Rect:范圍(元祖,元祖中有四個元素,分別是x,y,width,height)
'''
pygame.draw.rect(window,(255, 0, 0),(500, 300, 50, 100),1)
'''
5.畫多邊形
def polygon(Surface, color, pointlist, width=0)
Surface:畫在哪里
color:線的顏色
pointlist:點對于的列表
'''
pygame.draw.polygon(window,(0,255,0),[(321,50),(143,100),(240,150),(210,200),(330,250),4])
#展示內容
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
六、事件
1.所有的事件處理的入口就是這個for循環
2.for循環中代碼只有游戲事件發生后才會執行
事件的type:
- QUIT: 關閉按鈕被點擊事件
- MOUSEBUTTONDOWN:按下鼠標
- MOUSEBUTTONUP:松開鼠標
- MOUSEMOTION:移動鼠標
鍵盤事件:
- KEYDOWN:鍵盤按下
- KEYUP:鍵盤彈起
3.事件的pos --- 鼠標事件發生的位置(坐標)
4.事件的key值 --- 鍵盤事件被按的鍵相對應的編碼值
import pygame
from random import randint
if __name__ == '__main__':
pygame.init()
window = pygame.display.set_mode((600, 400))
window.fill((255, 255, 255))
pygame.display.flip()
while True:
# 所有的事件處理的入口就是這個for循環
# for循環中代碼只有游戲事件發生后才會執行
'''
事件的type:
QUIT: 關閉按鈕被點擊事件
MOUSEBUTTONDOWN:按下鼠標
MOUSEBUTTONUP:松開鼠標
MOUSEMOTION:移動鼠標
鍵盤事件:
KEYDOWN:鍵盤按下
KEYUP:鍵盤彈起
b.事件的pos --- 鼠標事件發生的位置(坐標)
c.事件的key值 --- 鍵盤事件被按的鍵相對應的編碼值
'''
for event in pygame.event.get():
if event.type == pygame.QUIT:
print('點擊關閉按鈕')
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
#鼠標按下做的事情
pygame.draw.circle(window, (randint(0, 255), randint(0, 255), randint(0, 255)), event.pos, 20)
pygame.display.flip()
print(event.pos)
print('按下鼠標')
elif event.type == pygame.MOUSEBUTTONUP:
pygame.draw.circle(window, (randint(0, 255), randint(0, 255),randint(0,255)), event.pos, randint(20, 50) )
pygame.display.flip()
print('鼠標彈起')
elif event.type == pygame.MOUSEMOTION:
print('鼠標正在移動')
elif event.type == pygame.KEYDOWN:
print(event.key)
print('鍵盤按下')
elif event.type == pygame.KEYUP:
print('鍵盤彈起')
七、動畫原理
不斷生成一個新的圖形讓他移動,并且用背景色把之前的圖案覆蓋,產生動畫的效果
if __name__ == '__main__':
# 初始化
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
pygame.display.flip()
# 球的圓心坐標
x = 100
y = 100
r = 50
add = 4
y_speed = 2
# 游戲循環
while True:
# 延遲
# pygame.time.delay(10)
# 將之前紙上的內容給覆蓋
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()
八、按住圖案移動的原理
顯示一張圖片,獲取圖片的長度和寬度并保存圖片的坐標,判定鼠標是否在圖片的的坐標和長寬度上面,按下鼠標讓圖片移動
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('./11.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
# 保存圖片的坐標
image_x, image_y = 100, 100
# 游戲循環
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 鼠標按下
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
# 鼠標移動事件
# (鼠標在移動并且flag是True)
if event.type == pygame.MOUSEMOTION and flag:
# 填充背景色,覆蓋原來的內容
window.fill((255, 255, 255))
# 在鼠標移動的位置渲染圖片
# 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()