天天看點

python的pygame遊戲架構

import pygame

from pygame.locals import *

def main():

    # 初始化螢幕

    pygame.init()

    screen = pygame.display.set_mode((480,360))

    pygame.display.set_caption('遊戲世界歡迎你')

    # 填充背景

    background = pygame.Surface(screen.get_size())       #建立一個'面'對象,可以認為它是一張透明的紙      

    background = background.convert()

    background.fill((20, 20, 50))

    # 顯示文本

    font = pygame.font.Font("C:/windows/fonts/msyh.ttf", 36)

    text = font.render("李興球的pygame遊戲世界歡迎你", 1, (10, 250, 250))

    textpos = text.get_rect()                           # textpos是一個矩形對象

    textpos.centerx = background.get_rect().centerx

    textpos.centery = background.get_rect().centery  

    background.blit(text, textpos)                      #把文本繪制到背景上

    # 繪制背景到螢幕上

    screen.blit(background, (0, 0))

    pygame.display.flip()

    # 事件循環

    while 1:

        for event in pygame.event.get():

            if event.type == QUIT:

                break

        screen.blit(background, (0, 0))

        pygame.display.flip()

    pygame.quit()

if __name__ == '__main__': main()

繼續閱讀