pygame

作者: pubalabala | 来源:发表于2019-08-27 14:04 被阅读0次

基本流程

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

相关文章

  • 2018-09-04-pygame

    一、pygame基本操作 import pygame——导入pygame模块 pygame.init()——初始化...

  • Pygame入门--飞机大战案例

    Pygame的快速入门 #导入pygame模块 import pygame #游戏初始化 pygame.init(...

  • Day_10 异常与pygame

    异常捕获 pygame操作流程 pygame显示文字 pygame显示图片与图片操作 pygame基本显示

  • Pygame-hello world

    使用pygame 模块名功能pygame.cdrom访问光驱pygame.cursors加载光标pygame.di...

  • pygame - alphabet

    pygame install pygame install[https://www.pygame.org/wiki...

  • Day12 pygame

    1.pygame基本操作: 1.导入pygame: import pygame.2.初始化:pygame init...

  • Day-18正则表达式2

    pygame游戏基本框架的创建 pygame中图片的显示 字体的显示 图形 Pygame Pygame有很多的模块...

  • day11-pygame笔记

    1pygame事件 import pygame pygame.display.set_caption('游戏事件'...

  • Python——Pygame模块

    学习资料: Pygame官网 pygame系列 PyGame - Python Wiki 用Python和Pyga...

  • pygame简介

    简介 关于Pygame的基本信息,pygame是什么,谁会被Pygame吸引,并且在哪里找到它。 Pygame是被...

网友评论

      本文标题:pygame

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