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
以上是我认为对这个问题很重要的代码,如果您需要不同的代码,请告诉我。
我比较新,所以有什么帮助谢谢!
解决方案
该问题是由
if key[pygame.K_SPACE] == False: self.jumped = False
此代码在释放self.jumped = False
时立即设置SPACE,这将启用下一个跳转。
删除这 2 行代码,但self.jumped = False
在 palyer 与地面碰撞时设置:
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
推荐阅读
- javascript - ReactJS:Javascript 不针对反应中的 div 元素,而是针对 html 中的 div 元素
- sas - 如何在 SAS 中将字符串(2013 年 1 月)转换为日期(201301)值?
- php - 下载文件,用户决定在哪里找到它
- c# - 我可以使用字典来根据我称为键的变量的值来决定采取哪些操作吗?
- c - poll(2) 在与 Web 服务器的连接上未检测到 POLLIN
- autodesk-forge - Forge 的 VR AR 工具包
- laravel - 如何将资源数据中的数据绑定到 Blade Laravel?
- java - 容器/Pod 中的 Firestore 事件监听器
- javascript - 制表符自动完成自定义编辑器和 css。无法在单元格外显示编辑器
- android - 如何根据来自服务器的 x,y 坐标获取精确的 x,y 坐标以在 WebRtc 中绘制叠加层