1.导入pygame库
import pygame
2.引入pygame常量
from pygame.locals import *
3.初始化pygame常量
pygame.init()
4.创建窗口,pygame.display.set_mode(resolution=(0,0),flags=0,depth=0),resolution窗口大小,flags显示屏类型,
pygame.FULLSCREEN,控制全屏,0或者1来控制
pygame.HWSURFACE 控制是否进行硬件加速
pygame.RESIZABLE 控制窗口是否可以调节大小
screen = pygame.display.set_mode((480, 853), pygame.RESIZABLE, 0)
5.设置窗口的标题,pygame.display.get_caption()获得窗口的标题
pygame.display.set_caption("文字显示")
6.创建文字对象,并显示
white = 255,255,255
blue = 0,0,200
myfont = pygame.font.Font(None,60)
textImage = myfont.render("Hello Pygame", True, white)
screen.fill(blue)
screen.blit(textImage, (10,200))
pygame.display.update()
7.此时运行程序,会看到一个窗口一闪而过。让它长时间显示,放置到一个循环中
# -*- coding:utf-8 -*-
import pygame
import sys
from pygame.locals import *
white = 255,255,255
blue = 0,0,200
pygame.init()
screen = pygame.display.set_mode((480, 853), pygame.RESIZABLE, 0)
pygame.display.set_caption("pygame学习(一)文字显示")
myfont = pygame.font.Font(None,60)
textImage = myfont.render("Hello Pygame", True, white)
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
sys.exit()
screen.fill(blue)
screen.blit(textImage, (10,200))
pygame.display.update()