Код: Выделить всё
import pygame
import glob
import os
# Initialize Pygame
pygame.init()
# Set the screen dimensions
screen_width = 1920
screen_height = 1080
screen = pygame.display.set_mode((screen_width, screen_height))
# Define a function to load frames from a folder
def load_frames(folder):
frame_paths = glob.glob(os.path.join(folder, '*.png'))
frames = [pygame.image.load(frame_path) for frame_path in sorted(frame_paths)]
return frames
# Define a function to initialize animations
def initialize_animations():
animations = {
"idle": (load_frames('C:\\Users\\t\\Desktop\\a\\idle-t'), False),
"kick": (load_frames('C:\\Users\\t\\Desktop\\a\\kick-t'), False),
"uppercut": (load_frames('C:\\Users\\t\\Desktop\\a\\uppercut-t'), False),
"jumpkick": (load_frames('C:\\Users\\t\\Desktop\\a\\jumpkick-t'), False),
"2footkick": (load_frames('C:\\Users\\t\\Desktop\\a\\2footkick-t'), False),
"smoke2foot": (load_frames('C:\\Users\\t\\Desktop\\a\\smoke-t'), False)
}
return animations
# Set the frame rate
clock = pygame.time.Clock()
fps = 24
# Main loop
running = True
current_animation = "idle"
frame_index = 0
animations = initialize_animations()
animation_playing = False # Flag to track if an animation is currently playing
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif not animation_playing: # Process key events only if no animation is playing
if event.type == pygame.MOUSEBUTTONDOWN:
# Reset all animation flags to False
for anim in animations:
animations[anim] = (animations[anim][0], False)
if event.button == 1: # Left mouse button
current_animation = "kick"
elif event.button == 3: # Right mouse button
current_animation = "uppercut"
elif event.button == 2: # Middle mouse button
current_animation = "jumpkick"
elif event.button == 4: # Scroll up
current_animation = "2footkick"
frame_index = 0
# Set the flag of the current animation to True
animations[current_animation] = (animations[current_animation][0], True)
animation_playing = True # Set the flag to indicate an animation is playing
# Fill the screen with a background color
screen.fill((225, 255, 255))
# Display the current frame
screen.blit(animations[current_animation][0][frame_index], (0, 0))
pygame.display.update()
# Increment the frame counter
frame_index = (frame_index + 1) % len(animations[current_animation][0])
# Check if animation has finished
if frame_index == len(animations[current_animation][0]) - 1:
# Reset the flag of the current animation to False
animations[current_animation] = (animations[current_animation][0], False)
# Switch to idle animation
current_animation = "idle"
animation_playing = False # Reset the flag since animation is finished
# Delay to achieve the desired frame rate
clock.tick(fps)
# Quit Pygame
pygame.quit()
Подробнее здесь: https://stackoverflow.com/questions/783 ... nimation-i