-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmypygame.py
More file actions
32 lines (27 loc) · 1.07 KB
/
mypygame.py
File metadata and controls
32 lines (27 loc) · 1.07 KB
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
import sys
import pygame
#from pygame.locals import QUIT
# 初始化
pygame.init()
# 建立 window 視窗畫布,大小為 800x600
window_surface = pygame.display.set_mode((800, 600))
# 設置視窗標題為 Hello World:)
pygame.display.set_caption('Hello World:)')
# 清除畫面並填滿背景色
window_surface.fill((255, 255, 255))
# 宣告 font 文字物件
head_font = pygame.font.SysFont(None, 60)
# 渲染方法會回傳 surface 物件
text_surface = head_font.render('Hello World!', True, (0, 0, 0))
# blit 用來把其他元素渲染到另外一個 surface 上,這邊是 window 視窗
window_surface.blit(text_surface, (10, 10))
# 更新畫面,等所有操作完成後一次更新(若沒更新,則元素不會出現)
pygame.display.update()
# 事件迴圈監聽事件,進行事件處理
while True:
# 迭代整個事件迴圈,若有符合事件則對應處理
for event in pygame.event.get():
# 當使用者結束視窗,程式也結束
if event.type == QUIT:
pygame.quit()
sys.exit()