程式設計[3] Pygame 視窗創建

[程式語言] Python

[牛哥哥的小程式][Python]  Pygame 視窗創建

 

[演算法]

這邊是試著用pygame module 建一個正常的遊戲主視窗,主要是需要熟練一下pygame視窗是不能直接 quit 的,他需要透過偵查到使用者點即關閉視窗的Event才能進入quit處理區,也就是說一個 Helloe World Pygame 版本,至少需要包含基本的Event 監聽Quit事件和處理邏輯。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    # ***** 遊戲開始後 需要有一個 迴圈檢測 使用者是不是結束了遊戲 *****
    while True:
        # 取得所有的Event
        for event in pygame.event.get():
            # 如果event是QUIT,也就是按右上角的x
            if event.type == pygame.QUIT:
                # 將pygame殺掉
                pygame.quit()
                # 終止程式
                sys.exit(0)
        # 一直更新pygame的畫面
        pygame.display.update()

[程式架構]

  1. Step 1. Pygame 需要先進行基本初始化.
    pygame.init()
  2. Step 2. New 一個特定大小的 Pygame 遊戲視窗。.
    pgWindow = pygame.display.set_mode((600,300))
    New 完視窗後可以對視窗內容放置特定字體的文字內容及做一些簡單設置,但在此例中這些並非必要。
    # Set PyWindow background Color
       pgWindow.fill((0,255,255))
    # Set PyWindow caption
       pygame.display.set_caption("pygame window demo")   
    # a font object with size for H1 Text, default font type( None).
        pgFontH1 = pygame.font.Font(None, 40) # font size as 48, None: 不指定字體
    # font_object.render(text, "去除鋸齒?", Text Color, Text Background Color)
        pgWindowText = pgFontH1.render("Hello Pygame World",True,(0,0,0))
    # Print text on pygame window at point(30,50)
        pgWindow.blit(pgWindowText, (30,50))
  3. Step 3. 執行無限迴圈監聽quit event, quit Event 被捕捉,執行關閉程式的任務,最後無限迴圈必須透過函數 pygame.display.update() 執行必要的刷新視窗內容功能。

[程式碼pygame_window_demo.py

GitHub : Python_test2/pygame_window_demo.py at main · jackterrylau/Python_test2 (github.com)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pygame, os, sys, time, random
from pygame.locals import *

if __name__ == '__main__':
    ### Step 1. initialize pygame module
    pygame.init()
    ###

    ### Step 2. New a pygame screen window with size(width,height)
    pgWindow = pygame.display.set_mode((600,300))

    # Set Screen widow background Color
    pgWindow.fill((0,255,255))
    pygame.display.set_caption("pygame window demo")

    ###
    # a font object with size, no assign font type.

    pgFontH1 = pygame.font.Font(None, 40) # font size as 48, None: 不指定字體

    # font_object.render(text, "去除鋸齒?", Text Color, Text Background Color)

    pgWindowText = pgFontH1.render("Hello Pygame World",True,(0,0,0))

    # Print text on pygame window which point = (x,y) = (30,50)

    pgWindow.blit(pgWindowText, (30,50))

    pgFontContent = pygame.font.Font(None, 24)
    pgWindowContentText1 = pgFontContent.render("Step 1. initialize pygame module",True,(0,0,0))

    pgWindow.blit(pgWindowContentText1, (50,100))

    pgWindowContentText2 = pgFontContent.render("Step 2. New a pygame screen window with size(width,height)",
                                                True,(0,0,0))
    pgWindow.blit(pgWindowContentText2, (50,120))
    pgWindowContentText3 = pgFontContent.render("Step 3. Event Loop with a quit check",True,(0,0,0))
                                            
    pgWindow.blit(pgWindowContentText3, (50,140))

    # ***** 遊戲開始後 需要有一個 迴圈檢測 使用者是不是結束了遊戲 *****
    while True:
        # 取得所有的Event
        for event in pygame.event.get():
            # 如果event是QUIT,也就是按右上角的x
            if event.type == pygame.QUIT:
                # 將pygame殺掉
                pygame.quit()
                # 終止程式
                sys.exit(0)
        # 一直更新pygame的畫面
        pygame.display.update()

2024720日星期六

arrow
arrow
    文章標籤
    python pygame 程式設計
    全站熱搜
    創作者介紹
    創作者 jackterrylau 的頭像
    jackterrylau

    儒道哲學的浪漫人生

    jackterrylau 發表在 痞客邦 留言(0) 人氣()