import pygame
import sys
pygame.init()
bg = (0, 0, 0)
size = width, height = 600, 400
#设置界面尺寸
screen = pygame.display.set_mode(size)
#设置界面的标题
pygame.display.set_caption("my name is zhaoshuo")
#用实例化font
font = pygame.font.Font(None, 20)
#设置坐标
position = 0
line_height = font.get_linesize()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#用render方法将字体渲染surface对象
screen.blit(font.render(str(event), True, (0, 255, 0)), (0, position))
position += line_height
if position > height:
position = 0
screen.fill(bg)
#更新界面
pygame.display.flip()
基本思路:
1.将尺寸初始化
2.在while True里将字体用pygame里的render方法将字体渲染成surface对象
外加一个将按键操作记录下来的程序:
import pygame
import sys
pygame.init()
bg = (0, 0, 0)
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
pygame.display.set_caption("my name is zhaoshuo")
f = open('record.txt', 'w')
while True:
for event in pygame.event.get():
f.write(str(event) + '\n')
if event.type == pygame.QUIT:
f.close()
sys.exit()