实现屏幕控制功能的需求
游戏全屏
游戏屏幕大小可调节
游戏屏幕无边框
更改游戏标题栏内容
更改游戏图标
屏幕控制的重要函数
屏幕尺寸和模式
pygame.display.set_mode(r=(0,0),flags=0)
r是游戏屏幕的分辨率,采用(width,height)方式输入,flags用来控制显示类型,可用|组合使用,常用显示标签如下:
pygame.RESIZABLE 窗口大小可调
pygame.NOFRAME 窗口没有边界显示
pygame.FULLSCREEN 窗口全屏显示
pygame.display.Info()
产生一个显示信息对象VideoInfo,表达当前屏幕的参数信息,拥有很多参数,其中current_w:当前显示模式或窗口的像素宽度,current_h:当前显示模式或窗口的像素高度尤其重要
窗口标题和图标
pygame.display.set_caption()
pygame.display.set_icon()
pygame.display.get_caption()
窗口感知和刷新
pygame.display.get_active()
pygame.display.flip()
pygame.display.update()
全屏的壁球小游戏代码
# Unit PYG03: Pygame Wall Ball Game version 4 全屏型
import pygame,sys
pygame.init()
vInfo = pygame.display.Info() #窗口全屏显示
size = width, height = vInfo.current_w, vInfo.current_h
speed = [1,1]
BLACK = 0, 0, 0
#print(pygame.display.Info())
#screen = pygame.display.set_mode(size, pygame.RESIZABLE) #窗口大小可调
#screen = pygame.display.set_mode(size, pygame.NOFRAME) #窗口无边框
screen = pygame.display.set_mode(size, pygame.FULLSCREEN) #窗口全屏显示
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
壁球伸缩型小游戏
Pygame.VIDEORESIZE
这是一种窗口大小更改的事件,事件发生后,返回event.size元组,包含新窗口的宽度和高度,其中.size[0]为宽度,.size[1]为高度
# Unit PYG03: Pygame Wall Ball Game version 5 伸缩型
import pygame,sys
pygame.init()
size = width, height = 600, 400
speed = [1,1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE) #窗口大小可调
#screen = pygame.display.set_mode(size, pygame.NOFRAME) #窗口无边框
#screen = pygame.display.set_mode(size, pygame.FULLSCREEN) #窗口全屏显示
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
窗口标题和图标函数
pygame.display.set_caption(title,icontitle=None)
title设置窗口的标题内容,icontitle设置图表化后的小标题
pygame.display.get_caption()
返回当前设置窗口的标题及小标题内容,返回结构为(title,icontitle)
pygame.display.set_icon(surface)
设置窗口的图标效果,图标是一个Surface对象
壁球图标型小游戏代码
# Unit PYG03: Pygame Wall Ball Game version 6 图标型
import pygame,sys
pygame.init()
size = width, height = 600, 400
speed = [1,1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE) #窗口大小可调
icon = pygame.image.load("PYG03-flower.png")
pygame.display.set_icon(icon)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
Pygame窗口感知和刷新运用
pygame.display.get_actibe()
当窗口在系统中显示时返回True,否则返回False
pygame.display.flip()重新绘制整个窗口
pygame.display.update()进重新绘制窗口中有变化的区域,相比.flip()执行更快
壁球感知型小游戏
# Unit PYG03: Pygame Wall Ball Game version 7 感知型
import pygame,sys
pygame.init()
size = width, height = 600, 400
speed = [1,1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE) #窗口大小可调
icon = pygame.image.load("PYG03-flower.png")
pygame.display.set_icon(icon)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
if pygame.display.get_active():
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)