Python 如何在pygame中按键后更改屏幕上的精灵

Python 如何在pygame中按键后更改屏幕上的精灵,python,pygame,sprite,Python,Pygame,Sprite,我是Pygame新手,仍在学习Python。我正在使用Python 3.4。 我遵循了这个惊人的游戏创作教程:。在完成课程后,我决定稍微改变一下游戏,并成功地增加了一些动作,修复了一些bug。然而,现在我试图弄清楚当我按下箭头键时如何改变精灵,使它看起来像是倾斜的 我有一个名为racecar.png(这是平面)和move\u right.png(看起来像是倾斜的)。我的最终目标是,当我按下向右箭头时,move\u right.png将显示,直到我松开键,在这种情况下,它将返回到racecar.p

我是Pygame新手,仍在学习Python。我正在使用Python 3.4。 我遵循了这个惊人的游戏创作教程:。在完成课程后,我决定稍微改变一下游戏,并成功地增加了一些动作,修复了一些bug。然而,现在我试图弄清楚当我按下箭头键时如何改变精灵,使它看起来像是倾斜的

我有一个名为
racecar.png
(这是平面)和
move\u right.png
(看起来像是倾斜的)。我的最终目标是,当我按下向右箭头时,
move\u right.png
将显示,直到我松开键,在这种情况下,它将返回到
racecar.png
。我查阅了如何使用sprite sheets,但失败了,如何制作动画,观看了YouTube,用谷歌搜索了我的问题,但我找不到其他有这个问题的人。我发现的大部分内容都是关于在屏幕上移动精灵的

代码如下:

import pygame
import random
import time

pygame.init()

car_width = 100
car_height = 100

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A Macross Simumater')

black = (0,0,0)
white = (255, 255, 255)
red = (255, 0,0)
green = (0,255,0)
greent = (0, 150, 0)
blue = (0,0, 200)
dark_blue = (0, 0, 50)

clock = pygame.time.Clock()

carImp = pygame.image.load('racecar.png')

gameIcon = pygame.image.load('caricon.png')

#background = pygame.image.load('background.png')

pygame.display.set_icon(gameIcon)

def quitgame():
    pygame.quit()
    quit()

def unpause():
    global pause
    pause = False

def paused():

    largeText = pygame.font.SysFont("comicsansms",115)
    TextSurf, TextRect = text_objects("Paused", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)


    while pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        button("Continue",150,450,100,50,green,black,unpause)
        button("Quit",550,450,100,50,red,black,quitgame)

        pygame.display.update()
        clock.tick(15)   


def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: " + str(count), True, black)
    gameDisplay.blit(text, (0,0))

def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def car(x, y):
    gameDisplay.blit(carImp, (x, y))

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

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf', 115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()

def crash():


    largeText = pygame.font.SysFont("comicsansms",115, (0, 0, 0))
    TextSurf, TextRect = text_objects("You Crashed", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        button("Play Again",150,450,100,50,green,black,game_loop)
        button("Quit",550,450,100,50,red,black,quitgame)

        pygame.display.update()
        clock.tick(15) 

def button(msg,x,y,w,h,ic,ac, action = None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    print(click)

    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x, y, w, h))

        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

    smallText = pygame.font.Font('freesansbold.ttf', 21)
    textSurf, textRect = text_objects("GO!", smallText)
    textRect.center = ((150+(100/2)), (450+(50/2)))
    gameDisplay.blit(textSurf, textRect)

    textSurf, textRect = text_objects("QUIT!", smallText)
    textRect.center = ((550+(100/2)), (450+(50/2)))
    gameDisplay.blit(textSurf, textRect)

def game_intro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf', 115)
        TextSurf, TextRect = text_objects('Macross',largeText)
        TextRect.center = ((display_width/2), (display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        mouse = pygame.mouse.get_pos()

        button("GO!", 150, 450, 100, 50, green, black, game_loop)
        button("Quit", 550,450,100,50, red, black, quitgame)

        pygame.display.update()
        clock.tick(15)

def game_loop():
    global pause

    pygame.mixer.music.load('Macross Plus - Voices HQ MV Karaoke Instrumental Japanese.mp3')
    pygame.mixer.music.play(-1)

    x = (display_width * 0.45)
    y = (display_height * 0.8)

    global x_change
    global y_change

    x_change = 0
    y_change = 0


    pause = False

    dodged = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 4
    thing_width = 50
    thing_height = 50

    thingt_startx = random.randrange(0, display_width)
    thingt_starty = -600
    thingt_speed = 7
    thingt_width = 100
    thingt_height = 100


    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True


            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    x_change = 8
                elif event.key == pygame.K_LEFT:
                    x_change = -8
                elif event.key == pygame.K_p:
                    pause = True
                    paused()
                elif event.key == pygame.K_UP:
                    y_change = -8
                elif event.key == pygame.K_DOWN:
                    y_change = 8
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    x_change = 0
                    y_change = 0

        x += x_change
        y += y_change

        gameDisplay.fill(dark_blue)

        things(thing_startx, thing_starty, thing_width, thing_height, greent)
        thing_starty += thing_speed
        things(thingt_startx, thingt_starty, thingt_width, thingt_height, greent)
        thingt_starty += thingt_speed

        car(x, y)


        if x > display_width - car_width or x < 0:
            crash()
        if y > display_width - car_width or y < 0:
            y_change = 0

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0, display_width)
            dodged += 1
            thing_speed += 1
            thing_width += (dodged * 1.2)
            thing_height += (dodged * 2)
            thingt_starty = 0 - thing_height
            thingt_startx = random.randrange(0, display_width)
            dodged += 1
            thingt_speed += 1
            thingt_width += (dodged * 1.2)
            thingt_height += (dodged * 2)

        if thing_starty < y:
            if y < thing_starty+thing_height:
                if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
                    crash()
        elif thing_starty > y:
            print('passed')

        if thingt_starty < y:
            if y < thingt_starty+thingt_height:
                if x > thingt_startx and x < thingt_startx + thingt_width or x +     car_width > thingt_startx and x + car_width < thingt_startx + thingt_width:
                    crash()
        elif thingt_starty > y:
            print('PASSED')





        things_dodged(dodged)
        pygame.display.update()
        clock.tick(60)



game_intro()
game_loop()
pygame.quit()
quit()
导入pygame
随机输入
导入时间
pygame.init()
轿厢宽度=100
轿厢高度=100
显示宽度=800
显示高度=600
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('A Macross Simumater')
黑色=(0,0,0)
白色=(255,255,255)
红色=(255,0,0)
绿色=(0255,0)
格林特=(0,150,0)
蓝色=(0,0200)
深蓝色=(0,0,50)
clock=pygame.time.clock()
carImp=pygame.image.load('racecar.png')
gameIcon=pygame.image.load('caricon.png')
#background=pygame.image.load('background.png')
pygame.display.set_图标(游戏图标)
def quitgame():
pygame.quit()
退出
def unpause():
全局暂停
暂停=错误
def暂停():
largeText=pygame.font.SysFont(“comicsansms”,115)
TextSurf,TextRect=text\u对象(“暂停”,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
暂停时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
按钮(“继续”,150450100,50,绿色,黑色,取消暂停)
按钮(“退出”,550450100,50,红色,黑色,退出游戏)
pygame.display.update()
时钟滴答(15)
def道奇事件(计数):
font=pygame.font.SysFont(无,25)
text=font.render(“减淡:”+str(计数),真,黑色)
blit(文本,(0,0))
定义事物(thingx、thingy、thingw、thingh、颜色):
pygame.draw.rect(游戏显示,颜色,[thingx,thingy,thingw,thingh])
def汽车(x,y):
游戏显示.blit(carImp,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
博弈(循环)
def crash():
largeText=pygame.font.SysFont(“comicsansms”,115,(0,0,0))
TextSurf,TextRect=text_对象(“您崩溃了”,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
按钮(“再次播放”,150450100,50,绿色,黑色,游戏循环)
按钮(“退出”,550450100,50,红色,黑色,退出游戏)
pygame.display.update()
时钟滴答(15)
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
打印(单击)
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(游戏显示,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
行动()
其他:
pygame.draw.rect(游戏显示,ic,(x,y,w,h))
smallText=pygame.font.font('freesansbold.ttf',21)
textSurf,textRect=text\u对象(“GO!”,smallText)
textRect.center=((150+(100/2)),(450+(50/2)))
blit(textSurf,textRect)
textSurf,textRect=text\u对象(“退出!”,smallText)
textRect.center=((550+(100/2)),(450+(50/2)))
blit(textSurf,textRect)
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(白色)
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text\u对象('macros',largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
mouse=pygame.mouse.get_pos()
按钮(“开始!”,150,450,100,50,绿色,黑色,游戏循环)
按钮(“退出”,550450100,50,红色,黑色,退出游戏)
pygame.display.update()
时钟滴答(15)
def game_loop():
全局暂停
pygame.mixer.music.load('Macross Plus-Voices HQ MV Karaoke器乐日语.mp3')
pygame.mixer.music.play(-1)
x=(显示宽度*0.45)
y=(显示高度*0.8)
全球x_变化
全球y_变化
x_变化=0
y_变化=0
暂停=错误
减淡=0
thing\u startx=random.randrange(0,显示宽度)
事情开始=-600
速度=4
宽度=50
物体高度=50
thingt_startx=random.randrange(0,显示宽度)
thingt_starty=-600
thingt_速度=7
thingt_宽度=100
东西高度=100
gameExit=False
不退出游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
gameExit=True
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_RIGHT:
x_变化=8
elif event.key==pygame.K_左:
def car(x, y):
    gameDisplay.blit(carImp, (x, y))
car_image_names = ["racecar", "move_right", "move_left"]
car_sprites = dict(((img_name, pygame.image.load(img_name + ".png"))
                        for img_name in car_image_names)
carImp = car_sprites["racecar"]
if x_change == 0:
    carImp = car_sprites["racecar"]
elif x_change > 0:
    carImp = car_sprites["move_right"]
elif x_change < 0:
    carImp = car_sprites["move_left"]