【Pygame基本使用】

pygame是什么

 pygame是跨平台Python模块,专门为电子游戏设计,包含图像、声音等;简单的说它是别人已经编写好的程序,并放在了一个类似库里,专门给别人使用的;

pygame的图形接口

https://www.jianshu.com/p/352f28c32865

pygame常用模块
模块名功能
pygame.cdrom访问光驱
pygame.cursors加载光驱
pygame.display访问显示设备
pygame.draw绘制形状、线和点
pygame.event管理事件
pygame.font使用字体
pygame.image加载和存储图片
pygame.joystick使用游戏手柄或类似的东西
pygame.key读取键盘按键
pygame.mixer声音
pygame.mouse鼠标
pygame.movie播放视频
pygame.music播放音频
pygame.overlay访问高级视频叠加
pygame.rect管理矩形区域
pygame.sndarray操作声音数据
pygame.sprite操作移动图像
pygame.surface管理图像和屏幕
pygame.surfarray管理点阵图像数据
pygame.time管理时间和帧信息
pygame.transform缩放和移动图像
图像的翻转、旋转、放缩

 pygame.transform模块内置许多实现对图像、旋转、放缩的函数;

创建一个游戏窗口,宽和高设置为640×480
import sys # 导入sys模块
import pygame # 导入pygame模块

pygame.init()
size=width,height=640,480
screen=pygame.display.set_mode(size)

 现在我们将给这个窗口放进去一个图片填充,我们该怎么办呢

import sys
import pygame

pygame.init()						# 初始化pygame
size=width,height=700,600 			#设置大小
screen=pygame.display.set_mode(size)# 显示窗口

color=(0,0,0) 						# 设置填充颜色
charlotte=pygame.image.load("1.jpg") 	# 记载图片
charlotterect=charlotte.get_rect() 			# 获取矩形区域

while True:
    for event in pygame.event.get():
         if event.type == pygame.KEYDOWN:
             sys.exit()
    screen.fill(color)				# 填充颜色
    screen.blit(charlotte,charlotterect)		# 将图片画到窗口上
    pygame.display.flip()			# 更新全部显示
pygame.quit()

 图中 while True里的代码,pygame.event.get()能获得事件队列,使用for…in遍历事件,然后根据type属性判断事件类型,这里事件处理与GUI类似,入event.type等于pygame.QUIT表示检测到关闭窗口事件,pygame.KEYDOWN表示键盘按下事件,pygame.MOUSEBUTTONDUWN表示鼠标按下事件等。
 下面的是我们打算插入的图片,它是1920×1080的,而我们的窗口是700×600的,所以我们打算把它缩小一下,然后再添进去;
在这里插入图片描述
 这里我们需要用到pygame.transform.scale(surface,(width,heigth)),参数1是一个包含需要使用图片的surface,第二个参数是放缩后图片大小的元组,它返回一个放缩后图片的surface
 我们在程序中添加了该函数,结果如下

# 上面省略
charlotte=pygame.image.load("1.jpg")
charlotte=pygame.transform.scale(charlotte,size)
charlotterect=charlotte.get_rect() # 获取矩形区域
# 下面省略

 它放缩后结果如下
在这里插入图片描述
 它看起来不是那么的完美,主要是因为我们初始设置窗口的时候,它的比例与图片的长宽比例不同,我们又发现了surface的一些方法如下:

方法名功能
pygame.Surface.blit将一个图像画到另一个图像上
pygame.Surface.convert转换图像的像素格式
pygame.Surface.convert_alpha转换图像的像素格式,包含alpha通道的转换
pygame.Surface.fill使用颜色填充Surface
pygame.Surface.get_rect获取Surface的矩形区域

 我们发现了Surface的get_rect方法;以及Surface的get_heigth和get_width方法;

import sys
import pygame
charlotte=pygame.image.load("1.jpg")
charlotterect=charlotte.get_rect()
print(charlotterect)
print(charlotterect[0])
print(charlotterect[1])
print(charlotterect[2])
print(charlotterect[3])
# <rect(0, 0, 1920, 1080)>
# 0
# 0
# 1920
# 1080

 我们发现get_rect的结果是一个四元组,并且它可以通过[]进行访问,它代表的是获取的图片的矩形区域的上下左右四个位置,所以最后我们可以这样做;

import sys
import pygame

charlotte=pygame.image.load("1.jpg")                   # 加载图片
charlotterect=charlotte.get_rect()                     # 获取矩形区域
size=(int(charlotterect[2]/3),int(charlotterect[3]/3)) # 这里把图片给缩小为原来的1/3
charlotte=pygame.transform.scale(charlotte,size)       # 进行对原图片的缩小并返回一个缩小后图片的surface

pygame.init()										   # 初始化pygame
screen=pygame.display.set_mode(size)			       # 显示窗口
color=(0,0,0)										   # 设置填充颜色
# charlotte=pygame.transform.scale(charlotte,size)
# charlotterect= (charlotte.get_rect())  # 获取矩形区域
# pygame.init()
# size=width,height=700,600
# screen=pygame.display.set_mode(size)
# color=(0,0,0)

while True:
    for event in pygame.event.get():
         if event.type == pygame.KEYDOWN:
             sys.exit()
    screen.fill(color)
    screen.blit(charlotte,charlotterect)
    pygame.display.flip()
pygame.quit()

 最后结果如下
在这里插入图片描述
 看起来很完美

  • 35
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值