【发布时间】:2020-09-25 13:40:47
【问题描述】:
编辑: 我现在更新了缩进和其他一些数字(让玩家从地板开始而不跳得那么高)。现在它跳到了一个很好的高度,但它没有回到地板上。关于如何解决这个新问题的任何想法?
如果我把 player_movement = 2 放在最后一个 else 中(重置变量),它会慢慢下降,但我需要设置一个障碍,这样它就不会离开屏幕底部。但是,我已经拥有的代码应该可以为我做到这一点……对吧?
更新代码:
if not(isJump):
if keys[pygame.K_UP]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
player_movement -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 10
# This will execute when jump is finished
else:
# Resetting Variables
jumpCount = 10
isJump = False
我最初开始学习本教程:https://www.youtube.com/watch?v=UZg49z76cLw (通过制作 Flappy Bird 来学习 pygame)。但我决定我只想让“玩家”在地板上并能够跳跃(而不是像飞鸟那样重力/跳跃)。
然后我将该教程与:https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/ 结合起来。在加入跳跃之前,“玩家”正在地板上休息。随着跳跃,“玩家”在屏幕顶部闪烁。如何解决此问题以使其正常工作?我尝试查找它,我能找到的只是如何像在屏幕上绘制的框一样跳跃,而不是使用 screen.blit()。
请参阅下面的代码:
import pygame
def draw_floor():
"""Sets one floor after the first"""
screen.blit(floor_surface, (floor_x_pos, 900))
screen.blit(floor_surface, (floor_x_pos + 576, 900))
# Needed to start pygame
pygame.init()
# Creating the screen ((width, height))
screen = pygame.display.set_mode((576, 1024))
# Creating FPS
clock = pygame.time.Clock()
# Game variables
isJump = False
jumpCount = 10
player_movement = 0
# Importing background image into game
bg_surface = pygame.image.load('assets/bg_day.png').convert()
# Making background image larger
bg_surface = pygame.transform.scale2x(bg_surface)
# Importing floor image into the game
floor_surface = pygame.image.load('assets/base.png').convert()
# Making floor image larger
floor_surface = pygame.transform.scale2x(floor_surface)
# Floor variable to move floor
floor_x_pos = 0
# Importing player image into the game
player_surface = pygame.image.load('assets/player.png').convert()
# Making player image larger
player_surface = pygame.transform.scale2x(player_surface)
# Make a collision box around player -- center of rectangle x,y
player_rect = player_surface.get_rect(center=(100, 899))
run = True
while run:
# Checks for all events running
for event in pygame.event.get():
# if event type is quitting:
if event.type == pygame.QUIT:
# set run to False to close loop
run = False
keys = pygame.key.get_pressed()
if not(isJump):
if keys[pygame.K_UP]:
isJump = True
else:
if jumpCount >= -10:
player_movement -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
# This will execute when jump is finished
else:
# Resetting Variables
jumpCount = 10
isJump = False
# Setting the background (screen_bg,(x,y)) : (0,0) = top left
screen.blit(bg_surface, (0, 0))
player_rect.centery = player_movement
# Putting player on screen
screen.blit(player_surface, player_rect)
# Moving the floor
floor_x_pos += -1
# Setting the floor surface to go infinitely
draw_floor()
if floor_x_pos <= -576:
floor_x_pos = 0
# Draws anything from above in while loop and draws on the screen
pygame.display.update()
# Setting FPS - won't run faster than [120] frames per second
clock.tick(120)
# Quits game
pygame.quit()
【问题讨论】: