簡體   English   中英

如何使用 pyxel 和 python 從一個場景過渡到另一個場景

[英]How to transition from one scene to another using pyxel and python

我正在開發基於選擇的基於文本的角色扮演游戲。 我的角色扮演游戲中的這個場景有一個計時器。 當玩家成功過渡到另一個場景時,我正在嘗試找出一種重置計時器本身的方法。 為此,還需要每次都重置計時器。 有什么建議么?

我試圖通過使用另一個 function escape() 從這個原始場景過渡到下一個場景,從而從一個場景過渡到另一個場景。 問題是計時器沒有重置,它會自動轉到“你做到了”。

import time
import pyxel

class App:
    def __init__(self):
        pyxel.init(250, 150, display_scale=3, title='Kingsforth')
        self.timer = True
        self.start = time.time()
        self.time_left = None
        pyxel.run(self.update, self.draw)

    def update(self):
        self.time_left = 5 - int(time.time() - self.start)
        if self.time_left >= 0 and (pyxel.btn(pyxel.KEY_LEFT) or pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.KEY_UP)):
            self.timer = False

    def draw(self):
        pyxel.cls(pyxel.COLOR_BLACK)
        if self.timer and self.time_left >= 0:
            pyxel.text(x=1, y=1, s='You walk into an opening filled with guards! Where do you go?', col=pyxel.COLOR_WHITE)
            pyxel.text(x=1, y=10, s='Options:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=35, y=10, s='Left, Right, Up', col=pyxel.COLOR_GREEN)
            pyxel.text(x=1, y=20, s='Time left:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=45, y=20, s=str(self.time_left), col=pyxel.COLOR_RED)
        elif self.timer and self.time_left < 0:
            pyxel.text(x=1, y=1, s='The guards catch you. They execute you on the spot.. You die.', col=pyxel.COLOR_WHITE)
        elif not self.timer:
         self.escape()

    def escape(self):
        pyxel.cls(pyxel.COLOR_BLACK)
        if self.timer and self.time_left >= 0:
            pyxel.text(x=1, y=1, s='You run into junk yard filled with debree.. Where do you go?', col=pyxel.COLOR_WHITE)
            pyxel.text(x=1, y=10, s='Options:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=35, y=10, s='Left, Right, Up', col=pyxel.COLOR_GREEN)
            pyxel.text(x=1, y=20, s='Time left:', col=pyxel.COLOR_WHITE)
            pyxel.text(x=45, y=20, s=str(self.time_left), col=pyxel.COLOR_RED)
        elif self.timer and self.time_left < 0:
            pyxel.text(x=1, y=1, s='The guards catch you. They execute you on the spot.. You die.', col=pyxel.COLOR_WHITE)
        elif not self.timer:
            pyxel.text(x=1, y=1, s='You did it!', col=pyxel.COLOR_WHITE)


App()

最好將游戲文本和實際代碼分開。 您可以將其存儲在單獨的變量中。

import time
import pyxel

SCENES = [{'Description': 'You walk into an opening filled with guards! Where do you go?',
           'Dead': 'The guards catch you. They execute you on the spot.. You die.'},
          {'Description': 'You run into junk yard filled with debree.. Where do you go?',
           'Dead': 'The guards catch you. They execute you on the spot.. You die.'},
          {'Description': 'You did it!'}]


class App:
    def __init__(self):
        pyxel.init(250, 150, display_scale=3, title='Kingsforth')
        self.scene_id = 0
        self.start = time.time()
        self.time_left = None
        pyxel.run(self.update, self.draw)

    def update(self):
        self.time_left = 5 - int(time.time() - self.start)
        if self.time_left >= 0 and SCENES[self.scene_id].get('Dead') and (pyxel.btnr(pyxel.KEY_LEFT) or pyxel.btnr(pyxel.KEY_RIGHT) or pyxel.btnr(pyxel.KEY_UP)):
            self.scene_id += 1
            self.start = time.time()
            self.time_left = 0

    def draw(self):
        pyxel.cls(pyxel.COLOR_BLACK)
        if SCENES[self.scene_id].get('Dead'):
            if self.time_left >= 0:
                pyxel.text(x=1, y=1, s=SCENES[self.scene_id]['Description'], col=pyxel.COLOR_WHITE)
                pyxel.text(x=1, y=10, s='Options:', col=pyxel.COLOR_WHITE)
                pyxel.text(x=35, y=10, s='Left, Right, Up', col=pyxel.COLOR_GREEN)
                pyxel.text(x=1, y=20, s='Time left:', col=pyxel.COLOR_WHITE)
                pyxel.text(x=45, y=20, s=str(self.time_left), col=pyxel.COLOR_RED)
            else:
                pyxel.text(x=1, y=1, s=SCENES[self.scene_id]['Dead'], col=pyxel.COLOR_WHITE)
        else:
            pyxel.text(x=1, y=1, s=SCENES[self.scene_id]['Description'], col=pyxel.COLOR_WHITE)

App()

Output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM