您的位置:首页 > 其它

pygame学习(二)在窗口中绘制图像

2018-03-04 16:31 225 查看

绘制图像

在上一篇中已经能够创建出简单的窗口,现在开始将图像绘制到窗口中import pygame
import 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也可作为目的地传递,矩形的左上角作为目标位置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: