day_009 Python中的pygame基础

2018-07-27  本文已影响0人  HavenYoung

一、导入pygame

点击file→settings


第一步

找到Project Interpreter 点击'+'号


第二步

输入pygame ,选中Pygame,点击install package,然后等待下载安装


第三步

如果安装失败,打开命令提示符,输入pip install pygame,可进行下载安装


image.png

二、基本操作

1.基本架构

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

        # 其他操作
显示框

2.显示文字

import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    # 设置窗口的背景颜色
    screen.fill((255, 255, 255))


    # 创建字体对象
    """
    创建系统字体
    SysFont(name, size, bold=False, italic=False, constructor=None)
    name --- 字体名  --- Pristina
    size --- 字体大小
    bold --- 加粗
    italic --- 倾斜
    """
    # font = pygame.font.SysFont('Pristina', 30)

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


    # 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)--- 灰色 x越大灰度越浅
    """
    surface = font.render('你好,python', True, (0, 255, 0))
    print(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(0)
显示文字

3.显示图片

import pygame

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

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

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

    """
    b.图片形变
    transform: 形变包含缩放、旋转和平移
    scale(缩放对象,新的大小)---返回一个缩放后的图片对象
    rotate(旋转对象,旋转角度):旋转,角度(-360~360)正逆时针,负顺时针
    rotozoom(旋转对象,旋转角度,缩放比例):旋转缩放
    """
    # image = pygame.transform.scale(image, (600,450))
    # image = pygame.transform.rotate(image, 90)
    image = pygame.transform.rotozoom(image, -90, 0.5)



    # 2.将图片对象渲染到窗口上
    screen.blit(image, (100, 0))
    # 3.展示在屏幕上
    pygame.display.flip()


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit(0)
显示图片

4.显示图形

import pygame
import math
import random


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

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

    # 画多条直线,根据点画折线
    """
    lines(画线的位置,颜色,closed,点的列表,width=1)
    """
    pygame.draw.lines(screen, (0,0,255),True,[(10,200), (300,40), (500, 60)])


    # 2.画jux
    """
    rect(Surface, color, Rect, width=0)
    width=0表示填充
    """
    pygame.draw.rect(screen, (255,255,0),(300,150,100,50))

    # 3.画曲线
    """
    arc(Surface, color, Rect, start_angle, stop_angle, width=1)
    Rect ---- 矩形框(x,y,width,height)
    start_angle ----
    stop_angle ----
    """
    pygame.draw.arc(screen, (255, 0, 0), (300,150,100,50), math.pi, math.pi*2)

    # 4.画圆
    """
    circle(Surface, color, pos, radius, width=0)
           位置,   颜色,   圆心, 半径 
    """
    pygame.draw.circle(screen, (random.randint(0,255),random.randint(0,255),0), (600,300), 50, 5)


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


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


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

显示图形
上一篇下一篇

猜你喜欢

热点阅读