0
import pygame
import time
pygame.init()

win = pygame.display.set_mode((1280,800))
pygame.display.set_caption("Last One Standing")

appwallpaper = pygame.image.load('/Users/arnav/Downloads/AppWallpaper.jpeg') #setting     the apps wallpaper storing it in a variable

pygame.display.set_icon(appwallpaper) #setting the apps wallpaper as the poster

batmanleft01 = pygame.image.load('/Users/arnav/Downloads/Batmanleft1.png')
batmanleft02 = pygame.image.load('/Users/arnav/Downloads/Batmanleft2.png')
batmanleft03 = pygame.image.load('/Users/arnav/Downloads/Batmanleft3.png')

batmanleft1 = pygame.transform.scale(batmanleft01, (340, 308))
batmanleft2 = pygame.transform.scale(batmanleft02, (340, 308))
batmanleft3 = pygame.transform.scale(batmanleft03, (340, 308))

walkLeft3 = [batmanleft1, batmanleft2, batmanleft3]

batmanright01 = pygame.image.load('/Users/arnav/Downloads/Batmanright1.png')
batmanright02 = pygame.image.load('/Users/arnav/Downloads/Batmanright2.png')
batmanright03 = pygame.image.load('/Users/arnav/Downloads/Batmanright3.png')

batmanright1 = pygame.transform.scale(batmanright01, (152, 205))
batmanright2 = pygame.transform.scale(batmanright02, (149, 217))
batmanright3 = pygame.transform.scale(batmanright03, (149, 205))

walkRight3 = [batmanright1, batmanright2, batmanright3]

batmanleft04 = pygame.image.load('/Users/arnav/Downloads/Batmanattack2Left.png')
batmanleft05 = pygame.image.load('/Users/arnav/Downloads/Batmanattack3Left.png')
batmanleft06 = pygame.image.load('/Users/arnav/Downloads/Batmanattack4Left.png')

batmanleft4 = pygame.transform.scale(batmanleft04, (178, 226))
batmanleft5 = pygame.transform.scale(batmanleft05, (178, 226))
batmanleft6 = pygame.transform.scale(batmanleft06, (178, 226))

walkLeft4 = [batmanleft4, batmanleft5, batmanleft6]

batmanright04 = pygame.image.load('/Users/arnav/Downloads/Batmanattack2.png')
batmanright05 = pygame.image.load('/Users/arnav/Downloads/Batmanattack3.png')
batmanright06 = pygame.image.load('/Users/arnav/Downloads/Batmanattack2.png')

batmanright4 = pygame.transform.scale(batmanright04, (205, 205))
batmanright5 = pygame.transform.scale(batmanright05, (266, 205))
batmanright6 = pygame.transform.scale(batmanright06, (205, 205))

walkRight4 = [batmanright4, batmanright5, batmanright6] 

char02 =  pygame.image.load('/Users/arnav/Downloads/Batman.png')
char2 = pygame.transform.scale(char02, (178, 226))

bg1 = pygame.image.load('/Users/arnav/Downloads/fighterbackground.png')
bg = pygame.transform.scale(bg1, (1280, 800))
x = 50
y = 450
width = 40
height = 60
vel = 5

clock = pygame.time.Clock()

isJump = False
jumpCount = 10

left = False
right = False
attackl = False
attackr = False
walkCount = 0

currentWalk = walkLeft3

def redrawGameWindow():
    global walkCount

    win.blit(bg, (0,0))
    
    if walkCount + 1 >= 12:
        walkCount = 0
        
    if left:  
        currentWalk = walkLeft3
        win.blit(currentWalk[walkCount//4], (x,y))
        walkCount += 1   
    elif right:
        currentWalk = walkRight3
        win.blit(currentWalk[walkCount//4], (x,y))
        walkCount += 1
    elif attackl:              
        currentWalk = walkLeft4
        win.blit(currentWalk[walkCount//4], (x,y))
        walkCount += 1
        
    elif attackr:              
        currentWalk = walkRight4
        win.blit(currentWalk[walkCount//4], (x,y))
        walkCount += 1    

    else:
        win.blit(char2, (x, y))
        walkCount = 0
    
    pygame.display.update() 



run = True

while run:
    clock.tick(27)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] or keys[ord('a')] and x > vel: 
        x -= vel
        left = True
        right = False
        attackl = False
        attackr = False
        currentWalk = walkLeft3

    elif keys[pygame.K_RIGHT] or keys[ord('d')] and x < 1000 - vel - width:  
        x += vel
        left = False
        right = True
        attackl = False
        attackr = False
        currentwalk = walkRight3
    
    elif keys[ord('n')]:  
        left = False
        right = False 
        attackl = True
        attackr = False
        currentWalk = walkLeft4    
    
    elif keys[ord('z')]:   
        left = False
        right = False 
        attackl = True
        attackr = False
        currentWalk = walkLeft4   
    
    elif keys[ord('m')]:   
        left = False
        right = False 
        attackl = False
        attackr = True
        currentWalk = walkRight4    
    
    elif keys[ord('x')]:   
        left = False
        right = False 
        attackl = False
        attackr = True
        currentWalk = walkRight4       
    
    else: 
        left = False
        right = False
        attackl = False
        walkCount = 0
    
    if not(isJump):
        if keys[pygame.K_UP] or keys[ord('w')]:
            isJump = True
            left = False
            right = False
            attackl = False
            walkCount = 0
    else:
        if jumpCount >= -10:
            y -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else: 
            jumpCount = 10
            isJump = False

        
    if x <= 0:
        x = 0
    elif x >= 1100:
        x = 1100

    

    redrawGameWindow()


pygame.quit()

I am trying to add a cooldown for when you attack (press z and press x) so you cant just spam attacks, I know you can use the time.sleep function to break up actions but because its animated, whenever I try to break up the actions, it has a 1 second delay between each image, how do I fix this? (when answering could you please explain what you did as I am new to coding and want to learn)

  • 1
    `time.sleep()` blocks the execution of code at that point so nothing else can run. Instead you want to store the value of `time.time()` and compare it when they next press attack to make sure enough time has passed, eg `if time.time() - last_hit_time > cooldown_time` – Peter Gibson Feb 04 '22 at 03:05
  • @PeterGibson, How would I be able to add this into my code? –  Feb 06 '22 at 06:59

0 Answers0