Pygame: Timer For Time Alive
Question: Hello I am currently making a game in python and I am trying to make a timer
which I have never attempted before, hence asking this question. What I really
need to know is how to loop this small area where it says #Timer. Any help
will be appreciated, thank you.

    
    
    import pygame, time
    import pygame.mixer
    from bullet import Bullet
    from constants import DIR_LEFT, DIR_RIGHT
    
    # Player
    class Player(pygame.sprite.Sprite):
    
        def __init__(self, x, y, gravity):
            pygame.mixer.init()
    
            # Player dimensions and position
    
            # Player image and animation
            self.images = []
            self.images.append(pygame.image.load('images/Sprites/player.png'))
            self.images.append(pygame.image.load('images/Sprites/player2.png'))
            #~ self.images.append(pygame.image.load('ball1.png'))
            #~ self.images.append(pygame.image.load('ball2.png'))
            self.maxImage = len(self.images)
            self.currentImage = 0
    
            self.jumpSound = pygame.mixer.Sound('sounds/jump.ogg')
            self.shootSound = pygame.mixer.Sound('sounds/laser.ogg')
    
            #~ self.rect = pygame.Rect(x, y, 80, 80)
            self.rect = self.images[0].get_rect()
            self.rect.x = x
            self.rect.y = y
    
            self.timeTarget = 10
            self.timeNum = 0
    
            self.velX = 0
            self.velY = 0
    
            self.health = 200
            self.score = 0
            self.alivetime = 0
            self.second = 1000
    
            self.direction = DIR_RIGHT
    
            # Jump and gravity
            self.jumping = False
            self.on_ground = False
            self.origJumpVel = 15
            self.jumpVel = self.origJumpVel
            self.gravity = 0.5
    
        # Jump inputs
        def do_jump(self):
            if self.jumping and not self.on_ground:
                self.velY = -self.jumpVel
                self.jumpVel -= self.gravity
    
            if self.on_ground:
                self.jumping = False
                self.jumpVel = self.origJumpVel
                self.velY = 0
                self.on_ground = True
    
        def handle_events(self, event):
    
            if event.type == pygame.KEYDOWN:
    
                if event.key == pygame.K_SPACE:
                    self.jumpSound.play(0)
                    if not self.jumping:
                        self.jumping = True
                        self.on_ground = False
    
                if event.key == pygame.K_s:
                    self.shootSound.play(0)
    
                elif event.key == pygame.K_a:
                    #pygame.transform.flip(self.images[self.currentImage], False, False)
                    self.velX = -5
    
                elif event.key == pygame.K_d:
                    #pygame.transform.flip(self.images[self.currentImage], True, False)
                    self.velX = +5
    
            elif event.type == pygame.KEYUP:
                if event.key in (pygame.K_a, pygame.K_d):
                    self.velX = 0
    
            elif event.type == pygame.KEYUP:
                if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                    bullet.velX = 0
    
        # PLayer updates
        def update(self):
    
            #self.timeNum += 1
            # Animations
            #if self.direction == DIR_LEFT:
                #if self.timeNum == self.timeTarget:
                        #self.currentImage += 1
                    #if self.currentImage >= self.maxImage:
                        #self.currentImage = 0
                    #self.timeNum = 0
    
            # Timer
            if pygame.time.get_ticks() >= self.second:
                self.alivetime = +1
                pygame.time.get_ticks() == 0
    
            # Screen wrap
            if self.rect.right > 1280:
                self.rect.left = 0
    
            elif self.rect.left < 0:
                self.rect.right = 1280
    
            if self.velX < 0 and self.direction != DIR_RIGHT: # Moving right
                self.direction = DIR_RIGHT
                self.images[self.currentImage] = pygame.transform.flip(self.images[self.currentImage], True, False)
    
            elif self.velX > 0 and self.direction != DIR_LEFT: # Moving left
                self.direction = DIR_LEFT
                self.images[self.currentImage] = pygame.transform.flip(self.images[self.currentImage], True, False)
    
        # Player rendering
        def render(self, surface):
            surface.blit(self.images[self.currentImage], self.rect)
    


Answer: Run `player.update()` in mainloop in every loop and Timer will loop too.

**By The Way:**

if you try to set tick to zero in this

    
    
    pygame.time.get_ticks() == 0
    

than you are wrong. You can't change number of ticks.

Use this

    
    
    # __init__()
    
        self.time_to_change_alivetime = pygame.time.get_ticks() + self.second
    
    # update()
    
        if pygame.time.get_ticks() >= self.time_to_change_alivetime:
            self.alivetime += 1
            self.time_to_change_alivetime += self.second
    

or this (without `if`)

    
    
    # __init__()
    
        self.start_alivetime = pygame.time.get_ticks()
    
    # update()
    
        self.alivetime = (pygame.time.get_ticks() - self.start_alivetime) / self.second
    

or more precisely (1 minute = 60 seconds = 60 000 milliseconds)

    
    
    # __init__()
    
        self.start_alivetime = pygame.time.get_ticks()
    
    # update()
    
        milliseconds = pygame.time.get_ticks() - self.start_alivetime
    
        self.alivetime_minutes = milliseconds / 60000 
        self.alivetime_seconds = (milliseconds % 60000) / self.second
        self.alivetime_milliseconds = milliseconds % self.second
    

