【问题标题】:pygame images not loading?pygame图像未加载?
【发布时间】:2020-03-02 18:40:33
【问题描述】:

为什么我的悟空精灵没有加载我的左右图像看起来正确但我的其他人没有在运动中播放?我试图完成的事情是让我的角色图像在运动中正常工作 下面是带有完整代码描述链接的代码:

https://pastebin.com/umMJHNQj

import pygame

pygame.init()#We always need to initialize our pygame IN EVERY PROJECT/FILE

win = pygame.display.set_mode((500, 480))# Here win is representing "window" for our screen which we have set at 500 by 480

pygame.display.set_caption("First Game")#We are giving our Window/Screen a name

walkRight = [pygame.image.load('image/gokuR0.png'), pygame.image.load('image/gokutest2.png'), pygame.image.load('image/gokuR2.png')]
walkLeft = [pygame.image.load('image/gokuL0.png'), pygame.image.load('image/gokutest.png'), pygame.image.load('image/gokuL2.png')]
bg = pygame.image.load('image/bg.jpg')
char = pygame.image.load('image/goku sprite - standing.png')

clock = pygame.time.Clock()

class player():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.isJump = False
        self.left = False
        self.right = False
        self.walkCount = 0
        self.jumpCount = 10
        self.standing = False

这是我尝试更改代码但没有结果的地方。

def draw(self, win):
    if self.walkCount + 1 >= 8:
        self.walkCount = 0

    if not self.standing:
        if self.left:
            win.blit(walkLeft[self.walkCount // 100], (self.x, self.y))
            self.walkCount += 1
        elif self.right:
            win.blit(walkRight[self.walkCount // 100], (self.x, self.y))
            self.walkCount += 1
    else:
        if self.right:
            win.blit(walkRight[0], (self.x, self.y))
        else:
            win.blit(walkLeft[0], (self.x, self.y))
    pygame.display.flip()
    print(walkLeft)
    print(walkRight)
class projectile():
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self, win):
        pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)

所有这些图像都正常工作,但似乎无法让我的主要角色图像在运动中正常工作

class enemy():
    walkRight = [pygame.image.load("image/R1E.png"), pygame.image.load("image/R2E.png"), pygame.image.load("image/R3E.png"), pygame.image.load("image/R4E.png"), pygame.image.load("image/R5E.png"), pygame.image.load("image/R6E.png"), pygame.image.load("image/R7E.png"), pygame.image.load("image/R8E.png"), pygame.image.load("image/R9E.png"), pygame.image.load("image/R10E.png"), pygame.image.load("image/R11E.png")]
    walkLeft = [pygame.image.load("image/L1E.png"), pygame.image.load("image/L2E.png"), pygame.image.load("image/L3E.png"), pygame.image.load("image/L4E.png"), pygame.image.load("image/L5E.png"), pygame.image.load("image/L6E.png"), pygame.image.load("image/L7E.png"), pygame.image.load("image/L8E.png"), pygame.image.load("image/L9E.png"), pygame.image.load("image/L10E.png"), pygame.image.load("image/L11E.png")]
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.x, self.end]
        self.walkcount = 0
        self.vel = 3

    def draw(self,win):
        self.move()
        if self.walkcount + 1 >= 33:
            self.walkcount = 0

        if self.vel > 0:
            win.blit(self.walkRight[self.walkcount // 3], (self.x, self.y))
            self.walkcount += 1
        else:
            win.blit(self.walkLeft[self.walkcount // 3], (self.x, self.y))
            self.walkcount += 1
    print(walkLeft)



    def move(self):
        if self.vel > 0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkcount = 0

        else:
            if self.x - self.vel > self.path[0]:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.walkcount = 0



def redrawGameWindow():
    win.blit(bg, (0, 0))
    goblin.draw(win)
    man.draw(win)
    for bullet in bullets:
        bullet.draw(win)

    pygame.display.update()


# mainloop
man = player(200, 400, 85, 85)
goblin = enemy(100, 400, 64, 64, 450)
bullets = []
run = True
while run:
    clock.tick(27)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    for bullet in bullets:
        if bullet.x < 500 and bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        if man.left:
            facing = -1
        else:
            facing = 1

        if len(bullets) < 5:
            bullets.append(
                projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))

    if keys[pygame.K_LEFT] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False
    elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
        man.x += man.vel
        man.right = True
        man.left = False
        man.standing = False
    else:
        man.standing = True
        man.walkCount = 0

    if not (man.isJump):
        if keys[pygame.K_UP]:
            man.isJump = True
            man.right = False
            man.left = False
            man.walkCount = 0
    else:
        if man.jumpCount >= -10:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.y -= (man.jumpCount ** 2) * 0.5 * neg
            man.jumpCount -= 1
        else:
            man.isJump = False
            man.jumpCount = 10

    redrawGameWindow()

pygame.quit()

【问题讨论】:

  • 格式化代码,因为它不可读
  • 对我来说图像已加载,但您的逻辑无法按预期工作,并且它从不使用加载的图像。只需使用print() 查看变量中的值或学习如何使用调试器。
  • 它以为我的格式正确?!
  • 所有代码都使用code sample(按钮{})而不是blockquote - 它会更好地格式化并使用颜色。您当前的 walkRight = .. 格式不正确 - 它在一行中显示了一些代码,但它应该在两行中显示。
  • 代码又长又复杂——为了解决问题,它会被调试。您可以使用调试器或简单地使用print() 来完成它 - 在不同时刻查看不同变量中的值。通过这种方式,您将看到它是否按预期工作。

标签: python image pygame loading


【解决方案1】:

问题是self.walkCount 是(整数)除以100

win.blit(walkLeft[self.walkCount // 100], (self.x, self.y))

注意self.walkCount 永远不会超过 7,因为:

if self.walkCount + 1 >= 8:
   self.walkCount = 0

所以self.walkCount // 100的结果总是0。

无论如何,这段代码毫无意义,因为walkLeftwalkRight 是包含3 个图像的列表。我建议执行以下操作:

def draw(self, win):
    walkfps = 10

    if self.standing:
        self.walkCount = 0
        if self.right:
            win.blit(walkRight[0], (self.x, self.y))
        else:
            win.blit(walkLeft[0], (self.x, self.y))

    elif self.left:
        if self.walkCount // walkfps >= len(walkLeft):
            self.walkCount = 0 
        win.blit(walkLeft[self.walkCount // walkfps], (self.x, self.y))
        self.walkCount += 1

    elif self.right:
        if self.walkCount // walkfps >= len(walkRight):
            self.walkCount = 0 
        win.blit(walkRight[self.walkCount // walkfps], (self.x, self.y))
        self.walkCount += 1

如果图片变化太快,则增加walkfps

【讨论】:

  • 它有效,谢谢!你能解释一下我在代码中做错了什么吗?或者为什么它没有按照我想要的方式工作?
  • @RalphPerez 我已经在答案的第一部分解释了这一点。 self.walkCount 小于 8,因此 self.walkCount // 100 始终为 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 2014-03-07
相关资源
最近更新 更多