2.设置窗口主窗口
screen = pygame.display.set_mode(400,400)
3.填充主窗口背景,参数值RGB
screen.fill((156,156,156))
4.设置窗口标题
pygame.display.set.caption('小马哥不马虎')
5.更新屏幕内容
pygame.display.flip()
6.pygame.display其他方法
方法名称 | 说明 |
pygame.display.get_surface() | 获取当前显示的Surface对象 |
pygame.display.flip() | 更新整个待显示的Surface对象到屏幕上 |
pygame.display.update() | 更新部分软件界面显示 |
pygame.display.lnfo() | 产生一个VideInfo对象,包含了显示界面的相关信息 |
pygame.display.set_icon() | 设置左上角的游戏图标,图标尺寸大小为32*32 |
pygame.display.iconify() | 将显示的主窗口即Surface对象最小化,或者隐藏 |
pygame.display.get_active() | 当前显示界面显示在屏幕上时返回True,如果窗口被隐藏和最小化时则返回False |
三.Pygame Surface创建图像
1.Surface模块创建图像
语法格式:
Surface=pygame.Surface(size=(width,height),flags,depth)
·size:表示Surface对象的矩形区域大小
·flage:功能标志位,有两个可选参数值HWSURFACE和SPCALPHA,前者代表将创建的Surface对象存放于显存中,后者表示让图像的每一个像素包含一个alpha通道
·depth:指定像素的颜色深度,默认为自适应模式,由Pygame自动调节
代码实例:
import pygame
import sys
pygame.init()
#设置主窗口
screen = pygame.display.set_mode((400,400))
screen.fill('blue')
#设置窗口标题
pygame.display.set_caption('小马哥不马虎')
#创建一个图像
face = pygame.Surface((60,60),flags=pygame.HWSURFACE)
#填充图像
face.fill(color='pink')
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
#将图像添加到主屏幕上
screen.blit(face,(100,100))
#更新屏幕内容
pygame.display.flip()
效果:
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()