Код: Выделить всё
Traceback (most recent call last):
File "C:\Users\NotAddictionIfWarfrm\TOTAL CODE FILE\First_Game_Test\app.py", line 53, in
class Player(pygame.sprite.Sprite):
File "C:\Users\NotAddictionIfWarfrm\TOTAL CODE FILE\First_Game_Test\app.py", line 56, in Player
SPRITES = load_sprite_sheets("Character", 32, 32, True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\NotAddictionIfWarfrm\TOTAL CODE FILE\First_Game_Test\app.py", line 29, in load_sprite_sheets
path = join("assests", dir1, dir2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 149, in join
File "", line 164, in _check_arg_types
TypeError: join() argument must be str, bytes, or os.PathLike object, not 'int'
Код:
Код: Выделить всё
import os
from typing import Dict, List
import pygame.transform
import pygame
import random
import math
from os import listdir
from os.path import isfile, join
from pygame import Surface, SurfaceType
from pygame.examples.sprite_texture import sprite
pygame.init()
BG_COLOR = (255, 255, 255)
WIDTH, HEIGHT = 1600, 900
FPS = 60
PLAYER_VEL = 5
window = pygame.display.set_mode((WIDTH, HEIGHT))
def flip(sprites):
return [pygame.transform.flip(sprite, True, False) for sprite in sprites]
def load_sprite_sheets(dir1, dir2, width, height, direction=False):
path = join("assests", dir1, dir2)
images = [f for f in listdir(path) if isfile(join(path, f))]
all_sprites: dict[str, list[Surface | SurfaceType]] = {}
for image in images:
sprite_sheet = pygame.image.load(join(path, image)).convert_alpha()
sprites = []
for i in range(sprite_sheet.get_width() // width):
surface = pygame.Surface((width, height), pygame.SRCALPHA, 32)
rect = pygame.Rect(i * width, 0, width, height)
surface.blit(sprite_sheet, (0, 0), rect)
sprites.append(pygame.transform.scale2x(surface))
if direction:
all_sprites[image.replace(".png", "") + "_right"] = sprites
all_sprites[image.replace(".png", "") + "_left"] = flip(sprites)
else:
all_sprites[image.replace(".png", "")] = sprites
return all_sprites
class Player(pygame.sprite.Sprite):
COLOR = (255, 0, 0)
GRAVITY = 1
SPRITES = load_sprite_sheets("Character", 32, 32, True)
def __init__(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height)
self.x_vel = 0
self.y_vel = 0
self.mask = None
self.direction = "left"
self.animation_count = 0
self.fall_count = 0
def move(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
def move_left(self, vel):
self.x_vel = -vel
if self.direction != "left":
self.direction = "left"
self.animation_count = 0
def move_right(self, vel):
self.x_vel = vel
if self.direction != "right":
self.direction = "right"
self.animation_count = 0
def loop(self, fps):
#self.y_vel += min(1, (self.fall_count / fps) * self.GRAVITY)
self.move(self.x_vel, self.y_vel)
self.fall_count += 1
def draw(self, win, ):
self.sprite = self.SPRITES["idle_" + self.direction][0]
win.blit(self.sprite, (self.rect.x, self.rect.y))
def get_background_image(name):
image = pygame.image.load("assets/Background_2.png")
_, _, width, height = image.get_rect()
tiles = []
for i in range(WIDTH // width + 1):
for j in range(HEIGHT // height + 1):
pos = (i * width, j * height)
tiles.append(pos)
return tiles, image
def draw(window, background, bg_image, player):
for tile in background:
window.blit(bg_image, tile)
player.draw(window)
pygame.display.update()
def handle_move(player):
key = pygame.key.get_pressed()
player.x_vel = 0
if key[pygame.K_LEFT]:
player.move_left(PLAYER_VEL)
if key[pygame.K_RIGHT]:
player.move_right(PLAYER_VEL)
def main(window):
clock = pygame.time.Clock()
background, bg_image = get_background_image("background_2.png")
player = Player(100, 100, 50, 50)
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
player.loop(FPS)
handle_move(player)
draw(window, background, bg_image, player)
pygame.quit()
quit()
if __name__ == "__main__":
main(window)
Я опубликую zip-файл с ресурсами в комментариях
Я опубликую zip-архив с ресурсами в комментариях
p>
TLDR: код запускается неправильно, спрайты не загружаются, и я просто получаю ошибки
Подробнее здесь: https://stackoverflow.com/questions/790 ... et-loading