python - 游戏几秒后崩溃

标签 python python-3.x list pygame range

当我测试我的游戏时,我运行良好,但在几秒钟内游戏因错误而崩溃 列表索引超出范围

出了什么问题?

这是我的代码:

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

第二件事是当我不走到边缘时,游戏不会崩溃。 我与我观看的视频有相同的代码,但我不明白为什么它不起作用。 我读到了这个问题,但它与我的游戏无关。

最佳答案

您必须确保 animCount 分别不超过 walkLeftwalkRight 的长度。列表中只有 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))

关于python - 游戏几秒后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59909150/

相关文章:

python - mainwindow.close() 期间的信号

python - Django MySql,外键查询

python - 从与 Selenium 中的模式匹配的所有元素中获取文本

python - 如何在 while 循环中处理意外的 json 响应

python - 写一个高效的python邻接树生成脚本

java - 如何处理List<Tuple>

python - 同时枚举两个python列表?

python Selenium : Getting dynamic content within iframe

php - Python 相当于 PHP 的 __DIR__ 魔法常量?

python - 如何推迟/推迟 f 弦的评估?