0
import pygame
pygame.init()

win = pygame.display.set_mode((1280,800))
pygame.display.set_caption("First Game")

walkLeft = [pygame.image.load('/Users/arnav/Downloads/Jokerattack1.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack2.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack3.png')]
walkRight = [pygame.image.load('/Users/arnav/Downloads/Jokerattack1Right.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack2Right.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack3Right.png')]
Joker1 = pygame.image.load('/Users/arnav/Downloads/Joker.png')
char = pygame.transform.scale(Joker1, (80, 132)) 
bg1 = pygame.image.load('/Users/arnav/Downloads/GothamCity.jpg')
bg = pygame.transform.scale(bg1, (1280, 800))
x = 0
y = 400
width = 40
height = 60
vel = 5

clock = pygame.time.Clock()

isJump = False
jumpCount = 10

left = False
right = False
walkCount = 0

def redrawGameWindow():
    global walkCount

    win.blit(bg, (0,0))  
    if walkCount + 1 >= 9:
        walkCount = 0
    
    if left:  
        win.blit(walkLeft[walkCount//3], (x,y))
        walkCount += 1                          
    elif right:
        win.blit(walkRight[walkCount//3], (x,y))
        walkCount += 1
    else:
        win.blit(char, (x, y))
        walkCount = 0
    
    pygame.display.update() 



run = True

while run:
    clock.tick(12)

    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

    elif keys[pygame.K_RIGHT] or keys[ord('d')] and x < 900 - vel - width:  
        x += vel
        left = False
        right = True
    
    else: 
        left = False
        right = False
        walkCount = 0
    
    if not(isJump):
        if keys[pygame.K_UP] or keys[ord('w')]:
            isJump = True
            left = False
            right = False
            walkCount = 0
    else:
        if jumpCount >= -10:
            y -= (jumpCount * abs(jumpCount)) * 0.5
            jumpCount -= 1
        else: 
            jumpCount = 10
            isJump = False
        
        
def redrawGameWindow2():
    global walkCount

    if keys[pygame.K_SPACE]:
        walkLeft = [pygame.image.load('/Users/arnav/Downloads/Jokerattack1.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack2.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack3.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack4.png')]
        walkRight = 
[pygame.image.load('/Users/arnav/Downloads/Jokerattack1Right.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack2Right.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack3Right.png'), 
pygame.image.load('/Users/arnav/Downloads/Jokerattack3Right.png')]

    win.blit(bg, (0,0))  
    if walkCount + 1 >= 12:
        walkCount = 0
    
    if left:  
        win.blit(walkLeft[walkCount//3], (x,y))
        walkCount += 1                          
    elif right:
        win.blit(walkRight[walkCount//3], (x,y))
        walkCount += 1
    else:
        win.blit(char, (x, y))
        walkCount = 0
    
    pygame.display.update()             
        

    redrawGameWindow2() 




pygame.quit()

In this game I have images loaded to animate. When arrow keys are used, it walks normally. But when I press space I want it to walk and then attack. I want to add an attack image to the animation so I thought I could make a function and make it so if you press space you attack. When answering, could you please explain what you are doing as I am new to coding python and trying to learn as I go along.

0 Answers0