Pygame RPG 遊戲設計:視窗相關

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

本篇文章說明Pygame處理視窗程式相關的API用法,直接以程式碼的註解說明。
import pygame
import os
import sys

# 設定視窗位置
xPos = 100
yPos = 50
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (xPos, yPos)

pygame.init()

# 設定視窗大小
screen_width = 480
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))

# 設定視窗標題
pygame.display.set_caption('YunlinSONG PyGame Screen')

# 設定背景顏色
bgColor = (255, 0, 0)
screen.fill(bgColor)

# 設定Icon
screenIcon = pygame.image.load('tree.ico')
pygame.display.set_icon(screenIcon)

# 更新 Screen
pygame.display.flip()

# 等待接收到關閉視窗的事件訊息
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

在Pygame中,可以對視窗Screen做的事情都在pygame.display裡https://www.pygame.org/docs/ref/display.html。上述的程式碼裡,可看到要改變式窗的位置時,是設定SDL_VIDEO_WINDOW_POS系統變數,也就是Pygame有些功能沒有提供API。

沒有留言: