如何解决如何禁用我的播放器连续多次跳跃?小游戏
你好,所以我正在制作一个平台并设置了大多数东西(移动和碰撞),但我需要这样做,所以当我的玩家跳跃时,他必须等到他再次落地才能跳跃(我想删除双跳跃)
#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.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
并启用下一次跳转。
去掉这2行代码,但是当payer与地面碰撞时设置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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。