- Joined
- Dec 4, 2023
- Messages
- 1
- Reaction score
- 0
Hi, I'm relatively new to python and I am using pygame through IDLE (in case that matters idrk) to make a smash-bros/streetfighter style game.
I am in the early stages of it's development and I'm trying to make a jumping animation, however no matter what I try the animation will not work and it is driving me INSANE!
Any help would be greatly appreciated, maybe some pointers as to what other things I could refine?
I am in the early stages of it's development and I'm trying to make a jumping animation, however no matter what I try the animation will not work and it is driving me INSANE!
Any help would be greatly appreciated, maybe some pointers as to what other things I could refine?
Code:
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
fps = 60
screen_width = 1000
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Baet_Prototype")
tile_size = 100
bg = pygame.image.load("imgs/bg.png")
bg_img = pygame.transform.scale(bg, (screen_width, screen_height))
class Player1():
def __init__(self, x, y):
self.player1_walk_images_r = []
self.player1_walk_images_l = []
self.player1_idle_images_r = []
self.player1_idle_images_l = []
self.player1_jump_images_r = []
self.player1_jump_images_l = []
self.index = 0
self.walk_ani_counter = 0
self.idle_ani_counter = 0
self.jump_ani_counter = 0
self.jump_counter = 0
for num in range(1, 4):
player1_jump_image_right = pygame.image.load(f"imgs/Player_1/Jump_Ani{num}.png")
player1_jump_image_right = pygame.transform.scale(player1_jump_image_right, (86, 128))
player1_jump_image_left = pygame.transform.flip(player1_jump_image_right, True, False)
self.player1_jump_images_r.append(player1_jump_image_right)
self.player1_jump_images_l.append(player1_jump_image_left)
for num in range(1, 5):
player1_idle_image_right = pygame.image.load(f"imgs/Player_1/Idle_Ani{num}.png")
player1_idle_image_right = pygame.transform.scale(player1_idle_image_right, (86, 128))
player1_idle_image_left = pygame.transform.flip(player1_idle_image_right, True, False)
self.player1_idle_images_r.append(player1_idle_image_right)
self.player1_idle_images_l.append(player1_idle_image_left)
for num in range(1, 9):
player1_walk_image_right = pygame.image.load(f"imgs/Player_1/Walk_Ani{num}.png")
player1_walk_image_right = pygame.transform.scale(player1_walk_image_right, (86, 128))
player1_walk_image_left = pygame.transform.flip(player1_walk_image_right, True, False)
self.player1_walk_images_r.append(player1_walk_image_right)
self.player1_walk_images_l.append(player1_walk_image_left)
self.image = self.player1_idle_images_r[self.index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.vel_y = 0
self.jumped = False
self.direction = 0
def update(self):
dx = 0
dy = 0
cooldown = 5
key = pygame.key.get_pressed()
if key[pygame.K_w] == True and self.jumped == False and self.jump_counter < 1:
self.vel_y = -15
self.jumped = True
self.jump_counter += 1
self.jump_ani_counter += 1
if self.jump_ani_counter > cooldown:
self.jump_ani_counter = 0
self.index += 1
if self.index >= len(self.player1_jump_images_r):
self.index = 0
if self.direction == 1:
self.image = self.player1_jump_images_r[self.index]
if self.direction == -1:
self.image = self.player1_jump_images_l[self.index]
if key[pygame.K_w] == False:
self.jumped = False
if key[pygame.K_w] == True and self.jumped == False and self.jump_counter > 1:
self.vel_y = 0
if key[pygame.K_a] == True and key[pygame.K_d] == False:
dx -= 5
self.walk_ani_counter += 1
self.direction = -1
if key[pygame.K_a] == False and key[pygame.K_d] == True:
dx += 5
self.walk_ani_counter += 1
self.direction = 1
if key[pygame.K_a] == False and key[pygame.K_d] == False:
self.idle_ani_counter += 1
if self.idle_ani_counter > cooldown:
self.idle_ani_counter = 0
self.index += 1
if self.index >= len(self.player1_idle_images_r):
self.index = 0
if self.direction == 1:
self.image = self.player1_idle_images_r[self.index]
if self.direction == -1:
self.image = self.player1_idle_images_l[self.index]
if key[pygame.K_a] == True and key[pygame.K_d] == True:
self.idle_ani_counter += 1
if self.idle_ani_counter > cooldown:
self.idle_ani_counter = 0
self.index += 1
if self.index >= len(self.player1_idle_images_r):
self.index = 0
if self.direction == 1:
self.image = self.player1_idle_images_r[self.index]
if self.direction == -1:
self.image = self.player1_idle_images_l[self.index]
if self.walk_ani_counter > cooldown:
self.walk_ani_counter = 0
self.index += 1
if self.index >= len(self.player1_walk_images_r):
self.index = 0
if self.direction == 1:
self.image = self.player1_walk_images_r[self.index]
if self.direction == -1:
self.image = self.player1_walk_images_l[self.index]
self.vel_y += 1
if self.vel_y > 10:
self.vel_y = 10
dy += self.vel_y
for tile in world.tile_list:
if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
self.jump_counter = 0
if self.vel_y < 0:
dy = tile[1].bottom - self.rect.top
if self.vel_y >= 0:
dy = tile[1].top - self.rect.bottom
self.rect.x += dx
self.rect.y += dy
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
dy = 0
screen.blit(self.image, self.rect)
class World():
def __init__(self, data):
self.tile_list = []
wall_img = pygame.image.load("imgs/wall.png")
floor_img = pygame.image.load("imgs/floor.png")
row_count = 0
for row in data:
col_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(wall_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 2:
img = pygame.transform.scale(floor_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
col_count += 1
row_count += 1
def draw(self):
for tile in self.tile_list:
screen.blit (tile[0], tile[1])
world_data = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
player1 = Player1(100, screen_height - 428)
world = World(world_data)
run = True
while run == True:
clock.tick(fps)
screen.blit(bg_img, (0, 0))
world.draw()
player1.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()