我怎样才能禁止我的球员连续多次跳跃?皮加梅

2024-03-28 14:13:23 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在制作一个平台,并设置了大部分内容(移动和碰撞),但我需要这样做,当我的玩家在跳跃时,他必须等到再次触地才能跳跃(我想取消双跳)

#load images
bg_img = pygame.image.load('img/background1.jpg')
bg_img = pygame.transform.scale(bg_img, (1000,1000))
rect = bg_img.get_rect()
#class for player
class Player():
    def __init__(self, x, y):
        self.images_right = []
        self.images_left = []
        self.index = 0
        self.counter = 0
        for num in range(1,6):
            img_right = pygame.image.load(f'img/guy{num}.png')
            img_right = pygame.transform.scale(img_right, (40 , 80))
            img_left = pygame.transform.flip(img_right,True,False) #flips right image on the x axis {true} and not y axis {false}
            self.images_right.append(img_right)
            self.images_left.append(img_left)
        self.image = self.images_right[self.index]
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.width = self.image.get_width()
        self.height = self.image.get_height()
        self.vel_y = 0
        self.jumped = False
        self.direction = 0

    def update(self):
        dx = 0
        dy = 0
        walk_cooldown = 5

        #get keypresses
        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE] and self.jumped == False:
            self.vel_y = -15
            self.jumped = True
        if key[pygame.K_SPACE] == False:
            self.jumped = False
        if key[pygame.K_LEFT]:
            dx -= 5
            self.counter += 1
            self.direction = -1
        if key[pygame.K_RIGHT]:
            dx += 5
            self.counter += 1
            self.direction = 1
        if key[pygame.K_LEFT] == False and key[pygame.K_RIGHT] == False:
            self.counter = 0
            self.index = 0
            if self.direction == 1:
                self.image = self.images_right[self.index]
            if self.direction == -1:
                self.image = self.images_left[self.index]



        #animation
        if self.counter > walk_cooldown:
            self.counter = 0
            self.index += 1
            if self.index >= len(self.images_right):
                self.index = 0
            if self.direction == 1:
                self.image = self.images_right[self.index]
            if self.direction == -1:
                self.image = self.images_left[self.index]



        #add gravity
        self.vel_y += 1
        if self.vel_y > 10:
            self.vel_y = 10
        dy += self.vel_y

        #check for collision

        for tile in world.tile_list:
            #x direction collision
            if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
                dx=0




            #y direction collision
            if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
                #check if below ground (jumping)
                if self.vel_y <0:
                    dy = tile[1].bottom - self.rect.top
                    self.vel_y = 0
                #check if above ground(falling)
                elif self.vel_y >= 0:
                    dy = tile[1].top - self.rect.bottom

        #update player coordinates
        self.rect.x += dx
        self.rect.y += dy

以上是我认为对这个问题很重要的代码,如果您需要不同的代码,请告诉我

我比较新,所以任何东西都有帮助,谢谢


Tags: keyrectimageselfrightfalseimgget
1条回答
网友
1楼 · 发布于 2024-03-28 14:13:23

这个问题是由

if key[pygame.K_SPACE] == False:
   self.jumped = False

空间松弛时,此代码立即设置self.jumped = False,这将启用下一次跳转

删除这两行代码,但在飞机与地面碰撞时设置self.jumped = False

class Player():
    # [...]

    def update(self):
        # [...]

        key = pygame.key.get_pressed()
        if key[pygame.K_SPACE] and self.jumped == False:
            self.vel_y = -15
            self.jumped = True

        # if key[pygame.K_SPACE] == False:    
        #     self.jumped = False                        # < - DELETE

        # [...]

        #check for collision
        for tile in world.tile_list:
            # [...]

            # y direction collision
            if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
                #check if below ground (jumping)
                if self.vel_y < 0:
                    dy = tile[1].bottom - self.rect.top
                    self.vel_y = 0
             
                #check if above ground(falling)
                elif self.vel_y >= 0:
                    dy = tile[1].top - self.rect.bottom

                    self.jumped = False                  # < - INSERT
       

相关问题 更多 >