1.record
1.json數(shù)據(jù):
標準格式:
a.只能是一個數(shù)據(jù)
b.數(shù)據(jù)必須是json支持的數(shù)據(jù)類型
python模塊中的方法:load级历,loads靖秩,dump饿悬,dumps
2.異常捕獲:try-except-finally
3.第三方庫的導入
2.my_pygame
1.初始化游戲模塊
import pygame
pygame.init()
2.創(chuàng)建游戲窗口(set_mode設置窗口打下床嫌,并且返回)慷暂,窗口大小是一個元祖荧飞,元祖中需要兩個值烙无,分別是寬度和高度毫捣。單位:像素
window = pygame.display.set_mode((600, 600))
3.給窗口填充顏色
window .fill((255, 255, 255))
4.讓游戲一直運行详拙,直到點關閉按鈕結束
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
3.顯示圖片
import pygame
pygame.init()
window = pygame.display.set_mode((600, 600))
window.fill((255, 255, 255))
**獲取圖片,創(chuàng)建圖片對象**
image = pygame.image.load('./images/03.jpg')
**getsize():獲取大小蔓同,返回值是一個元祖饶辙,有兩個元素,分別是寬和高**
image_width, image_higth = image.get_size()
**渲染圖片**
window.blit(image,(0, 0))
**展示內(nèi)容**
pygame.display.flip()
**游戲循環(huán)**
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
4.形變
**初始化**
import pygame
pygame.init()
**創(chuàng)建窗口填充**
window = pygame.display.set_mode((500, 600))
window.fill((255, 255, 255))
**顯示圖片**
image = pygame.image.load('./images/04.jpg')
**縮放**transform.scale(縮放對象的大小)斑粱,將指定的對象縮放到指定的大小弃揽,返回縮放后的對象
new_image = pygame.transform.scale(image,(400, 600) )
**旋轉縮放**
//rotozoom(SurFcae, angle, scale)
//SurFace;旋轉對象
//angle:旋轉角度
//scale:縮放比例
new_image = pygame.tansform.rotozoom(iamge,(0, 0.5)) //縮小0.5
new_image = pygame.transform.rotozoom(iamge,45,0.5 )//旋轉45度,在縮小0.5
**顯示內(nèi)容**
pygame.display.flip()
**游戲循環(huán)**
while True"
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
5.顯示循環(huán)
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
window.fill((255, 255, 255))
顯示文字
1.創(chuàng)建字體對象
創(chuàng)建系統(tǒng)的字體對象:SysFont(name,size,bold = False, italic = False)
name:字體名
size:字體大小
bold:是否加粗
2.創(chuàng)建自定義的字體對象
Font(字體文件路徑则北,字體大锌笪ⅰ)
字體文件路徑:ttf文件
創(chuàng)建系統(tǒng)字體
font = pygame.font.SysFont('times',30)
創(chuàng)建自定義字體
font = pygame.font.Font('./files/aa.ttf', 30)
根據(jù)字體去創(chuàng)建文字對象
text = font.render('你好,pygame', True, (0, 0, 255),(255, 255, 0) )
渲染文件
window.blit(text, (50, 50))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
6.顯示圖形
import pygame
pygame.init()
window = pygame.display.set_mode((500,600))
window.fill((255, 255, 255))
1.畫直線
def line(Surface, color, start_pos, end_pos)
Surface:畫在那
color:線的顏色
start_pos:起點
end_pos:終點
width:線寬
2.畫水平線
pygame.draw.line(window,(255, 0, 0), (50, 100), (300, 100), 2)
畫錘線
pygame.draw.line(window,(0, 255, 0), (50, 100), (50, 300))
2.畫線段
def lines(Surface, color, closed, pointlist, )
Surface:花在哪
color:顏色
closed: True
pointlist:
3.畫圓
def circle(Surface, color, pos, radius, width)
Surface:畫在那
color:顏色
pos:圓心坐標
radius:半徑
width:線寬
4.畫矩陣
def rect(Surface, color, pos)
pygame.draw.rect(window, (255, 0, 0), (0, 100, 10, 20),)
pygame.draw.circle(window, (255, 255, 0), (200, 200), 150)
pygame.draw.lines(window, (0, 255, 0), True, [(100, 200), (150, 120), (140, 300)])
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
7.事件
import pygame
from random import randint
pygame.init()
windows = pygame.display.set_mode((400, 600))
windows.fill((255, 255, 255))
image = pygame.image.load('./images/04.jpg')
windows.blit(image,(0, 0))
pygame.display.flip()
while True:
//所有的時間處理的入口就是這個for循環(huán)
//for循環(huán)中代碼只有游戲事件發(fā)生后才會執(zhí)行
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
print('鼠標按下')
image = pygame.image.load('./images/05.jpg')
window.blit(image, (0, 0))
pygame.display.flip()
elif event.type == pygame.MOUSETTONUP:
print('鼠標彈起')
image = pygame.image.load('./images/06.jpg')
window.blit(image, (0, 0))
pygame.display.flip()
elif event.type == pygame.MOUSEMOTION:
print('鼠標正在移動')
8.動畫原理
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
window.fill((255, 255, 0))
x =150
y =150
y_speed = 1
x_speed = 1
while True:
//將之前紙上的內(nèi)容覆蓋
window.fill((255, 255, 0))
pygame.draw.circle(window, (255, 0, 0),(x, y), 10)
x += x_speed
y += y_speed
y +=y_speed
if x >790:
x_speed *=-1
elif x<10:
x_speed *= -1
if y<10:
y_speed *= -1
elif y>790:
y_speed *= -1
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
9.按住不放原理
import pygame
pygame.init()
window = pygame.display.set_mode((800, 800))
window.fill((255, 255, 255))
image = pygame.image.load('./images/04.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 = 100
image_y = 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:
# 填充背景色尚揣,覆蓋原來的內(nèi)容
window.fill((255, 255, 255))
# 在鼠標移動的位置渲染圖片
# window.blit(image, event.pos)
center_x, centet_y = event.pos
image_x, image_y = center_x - image_w /2, centet_y - image_h /2
window.blit(image,(center_x - image_w /2, centet_y - image_h /2))
pygame.display.update()