Help with Timer for Soccer Game..

People i need create a half-time timer in a Soccer Game…

Its a Timer with 90 minuts but in real time just 1 or 2 minutes to end the game, like Pes or Fifa…

And i dont know how to do…

Thanks,
André.

You could use a task for this, something like:

import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText
t = OnscreenText( scale = .15, mayChange = True )
def timer( task ):
  t.setText( str( int( round( task.time ) ) ) )
  return task.cont
taskMgr.add( timer, 'Timer' ) # or the next
# taskMgr.doMethodLater( 1, timer, 'Timer' )
run()

First thanks for reply…

I’m trying but i’m a little noob in Panda…i’m learning yet…

if you can help me the error i’m geting is:

Traceback (most recent call last):
  File "/Users/andrecarvalho/Desktop/Ultimate_Soccer_v1.0/Game.py", line 282, in <module>
    game.run()
  File "/Developer/Panda3D/lib/direct/showbase/ShowBase.py", line 2630, in run
    self.taskMgr.run()
  File "/Developer/Panda3D/lib/direct/task/Task.py", line 502, in run
    self.step()
  File "/Developer/Panda3D/lib/direct/task/Task.py", line 460, in step
    self.mgr.poll()
  File "/Users/andrecarvalho/Desktop/Ultimate_Soccer_v1.0/Game.py", line 195, in updateScene
    self.screen.update()
  File "/Users/andrecarvalho/Desktop/Ultimate_Soccer_v1.0/screen.py", line 55, in update
    taskMgr.add( self.timer(), 'Timer' ) # or the next
  File "/Users/andrecarvalho/Desktop/Ultimate_Soccer_v1.0/screen.py", line 33, in timer
    print(t.setText(str( int( round( task.time ) ) )) )
AttributeError: Screen instance has no attribute 'time'

The Code is here…

from direct.gui.OnscreenText import OnscreenText
import direct.directbase.DirectStart 
from pandac.PandaModules     import *

t = OnscreenText( scale = .15, mayChange = True )

class Screen():

    def __init__(self, score):

        self.score = score

        self.textScore = OnscreenText(
            text  = "Vermelha   0 - 0   Azul",
            style = 1,
            fg    = (1,1,1,1),
            pos   = (0, 0.90),
            align = TextNode.ACenter,
            scale = .07
        )



        self.addInstructions(-0.70, "[W/I]: Frente")
        self.addInstructions(-0.75, "[A/J]: Esquerda")
        self.addInstructions(-0.80, "[D/L]: Direita")
        self.addInstructions(-0.85, "[Q/U]: Driblar")
        self.addInstructions(-0.90, "[E/O]: Rematar")
        self.addInstructions(-0.95, "[ESC]: Quit")


    def timer(task):
        print(t.setText(str( int( round( task.time ) ) )) )
        return task.cont


# taskMgr.doMethodLater( 1, timer, 'Timer' )
#run()

    def addInstructions(self, position, message):
        return OnscreenText(
            text  = message,
            style = 1,
            fg    = (1,1,1,1),
            pos   = (.85, position),
            align = TextNode.ALeft,
            scale = .05
        )

    def update(self):
        self.textScore.setText(
            "Vermelha   " + str(self.score["goalRed"]) + " - "
            + str(self.score["goalBlue"]) + "   Azul"
        )
        taskMgr.add( self.timer(), 'Timer' ) # or the next

The game have a menu, and when the “Start Game” button is pressed, the Timer should appear counting from 0 to 90 minutos…but in real time counting just 1 or 2 minutes…

If you can help me i appreciate…

Thanks,
André.

def timer(task):

In your code, this is a method of the Screen class, so its signature should be something like:

def timer( self, task ):

And you shouldn’t pass the result of an invocation to the add() method:

taskMgr.add( self.timer(), 'Timer' )

but the reference of the method:

taskMgr.add( self.timer, 'Timer' )

very thanks…

But now, i just dont know how i show a timer from 0 to 90 and in real time 1 minute…
And the timer work, but appear on other window of Panda…it opens other window and the timer it on him…
And the timer should appear on the window of the Game, not on a grey window… :confused:

And how i stop the timer on 90?

Thanks,
André.

i got working the Timer…

With…

startTime = globalClock.getFrameTime()

self.myText=OnscreenText('00:00',scale=0.07,pos=(0.5,0.5))

def updateTime(self):
        nowTime = globalClock.getFrameTime() - startTime
        self.myText.setText(str (nowTime))

But now i have a problem…
The game start’s when i click on Button “Start Game”…but when i click on start the time its Global…dont start of Zero…

If anyone can help…thanks…
André
[/quote]

Maybe you are saving the value startTime at the wrong moment; try saving it when the match actually starts.