用小動畫在螢幕上繪製形狀,文字和影象

這個程式將在顯示屏上繪製一些形狀,畫出 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)