2018-09-0402-pygame笔记

制作一个游戏窗口

import pygame

if __name__ == '__main__':
    # 1. 初始化游戏模块
    pygame.init()

    # 2. 创建游戏窗口
    """
    display.set_mode(窗口大小): 创建一个窗口并且返回
    窗口大小: 是一个元祖,并且元祖中需要两个值分别表示宽度和高度(单位是像素)
    """
    window = pygame.display.set_mode((400, 600))


    # 3. 让游戏一直运行,直到点关闭按钮才结束
    flag = True
    while flag:
        # 获取游戏过程中产生的所有的事件
        for event in pygame.event.get():
            # type来判断事件的类型
            if event.type == pygame.QUIT:
                exit()  # 退出程序
                # flag = False

显示图片

import pygame

if __name__ == '__main__':
    # 1.初始化游戏模块
    pygame.init()

    # 2.创建窗口
    window = pygame.display.set_mode((600, 400))

    # 给窗口填充颜色
    """
    fill(颜色)
    颜色:计算机三原色(红、绿、蓝), 每个颜色对应的值的范围是0-255。可以通过改变三原色的值可以调配处不同的颜色
    颜色值:是一个元祖,元祖中三个元素,分别代表红绿蓝(rgb)
    (255, 0, 0)  --> 红色
    (0, 255, 0)  --> 绿色
    (0, 0, 255)  --> 蓝色
    (0, 0, 0)  --> 黑色
    (255, 255, 255)  --> 白色
    """
    window.fill((255, 255, 255))

    # a.获取图片,创建图片对象
    """
    image.load(图片路径): 获取本地的一张图片,返回图片对象
    """
    image = pygame.image.load('./files/luffy4.jpg')

    """
    get_size(): 获取大小,返回值是一个元祖,有两个元素,分别是宽和高
    """
    image_width, image_height = image.get_size()


    # b.渲染图片(将图片画在纸上)
    """
    blit(渲染对象, 位置)
    位置: 坐标(x, y), 值的类型是元祖,元祖有两个元素分别对应x坐标和y坐标
    """
    # window.blit(image, (0, 100))
    window.blit(image, (600-image_width, 400-image_height))


    # c.展示内容(将纸贴在画框上)
    pygame.display.flip()

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


形变

import pygame

if __name__ == '__main__':
    # 初始化和创建窗口
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    window.fill((255, 255, 255))   # 填充窗口的颜色

    # 图片相关
    # 1.加载图片(选图)
    image = pygame.image.load('./files/luffy4.jpg')

    """
    a. 缩放(指定大小)
    transform.scale(缩放对象, 目标大小) : 将指定的对象缩放到指定的大小,会返回缩放后的对象
    """
    new_image = pygame.transform.scale(image, (400, 600))

    """
    b. 旋转缩放(指定缩放比例)
    rotozoom(Surface, angle, scale)
    Surface:旋转缩放对象
    angle: 旋转的角度(0-360)
    scale:缩放比例,大于1放大,小于1缩小
    """
    new_image = pygame.transform.rotozoom(image, 45, 0.8)

    """
    c. 旋转
    rotate(Surface, angle)
    Surface: 旋转对象
    angle: 旋转角度
    """
    new_image = pygame.transform.rotate(image, 270)

    # 2.渲染图片
    window.blit(new_image, (0, 0))


    # 3.展示内容
    pygame.display.flip()




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

显示文字

import pygame

if __name__ == '__main__':
    # 初始化游戏和创建窗口
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    window.fill((255, 255, 255))

    # 显示文字
    # 1.创建字体对象
    """
    a.创建系统的字体对象
    SysFont(name, size, bold=False, italic=False)
    name: 字体名(系统支持的字体名)
    size: 字体大小
    bold: 是否加粗
    italic: 是否倾斜
    
    b.创建自定义的字体对象
    Font(字体文件路径, 字体大小)
    字体文件路径:ttf文件
    
    """
    # a.创建系统字体
    # font = pygame.font.SysFont('Times', 30)

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


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

    print(text.get_size())

    # 3.渲染文件
    window.blit(text, (50, 50))

    # 4.展示内容
    pygame.display.flip()

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

显示圆形

import pygame

from math import pi

if __name__ == '__main__':
    # 初始化,创建窗口
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    window.fill((255, 255, 255))

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


    # 展示内容
    pygame.display.flip()



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

事件

import pygame

from math import pi

if __name__ == '__main__':
    # 初始化,创建窗口
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    window.fill((255, 255, 255))

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


    # 展示内容
    pygame.display.flip()



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

动画原理

import pygame

if __name__ == '__main__':
    # 初始化
    pygame.init()
    window = pygame.display.set_mode((400, 600))
    window.fill((255, 255, 255))
    pygame.display.flip()

    # 球的圆心坐标
    x = 100
    y = 100
    r = 50
    add = 4
    y_speed = 2
    # 游戏循环
    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
        # r += add
        # if r >= 120 or r <= 20:
        #     add *= -1
        # 边界检测
        if y > 600 - r:
            y = 600 - r
            y_speed = -2
        elif y < 50:
            y = 50
            y_speed = 2

        # 事件检测
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

按住不放原理

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()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,635评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,543评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,083评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,640评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,640评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,262评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,833评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,736评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,280评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,369评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,503评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,870评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,340评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,460评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,909评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,512评论 2 359

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_X自主阅读 15,988评论 3 119
  • 前两天偶然发现支付宝有一个买火车票,领优惠的活动: 用户在12306官网、APP、火车站窗口及自助机使用支付宝每成...
    我是小徐同学阅读 242评论 0 0
  • “怎么回事啊?”妈妈问道。 “没事啊,就是失恋了,有点失恋的忧伤而已!”彬彬回答道,说完继续缩进被窝里。 妈妈瞪大...
    活火山宝宝阅读 150评论 0 0
  • 夜里的星辰 隆隆的机器声 让夜少了几分寂寞 天上那闪烁的星星 给夜增添了几分活力 凌晨的车间 依旧热闹 忙碌的工人...
    寻食阅读 237评论 0 1
  • 百无聊赖,我想给十年后的自己写封信。我怎么称呼十年后的自己呢?那时的自己比现在大,所以我称十年后的自己大姐吧,这样...
    流云芳菲阅读 383评论 0 2