Python 按下该键时,精灵会发生变化

Python 按下该键时,精灵会发生变化,python,pygame,Python,Pygame,我一直在关注youtube教程,开始学习python和pygmae,但在完成之前,我开始做一些修改。我想在我的代码中添加相同的精灵更改,但什么也没有得到。游戏运行起来就像根本没有代码一样 import pygame import time pygame.init() #as important as import pygame, always init pygame display_width = 600 display_height = 900 black = (0,0,0) white

我一直在关注youtube教程,开始学习python和pygmae,但在完成之前,我开始做一些修改。我想在我的代码中添加相同的精灵更改,但什么也没有得到。游戏运行起来就像根本没有代码一样

import pygame
import time

pygame.init() #as important as import pygame, always init pygame 
display_width = 600
display_height = 900

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

starship_width = 60

gameDisplay = pygame.display.set_mode((display_width,display_height)) #sets window size. Put between () so it's seen as a single parameter
pygame.display.set_caption("Starship") #changes window title display
clock = pygame.time.Clock() #sets clock as frames per second

ship_image_names = ["starship", "move_right", "move_left"]
ship_sprites = dict(((img_name, pygame.image.load(img_name + ".png"))
                        for img_name in ship_image_names))
starshipImp = ship_sprites["starship"]

def starship(x,y):  #sets image position
    gameDisplay.blit(starshipImp,(x,y))

def text_objects(text,font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def kb_disable():
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT or pygame.K_RIGHT or pygame.K_UP or pygame.K_DOWN:
                pass

def message_display(text):
    largeText = pygame.font.Font("theboldfont.ttf",70)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2-170))
    gameDisplay.blit(TextSurf,TextRect)

def message_display1(text):
    largeText = pygame.font.Font("theboldfont.ttf",70)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2-100))
    gameDisplay.blit(TextSurf,TextRect)
    pygame.display.update() 

    while not kb_disable(): 
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_y:
                    game_loop()
                elif event.key == pygame.K_n:
                    pygame.quit()
                    quit()


def replay():
    message_display("You Crashed")
    message_display1("Replay? (Y)(N)")


def game_loop():

    x = (display_width * 0.5 - 45)    
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    game_Exit = False # beginning game loop

    while not game_Exit: 
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5    
                if event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0 
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0




        x += x_change           
        y += y_change


        gameDisplay.fill(white) #background
        starship(x,y) #shows starship image
        if x_change == 0:
            starshipImp = ship_sprites["starship"]
        if x_change > 0:
            starshipImp = ship_sprites["move_right"]
        if x_change < 0:
            starshipImp = ship_sprites["move_left"]

        if x > display_width  - starship_width or x < 0:  #setting boundaries to left and right border
                replay()




        pygame.display.update() #update the screen
        clock.tick(120)


game_loop()
pygame.quit()
quit()
我还添加了一些重播功能,但它有缺陷,当我按指定的键时,它并不总是工作,不知道为什么

在pygame.K_DOWN附近或pygame.K的其他地方,如有任何帮助,将不胜感激_
在pygame.K_命令后添加所需的控制键名

是学习如何使用调试器的时候了=欢迎来到堆栈溢出。请拿着这本书读一读。您是否能够将您的代码简化为一个,以便人们更容易提供帮助?请正确设置您的答案格式并提供更多详细信息。