[英]How Can I Improve This Jump?
視頻<這是我的問題,我的跳躍跳躍得很快,而且我沒有時間從我跳躍的地方移動毆打有沒有一種方法可以在不減慢精靈 fps 的情況下改善這種跳躍? 如果我試圖減慢我的精靈 fps,這會導致我的精靈在我跳躍時多次播放它的 animation 方式。 我試圖使這個跳躍盡可能平滑,但現在它的跳躍方式很快,而且 animation 的播放速度也很快
我的跳躍在哪里
# our main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if playerman.direction == "right":
playerman.direction = "Idle"
if playerman.direction == "left":
playerman.direction = "leftid"
if keys[pygame.K_SPACE]: # -------------- IF WE CLICK SPACE THEN WE SHOULD PLAY JUMP ANIMATION
playerman.direction = "jump"
# player moving
if not playerman.isJump: # -------------- make our player fall
playerman.y += playerman.fall
playerman.fall += 1
playerman.isJump = False
collide = False
if playerman.rect.bottom >= 605:
collide = True
playerman.isJump = False
playerman.JumpCount = 10
playerman.y = 605 - playerman.height
if collide:
if keys[pygame.K_SPACE]:
playerman.isJump = True
playerman.fall = 1
# -------------- this is the jUmp logic where I am having the problem
else:
if playerman.JumpCount >= -10:
playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.4
playerman.JumpCount -= 1
else:
playerman.JumpCount = 10
playerman.isJump = False
if playerman.direction == "jump": # if we click jump then it should play the jump then go back to the "idle"
playerman.direction = "Idle"
我的鼠標精靈 class
# our player class
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.idle = [pygame.image.load("grey_mouse/idle (" + str(i) + ").png") for i in range(1, 21)]
self.leftid = [pygame.image.load("grey_mouse/id (" + str(i) + ").png") for i in range(1, 21)]
self.Right = [pygame.image.load("grey_mouse/walk (" + str(i) + ").png") for i in range(1, 9)]
self.left = [pygame.image.load("grey_mouse/left (" + str(i) + ").png") for i in range(1, 9)]
self.jump = [pygame.image.load("grey_mouse/jump (" + str(i) + ").png") for i in range(1, 9)]
self.color = color
self.rect = pygame.Rect(x,y,height,width)
self.fps = 60
self.next_frame_time = 0
self.clock = pygame.time.Clock()
self.direction = "jump"
self.direction = "Idle"
self.direction = "right"
self.direction = "left"
self.direction = "leftid"
self.speed = 3
self.anim_index = 0
self.fall = 0
self.isJump = False
self.jumpCount = 10
def get_rect(self):
self.rect.topleft = (self.x,self.y)
return self.rect
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "Idle":
self.clock.tick(self.fps)
image_list = self.idle
elif self.direction == "right":
self.clock.tick(self.fps)
image_list = self.Right
elif self.direction == "left":
self.clock.tick(self.fps)
image_list = self.left
elif self.direction == "leftid":
self.clock.tick(self.fps)
image_list = self.leftid
elif self.direction == "jump":
self.clock.tick(60)
image_list = self.jump
# Is it time to show the next animation frame?
time_now = pygame.time.get_ticks()
if ( time_now > self.next_frame_time ):
# set the time for the next animation-frame
inter_frame_delay = 1990 // self.fps
self.next_frame_time = time_now + inter_frame_delay # in the future
# move the current image to the next (with wrap-around)
self.anim_index += 1
if self.anim_index >= len( image_list ):
self.anim_index = 0
if self.anim_index >= len(image_list):
self.anim_index = 0
player_image = image_list[self.anim_index]
player_image = image_list[self.anim_index]
self.hitbox = (self.x, self.y + 30, 46,60)
player_rect = player_image.get_rect(center = self.get_rect().center)
player_rect.centerx += 0
player_rect.centery += -20
window.blit(player_image, player_rect)
我只是試圖回答。 也許您可以降低跌倒或跳躍的增量
if not playerman.isJump: # -------------- make our player fall
playerman.y += playerman.fall
playerman.fall += 0.1 #make the y increase slower, until you find the proper speed
playerman.isJump = False
跳躍時間取決於跳躍算法中使用的常數 10。 使用jumpCountStart
變量而不是此常量。
jumpCountStart = 50
跳躍的高度也取決於這個常數。 用10 / jumpCountStart
縮放跳躍,這樣跳躍的高度在跳躍減速時不會改變:
while run:
# [...]
if not playerman.isJump:
# [...]
pass
else:
if playerman.JumpCount >= -jumpCountStart:
jumpHeight = playerman.JumpCount * 10 / jumpCountStart
playerman.y -= (jumpHeight * abs(jumpHeight)) * 0.4 * 10 / jumpCountStart
playerman.JumpCount -= 1
else:
playerman.JumpCount = jumpCountStart
playerman.isJump = False
# [...]
不要忘記使用jumpCountStart
JumpCount
class player:
def __init__(self,x,y,height,width,color):
# [...]
self.JumpCount = jumpCountStart
Animation 與jumpCountStart = 50
:
您必須多次顯示相同的 animation 圖像。 例如,當jumpCountStart = 20
時,您必須將每個圖像顯示 2 次。 當jumpCountStart = 50
時,您必須將每個圖像顯示 5 次。
self.jump = [pygame.image.load("grey_mouse/jump (" + str(i) + ").png")
for i in range(1, 9)]
repeat = jumpCountStart // 10
self.jump = [self.jump[i // repeat] for i in range(len(self.jump) * repeat)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.