简体   繁体   English

Python pygame崩溃,修复似乎不起作用

[英]Python pygame crashing, fixes don't seem to work

I'm trying to make a platformer, and in the code below I'm trying to move one image ('bird.png') around on the background. 我正在尝试制作平台游戏,并在下面的代码中尝试在背景上四处移动一个图像('bird.png')。 However, every time I try to launch pygame it crashes without even loading any of the images. 但是,每次我尝试启动pygame时,它甚至都不会加载任何图像而崩溃。 I checked out the other pages and it still didn't fix the problem. 我签出了其他页面,但仍然无法解决问题。 There are probably a few mistakes in the code, but I can't check if it's working properly because as I said, pygame keeps crashing. 代码中可能存在一些错误,但是我无法检查它是否正常运行,因为正如我所说,pygame一直崩溃。 Is there anything I can do to fix it? 有什么我可以解决的吗?

PS. PS。 Sorry if it's a bit messy, and for the french words in the code :) 抱歉,如果有点混乱,请输入法文中的法语单词:)

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

I run the code and when the pygame window opens, it's black and after a few seconds I get the 'not responding' message (for the pygame window). 我运行代码,当pygame窗口打开时,它是黑色的,几秒钟后,我收到“不响应”消息(对于pygame窗口)。 However, the w=int(input("Déplacement: ")) part seems to work as it asks for an input. 但是, w=int(input("Déplacement: "))部分似乎可以正常工作,因为它要求输入。 Maybe it's something to do with the images? 也许与图像有关?

One of the simplest game loops consists of an "update" sequence and a "render" sequence that are called each time through the loop. 最简单的游戏循环之一由每次通过循环调用的“更新”序列和“渲染”序列组成。 Both of these sequences should take very little time to complete (the faster the better). 这两个序列都需要很少的时间来完成(越快越好)。 It could be that Python's built in input function is stopping the "update" sequence and causing the game to crash because it can't continue the loop. 可能是Python的内置input函数正在停止“更新”序列,并导致游戏崩溃,因为它无法继续循环。

The easiest way to solve this would be to just use Pygame's built-in key input methods. 解决此问题的最简单方法是仅使用Pygame的内置按键输入法。 It's also a good idea to break out the "render" and "update" sequences to help distinguish between the game logic and the rendering of images. 打破“渲染”和“更新”序列也是一个好主意,以帮助区分游戏逻辑和图像渲染。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM