如何使用 Pygame 修复我的代码以正确跳转?
How do I fix my code with Pygame to jump correctly?
编辑:
我现在更新了缩进和其他一些数字(让玩家从地板开始而不是跳得太高)。现在它跳到了一个很好的高度,但它并没有回到地板上。关于如何解决这个新问题有什么想法吗?
再搞一些,如果我把 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)。但我决定我只希望“玩家”在地板上并且能够跳跃(而不是 gravity/jumping 像 flappy bird)。
所以我将该教程与 https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/ 结合起来。在添加跳跃之前,'player' 停在地板上。随着跳跃,'player' 在屏幕顶部闪烁。我该如何解决这个问题才能正常工作?我尝试查找它,我所能找到的只是如何像在屏幕上绘制的框一样跳跃,而不是使用 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()
这是Indentation的事情。 else
案例属于 if not(isJump):
:
while run:
# [...]
if not(isJump):
if keys[pygame.K_UP]:
isJump = True
#<--| INDENTATION
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
编辑: 我现在更新了缩进和其他一些数字(让玩家从地板开始而不是跳得太高)。现在它跳到了一个很好的高度,但它并没有回到地板上。关于如何解决这个新问题有什么想法吗?
再搞一些,如果我把 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)。但我决定我只希望“玩家”在地板上并且能够跳跃(而不是 gravity/jumping 像 flappy bird)。
所以我将该教程与 https://techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/ 结合起来。在添加跳跃之前,'player' 停在地板上。随着跳跃,'player' 在屏幕顶部闪烁。我该如何解决这个问题才能正常工作?我尝试查找它,我所能找到的只是如何像在屏幕上绘制的框一样跳跃,而不是使用 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()
这是Indentation的事情。 else
案例属于 if not(isJump):
:
while run:
# [...]
if not(isJump):
if keys[pygame.K_UP]:
isJump = True
#<--| INDENTATION
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