Python pygame崩溃,修复似乎无法正常工作

时间:2016-05-06 15:12:22

标签: python crash pygame

我试图制作一个平台游戏,在下面的代码中,我试图在背景上移动一个图像(' bird.png')。但是,每次我尝试启动pygame时,它都会在没有加载任何图像的情况下崩溃。我检查了其他页面,它仍然没有解决问题。代码中可能存在一些错误,但我无法检查它是否正常工作,因为正如我所说,pygame一直在崩溃。我能做些什么来解决它吗?

PS。对不起,如果它有点乱,并且对于代码中的法语单词:)

import pygame 
from pygame import *

pygame.init()

TE=[] 

def perso(X):
    X = [0,448]
    while X != [640,0]:
         w=int(input("Déplacement: "))
#Right#
         if w==0:
              if X[1] == 608:
                   print("You can't leave the map")
              else:    
                   X[1] +=  32
                   print(X)     
#Left#
         elif w==1:
              if X[1] == 0:
                   print("You can't leave the map")
              else:
                   X[1] -= 32
                   print(X)     
#Down#  
         elif w==2:
              if X[0] == 456:
                   print("You can't leave the map")
              else:
                   X[0] += 24
                   print(X)     
#Up#
         elif w==3:
              if X[0] == 0:
                   print("You can't leave the map")
              else:
                   X[0] -= 24
                   print(X)
         else:
              print("non valable")
    print("Bravo!")

screen = pygame.display.set_mode((680, 488)) background_image = pygame.image.load("C:/Python34/Scripts/Images & Sounds/background(680x480).jpg").convert()
screen.blit(background_image,[0,0])

character = pygame.image.load("C:/Python34/Scripts/Images & Sounds/bird(40x40).png").convert()
screen.blit(character, (X[0],X[1]))

perso(TE)


flag

我运行代码,当pygame窗口打开时,它是黑色的,几秒钟后我就得到了“没有响应”。消息(对于pygame窗口)。但是,w=int(input("Déplacement: "))部分似乎可以正常工作,因为它要求输入。也许它与图像有关?

1 个答案:

答案 0 :(得分:1)

最简单的游戏循环之一包括“更新”序列和每次循环调用的“渲染”序列。这两个序列都需要很短的时间才能完成(越快越好)。可能是Python的内置input函数正在停止“更新”序列并导致游戏崩溃,因为它无法继续循环。

解决这个问题的最简单方法是使用Pygame的内置键输入法。打破“渲染”和“更新”序列以帮助区分游戏逻辑和图像渲染也是一个好主意。

import pygame 
from pygame import *

pygame.init()

screen = pygame.display.set_mode((680, 488)) 
background_image = pygame.image.load("C:/Python34/Scripts/Images & Sounds/background(680x480).jpg").convert()
character = pygame.image.load("C:/Python34/Scripts/Images & Sounds/bird(40x40).png").convert()

def move(X):
    keys = pygame.get_pressed()
    #Right#
    if keys[pygame.K_RIGHT] != 0:
        if X[1] == 608:
             print("You can't leave the map")
        else:    
             X[1] +=  32
             print(X)     
    #Left#
    elif keys[pygame.K_LEFT] != 0:
        if X[1] == 0:
             print("You can't leave the map")
        else:
             X[1] -= 32
             print(X)     
    #Down#  
    elif keys[pygame.K_DOWN] != 0:
        if X[0] == 456:
             print("You can't leave the map")
        else:
             X[0] += 24
             print(X)     
    #Up#
    elif keys[pygame.K_UP] != 0:
        if X[0] == 0:
             print("You can't leave the map")
        else:
             X[0] -= 24
             print(X)
    else:
        print("non valable")

    return X


def draw(X):
    screen.blit(background_image,[0,0])
    screen.blit(character, (X[0],X[1]))

Running = True
X = [0,448]
while Running:

    # Updated and draw
    X = move(X)
    draw(X)

    # Allow for the user to exit the game
    for i in pygame.event.get():
        if i.type==QUIT:
            Running = False
            exit()

    # End the game if
    if X == [640, 0]:
        Running = False
        exit()