day10 Pygame 2018-07-27

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

一、Pygame

  Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。

创建一个窗口

import pygame

if __name__ == '__main__':
    # 初始化pygame
    pygame.init()

    # 2. 创建游戏窗口
    # set.mode((宽度,高度))
    screen = pygame.display.set_mode((600,400))

    while True:
        # 检测事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # 退出游戏
                print('关闭按钮被点击')
                exit()
pygame创建的窗口

二、在pygame窗口上显示文字

1. 创建字体对象

创建系统字体
    SysFont(name, size, bold=False, italic=False, constructor=None)
    name —> 字体名
    size --> 字体大小
    bold --> 加粗
    italic --> 倾斜
    font = pygame.font.SysFont('楷体',420)
创建自定义字体
    Font(字体文件路径, 字体大小)
    font = pygame.font.Font('./font/aa.ttf', 180)

2. 根据字体去创建显示对象(文字)

render(self, text, antialias, color, background=None)
text -- 要显示的文字内容(str)
antialias -- 是否平滑
color -- 计算机三原色(红R,绿G,蓝B)(0~255)
                    (255, 0, 0) --> 红色
                    (0, 255, 0) --> 绿色
                    (0, 0, 255) --> 蓝色
                    (0, 0, 0) --> 黑色
                    (255, 255, 255) --> 白色
                    (a, a, a) --> 灰色(越接近255越浅)

3. 将内容添加到窗口上

bilt(需要显示的对象, 显示位置)
需要显示的对象 --> Surface类型的数据
显示位置 --> 坐标(x,y)
"""
screen.blit(surface, (100, 45))

4. 将窗口上的内容展示出来

pygame.display.flip()
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((1200, 400))
    screen.fill((204, 154, 49))
    # 1. 创建字体对象
    # font = pygame.font.SysFont('楷体',420)
    font = pygame.font.Font('./font/aa.ttf', 180)
    # 2. 根据字体去创建显示对象(文字)
    render(self, text, antialias, color, background=None)
    surface = font.render('精忠报国', True, (0, 255, 165))
    # 3. 将内容添加到窗口上
    screen.blit(surface, (230, 100))

    # 4. 将窗口上的内容展示出来
    pygame.display.flip()

    # 游戏循环
    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
pygame上显示的文字

三、在pygame上显示图片

1. 形变

transform: 包含缩放,旋转和平移

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

    # 1. 获取图片对象
    img = pygame.image.load('./images/img2.gif')


    # 获取图片大小
    img_size = img.get_size()
    print(img_size)

    # img = pygame.transform.scale(img, (200, 200))
  
    img = pygame.transform.rotate(img, 90)
    img = pygame.transform.rotozoom(img, -90, 2.25)
    screen.blit(img, (0, 0))
    pygame.display.flip()

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

四、在pygame上绘制图形

1. 画直线

2. 画曲线

3. 画矩形

4. 画圆

5. 画椭圆

import pygame
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((1000, 1000))
    screen.fill((233, 233, 233))
    pygame.draw.line(screen, (255, 0, 0), (0, 0), (1280, 720), 2)
    # pygame.draw.line(screen, (255, 0, 0), (1280, 0), (0, 720), 2)
    # pygame.draw.line(screen, (255, 0, 0), (0, 0), (0, 720), 2)
    # pygame.draw.line(screen, (255, 0, 0), (0, 0), (1280, 0), 2)
    # pygame.draw.line(screen, (255, 0, 0), (1280, 0), (1280, 720), 2)
    # pygame.draw.line(screen, (255, 0, 0), (0, 720), (1280, 720), 2)

    pygame.draw.lines(screen, (0, 255, 0), True, [(0,0), (1280, 0), (1280, 720), (0, 720), 2])

 
    from math import pi
    pygame.draw.arc(screen, (0, 0, 0), (0, 0, 500, 500), pi, pi*2, 3)

 
    pygame.draw.rect(screen, (2, 0, 255), (0, 0, 500, 500), 3)

   
    from random import randint
    pygame.draw.circle(screen, (randint(0,255), randint(0,255), randint(0,255)), (500,500), 100, 3)
    pygame.draw.ellipse(screen, (1, 90, 234), (30, 30, 200, 400), 3)
    pygame.display.flip()


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


pygame作的图

五、pygame动画效果的实现

  在pygame中, 动画的效果是通过背景图层的不断刷新, 表层图层的位置或者形状的细微变化来实现动画效果的

上一篇 下一篇

猜你喜欢

热点阅读