简体   繁体   中英

Adding a countdown timer in python

I'll admit, i am a newbie with python, but here is my issue.

the version is 2.6.5 (i know i'ts an old version but there's reasons to this) and livewires is used

Bascially this game has a bunch of colored balloons in which you need to click them to make them disappear. Adjacent balloons of the same color disappear along with the clicked balloon. Once the balloons are cleared it moves on to the next level.

I need to create a timer on the top right of my screen. This timer needs to countdown in seconds (from 30 might be a good start.) However no matter what i try, either the timer does not display or the numbers are overlap eachother. I would like to know how to do this, as it has been driving me up the wall as of late.

...Of course it also needs to end the game if it reaches zero and add more time if the level is complete...

But for now i just want to focus on displaying the timer and having it count down to zero on screen.

class Timer(games.Sprite):
    """ countdown timer """
    def __init__(self):
       timer_message = games.Text(
           value = 30,
           size = 50,
           color = red,
           x = 600,
           y = 30
        )
    def start(self):
        while self.timer_message.value != 0:
            time.sleep(1)
            self.timer_message.value -= 1
            game.screen.add(timer_message)

Alright. I fixed the "compressing balloons table" (accidentally deleted the self_update lol) problem, but now it is saying that "global name timer_message is not defined"... despite the fact that it says timer_message = games.Text

I would paste the whole code, but i can't get the indentation right (this is my first time using this website.)

So, I understand it's been some time and if you don't need an answer anymore that's alright.

For now it's hard to answer your question in general because I don't understand the structure of the rest of your code or how you're displaying graphics. However, I can tell you while you're getting the

global name timer_message is not defined

error. It's because when you define timer_message within the __init__ function you are defining it within the local scope of the function but not for the class. In order to make it accessible to the class you need to assign to self.timer_message.

This is a consequence of how python imitates object oriented programming, but making this change should address your immediate error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM