当前位置: 首页 > 知识库问答 >
问题:

当我的播放器接触地面时,我如何让我的空闲动画播放

魏波娃
2023-03-14

所以我为我的玩家制作了一个跳跃动画,我一直在想如何制作它,以便当我的玩家接触地面/平台时,它将停止播放我的跳跃动画,并将播放我的空闲动画。我试图让它播放动画,当玩家按下空格键,玩家的摔倒是

我想知道的是,它在做什么:https://gyazo.com/5cfda577628e5596d033b11a7486bbf9?token=6f82939da2c6b2363232b3e3f379aa07

        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
            else:
                if playerman.direction == "walk":
                    playerman.direction = "jump"
                else:
                    if playerman.direction == "lwak":
                        playerman.direction = "ljump"

我的完整代码

import pygame
pygame.init()
# The screen width and height
window = pygame.display.set_mode((700,500))
# The name of the screen
pygame.display.set_caption("Game")

# Player class
class Player():
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        # Animation for player
        self.idle = [pygame.image.load("HRI1.png"),
                    pygame.image.load("HRI2.png"),
                    pygame.image.load("HRI3.png"),
                    pygame.image.load("HRI4.png"),]
                    
        self.idlel = [pygame.image.load("HLI1.png"),
                      pygame.image.load("HLI2.png"),
                      pygame.image.load("HLI3.png"),
                      pygame.image.load("HLI4.png")]
        
        self.walk = [pygame.image.load("HRW1.png"),
                     pygame.image.load("HRW2.png"),
                     pygame.image.load("HRW3.png"),
                     pygame.image.load("HRW4.png"),
                     pygame.image.load("HRW5.png"),
                     pygame.image.load("HRW6.png")]
        
        self.lwalk = [pygame.image.load("HLW1.png"),
                      pygame.image.load("HLW2.png"),
                      pygame.image.load("HLW3.png"),
                      pygame.image.load("HLW4.png"),
                      pygame.image.load("HLW5.png"),
                      pygame.image.load("HLW6.png")]
        
        self.jump = [pygame.image.load("HRJ1.png"),
                     pygame.image.load("HRJ2.png"),
                     pygame.image.load("HRJ3.png")]

        self.ljump = [pygame.image.load("HLJ1.png"),
                     pygame.image.load("HLJ2.png"),
                     pygame.image.load("HLJ3.png")]

        self.direction = "idle"
        self.direction = "idlel"
        self.direction = "walk"
        self.direction = "lwalk"
        self.direction = "jump"
        self.direction = "ljump"
        
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,width,height)
        self.next_frame_time = 0
        self.fps = 10
        self.clock = pygame.time.Clock
        self.anim_index = 0
        self.idle = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.idle]
        self.idlel = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.idlel]
        self.walk = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.walk]
        self.lwalk = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.lwalk]
        self.jump = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.jump]
        self.ljump = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.ljump]

    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "idlel":
            image_list = self.idlel
        if self.direction == "walk":
            image_list = self.walk
        if self.direction == "lwalk":
            image_list = self.lwalk
        if self.direction == "jump":
            image_list = self.jump
        if self.direction == "ljump":
            image_list = self.ljump
        # Is it time to show next frame?
        time_now = pygame.time.get_ticks()
        if (time_now > self.next_frame_time):
            # time till the nect frame
            inter_time_delay = 1000 // self.fps
            self.next_frame_time = time_now + inter_time_delay
            # Show the next frame
            self.anim_index += 1
            if self.anim_index >= len(image_list):
                self.anim_index = 0

        if self.anim_index >= len(image_list):
            self.anim_index = 0
        player_image = image_list[self.anim_index]

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 3
        player_rect.centery -= 3
        window.blit(player_image, player_rect)
    
            
        
        

# Platform class
class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

# The color of **** hitbox
white = (255,255,255)

# Player size,cords, and hitbox color
playerman = Player(255,255,40,40,white)

# Platform size,cords,and hitbox color
platform1 = Platform(0,470,400,30,white)

# Platform list
platforms = [platform1]

#redrawing window so player dose not make the screen a mess
def redrawwindow():
    window.fill((0,0,0))

    playerman.draw()

    # making it so I do not have to draw every platform 1 by 1
    for Platform in platforms:
        Platform.draw()

# Fps of the game
fps = 30
clock = pygame.time.Clock()

# Runing the game/ the main loop
run = True
while run:
    # Making game run with fps
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    



    
        
    

    # telling what to do when we say the word 'key'
    keys = pygame.key.get_pressed()

    # Shortining playerman.x and playerman.y
    px,py = playerman.x,playerman.y



    # player 'A' movment
    if keys[pygame.K_a] and px > playerman.speed:
        px -= playerman.speed
        playerman.direction = "lwalk"
        
    # player 'D' movment
    elif keys[pygame.K_d] and playerman.x < 700 - playerman.width - playerman.speed:
        px += playerman.speed
        playerman.direction = "walk"

 
    # Playing idle animation
    else:
        if playerman.direction == "walk":
            playerman.direction = "idle"
        else:
            if playerman.direction == "lwalk":
                playerman.direction = "idlel"

    
                    

    
    
    
    # player 'W' movment
    if keys[pygame.K_w] and py > playerman.speed:
        py -= playerman.speed

    # player 'S' movment
    if keys[pygame.K_s] and py < 500 - playerman.height - playerman.speed:
        py += playerman.speed


    platform_rect_list = [p.rect for p in platforms]
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)


        
    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px


    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 0.5
        playerman.isJump = False
        

    # For player to get on top of platform
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
            else:
                if playerman.direction == "walk":
                    playerman.direction = "jump"
                else:
                    if playerman.direction == "lwak":
                        playerman.direction = "ljump"

            # Making it so player wont fall out of the map 
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.JumpCount = 10
                playerman.y = 500 - playerman.height

        # Player jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

        # What will happen when player jumps
    else:
        if playerman.JumpCount >= 0:    
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10


    # redrawing the window
    redrawwindow()
    # updating the game
    pygame.display.update()
# quiting the game
pygame.quit()



共有1个答案

谢典
2023-03-14

当播放器与平台碰撞且当前动画为“跳跃”时,切换到“空闲”动画:

collide = False
for Platform in platforms:
    if playerman.get_rect().colliderect(Platform.rect):
        collide = True
        playerman.isJump = False
        
        if playerman.direction == "jump":
            playerman.direction = "idle":
 类似资料:
  • 当我尝试播放我的音乐时,Discord机器人不会播放音乐。它使用ytdl核心和ffmpeg。我的代码是: 每当我尝试播放歌曲时,都会发生以下错误: (节点:5180)未处理的PromisejectionWarning:错误:找不到FFmpeg/avconv!在功能上。getInfo(C:\Users\picar\Desktop\DiscordMusicBot\node\u modules\pris

  • 你好,我目前正在开发一款生存射击游戏,我目前对一些事情感到沮丧。一个是我的游戏动画不起作用。我这里有播放器类的代码: 如果您想了解我的任何其他问题/挫折,请说出来。任何帮助将不胜感激。谢谢。

  • 主要目标是想让儿童可以以最好的体验来看动画片。 1、支持记录每一部动画片的当前播放位置,方便下次打开的时候可以接着看。 2、可以增加片头、片尾的时间点,不用每次都播放重复内容。 3、播放、暂停、上一集、下一集、快照、全屏等。

  • 我的XML代码: 片断中的代码 java.lang.NullPointerException:试图对com.google.Android.YouTube.Player.YouTubePlayerSupportFragment.OnStart(未知源)上的空对象引用调用虚拟方法“void com.google.Android.YouTube.Player.YouTubePlayerView.a()”

  • 当我一次又一次点击播放按钮时,它会同时播放多次。我想停止多重播放。这是代码: 媒体播放器和按钮的对象 按钮单击事件监听器以播放音频 按钮单击事件侦听器以停止音频 任何帮助都将不胜感激。谢谢

  • 在AndroidManifest中。xml我有一个: 我的广播文件: 我试图运行应用程序后关闭它在后台播放音乐。