python游戏库pygame经典教程_pygame教程(1)


2.Surface从外部加载图像

语法格式:

pygame.image.load('图片路径').convert()

代码实例:

import pygame
import sys

pygame.init()
#设置主窗口
screen = pygame.display.set_mode((400,400))
screen.fill('white')
#设置窗口标题
pygame.display.set_caption('小马哥不马虎')
#加载图片
image_surface = pygame.image.load('C:/Users/myun/Pictures/不知火舞.jpg')
image_surface.fill((0,0,255),rect=(100,100,100,50),special_flags=0)
image_surface.scroll(100,60)  #移动图片
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
    #将图像添加到主屏幕上
    screen.blit(image_surface,(0,0))
    #更新屏幕内容
    pygame.display.flip()

效果图:

Surface模块处理图像方法:

方法 说明
pygame.Surface.blit() 将一个图像绘制到另一个图像上
pygame.Surface.convert() 修改图像的格式
pygame.Surface.fill() 使用纯色填充Surface图像
pygame.Surface.scroll() 复制并移动Surface对象
pygame.Surface.set_alpha() 设置整个图像的透明度
pygame.Surface.get_at() 获取一个像素的颜色值
pygame.Surface.set_at() 设置一个像素的颜色值
pygame.Surface.get_palette() 获取Surface对象8位索引的调色板
pygame.Surface.map_rgb() 将一个RGBA颜色转换为映射的颜色值
pygame.Surface.set_clip() 设置该Surface对象的剪切区域
pygame.Surface.subsurface() 根据父对象创建一个新的子Surface对象
pygame.Surface.get_offset() 获取子Surface对象在父对象中的偏移量
pygame.Surface.get_size() 获取Surface对象的大小

四.Pygame Transform图像变形

Transform方法
| 方法 | 说明 |
| pygame.transform.scale() | 将图片缩放至指定的大小,并返回一个新的Surface对象 |
| pygame.transform.rotate() | 将图片旋转至指定角度 |
| pygame.transform.rotozoom() | 以角度旋转图像,同时将图像缩小或放大至指定的倍数 |


五.Pygame Time时间控制
1.游戏暂停

Pygame.time模块提供以下常用方法

方法 说明
pygam.time.get_ticks() 以毫秒为单位获取时间
pygame.time.wait() 使程序暂停一段时间
pygame.time.set_time() 创建一个定时器,即每隔一段时间就去执行一些动作
pygame.time.Clock() 创建一个时间对象来帮助我们确定游戏要以多大的帧数运行

代码实例:

import pygame
pygame.init()
screen =pygame.display.set_mode((500,400))
pygame.display.set_caption('时间设置')
#获取时间
t=pygame.time.get_ticks()
#暂停游戏3000毫秒
t1=pygame.time.wait(3000)
print(t1)
image_surface=pygame.image.load('C:/Users/myun/Pictures/不知火舞.jpg')
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            exit()
        screen.blit(image_surface,(0,0))
        pygame.display.update()

效果:三秒后加载图片


2.设置游戏的帧数率(FPS)

Clock()方法可以实现对游戏帧数的设置

方法 说明<
  • 26
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值