Python Pygame庫的學習之路(1)

一.繪製Hello Pygame

1.安裝Pygame庫,Pygame並非和Python打包到一塊兒,你須要獨立安裝它,安裝命令以下:python

pip install pygame

2.咱們開始編寫第一個Hello Pygame程序:字體

import pygame
from pygame.locals import *

3.定義咱們繪製Hello Pygame時使用的字體:spa

myfont = pygame.font.Font(None,60)

4.定義顏色變量:code

blue = 0,0,255
white = 255,255,255

textImage  = myfont.render("Hello Pygame",True,white) #咱們要繪製的文字

5.要繪製文本,咱們要清屏,進行繪製,而後再刷新顯示:ip

screen.fill(blue)
screen.blit(textImage,(100,100))
pygame.display.update()

6.若是你如今運行程序,程序會立馬閃退,因此咱們須要設置一個延遲:get

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()

7.下面,咱們來展現一下完整代碼:it

import pygame
import sys
from pygame.locals import *
white = 255,255,255
blue = 0,0,200
pygame.init()
screen = pygame.display.set_mode((600,500))
myfont = pygame.font.Font(None,60)
textImage = myfont.render("Hello Pygame",True,white)

while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill(blue)
    screen.blit(textImage,(150,100))
    pygame.display.update()
相關文章
相關標籤/搜索