July 27-day12-Python初识pygame

2018-07-27  本文已影响0人  慢节拍_2709

显示字体

import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    # 设置窗口的背景颜色
    screen.fill((255,255,255))
    # 1.创建字体对象
    """
    创建系统字体
    SysFont(name, size, bold=0, italic=0, constructor=None)
    name --> 字体名
    size --> 字体大小
    bold --> 加粗
    italic --> 倾斜
    """
    # font = pygame.font.SysFont('宋体',30)  因为我的系统中没有宋体所以显示不出来
    font = pygame.font.Font('./font/aa.ttf', 30)  #可以选择自行下载的字体.ttf格式

    #创建自定义字体
    #Font(字体文件路径,字体大小)
  
    # 2.根据字体去创建显示对象(文字)(找内容)
   """
    render(self, text, antialias, color, background=None)
    text ->要显示的文字内容(str)
    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('你好,Python',True,(255,40,160))

    # 3.将内容添加到窗口上
    """
    blit(需要显示的对象,显示位置)
    需要显示的对象 --> Surface类型的数据
    显示位置  --> 坐标(x, y)
    
    """
    screen.blit(surface, (150,150))

    # 4.将窗口上的内容展示出来(将画有文字的纸贴出来)
    pygame.display.flip()
    # 游戏循环
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:   #退出游戏循环
                exit()  

显示结果:


显示字体

显示图片

import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((600,400))
    screen.fill((255,40,160))   #这里是填充的颜色我选择的是骚粉,你可以自行调节RGB来选择你喜欢的颜色
    # 1.获取图片对象
    image = pygame.image.load('./font/冰女.png')

    """
    a.获取图片大小
    get_size()
    """
    image_size = image.get_size()  #通过这个方法可以得到图片的大小,方便自行调节
    print(image_size)
    """
    b.形变
    transform:形变包含缩放、旋转和平移
    scale(缩放对象,新的大小) -->返回一个缩放后的新对象
    """
    # new_image = pygame.transform.scale(image,(600,400))  #图片的变化会发生形变,等同于伸拉硬拽
    """
    旋转
    rotate(旋转对象,旋转角度)
    """
    image = pygame.transform.rotate(image,-90)
    """
    def rotozoom(旋转对象,旋转角度,缩放比例)
    """
    image = pygame.transform.rotozoom(image,90,2)   #这个方法放大的图片不会形变

    # 2.将图片对象渲染到窗口上

    screen.blit(image,(100,100))

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

显示结果:


Ice lady

显示图形

import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((1000,1000))
    screen.fill((255,40,160))
    """
    1.画直线
    line(Surface,color,start_pos,end_pos,width=1)
    Surface -> 画在哪个地方
    color -> 线的颜色
    start_pos -> 起点
    end_pos -> 终点
    width -> 线的宽度
    """
    pygame.draw.line(screen,(0,255,0), (78,59),(100,100),5)
    pygame.draw.line(screen,(0,0,255), (50,59),(150,200),20)
    """
    lines(画线的位置,颜色,closed,点的列表,width=1)
    此处closed对应的意思的,如果为True他会自行连接最后两个未连接的点形成闭环
    如果为False就不会形成闭环
    """
    pygame.draw.lines(screen,(100,100,100),True,[(10,20),(50,100),(300,200)])
    """
    画矩形
    rect(位置)
    """
    """
    2.画曲线
    arc(Surface,color,Rect,start_angle,stop_angle,width=1)
    Rect -> (x, y, width, height)矩形
    start_angle
    stop_angle
    """
    from math import pi
    pygame.draw.arc(screen,(0,0,0),(200,200,200,200),0,100)

    """
    3.画圆
    circle(位置,颜色,圆心位置,半径,width=0)

    """
    import random
    pygame.draw.circle(screen,\
                       (random.randint(0,255),random.randint(0,255),random.randint(0,255)),\
            (350,200), 100)

    """
    画椭圆
    ellipse(Surface, color, Rect, width=0)
    """
    pygame.draw.ellipse(screen,(0,100,0),(100,300,200,80),1)

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

显示结果:


画图了解一哈
上一篇 下一篇

猜你喜欢

热点阅读