如何禁用我的玩家连续多次跳跃?Pygame



你好,所以我正在制作一个平台,并设置了大多数东西(移动和碰撞(,但我需要制作它,这样当我的玩家跳跃时,他必须等到他再次落地才能跳跃(我想取消双跳(

#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

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

我比较新,所以任何事情都有帮助,谢谢!

问题由引起

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

此代码在SPACE相关时立即设置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

最新更新