壁球小游戏(文字型+色彩型)
import pygame, sysimport pygame.freetype
pygame.init()
size = width, height = 600, 400
speed =
GOLD = 255, 251, 0
BLACK = 0, 0, 0
pos =
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame文字绘制")
f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
fps = 300
fclock = pygame.time.Clock()
bgcolor = pygame.Color("black")
def RGBChannel(a):
return 0 if a<0 else (255 if a>255 else int(a))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pos < 0 or pos + f1rect.width > width:
speed = - speed
if pos <0 or pos + f1rect.height > height:
speed = - speed
pos = pos + speed
pos = pos + speed
bgcolor.r = RGBChannel(pos*255/f1rect.width)
bgcolor.g = RGBChannel(pos*255/f1rect.height)
bgcolor.b = RGBChannel(min(speed,speed)*255/max(speed,speed,1))
screen.fill(bgcolor)
f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
screen.blit(f1surf, (pos, pos))
pygame.display.update()
fclock.tick(fps)
页:
[1]