【问题标题】:Game is crashing after few seconds游戏在几秒后崩溃
【发布时间】:2020-01-25 12:21:03
【问题描述】:

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

怎么了?

这是我的代码:

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

第二件事是游戏是t crashing when I dont 走到边缘。 我观看的视频具有相同的代码,但我不t understand, why it doesnt 工作。 我读过这个问题,但它与我的游戏无关。

【问题讨论】:

标签: python python-3.x list pygame range


【解决方案1】:

您必须确保animCount 不超过walkLeftwalkRight 的长度。列表中只有 4 张图片,所以你必须评估 animCount &gt;= 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))

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多