几秒钟后游戏就崩溃了

2024-09-27 21:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

当我测试我的游戏时,我做得很好,但在几秒钟内游戏就因为错误而崩溃了 列表索引超出范围

怎么了

这是我的密码:

import pygame

pygame.init()

win = pygame.display.set_mode((800, 600))

pygame.display.set_caption('World of Fighters')

clock = pygame.time.Clock()

#player 2
x2 = 720
y2 = 500

width2 = 50
height2 = 50
speed2 = 15

left2 = False
right2 = False
animCount2 = 0

isJump2 = False
jumpCount2 = 10

#green
color2 = (0, 200, 0 )

#player 1
x = 50
y = 520

width = 50
height = 50
speed = 15

left = False
right = False
animCount = 0

isJump = False
jumpCount = 10

#animation
walkRight = [pygame.image.load('g_right1.png'),
pygame.image.load('g_right2.png'), pygame.image.load('g_right3.png'),
pygame.image.load('g_right4.png')]

walkLeft = [pygame.image.load('g_left1.png'),
pygame.image.load('g_left2.png'), pygame.image.load('g_left3.png'),
pygame.image.load('g_left4.png')]

playerStand = pygame.image.load('g_stand.png')
bg = pygame.image.load('bg.jpg')

def drawWindow():
    global animCount 
    win.blit(bg, (0, 0))

    if animCount + 1 >= 30:
        animCount = 0

    if left:
        win.blit(walkLeft [animCount // 5], (x, y))
        animCount += 1
    elif right:
        win.blit(walkRight [animCount // 5], (x, y))
        animCount += 1
    else:
        win.blit(playerStand, (x, y))

    pygame.display.update()


#blue
color = (0, 0, 255)

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()

    #p1
    if keys[pygame.K_a] and x > 5:
        x -= speed
        left = True
        right = False   
    elif keys[pygame.K_d] and x < 800 - width - 5:
        x += speed
        left = False
        right = True
    else:
        left = False
        right = False
        animCount = 0
    if not(isJump):
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if jumpCount >= -10:
            if jumpCount < 0:
                y += (jumpCount ** 2) / 2
            else:
                y -= (jumpCount ** 2) / 2
            jumpCount -= 1

        else:
            isJump = False
            jumpCount = 10


    #p2
    if keys[pygame.K_LEFT] and x2 > 5:
        x2 -= speed2
        left2 = True
        right2 = False  
    elif keys[pygame.K_RIGHT] and x2 < 800 - width2 - 5:
        x2 += speed2
        left2 = False
        right2 = True
    else:
        left2 = False
        right2 = False
        animCount2 = 0
    if not(isJump2):

        if keys[pygame.K_RCTRL]:
            isJump2 = True
    else:
        if jumpCount2 >= -10:
            if jumpCount2 < 0:
                y2 += (jumpCount2 ** 2) / 2
            else:
                y2 -= (jumpCount2 ** 2) / 2
            jumpCount2 -= 1

        else:
            isJump2 = False
            jumpCount2 = 10

    drawWindow()

pygame.quit()

第二件事是,游戏不会走到边缘。 我的代码与我观看的视频相同,但我不工作。 我读过关于这个问题的文章,但它与我的游戏无关


Tags: imagefalsetrueifpngloadkeyspygame
1条回答
网友
1楼 · 发布于 2024-09-27 21:30:14

您必须确保animCount分别不超过walkLeft的长度walkRight。列表中只有4个图像,因此您必须评估animCount >= 20

def drawWindow():
    # [...]
    if animCount >= 20:
        animCount = 0

或者

def drawWindow():
    # [...]

    if left:
        if animCount >= len(walkLeft)*5:
            animCount = 0
        win.blit(walkLeft [animCount // 5], (x, y))
        animCount += 1
    elif right:
        if animCount >= len(walkRight)*5:
            animCount = 0
        win.blit(walkRight [animCount // 5], (x, y))
        animCount += 1
    else:
        win.blit(playerStand, (x, y))

相关问题 更多 >

    热门问题