11-Python笔记整理(pygame)

2018-07-27  本文已影响0人  李小萌mmm

pygame

 #1.初始化pygame
pygame.init()
#2.设置窗口的大小,单位为像素
screen = pygame.display.set_mode((600,400))
#3.窗口背景颜色
screen.fill((255,255,255))
RGB.png

color->灰色(x,x,x)


文字

字体设置
#字体设置
    #1.创建字体对象
    font = pygame.font.SysFont('宋体',50)

    #创建自定义字体
    #Font(字体文件路径,字体大小)
    font =pygame.font.Font('./font/aa.ttf',25)
#2.根据字体创建显示对象(文字)
    surface=font.render('今天 好开心呀 hahaha ',True,(0,0,0))

显示控制

    #3.将内容添加到窗口上
    #blit(需要显示的对象,显示的位置)
    screen.blit(surface,(100,100))

    #4.将窗口上的内容展示出来
    pygame.display.flip()

检测事件

while True
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print('关闭被点击')
                exit()

图片

    image = pygame.image.load('./144.jpg')

   
    transform( 形变):包括缩放,旋转和平移

    #scale(缩放对象,新的大小)
    image = pygame.transform.scale(image, (360, 600))

    #rotate(旋转对象,选择大小)
    image = pygame.transform.rotate(image,-90)

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

     #图片渲染到窗口上
    screen.blit(image, (0, 0))
    #获得图片大小
    image_size = image.get_size()
    print(image_size)


图形

'''
    1,画直线
    line(Surface,color,start_pos,end_pos,width=1)
    Surface - 画在哪个地方
    color - 线的颜色
    start_pos-起点
    end_pos-终点
    width - 宽度
    pygame.draw.line(screen,(255,0,0),(0,0),(300,300),5)


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



    #2.画曲线(arc)
    #arc(Surface, color, Rect, start_angle, stop_angle, width=1)
    from math import pi
    pygame.draw.arc(screen,(0,0,0),(0,0,200,200),pi/2,pi)

    #3.画矩形(rect)
    pygame.draw.rect(screen,(255,255,0),(0,0,200,200))

   
    import random
    #4.画圆(circle)
    pygame.draw.circle(screen,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),\
                               (300,200),100)

上一篇 下一篇

猜你喜欢

热点阅读