《零基礎入門學習Python》第081講:Pygame:提高遊戲的顏值1
毋庸置疑,在這個出門看臉的時代,高顏值的界面會給你的遊戲帶來更多的眼球。這兩節課我們就來談談如何提高Pygame遊戲的顏值。
我們一直通過display模塊的set_mode() 方法來指定界面的大小,這個方法會返回一個Surface 對象,我們就使用這個Surface 對像作為界面。今天我們就來仔細講解set_mode() 方法。
set_mode(resolution = (0, 0), flags = 0, depth = 0) -> Surface
這個方法有三個參數,
第一個參數resolution 用於指定界面的大小。如果什麼都不給的話,就是使用默認的(0, 0),Pygame 會根據當前的屏幕分辨率來創建一個窗口。大家要注意,這一功能只有在SDL版本高於或等於1.2.10時才會實現。如果是低於這個版本號,就會拋出異常。所以一般情況下,第一個參數還是給定較好。
第二個參數flags是指定擴展選項,同時指定多組選項可以使用管道操作符"|",這兒是flags參數提供的幾個可選項:
選項
|
含義
|
| pygame.FULLSCREEN | 創建一個全屏顯示 |
| pygame.DOUBLEBUF | 1.雙緩沖模式
2.推薦和HWSURFACE或OPENGL一起使用 |
| pygame.HWSURFACE | 硬件加速,只有在FULLSCREEN 下可以使用 |
| pygame.OPENGL | 創建一個OPENGL 渲染的顯示 |
| pygame.RESIZABLE | 創建一個可調整尺寸的窗口 |
| pygame.NOFRAME | 創建一個沒有邊框和控制按鈕的窗口 |
第三個參數depth 用於指定顏色的位數,一般不推薦自己設置,Pygame 會根據當前的操作系統去取一個最合適的值。
大家可能會注意一個情況,我們玩的大多數遊戲都是全屏模式,不是全屏還不給玩,你知道是為什麼嗎?
因為全屏的好處有太多了,你可以顯示更多的內容,可以開啟硬件加速,最重要的是你一個程序霸占了所有的屏幕,其他軟件就不能玩了。
全屏模式在Pygame中非常簡單,只需要將第二個參數設置為 FULLSCREEN 即可,同時,我們還可以加上硬件加速。
我們需要關聯一個快捷鍵,讓我們的全屏得到控制。
代碼如下:
-
-
-
-
-
-
-
size = width, height = 600, 400
-
-
-
-
-
-
-
clock = pygame.time.Clock()
-
-
-
screen = pygame.display.set_mode(size)
-
-
pygame.display.set_caption("初次见面,请大家多多关照!")
-
-
-
turtle = pygame.image.load("python.png")
-
-
position = turtle.get_rect()
-
-
-
r_head = pygame.transform.flip(turtle, True, False)
-
-
-
for event in pygame.event.get():
-
if event.type == pygame.QUIT:
-
-
-
if event.type == pygame.KEYDOWN:
-
if event.key == pygame.K_LEFT:
-
-
-
if event.key == pygame.K_RIGHT:
-
-
-
if event.key == pygame.K_UP:
-
-
if event.key == pygame.K_DOWN:
-
-
-
-
if event.key == pygame.K_F11:
-
fullscreen = not fullscreen
-
-
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN | pygame.HWSURFACE)
-
-
screen = pygame.display.set_mode(size)
-
-
-
position = position.move(speed)
-
-
if position.left < 0 or position.right > width:
-
-
turtle = pygame.transform.flip(turtle, True, False)
-
-
-
-
if position.top < 0 or position.bottom > height:
-
-
-
-
-
-
screen.blit(turtle, position)
-
-
-
-
-
-
(其實,上面的程序在全屏時還存在一個小BUG,不知道大家發現了沒有,小蛇蛇在原始尺寸的位置就調頭了,大家可以動手修改一下,很簡單的。)(修改的代碼如下:)
-
-
-
-
-
-
-
size = width, height = 600, 400
-
-
-
-
-
-
-
clock = pygame.time.Clock()
-
-
-
screen = pygame.display.set_mode(size)
-
-
pygame.display.set_caption("初次见面,请大家多多关照!")
-
-
-
turtle = pygame.image.load("python.png")
-
-
position = turtle.get_rect()
-
-
-
r_head = pygame.transform.flip(turtle, True, False)
-
-
-
for event in pygame.event.get():
-
if event.type == pygame.QUIT:
-
-
-
if event.type == pygame.KEYDOWN:
-
if event.key == pygame.K_LEFT:
-
-
-
if event.key == pygame.K_RIGHT:
-
-
-
if event.key == pygame.K_UP:
-
-
if event.key == pygame.K_DOWN:
-
-
-
-
if event.key == pygame.K_F11:
-
fullscreen = not fullscreen
-
-
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN | pygame.HWSURFACE)
-
width , height = 1920, 1080
-
-
screen = pygame.display.set_mode(size)
-
-
-
-
position = position.move(speed)
-
-
if position.left < 0 or position.right > width:
-
-
turtle = pygame.transform.flip(turtle, True, False)
-
-
-
-
if position.top < 0 or position.bottom > height:
-
-
-
-
-
-
screen.blit(turtle, position)
-
-
-
-
-
-
接下來我們又發現了一個BUG,就是如果在全屏模式下,小蛇蛇跑出了原始尺寸的邊界的時候恢復原始尺寸,小蛇蛇就不見了,在這種情況下,我們應該復位:
-
-
-
-
-
-
-
size = width, height = 600, 400
-
-
-
-
-
-
-
clock = pygame.time.Clock()
-
-
-
screen = pygame.display.set_mode(size)
-
-
pygame.display.set_caption("初次见面,请大家多多关照!")
-
-
-
turtle = pygame.image.load("python.png")
-
-
position = position_0= turtle.get_rect()
-
-
-
r_head = pygame.transform.flip(turtle, True, False)
-
-
-
for event in pygame.event.get():
-
if event.type == pygame.QUIT:
-
-
-
if event.type == pygame.KEYDOWN:
-
if event.key == pygame.K_LEFT:
-
-
-
if event.key == pygame.K_RIGHT:
-
-
-
if event.key == pygame.K_UP:
-
-
if event.key == pygame.K_DOWN:
-
-
-
-
if event.key == pygame.K_F11:
-
fullscreen = not fullscreen
-
-
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN | pygame.HWSURFACE)
-
width , height = 1920, 1080
-
-
screen = pygame.display.set_mode(size)
-
-
if position.right > width or position.bottom > height:
-
-
-
-
position = position.move(speed)
-
-
if position.left < 0 or position.right > width:
-
-
turtle = pygame.transform.flip(turtle, True, False)
-
-
-
-
if position.top < 0 or position.bottom > height:
-
-
-
-
-
-
screen.blit(turtle, position)
-
-
-
-
-
-
當我們運行程序時按下F11,就會全屏顯示,這裡的全屏尺寸(1920,1080)是我知道自己電腦的尺寸設置的。但是你的遊戲是開發給大家用的,這樣子你就不知道用戶的顯示器的尺寸了,那怎麼辦呢?
事實上有一種方法可以獲得,Pygame 我們可以使用display 模塊裡的list_modes() 方法。
我們打開IDLE 嘗試一下:
-
-
-
Hello from the pygame community. https://www.pygame.org/contribute.html
-
-
-
>>> pygame.display.list_modes()
-
[(1920, 1080), (1680, 1050), (1440, 900), (1280, 1024), (1280, 960), (1280, 720), (1024, 768), (800, 600)]
它會打印當前顯示器支持的所有分辨率尺寸。list_modes() 是返回一個列表,從大到小依次列舉當前顯示器支持的分辨率,所以全屏的話,就是將這個列表獲取之後,取第一個元素作為set_mode() 的尺寸即可。
我們接下來談談使得窗口可變。默認情況下窗口是不可以通過拖拽來改變窗口大小的。
你想一下,遊戲裡面的任務都是提前設定好尺寸的,被你一拖拽,馬上面目全非了。
儘管如此,Pygame 還是提供了支持改變窗口尺寸的選項。就是設置 RESIZABLE 選項來實現。
如果用戶調整窗口尺寸時,會引發一個事件,叫做VIDEORESIZE。
如果只是在set_mode() 中添加參數pygame.RESIZABLE ,如下:
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
顯然是沒有任何作用的,我們需要關注 VIDEORESIZE 事件,並執行相應操作,如下:
-
-
-
-
-
-
-
size = width, height = 600, 400
-
-
-
-
-
-
-
clock = pygame.time.Clock()
-
-
-
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
-
-
pygame.display.set_caption("初次见面,请大家多多关照!")
-
-
-
turtle = pygame.image.load("python.png")
-
-
position = position_0 = turtle.get_rect()
-
-
-
r_head = pygame.transform.flip(turtle, True, False)
-
-
-
for event in pygame.event.get():
-
if event.type == pygame.QUIT:
-
-
-
if event.type == pygame.KEYDOWN:
-
if event.key == pygame.K_LEFT:
-
-
-
if event.key == pygame.K_RIGHT:
-
-
-
if event.key == pygame.K_UP:
-
-
if event.key == pygame.K_DOWN:
-
-
-
-
if event.key == pygame.K_F11:
-
fullscreen = not fullscreen
-
-
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN | pygame.HWSURFACE)
-
width , height = 1920, 1080
-
-
screen = pygame.display.set_mode((600, 400))
-
-
if position.right > width or position.bottom > height:
-
-
-
-
if event.type == pygame.VIDEORESIZE:
-
-
-
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
-
if position.right > width or position.bottom > height:
-
-
-
-
position = position.move(speed)
-
-
if position.left < 0 or position.right > width:
-
-
turtle = pygame.transform.flip(turtle, True, False)
-
-
-
-
if position.top < 0 or position.bottom > height:
-
-
-
-
-
-
screen.blit(turtle, position)
-
-
-
-
-
-
0 留言:
發佈留言