pygame

基本流程

import pygame
#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()

文本显示

"""__author__ == Jefferson"""
import pygame


if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((700,400))
    #设置窗口颜色
    screen.fill((38,54,64))
    #1. 创建字体对象
    '''
    创建系统字体
    SysFont(name, size, bold=False, italic=False)
    name:字体名
    size:字体大小
    bold:加粗
    italic:倾斜
    font = pygame.font.SysFont('times',50)
    '''

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

    #2. 根据字体创建显示对象
    '''
    render(text, antialias, color, background=None)
    text: 要显示的文字内容
    antialias: 是否平滑
    color: 文字颜色 RGB 值的范围0~255
    红色: (255,0,0)
    绿色: (0,255,0)
    蓝色: (0,0,255)
    '''
    surface = font.render('流批!',True,(0,216,255))
    '''
    3. 将内容添加到窗口上
    blit(需要显示的对象, 显示位置)
    需要显示的对象: Surface类型的数据
    显示位置: 坐标(x,y)
    '''
    screen.blit(surface,(0,50))

    '''
    4. 将窗口上的内容展示出来(将画有文字的纸贴出来)
    '''
    pygame.display.flip()



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

显示图片

"""__author__ == Jefferson"""
import pygame

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((690,685))
    screen = pygame.display.set_mode((405, 810))
    screen.fill((255,255,255))
    #1. 获取图片对象
    image = pygame.image.load('./image/2.jpg')
    image_dog = pygame.image.load('./image/1.jpg')
    #获取图片大小
    image_size = image.get_size()
    print(image_size)
    '''
    形变:
    transform: 形变包喊缩放,旋转和平移
    scale(缩放对象,新的大小): 返回一个缩放后的新对象
    new_image = pygame.transform.scale(image,(50,50))
    rotate(旋转对象,旋转角度)
    new_image = pygame.transform.rotate(image,180)
    '''
    image_dog = pygame.transform.rotozoom(image_dog,0,0.6)


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


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

显示图形

"""__author__ == Jefferson"""
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=1)
    Surface: 画在哪个地方
    color: 线的颜色
    start_pos: 起点
    end_pos: 终点
    width: 线的宽度
    '''
    pygame.draw.line(screen,(255,0,0),(78,59),(100,100),2)
    pygame.draw.line(screen,(0,255,0),(0,50),(130,100),2)
    #lines(画线的位置, 颜色, closed, 点的列表, width)
    pygame.draw.lines(screen,(0,216,255),True,[(10,10),(50,50),(60,15)],5)

    '''
    画矩形
    rect(位置, 颜色, (x,y,width,height),with)
    '''



    '''
    2. 画曲线
    arc(Surface,color,Rect,start_angle,stop_angle,width)
    Rect: (x,y,width,height)矩形
    start_angle:
    stop_angle:
    
    
    '''
    from math import pi
    pygame.draw.arc(screen,(0,0,0),(0,0,200,200),pi/2,pi)


    '''
    3. 画圆
    circle(位置,颜色,圆心位置,半径,width=0)
    
    '''
    import random
    pygame.draw.circle(screen,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),(400,200),100)


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



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

推荐阅读更多精彩内容