**已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
这个问题发生在我的支持文件和我的播放器文件中的import_assets函数中。我确保我的文件夹与他的文件夹名称完全相同,唯一的区别是我的实际图像名称,我不需要使图像名称与他的完全相同,只是文件夹。我关注的yt视频-https://www.youtube.com/watch?v=T4IX36sP_0c&t=2436s- 40:38是他运行代码的地方,也是我出错的地方
主要的
import pygame
import sys
from level import Level
from settings import *
class Game:
def __init__(self):
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('sprout valley')
self.clock = pygame.time.Clock()
self.level = Level()
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
dt = self.clock.tick()/1000
self.level.run(dt)
pygame.display.update()
if __name__ == '__main__':
game = Game()
game.run()
水准仪
import pygame
from settings import *
from player import Player
class Level:
def __init__(self):
#get the display surface
self.display_surface = pygame.display.get_surface()
#sprite groups
self.all_sprites = pygame.sprite.Group()
self.setup()
def setup(self):
self.player = Player((640,360), self.all_sprites)
def run(self, dt):
self.display_surface.fill('black')
self.all_sprites.draw(self.display_surface)
self.all_sprites.update(dt)
播放器
import pygame
from settings import *
from support import *
class Player(pygame.sprite.Sprite):
def __init__(self, pos, group):
super().__init__(group)
self.import_assets()
#general setup
self.image = pygame.Surface((32,64))
self.image.fill('green')
self.rect = self.image.get_rect(center = pos)
#movement attributes
self.direction = pygame.math.Vector2()
self.pos = pygame.math.Vector2(self.rect.center)
self.speed = 200
def import_assets(self):
self.animations = {'up': [], 'down': [],'left': [],'right': [],
'right_idle':[],'left_idle':[],'up_idle':[],'down_idle':[],
'right_hoe': [], 'left_hoe': [], 'up_hoe': [], 'down_hoe': [],
'right_axe': [], 'left_axe': [], 'up_axe': [], 'down_axe': [],
'right_water': [], 'left_water': [], 'up_water': [], 'down_water': []}
for animation in self.animations.keys():
full_path = '../graphics/character/' + animation
self.animations[animation] = import_folder(full_path)
print(self.animations)
def input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.direction.y = -1
elif keys[pygame.K_DOWN]:
self.direction.y = 1
else:
self.direction.y = 0
if keys[pygame.K_RIGHT]:
self.direction.x = 1
elif keys[pygame.K_LEFT]:
self.direction.x = -1
else:
self.direction.x = 0
def move(self, dt):
#normalizing a vector - make the direction a fixed number for any direction
if self.direction.magnitude() > 0:
self.direction = self.direction.normalize()
#horizontal movement
self.pos.x += self.direction.x * self.speed * dt
self.rect.centerx = self.pos.x
#vertical movement
self.pos.y += self.direction.y * self.speed * dt
self.rect.centery = self.pos.y
def update(self, dt):
self.input()
self.move(dt)
支持
from os import walk
import pygame
def import_folder(path):
surface_list = []
for _, __, img_files in walk(path):
for image in img_files:
full_path = path + '/' + image
image_surf = pygame.image.load(full_path).convert_alpha()
surface_list.append(image_surf)
return surface_list
1条答案
按热度按时间gojuced71#
尝试将文件限制为以
.png
结尾的文件你可以通过添加一个
if
语句来做到这一点。@Kingsley在评论中的另外两个有价值的观点:.lower()
,以便捕获“.PNG”、“.Png”和其他类似变体os.path.join
,以防有一天代码在一个系统上运行时,段之间不只是“/”。例如,在Windows中,它是反斜杠字符。