How to know if the spacebar is pressed for more than 2 seconds in pygame

Viewed 179

So, I am making a simple game using pygame in which a ninja has to dodge obstacles in front of him, the problem is that I can't get how to know if the spacebar(used for jump) is pressed for more than 2 seconds.

I have searched on many sites for this answer but couldn't get one.

Pls help.

Here is my code:

import pygame,time #import


pygame.init() #pygame initialization

screen = pygame.display.set_mode((0,0))
pygame.quit()

canvas = pygame.display.set_mode((1000,780)) #creating screen
caption = pygame.display.set_caption("Ninja Run") #creating caption

iconImage = pygame.image.load("C:/Garv/python/pygame/Ninja Game/Ninja_logo.png")
icon = pygame.display.set_icon(iconImage) #drawing icon image

land = pygame.image.load("C:/Garv/python/pygame/Ninja Game/land.jfif") #land image
sky = pygame.image.load("C:/Garv/python/pygame/Ninja Game/sky.jfif") # sky image

x = 50
y = 594

start = time.time()
ninjaRun = [pygame.image.load("C:/Garv/python/pygame/Ninja Game/ninja running 1.jpg"),pygame.image.load("C:/Garv/python/pygame/Ninja Game/ninja running 2.jpg")] #ninja images
ninjaImage = ninjaRun[0]

# ninjaStars = pygame.image.load("C:/Garv/python/pygame/Ninja Game/ninja stars.png")

class Ninja:
    def __init__(self,canvas,start,run,image,x,y): #Initializing of variables
        self.canvas = canvas
        self.start = start
        self.run = run
        self.image = image
        self.x = x
        self.y = y
    def ninja(self): #ninja movement method
        self.image = ninjaRun[0]
        self.time = 15
        self.sec = int((time.time()-self.start)*self.time)
        if self.sec % 2 != 0:
            self.image = ninjaRun[0]
        elif self.sec % 2 == 0:
            self.image = ninjaRun[1]
            
        self.canvas.blit(self.image, (self.x,self.y)) #ninja draw
        
        
player = Ninja(canvas,start,ninjaRun,ninjaRun[0],x,y) #calling Ninja class

cnt = 0

startTime = time.time()
        
while True: #main loop
    for event in pygame.event.get(): #event handler
        if event.type == pygame.QUIT: #quit handler
            pygame.quit()
            import sys
            sys.exit()
        
        if event.type == pygame.KEYDOWN: #keydown
            if event.key == pygame.K_SPACE: 
                player.y -= 200
                if player.y < 394:
                    player.y += 200
        if event.type == pygame.KEYUP: #keyup
            if event.key == pygame.K_SPACE:
                player.y += 200
                
                
    # canvas.blit(ninjaStars, (500,500))
    pygame.draw.rect(canvas,(250,250,250),(500,500,20,20))
    canvas.blit(land, (0,718)) #land drawing
    canvas.blit(sky, (0,0)) #sky drawing
    player.ninja() #ninja drawing
    
    pygame.display.update() #display update
    
    
1 Answers

Use pygame.time.get_ticks() to get the number of milliseconds since pygame.init() was called:

while True: #main loop
    for event in pygame.event.get(): #event handler
        if event.type == pygame.QUIT: #quit handler
            # [...]
        
        if event.type == pygame.KEYDOWN: #keydown
            if event.key == pygame.K_SPACE: 
                space_start = pygame.time.get_ticks() 
                # [...]
                
        if event.type == pygame.KEYUP: #keyup
            if event.key == pygame.K_SPACE:
                space_end = pygame.time.get_ticks() 
                if space_end - space_start >= 2000:
                    # [...]
Related