day10-异常与pygame

01.异常

  • 异常捕获
try:
    需要捕获异常的代码(就是这段代码如果出现异常,不让程序崩溃)
except:
    只要try后面的代码出现异常都会执行这行代码,并且程序不会崩溃
try:
    需要捕获异常的代码块
except 异常类型:
    出现指定异常后才会执行
  • try--except执行过程:先执行try后面的代码块,只要出现异常就使用except去捕获,如果能捕获到,就直接进入except执行里面的代码块,执行完成后,再执行后面的其他语句。如果捕获不到,就直接报错。
    如果try后面的代码块中,没有异常。那么执行完代码块中的内容直接执行其他的语句

  • 想要捕获多个异常:except(错误类型1,错误类型2....):

try:
    代码块1
except:
    代码块2
finally:
    代码块3
try:
    代码块1
except 错误类型1:
    代码块2
except 错误类型2:
    代码块3

代码块3是在代码块1中没有异常和代码块1中出现异常被捕获到都会执行

raise:抛出异常
  • 总结:1.异常捕获不是什么时候都要用的,只有在程序员清楚会出现异常,并且想要自己来处理异常,而不是让程序崩溃的情况才异常捕获
    2.使用异常捕获的时候,不能让except直接捕获到所有的异常,而是捕获特定异常
if __name__ == '__main__':

    try:
        print([1,2,3][5])
        print({'a': 'b', 'b': 1}['c'])
    except (IndexError,KeyError):
        print('出现异常')

    print('=======')

02.pygame窗口

import pygame
if __name__ == '__main__':
    # 1.初始化pygame
    pygame.init()

    # 2.创建游戏窗口
    # set_mode(宽度,高度)
    screen = pygame.display.set_mode((600,400))

    # 3.游戏循环
    while True:
        # 检测事件
        for event in pygame.event.get():
            # 检测窗口上的关闭按钮是否被点击
            if event.type == pygame.QUIT:
                # 退出游戏
                print('关闭按钮被点击')
                exit()

        # 其他操作

03.pygame-显示名字

import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    # 设置窗口的背景颜色
    screen.fill((255,255,255))


    # 1.创建字体对象
    """
    创建系统字体
    SysFont(name, size, bold=0, italic=0)
    name --->字体名
    size --->字体大小
    bold --->加粗
    italic -->倾斜
    """
    # font = pygame.font.SysFont('Times',80)
    """
    创建自定义字体
    Font(字体文件路径,字体大小)
    """
    font = pygame.font.Font('./font/font/aa.ttf',80)


    # 2.根据字体去创建显示对象(文字)
    """
     render(self, text, antialias, color, background=None)
    text ->要显示的文字内容
    antialias ->是否平滑
    color ->计算机三原色(红绿蓝),RGB颜色值的范围都是0-255
            (255,0,0) ->红色
            (0,255,0) ->绿色
            (0,0,255) ->蓝色
            (0,0,0)->黑色
            (255,255,255)->白色
            (X,X,X)->灰色
    """
    surface  = font.render('七一欧',True,(255,0,0))

    # 3.将内容添加到窗口上
    """
    blit(需要显示的对象,显示的位置)
    需要显示的对象 -->SurFace类型的数据
    显示位置--->坐标(x,y)
    """
    screen.blit(surface,(100,100))
    # 将窗口上的内容展示出来(将画有文字的纸贴出来)
    pygame.display.flip()
    # 游戏循环
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

04.pygame显示图片

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,600))
    screen.fill((255,255,255))

    # 1.获取图片对象
    image = pygame.image.load('./font/images/luffy.jpg')
    """
    a.获取图片大小
    get_size()
    """
    image_size = image.get_size()
    print(image_size)

    """
    b.形变:
    transform:形变包含缩放、旋转、平移
    scale(缩放对象,新的大小) -->返回一个缩放后的新对象
    """
    # image = pygame.transform.scale(image,(100,100))
    """
    旋转
    rotate(旋转对象,旋转角度)----角度是0-360对应的度数
    """
    image = pygame.transform.rotate(image,0)

    """
    def rotozoom(旋转对象,旋转角度,缩放比例)
    """
    image = pygame.transform.rotozoom(image,0,1)


    # 2.将图片对象渲染到窗口上
    screen.blit(image,(0,0))
    # screen.blit(image,(60,60))
    # 3.展示在屏幕上
    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

05.pygame显示图形

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    screen.fill((255,255,255))

    """
    1.画直线
    line(Surface,color,star_pos,end_pos,width=1)
    SurFace ->画在哪个地方
    color ->线的颜色
    star_pos ->起点
    end_pos ->终点
    width ->线的宽度
    """
    pygame.draw.line(screen,(255,0,0),(78,59),(100,100),5)
    pygame.draw.line(screen,(255,0,0),(99,59),(100,100),5)

    """
    lines(画线的位置,颜色,closed,点的列表,width=1)
    """
    pygame.draw.lines(screen,(0,0,255),False,[(10,10),(200,50),(100,100)])

    """
    画曲线
    arc(SurFace,color,Rect,start_angle,stop_angle,width=1)
    Rect ->(x,y,width,height)矩形
    start_anale
    stop_angele
    """
    from math import pi
    pygame.draw.arc(screen,(0,0,0),(0,0,100,100),pi,pi*2)

    """
    3.画圆
    circle(位置, 颜色,圆心位置,半径,width=0)
    """
    import random
    pygame.draw.circle(screen,\
            (random.randint(0,255),random.randint(0,255),random.randint(0,255)),\
            (400,200),100)


    # 将内容展示在屏幕上
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

06.pygame动画原理

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((1024,600))
    screen.fill((255,255,255))
    x = 0
    y = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

        x += 1
        y += 1

        screen.fill((255,255,255))
        pygame.draw.circle(screen,(255,0,0),(x,y),80)
        pygame.display.flip()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 Python最强大的结构之一就是它的异常处理能力,所有的标准异常都使用类来实现,都是基类Exceptio...
    随风化作雨阅读 3,085评论 0 1
  • 一、快捷键 ctr+b 执行ctr+/ 单行注释ctr+c ...
    o_8319阅读 5,858评论 2 16
  • 引言 在程序运行过程中(注意是运行阶段,程序可以通过编译),如果JVM检测出一个不可能执行的操作,就会出现运行时错...
    Steven1997阅读 2,482评论 1 6
  • 今天来看了最近大家极力推荐的红海行动,确实全程无尿点,更是让我觉得活在和平年代的幸福。 军人为了目标和必须完成任务...
    树子丨ageLOC阅读 129评论 1 2
  • 开课至今已一月有余,这期间的课程经历了C语言,电装培训,选主修科目,我选择了嵌入式,有幸进行了C语言进一步的学习。...
    刘泽清阅读 137评论 0 1