pygame

作者: 葡萄柚子茶 | 来源:发表于2018-09-04 20:29 被阅读0次

    一.pygame基本操作

    import pygame
    #1.初始化游戏模块
    pygame.init()
    
    #2.创建游戏窗口
    """
    set_mode(窗口大小)创建一个窗口,并且返回
    窗口大小:是一个元祖,并且元祖需要两个值(高,宽),单位是像素
    """
    window = pygame.display.set_mode((600,400))
    #input()阻止程序结束,让窗口显示
    
    #3.让游戏一直运行,直到点关闭才结束
    flag = True
    while flag:
        # 获取游戏中产生的所有事件--点鼠标,键盘
        for event in pygame.event.get():
            #type来判断事件的类型,QUIT是点×
            if event.type == pygame.QUIT:
                #exit()#退出程序
                flag = False
    

    注:每次都要先导入这些程序

    二.显示图片

    一共分为三个步骤:
    a.获取图片,创建图片对象
    b.渲染图片(将图片画在纸上)
    c.展示内容(将纸贴在画框上)

    import pygame
    pygame.init()
    window = pygame.display.set_mode((600,400))
    window.fill((255,255,255))
    image = pygame.image.load('./files/luffy4.jpg')
    window.blit(image,(0,100))
    pygame.display.flip()
    while True:
      for event in pygame.event.get():
        if event.type == pygame.QUIT:
          exit()
    

    三.图片形变

    先执行导入pygame模块和加载图片
    1.缩放(指定大小)
    transform.scale(缩放对象, 目标大小) : 将指定的对象缩放到指定的大小,会返回缩放后的对象

        new_image = pygame.transform.scale(image, (400, 600))
    
    

    2.旋转缩放(指定缩放比例)
    rotozoom(Surface, angle, scale)
    Surface:旋转缩放对象
    angle: 旋转的角度(0-360)
    scale:缩放比例,大于1放大,小于1缩小

        new_image = pygame.transform.rotozoom(image, 45, 0.8)
    
    

    3.旋转
    rotate(Surface, angle)
    Surface: 旋转对象
    angle: 旋转角度

        new_image = pygame.transform.rotate(image, 270)
    
    

    四.显示文字

    import pygame
    pygame.init()
        window = pygame.display.set_mode((400, 600))
        window.fill((255, 255, 255))
        pygame.display.flip()
    
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
    

    1.创建字体对象
    a.创建系统的字体对象
    SysFont(name, size, bold=False, italic=False)
    name: 字体名(系统支持的字体名)
    size: 字体大小
    bold: 是否加粗
    italic: 是否倾斜

    b.创建自定义的字体对象
    Font(字体文件路径, 字体大小)
    字体文件路径:ttf文件

    # b.创建自定义字体
    font = pygame.font.Font('./files/aa.ttf', 30)
    

    2.根据字体去创建字体对象
    render(text, antialias, color, background=None)
    text: 需要显示的文字(字符串)
    antialias: 是否平滑(布尔)
    color: 颜色
    background:背景颜色

    #2.创建字体对象
    text = font.render('你好啊',True,(0, 0, 255), (255, 255, 0))
    # 3.渲染文件
    window.blit(text,(50,50))
    # 4.展示内容
    pygame.display.flip()
    

    五.显示图形

    1.画线段
    def line(Surface, color, start_pos, end_pos, width=1)
    Surface: 画在哪儿
    color:线的颜色
    start_pos: 起点
    end_pos:终点
    width: 线宽

    # 画一条水平线
        pygame.draw.line(window, (255, 0, 0), (50, 100), (200, 100))
    # 画一条垂直线
        pygame.draw.line(window, (0, 255, 0), (50, 100), (50, 200), 2)
    

    2.画线段(折线)
    def lines(Surface, color, closed, pointlist, width=1)
    Surface: 画在哪儿
    color: 线的颜色
    closed: 是否闭合(是否连接起点和终点)
    pointlist:点对应的列表

     pygame.draw.lines(window, (0, 0, 255), True, [(100, 200), (150, 120), (140, 300)])
    

    3.画圆
    def circle(Surface, color, pos, radius, width=0)
    Surface: 画在哪儿
    color: 颜色
    pos:圆心坐标
    radius:半径
    width: 线宽,0 -> 填充

    pygame.draw.circle(window, (255, 255, 0), (200, 300), 100, 0)
    
    

    4.画矩形
    def rect(Surface, color, Rect, width=0)
    Surface:画在哪儿
    color: 颜色
    Rect:范围(元祖,元祖中有四个元素,分别是x,y,width,height)

    pygame.draw.rect(window, (0, 255, 0), (10, 100, 50, 100))
    

    5.画多边形
    polygon(Surface, color, pointlist, width=0)

        pygame.draw.polygon(window, (0, 255, 255), [(300, 50), (250, 40),(100, 50), (200, 150)])
    
    
    6.画椭圆
    def ellipse(Surface, color, Rect, width=0)
    
        pygame.draw.ellipse(window, (123, 200, 210), (10, 200, 150, 60))
    
    
    7.画弧线
    def arc(Surface, color, Rect, start_angle, stop_angle, width=1)
    start_angle: 0-2pi
    stop_angle:
    pi --- 180°   1° --- pi/180
    59° = pi/180 * 59
    
        pygame.draw.arc(window, (255, 0, 0), (200, 400, 100, 100), pi/4+pi, pi*3/4+pi, 3)
    

    六.事件

    a.事件的type --- 决定发生的是什么事件
    QUIT : 关闭按钮被点击事件

    鼠标事件:
    MOUSEBUTTONDOWN: 鼠标按下事件
    MOUSEBUTTONUP: 鼠标按下松开时对应的事件
    MOUSEMOTION:鼠标移动事件

    键盘事件:
    KEYDOWN: 键盘按下
    KEYUP: 键盘弹起

    b.事件的pos --- 鼠标事件发生的位置(坐标)

    c.事件的key --- 键盘事件被按的键对应的编码值

    for event in pygame.event.get():
      if event.type==pygame.QUIT:
        exit()
      elif event.type == pygame.MOUSEBUTTONDOWN:
    # 鼠标按下要做的事情就写在这儿
    # 鼠标按下一次画一个球
      pygame.draw.circle(window, (randint(0, 255), randint(0, 255),\
                                             randint(0,255)),event.pos,randint(20, 50))
        pygame.display.flip()
      elif event.type == pygame.MOUSEBUTTONUP:
                    print('鼠标弹起', event.pos)
      elif event.type == pygame.MOUSEMOTION:
                    # 鼠标按下一次画一个球
                    #pygame.draw.circle(window, (randint(0, 255), randint(0, 255), \
                                                #randint(0, 255)), event.pos, 20)
                    #pygame.display.flip()
                    # print('鼠标正在移动', event.pos)
                    pass
      elif event.type == pygame.KEYDOWN:
                    print('键盘按下', event.key, chr(event.key))
      elif event.type == pygame.KEYUP:
                    print('键盘弹起')
    
    

    七.动画原理

        while True:
    
            # 延迟
            # pygame.time.delay(10)
    
            # 将之前纸上的内容给覆盖
            window.fill((255, 255, 255))
            # 不断的画圆
            pygame.draw.circle(window, (255, 0, 0), (x, y), r)
            pygame.display.update()
    
            # 改变y值让圆在垂直方向移动
            y += y_speed
           
            # 边界检测
            if y > 600 - r:
                y = 600 - r
                y_speed = -2
            elif y < 50:
                y = 50
                y_speed = 2
    

    八.按住不放原理

    import pygame
    
    if __name__ == '__main__':
        # 游戏初始化
        pygame.init()
        window = pygame.display.set_mode((400, 600))
        window.fill((255, 255, 255))
        # pygame.display.flip()
    
        # 1.显示一张图片
        image = pygame.image.load('./files/luffy4.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, image_y = 100, 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, center_y = event.pos
                    image_x, image_y = center_x - image_w/2, center_y-image_h/2
                    window.blit(image, (image_x, image_y))
                    # 更新屏幕的显示
                    pygame.display.update()
    
    
    
    

    相关文章

      网友评论

          本文标题:pygame

          本文链接:https://www.haomeiwen.com/subject/dvjnwftx.html