pygame学习(二)在窗口中绘制图像
2018-03-04 16:31
225 查看
绘制图像
在上一篇中已经能够创建出简单的窗口,现在开始将图像绘制到窗口中import pygameimport sys
screen = pygame.display.set_mode((900,600))
image = pygame.image.load('alien_invasion\images\ship.bmp') #加载图片并赋值给image
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill((111,111,111))
img_rect = image.get_rect() #获取图片的矩形区域
screen_rect = screen.get_rect() #获取窗口的矩形区域
img_rect.centerx = screen_rect.centerx #将窗口的矩形x坐标值赋值给图片的矩形x坐标值
img_rect.centery = screen_rect.centery #如上
screen.blit(image,img_rect) #在screen上绘制image图片,第二个参数为目标位置
pygame.display.flip()此时结果如下:
小结:
pygame.image.load()
pygame.image.load() #从文件源加载图像,可以传递文件名或python文件类对象。 load(filename) -> Surface load(fileobj, namehint="") -> Surface
get_rect()
get_rect() #获取返回surface的矩形区域,这个矩形将始终从0开始,宽带为0,高度和图像大小相同。 get_rect(** kwargs) - > Rect
Rect属性值
Pygame使用Rect对象来存储和操作矩形区域,Rect对象具有几个可用于移动和对齐Rect的虚拟属性:x,y top, left, 4000 bottom, right topleft, bottomleft, topright, bottomright midtop, midleft, midbottom, midright center, centerx, centery size, width, height w,h
blit()
blit() #在另一个surface上绘制surface blit(source,dest,area = None,special_flags = 0) - > Rect 可以使用dest参数定位绘图。Dest可以是代表源的左上角的一对坐标。Rect也可作为目的地传递,矩形的左上角作为目标位置。
相关文章推荐
- OpenCV 2 学习笔记(15): 绘制图像直方图
- opencv学习之(五)-直方图计算和绘制图像直方图
- 用glew,glfw实现opengl绘制3D学习笔记1-实现一个窗口
- libgdx 学习笔记二 绘制图像
- 【OpenCV学习】图像轮廓的提取和绘制
- 图像绘制功能【OpenCV学习笔记5】
- OpenGL学习二十四:绘制像素,位图,图像
- C#学习笔记(十九):图形绘制进阶-图像(双倍缓冲)
- R语言学习四 图像的绘制一
- 【转】我的opengl编程学习(一)(简介、绘制图像、三维观
- GStreamer如何让videosink在指定的窗口进行图像绘制?
- libgdx 学习笔记二 绘制图像
- pygame学习(一)创建最简单窗口以及设置背景色
- opencv学习笔记12 图像轮廓的提取和绘制
- Windows GDI学习笔记(二)——窗口的绘制
- 我的opengl编程学习(一)(简介、绘制图像、三维观察、光照)
- Pygame学习(3)-----如何对图像进行翻转,缩放,旋转
- egret学习日记2图像绘制及高级图像
- pygame的学习以及python的巩固(窗口尺寸的显示)
- Halcon学习之八:图像区域叠加与绘制