Python 我怎样才能禁止我的球员连续多次跳跃?皮加梅
您好,我正在制作一个平台,并设置了大部分内容(移动和碰撞),但我需要这样做,当我的玩家在跳跃时,他必须等到再次触地才能跳跃(我想取消双跳)Python 我怎样才能禁止我的球员连续多次跳跃?皮加梅,python,pygame,Python,Pygame,您好,我正在制作一个平台,并设置了大部分内容(移动和碰撞),但我需要这样做,当我的玩家在跳跃时,他必须等到再次触地才能跳跃(我想取消双跳) #加载图像 bg_img=pygame.image.load('img/background1.jpg')) bg_img=pygame.transform.scale(bg_img,(10001000)) rect=bg_img.get_rect() #为运动员上课 类播放器(): 定义初始化(self,x,y): self.images_right=[]
#加载图像
bg_img=pygame.image.load('img/background1.jpg'))
bg_img=pygame.transform.scale(bg_img,(10001000))
rect=bg_img.get_rect()
#为运动员上课
类播放器():
定义初始化(self,x,y):
self.images_right=[]
self.images_left=[]
self.index=0
self.counter=0
对于范围(1,6)中的num:
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)#在x轴{True}而不是y轴{False}上翻转右图像
self.images\u right.append(img\u right)
self.images\u left.append(img\u left)
self.image=self.images\u right[self.index]
self.rect=self.image.get_rect()
self.rect.x=x
自校正y=y
self.width=self.image.get_width()
self.height=self.image.get_height()
self.vel_y=0
self.com=False
自我定向=0
def更新(自我):
dx=0
dy=0
步行冷却时间=5
#得到按键
key=pygame.key.get_pressed()
如果键[pygame.K_SPACE]和self.hopped==False:
self.vel_y=-15
self.com=True
如果键[pygame.K_SPACE]==False:
self.com=False
如果键[pygame.K_左]:
dx-=5
self.counter+=1
self.direction=-1
如果键[pygame.K_RIGHT]:
dx+=5
self.counter+=1
自我定向=1
如果键[pygame.K_LEFT]==False且键[pygame.K_RIGHT]==False:
self.counter=0
self.index=0
如果self.direction==1:
self.image=self.images\u right[self.index]
如果self.direction==-1:
self.image=self.images\u左[自索引]
#动画
如果self.counter>walk\u冷却:
self.counter=0
自索引+=1
如果self.index>=len(self.images\u right):
self.index=0
如果self.direction==1:
self.image=self.images\u right[self.index]
如果self.direction==-1:
self.image=self.images\u左[自索引]
#增加重力
self.vel_y+=1
如果自我水平>10:
self.vel_y=10
dy+=自我水平
#检查有无碰撞
对于world.tile\u列表中的tile:
#x方向碰撞
如果平铺[1]。碰撞矩形(self.rect.x+dx,self.rect.y,self.width,self.height):
dx=0
#y方向碰撞
如果平铺[1].colliderect(self.rect.x,self.rect.y+dy,self.width,self.height):
#检查是否低于地面(跳跃)
如果self.vel_y=0:
dy=平铺[1]。顶部-self.rect.bottom
#更新玩家坐标
self.rect.x+=dx
自校正y+=dy
以上是我认为对这个问题很重要的代码,如果您需要不同的代码,请告诉我
我比较新,所以任何东西都有帮助,谢谢 问题是由
if key[pygame.K_SPACE]==False:
self.com=False
当空间松弛时,此代码立即设置self.hopped=False
,从而启用下一次跳转
删除这两行代码,但在行人与地面碰撞时设置self.hopped=False
:
class Player():
# [...]
def更新(自我):
# [...]
key=pygame.key.get_pressed()
如果键[pygame.K_SPACE]和self.hopped==False:
self.vel_y=-15
self.com=True
#如果键[pygame.K_SPACE]==False:
#self.jumped=False#=0:
dy=平铺[1]。顶部-self.rect.bottom
self.jumped=False#通过去除与播放器的垂直运动无关的任何内容,您可以大幅减少此处的代码量。它帮助读者(甚至你自己)关注这个问题。这个问题解决了吗?请阅读并感谢。这对我帮助很大。。。我不敢相信我在阅读代码时竟然没有意识到要这么做。
#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