用小动画在屏幕上绘制形状,文字和图像

这个程序将在显示屏上绘制一些形状,画出 Hello World! 在屏幕中间,让图像进入窗口的每个角落。你可以使用所需的每个图像,但是你需要将图像文件放在与程序相同的目录中。

整个代码:

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 30 #frames per second setting
fpsClock = pygame.time.Clock()

#set up the window
screen = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('animation')

#set up the colors
white = (255, 255, 255)
black = (  0,   0,   0)
green = (0, 255, 0)
blue = (0, 0, 180)
red   = (255,   0,   0)

image  = pygame.image.load('image.png')
imagex = 360
imagey = 260
direction = 'left'

# text setting
font_obj = pygame.font.Font('freesansbold.ttf', 32)
text_surface_obj = font_obj.render('Hello World!', True, GREEN, BLUE)
text_rect_obj = text_surface_obj.get_rect()
text_rectObj.center = (200, 150)

while True: # the main game loop
    screen.fill(WHITE)

    # draw a green polygon onto the surface
    pygame.draw.polygon(screen, green, ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)))

    # draw some blue lines onto the surface
    pygame.draw.line(screen, blue, (60, 60), (120, 60), 4)
    pygame.draw.line(screen, blue, (120, 60), (60, 120))
    pygame.draw.line(screen, blue, (60, 120), (120, 120), 4)

    # draw a blue circle onto the surface
    pygame.draw.circle(screen, blue, (300, 50), 20, 0)

    # draw a red ellipse onto the surface
    pygame.draw.ellipse(screen, red, (100, 150, 40,80), 1)

    # draw a red rectangle onto the surface
    pygame.draw.rect(screen,red, (200, 150, 100, 50))

    # draw the text onto the surface
    screen.blit(text_surface_obj, text_rect_obj)

    #the animation of the image
    if direction == 'right':
        imagex += 5
        if imagex == 360:
            direction = 'down'
    elif direction == 'down':
        imagey += 5
        if imagey == 260:
            direction = 'left'
    elif direction == 'left':
        imagex -= 5
        if imagex == 20:
            direction = 'up'
    elif direction == 'up':
        imagey -= 5
        if imagey == 20:
            direction = 'right'
    screen.blit(image, (imagex, imagey))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()
    fpsClock.tick(FPS)

绘制白色背景:

screen.fill(white)

绘制多边形:

在此功能中,你可以定义显示表面,颜色和多边形每个角的位置,你可以顺时针和逆时针执行此操作。

pygame.draw.polygon(screen, green, ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)))

绘制线条:

在这里,你可以定义显示表面,颜色,第一个点和最后一个点以及线的宽度。

pygame.draw.line(screen, blue, (60, 60), (120, 60), 4)
pygame.draw.line(screen, blue, (120, 60), (60, 120))
pygame.draw.line(screen, blue, (60, 120), (120, 120), 4)

绘制圆圈:

在此功能中,你可以定义显示表面,颜色,位置,半径和圆的宽度(0 表示一个普通圆)。

pygame.draw.circle(screen, blue, (300, 50), 20, 0)

绘制椭圆:

在此功能中,你可以定义显示表面,颜色,位置,水平尺寸,垂直尺寸和椭圆的宽度

pygame.draw.ellipse(screen, red, (100, 150, 40,80), 1)

绘制矩形:

在此功能中,你可以定义矩形的显示表面,颜色,位置以及垂直和水平尺寸。

pygame.draw.rect(screen,red, (200, 150, 100, 50))

定义文本:

首先定义文本的类型和大小,我使用 pygame 获得的基本字体。

font_obj = pygame.font.Font('freesansbold.ttf', 32)

然后定义实际文本,如果你想要粗体(True / False),文本的颜色,如果要标记文本,则定义标记的颜色。

text_surface_obj = font_obj.render('Hello World!', True, green, blue)

如果要标记文本或想要定义文本的中心,则必须使用此函数告诉 pygame:

text_rect_obj = text_surface_obj.get_rect()

之后,你可以使用此功能定义文本的中心:

text_rect_obj.center = (200, 150)

绘制文字:

如果你标记了文本或定义了中心,则必须以这种方式绘制文本:

screen.blit(text_surface_obj, text_rectObj)

否则你绘制文本,但是你需要定义位置,所以你这样做:

DISPLAYSURF.blit(textSurfaceObj, (100,50))

定义图像:

在此定义要使用的图像,起始位置(x 和 y 坐标)以及图像的方向。

image  = pygame.image.load('image.png')
imagex = 360
imagey = 260
direction = 'left'

动画图像:

在这里你检查图像的方向,如果它到达一个角落,如果是这样改变方向,如果没有,将它移动 5 个像素在同一方向并再次绘制图像。这就是我们对这部分代码所做的事情:

if direction == 'right':
    imagex += 5
    if imagex == 360:
        direction = 'down'
elif direction == 'down':
    imagey += 5
    if imagey == 260:
        direction = 'left'
elif direction == 'left':
    imagex -= 5
    if imagex == 20:
        direction = 'up'
elif direction == 'up':
    imagey -= 5
    if imagey == 20:
        direction = 'right'
screen.blit(image, (imagex, imagey))

注意:我的图像是 20 x 20 像素,我使用 if imagex == 360 if imagey == 260: 因为我的图像距离边缘 20 像素,如果你的图像有不同的大小,你将不得不改变数字。

检查你是否退出该计划:

在这里,我们检查你是否关闭了程序窗口。

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()

更新显示:

在这里,你告诉 pygame 更新显示,以便你绘制的所有内容都显示在屏幕上。

pygame.display.update()

定义每秒帧数:

在这里你告诉 pygame 足够睡眠,以便尊重每秒帧数设置。

fpsClock.tick(FPS)