吴裕雄--天生自然python学习笔记:python 用pygame模块加载图片

加载图片
使用几何绘图无法画出精细的图形,所以我们可以把现成的图片加载到 Pygam e 
中直接使用 。 加载图片的语法为 :

图片加载后通常会用 convert 方法加以处理, 以增加显示速度,语法为:

例如,载入 media 文件夹中的 imgOl.jpg 图片文件井保存至 image 变量:

 

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 280))
pygame.display.set_caption("载入图片")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0,255,0))

image = pygame.image.load("F:\\pythonBase\\pythonex\\ch14\\media\\img01.jpg")
image.convert()
compass = pygame.image.load("F:\\pythonBase\\pythonex\\ch14\\media\\compass.png")
compass.convert()

background.blit(image, (20,10))
background.blit(compass, (400,50))

running = True
screen.blit(background, (0,0))
pygame.display.update()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

 插入文本

 例如,插入中文及英文文本 :

import pygame

pygame.init()
screen = pygame.display.set_mode((500, 100))
pygame.display.set_caption("加载图片")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0,255,0))  #背景为录色

font1 = pygame.font.SysFont("simhei", 24)
text1 = font1.render("显示中文", True, (255,0,0), (255,255,255))  #中文,不同背景色
background.blit(text1, (20,10))
text2 = font1.render("Show english.", True, (0,0,255), (0,255,0))  #英文,相同背景色
background.blit(text2, (20,50))

running = True
screen.blit(background, (0,0))
pygame.display.update()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

猜你喜欢

转载自www.cnblogs.com/tszr/p/12036474.html