按下键时精灵变化

时间:2018-09-26 12:09:53

标签: python pygame

我一直在关注youtube教程,从python和pygmae开始,但是在完成之前先进行修改(https://www.youtube.com/watch?v=dX57H9qecCU&index=5&list=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO)。我想在代码中添加相同的“按下时的Sprite更改”(how to change a sprite on the screen after pressing a key in pygame),但是我什么也没得到。游戏就好像完全没有代码一样运行。

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

我还添加了某种重播功能,但是它很麻烦,当我按指定的键时,它并不总是有效,不知道为什么。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:-1)

在pygame.K_DOWN附近或其他与pygame.K _ 在pygame.K_命令之后,添加您想要的控制键名称