1.record
1.json数据:
标准格式:
a.只能是一个数据
b.数据必须是json支持的数据类型
python模块中的方法:load,loads,dump,dumps
2.异常捕获:try-except-finally
3.第三方库的导入
2.my_pygame
1.初始化游戏模块
import pygame
pygame.init()
2.创建游戏窗口(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))
**获取图片,创建图片对象**
image = pygame.image.load('./images/03.jpg')
**getsize():获取大小,返回值是一个元祖,有两个元素,分别是宽和高**
image_width, image_higth = image.get_size()
**渲染图片**
window.blit(image,(0, 0))
**展示内容**
pygame.display.flip()
**游戏循环**
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
4.形变
**初始化**
import pygame
pygame.init()
**创建窗口填充**
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
**显示内容**
pygame.display.flip()
**游戏循环**
while True"
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
5.显示循环
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
window.fill((255, 255, 255))
显示文字
1.创建字体对象
创建系统的字体对象:SysFont(name,size,bold = False, italic = False)
name:字体名
size:字体大小
bold:是否加粗
2.创建自定义的字体对象
Font(字体文件路径,字体大小)
字体文件路径:ttf文件
创建系统字体
font = pygame.font.SysFont('times',30)
创建自定义字体
font = pygame.font.Font('./files/aa.ttf', 30)
根据字体去创建文字对象
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循环
//for循环中代码只有游戏事件发生后才会执行
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:
//将之前纸上的内容覆盖
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:
# 填充背景色,覆盖原来的内容
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()