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()

创建的窗口如下:


image.png

窗口中显示文字

import pygame
from pygame import font

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

    #设置窗口的背景颜色
    screen.fill((255,255,0))

    #1.创建字体对象(找一只笔)
    """
    SysFont(name, size, bold=0, italic=0, constructor=None)
    name->字体名
    size->字体大小
    bold->加粗  (有默认值,不用传参)
    italic->倾斜  (有默认值,不用传参)
    """
    # font=pygame.font.SysFont('Script',50)

    """
    #创建自定义字体
    Font(字体文件路径,字体大小)
    """
    font=pygame.font.Font('./font/bbb.ttf',50)

    #2.根据字体去创建显示对象(找内容)
    """
    render渲染
    render(self, text, antialias, color, background=None)
    self
    text->要显示的文字内容(str)
    antialias->是否平滑(True,False)
    color->计算机三原色(红R、绿G、蓝B)RGB颜色,值得范围0-255
            (255,255,255)->白色  
            (0,0,0)->黑色   
            (0,255,0)->绿色
            (255,0,0)->红色
            (x,x,x)->灰色
    """
    surface=font.render('你好,hello pygame',True,(0,255,0))
    print(surface,type(surface))

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

    screen.blit(surface,(100,100))

    #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,255,255))

    #1.获取图片对象
    image=pygame.image.load('./image./1.jpg')

    """
    (1)获取图片大小
    get_size()
    """
    image_size=image.get_size()
    print(image_size)

    """
    (2)图片的缩放
    transform(形变):形变包含缩放、旋转和平移
    
    scale(缩放对象,新的大小)--->返回一个缩放后的新对象(缩放到指定大小)
    """
    image=pygame.transform.scale(image,(600,400))
    """
    旋转
    rotate(旋转对象,旋转角度)
    """
    image=pygame.transform.rotate(image,180)

    """
    旋转、缩放(按比例)
    def rotozoom(旋转对象,旋转的角度,缩放比例)
    """
    image=pygame.transform.rotozoom(image,270,0.5)


    #2.将图片对象渲染到窗口上
    screen.blit(image,(0,0))

    #3.展示在屏幕上
    pygame.display.flip()


    #循环游戏:
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit()

运行效果:


image.png

四、显示图形


import pygame

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

    """
    1.画直线
    line(外观对象,线颜色,起点,终点,线条宽度)
    Surface->画在哪里
    color->线的颜色
    start_pos->起点
    end_pos->终点
    width->线的宽度
    lines(外观对象,线条颜色,是否闭合,点的列表,width=1)
    """
    pygame.draw.line(screen,(0,255,0),(50,50),(100,100),5)
 
    pygame.draw.lines(screen,(255,0,0),True,[(10,10),(20,50)])


    """
    2.画矩形
    rect(位置,颜色,(起点坐标,宽,高),线宽为零即填充)
    """
    pygame.draw.rect(screen,(0,255,0),(0,0,200,200),0)

    """
    3.画曲线
    arc(图形对象,颜色,范围,起始角度,终止角度,线宽)
    Rect->(x,y,width,height)矩形
    """
    from math import pi
    pygame.draw.arc(screen,(0,0,0),(200,200,100,100),pi,2*pi)

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

    pygame.draw.circle(screen,\
           (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),\
                       (400, 200), 100)


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


    #将内容展示在屏幕上
    pygame.display.flip()

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

运行效果:


image.png

五、动画原理

import pygame


if __name__ == '__main__':
    pygame.init()
    screen=pygame.display.set_mode((600,400))
    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,255,0),(x,y),80)
        pygame.display.flip()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,020评论 3 119
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 送你一株百合花: 详细步骤如下: 1. 【勾勒布局】 用简单的基础形状勾勒整幅图的大致框架布局,花朵与叶子之间相互...
    拾零阅读 1,349评论 4 9
  • 关于医疗改革的满意度研究包含了各个方面,最终的数据结果和结论是这些方面综合影响的结果。目前来看满意度研究至少基于目...
    游很慢的鱼笨阅读 2,343评论 1 8
  • (每天读一遍,你将获得宇宙无限的正能量) 从今天开始的每一天 我已经改变成为一个全新的人 我充满了灵性和爱 我的全...
    清心阁阅读 311评论 0 0