問題

在寫AVG遊戲的時候,常常遇到立繪需要變更的狀況。考量以下場景

  1. 人物A平常表情
  2. 人物A笑
  3. 人物A平常表情
  4. 人物A立繪從場上消失

程式碼可能會長這樣

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
image a_normal = "a_normal.png"
image a_laugh = "a_laugh.png"

label start:
    show a_normal
    
    pause 2
    
    hide a_normal
    show a_laugh
    
    puase 2
    
    hide a_laugh
    show a_normal
    
    pause 2
    
    hide a_normal

如果要人物A換動作,就得先記得他上次是哪個圖片,隱藏之後再show新的,想想就累。

解法

renpy提供了一個方便的解法。在取變數名稱的時候可以用兩個識別字來描述。
例如可以把立繪取叫

1
2
image a normal = "a_normal.png"  
image a laugh = "a_laugh.png"  

如此一來,renpy就會知道a normal和 a laugh為同一人的不同種表情,因為前識別字a都一樣。
這樣show a normal時如果a laugh在場上就會自動隱藏了。
最後若要將a隱藏起來的時候只需要hide前識別字即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
image a normal = "a_normal.png"
image a laugh = "a_laugh.png"

label start:
    show a normal
    
    pause 2
    
    show a laugh
    
    puase 2

    show a normal
    
    pause 2
    
    hide a

省了不少程式碼,也不需要記上次是哪種立繪。