【问题标题】:Is there a way to blit multiple images or a list of images in pygame有没有办法在pygame中对多个图像或图像列表进行blit
【发布时间】:2019-06-28 12:24:45
【问题描述】:

我整天都在试图弄清楚这一点,并搜索了无数帖子。我试图弄清楚如何让我的精灵执行攻击动画,但实际上没有关于如何在线执行此操作的文档或信息。如果我向右移动,我已经将它带到了他将执行旋转攻击动画的位置,但是经过这么多次旋转后,游戏将崩溃并显示“列表索引超出范围”。此外,如果角色静止不动,按攻击键“w”绝对没有任何作用。我已经包含了我的代码的副本。任何帮助将不胜感激。

import pygame
import os
os.chdir('C:\\Users\\Name\\Desktop\\Character Animation\\')
pygame.init()
pygame.mixer.init()

WIDTH = 1920
HEIGHT = 1080
FPS = 60

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Game')

''' IMAGE RESIZING___________________________________________________________'''
wRight1 = pygame.image.load('row2_1.png')
wRight1 = pygame.transform.scale(wRight1, (80,80))
wRight2 = pygame.image.load('row2_2.png')
wRight2 = pygame.transform.scale(wRight2, (80,80))
wRight3 = pygame.image.load('row2_3.png')
wRight3 = pygame.transform.scale(wRight3, (80,80))
wRight4 = pygame.image.load('row2_4.png')
wRight4 = pygame.transform.scale(wRight4, (80,80))
wRight5 = pygame.image.load('row2_5.png')
wRight5 = pygame.transform.scale(wRight5, (80,80))
wRight6 = pygame.image.load('row2_6.png')
wRight6 = pygame.transform.scale(wRight6, (80,80))
wRight7 = pygame.image.load('row2_7.png')
wRight7 = pygame.transform.scale(wRight7, (80,80))``
wRight8 = pygame.image.load('row2_8.png')
wRight8 = pygame.transform.scale(wRight8, (80,80))


wLeft1 = pygame.image.load('row10_1.png')
wLeft1 = pygame.transform.scale(wLeft1, (80,80))
wLeft2 = pygame.image.load('row10_2.png')
wLeft2 = pygame.transform.scale(wLeft2, (80,80))
wLeft3 = pygame.image.load('row10_3.png')
wLeft3 = pygame.transform.scale(wLeft3, (80,80))
wLeft4 = pygame.image.load('row10_4.png')
wLeft4 = pygame.transform.scale(wLeft4, (80,80))
wLeft5 = pygame.image.load('row10_5.png')
wLeft5 = pygame.transform.scale(wLeft5, (80,80))
wLeft6 = pygame.image.load('row10_6.png')
wLeft6 = pygame.transform.scale(wLeft6, (80,80))
wLeft7 = pygame.image.load('row10_7.png')
wLeft7 = pygame.transform.scale(wLeft7, (80,80))
wLeft8 = pygame.image.load('row10_8.png')
wLeft8 = pygame.transform.scale(wLeft8, (80,80))


spinAttackR1 = pygame.image.load('spinattackright1.png')
spinAttackR1 = pygame.transform.scale(spinAttackR1, (90,90))
spinAttackR2 = pygame.image.load('spinattackright2.png')
spinAttackR2 = pygame.transform.scale(spinAttackR2, (90,90))


standStill1 = pygame.image.load('standStill1.png')
standStill1 = pygame.transform.scale(standStill1, (80,80))
standStill2 = pygame.image.load('standStill2.png')
standStill2 = pygame.transform.scale(standStill2, (80,80))
standStill3 = pygame.image.load('standStill3.png')
standStill3 = pygame.transform.scale(standStill3, (80,80))
standStill4 = pygame.image.load('standStill4.png')
standStill4 = pygame.transform.scale(standStill4, (80,80))

'''_________________________________________________________________________'''

bg = pygame.image.load('bg.png')
spinAttackList = [spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2,
                  spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2,
                  spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2,
                  spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2,
                  spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2,
                  spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2,
                  spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2,
                  spinAttackR1, spinAttackR2, spinAttackR1, spinAttackR2]
walkRight = [wRight1, wRight2,
             wRight3, wRight4,
             wRight5, wRight6,
             wRight7, wRight8]
walkLeft = [wLeft1, wLeft2,
             wLeft3, wLeft4,
             wLeft5, wLeft6,
             wLeft7, wLeft8]

#char = [standStill1, standStill2, standStill3, standStill4]

char = pygame.image.load('topDownattackRight3.png')
char = pygame.transform.scale(char, (80,80))
#R = char.get_rect()

clock = pygame.time.Clock()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

x = 50
y = 770
width = 64
height = 64
vel = 5

isJump = False
jumpCount = 10

spin = False
left = False
right = False
walkCount = 0
attackCount = 0

'''DRAWING SECTION___________________________________________________________'''
def redrawGameWindow():
    global walkCount
    global attackCount
    screen.blit(bg, (0,0))

    if walkCount + 1 >= 24:
        walkCount = 0

    if left:
        screen.blit(walkLeft[walkCount//3], (x,y))
        walkCount += 1
        attackCount += 1
    elif right:
        screen.blit(walkRight[walkCount//3], (x,y))
        walkCount += 1
        attackCount += 1
    else:
        screen.blit(char, (x,y))

    if spin:
        screen.blit(spinAttackList[attackCount//3], (x,y))

    pygame.display.update()


#mainloop
run = True
while run:
    clock.tick(30)

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

    keys = pygame.key.get_pressed()
#_________________________________________________________________________
    if keys[pygame.K_w]:
        spin = True

    if keys[pygame.K_LEFT] and x > vel:
        x -= vel
        left = True
        right = False
    elif keys[pygame.K_RIGHT] and x < WIDTH - 40:
        x += vel
        right = True
        left = False
    else:
        right = False
        left = False
        walkCount = 0
        spin = False

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

    redrawGameWindow()

pygame.quit()

【问题讨论】:

  • plenty of examples 其他人如何解决它。 SO 上还有关于如何为精灵设置动画的资源。你看过哪些?
  • 我一直在查看文档,包括 pygame 文档中关于移动图像的教程。我也尝试过实现 youtube 教程中的代码,并根据我的需要专门定制它们,但他们正在制作不同类型的游戏。我搜索了有关如何无限迭代列表的帖子。如何在 python 中重复列表。读到 image.blit 不能 blit 列表,尽管 image.blit 很好地对我的行走图像列表进行了 blit。也许我只是不知道要寻找什么?我可以向你保证,如果你怀疑,我不会偷懒并试图找到一个简单的答案。
  • 如果你想轮换列表,你可以做walkLeft[walkCount % len(walkLeft-1)]?让它自己循环。假设它是具有列表开始/结束的完美动画,并且您不必向后执行动画。 (另外,不是说你很懒嘿嘿。但我认为解决方案比问题更简单)
  • 再次您好,感谢您的回复。抱歉,如果我之前的评论出错了。我真的很难处理这个问题,我不知道要寻找什么,因为我是 pygame 和一般编程的新手。我会研究你的建议,但似乎我的问题不在于代码的步行部分,而更多的是攻击动画试图与步行动画同时发生,攻击动画似乎用完了列表中的图像,而我仍然按下键并且游戏崩溃。
  • 呵呵,不用担心 :) 哦,我想我们需要看看有问题的精灵。因为我不确定你应该如何为你的步行+攻击动画使用两个单独的图像。通常这些是相同的图像对象。这种方式通常更容易解决。因为将两张图像拼接在一起 - 相比之下,如果您手动进行,则非常困难。

标签: python pygame


【解决方案1】:

每当您增加索引时,您必须确保它们不会溢出可用图像的数量。你这样做是为了walkCount,但不是为了attackCount

modulo operator% 很方便,正如 cmets 中所建议的那样。

所以你可以改变你的重绘功能:

def redrawGameWindow():
    global walkCount
    global attackCount
    screen.blit(bg, (0,0))

    if left:
        screen.blit(walkLeft[walkCount//3], (x,y))
        walkCount += 1
        walkCount %= len(walkLeft)
        attackCount += 1
        attackCount %= len(spinAttackList)

    elif right:
        screen.blit(walkRight[walkCount//3], (x,y))
        walkCount += 1
        walkCount %= len(walkRight)
        attackCount += 1
        attackCount %= len(spinAttackList)
    else:
        screen.blit(char, (x,y))

    if spin:
        screen.blit(spinAttackList[attackCount//3], (x,y))

    pygame.display.update()

我不确定你为什么要使用整数除法运算符//,我已经不管它了,但我想你可能打算使用%,在这种情况下,后续的%= 行是不必要的。

如果你想要更优雅的东西,你可以使用cycling iterator

【讨论】:

    【解决方案2】:

    检查一下我已经这样做了

    def drop_enemies(enemy_list):
    delay = random.random()
    if len(enemy_list) < 10 and delay < 0.1:
        x_pos = random.randint(0, WIDTH - enemy_size)
        y_pos = 0
        enemy_list.append([x_pos, y_pos])
    
    
    def draw_enemies(enemy_list):
    for enemy_pos in enemy_list:
        pygame.draw.rect(screen, BLUE, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))
    

    【讨论】:

      【解决方案3】:

      试试这个。它对我有用:

      #x, y, w, h are for position and no is number of images
      
      def ims(no, x, y, w, h):
          idt = True
          pino = 0
      
          while idt:
              ec()
      
              if pino != no:
                  file = str("xyz") + str(pino) + ".png" #filepath, with image names as 0.png, 1.png so on
                  pino += 1
                  pic = pygame.image.load(file).convert_alpha()
                  pygame.draw.rect(screen, bgc, (x, y, w, h)
      
              pygame.display.update()
              clock.tick(15)
      

      【讨论】:

        猜你喜欢
        • 2018-04-08
        • 2016-01-23
        • 2020-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-23
        • 1970-01-01
        相关资源
        最近更新 更多