异常捕获
try:
"""
需要捕获异常的代码
"""
except:
"""
出现异常会出现的代码
"""
try:
'''
出现异常会出现的代码
'''
except '''错误类型''':
'''
捕获到指定的错误类型,才执行的代码
'''
if __name__ == '__main__':
try:
a=0
b=3
print(b/a)
except ZeroDivisionError:
print('error')
pygame操作流程
# 初始化
pygame.init()
# 创建窗口
screen =pygame.display.set_mode((600,400))
# 游戏循环
while True:
# 检测事件
for event in pygame.event.get():
# 检测窗口关闭按钮是否被点击
if event.type == pygame.QUIT:
print('game over')
exit()
# 退出游戏
# 其他操作
pygame显示文字
pygame.init()
screen = pygame.display.set_mode((600,400))
screen.fill((230,230,39))
# 设置窗口背景颜色
# 创建字体对象
# 创建系统字体
# SysFont(name, size, bold=0, italic=0, constructor=None):
# name-字体名 size-字体大小 bold-加粗 italic-倾斜
# font = pygame.font.SysFont('Times',20)
# 2.根据字体创建显示对象
# 创建自定义字体Font(字体文件路径,字体大小)
font = pygame.font.Font('./font/aa.ttf',50)
surface = font.render('hello开始',True,(230,39,39))
# text-要显示的文字内容,antialias-抗锯齿,color-颜色,backgroud-背景
# color->计算机三原色(红绿蓝),值的范围都是0-255
# render(self, text, antialias, color, background=None)
# 3.将内容添加到窗口上
# blit(需要显示的对象,显示位置)
# 显示的对象-->Surface类型的数据
# 显示位置-->坐标(x,y)
#
screen.blit(surface,(100,100))
# 4.将窗口上的内容展示出来
# pygame.display.flip()
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
pygame显示图片与图片操作
pygame.init()
screen = pygame.display.set_mode((324,310))
screen.fill((230,39,39))
font = pygame.font.Font('./font/aa.ttf',20)
surface = font.render('路飞',True,(230,39,39))
# 1.获取图片对象
image = pygame.image.load('./file/2.jpg')
image_size = image.get_size()
# 获取图片大小
print(image_size)
# 形变,transform:形变包含缩放scale、旋转、平移
# scale1 = pygame.transform.scale(image,(200,200))
# screen.blit(scale1,(5,5))
# rotate(旋转对象,旋转角度)负数为顺时针反向
# rotate1 = pygame.transform.rotate(image,-45)
# screen.blit(rotate1,(5,5))
# rotozoom(Surface,angle,scale)旋转缩放,参数:对象,角度,缩放比例
# rotozoom1 = pygame.transform.rotozoom(image,0,0.5)
# screen.blit(rotozoom1,(5,5))
# 2.将图片对象渲染到窗口上
# screen.blit(image,(5,5))
# 3.展示屏幕上
screen.blit(surface,(5,10))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
pygame基本显示
screen = pygame.display.set_mode((400,400))
screen.fill((230,200,0))
'''
1.画直线
line(Surface,color,start_pos,end-pos,width=1)
Surface->画在哪里 start_pops->起点
'''
# 正方形
# pygame.draw.line(screen,(255,0,0),(0,0),(0,200),2)
# pygame.draw.line(screen, (255, 0, 0), (0, 0), (200, 0), 2)
# pygame.draw.line(screen, (255, 0, 0), (0, 200), (200, 200), 2)
# pygame.draw.line(screen, (255, 0, 0), (200, 200), (200, 0), 2)
# 多个点closed关闭
# pygame.draw.line(screen, (255, 0, 0), False,[], 2)
# 展示
# 画曲线
# arc(Surface,color,Rect,start_angle,stop_angle,width=1)
# Rect->(x,y.width,height)矩形
# start_angle-开始角度
# 4分之一圆
# 在定义的矩形框内画内切圆,开始角度与结束角度决定圆的范围
from cmath import pi
pygame.draw.arc(screen,(0,0,0),(200,200,100,100),pi,2*pi)
pygame.draw.arc(screen, (0, 0, 0), (250, 300, 5, 5), pi, 2*pi)
pygame.draw.arc(screen, (0, 0, 0), (0, 200, 100, 100), 0, 2*pi)
pygame.draw.arc(screen, (0, 0, 0), (50, 300, 5, 5), 0, 2*pi)
# pygame.draw.arc(screen, (0, 0, 0), (200, 200, 100, 100), pi / 2, pi)
# 画圆
# circle(位置,颜色,圆心位置,半径,width-0)
import random
pygame.draw.circle(screen,(random.randint(0,255),20,20),(100,100),50)
pygame.display.flip()
# 椭圆的画法:矩形内切 (0,2pi)或ellipse
pygame.draw.ellipse(screen,(0,100,0),(100,100,200,80),1)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
exit()