我的 Pygame 表面没有被重力推回
My Pygame surface is not getting pushed back down by gravity
我真的很希望 purple_idle_rect 能够在空中跳跃,然后被重力拉下来。这里真正发生的是,一旦我跳跃,我就会被困在空中,然后一旦我再次按下跳跃按钮,我就会坠落。基本上,我需要按两次向上箭头键才能完全跳跃并降落在地面上。
import pygame
# import n stuff
import pygame
pygame.init()
# Screen and setup
Screen = pygame.display.set_mode((900, 500))
# Velocity
Velocity = 8
# Purple who sleep
purple_sleeping = pygame.transform.scale(pygame.image.load("purple_guy_sleeping.png"), (100, 100))
purple_sleeping_rect = purple_sleeping.get_rect(center=(450, 302))
# Purple who stand
purple_idle = pygame.transform.scale(pygame.image.load("purple_man_idle.png"), (100, 100))
purple_idle_rect = purple_idle.get_rect(midbottom = (100,360))
# The ground
ground = pygame.transform.scale(pygame.image.load("a2889b85d5c7122.png"), (800, 100))
# Main loop
Run = True
FPS = 60
Clock = pygame.time.Clock()
# All of our keys
pressed_keys = {"left": False, "right": False}
# Gravity
player_gravity = 0
isJump = False
while Run:
pygame.display.update()
Screen.fill("black")
Screen.blit(ground, (45, 350))
Screen.blit(purple_sleeping, purple_sleeping_rect)
Screen.blit(purple_idle, purple_idle_rect)
Clock.tick(FPS)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = True
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = True
if event.key == pygame.K_UP and purple_idle_rect.bottom == 360:
player_gravity -=20
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = False
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = False
if purple_idle_rect.bottom == 360:
isJump = False
if not isJump:
purple_idle_rect.bottom = 360
if purple_idle_rect.bottom < 360:
isJump = True
if isJump:
player_gravity += 20
if pressed_keys["left"]: # == True is implied here
purple_idle_rect.x -= 10
if pressed_keys["right"]:
purple_idle_rect.x += 10
purple_idle_rect.bottom += player_gravity
if purple_idle_rect.bottom > 360:
purple_idle_rect.bottom = 360
pygame.display.update()
我稍微改变了你的跳跃,这对我来说是这样的:
player_gravity = 20
isJump = False
while Run:
# pygame.display.update()
Screen.fill("black")
Screen.blit(ground, (45, 350))
Screen.blit(purple_sleeping, purple_sleeping_rect)
Screen.blit(purple_idle, purple_idle_rect)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = True
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = True
if event.key == pygame.K_UP and purple_idle_rect.bottom == 360:
isJump = True
player_gravity = -player_gravity
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = False
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = False
if pressed_keys["left"]: # == True is implied here
purple_idle_rect.x -= 10
if pressed_keys["right"]:
purple_idle_rect.x += 10
# Set gravity back
if isJump:
player_gravity += 1
if player_gravity >= 20:
isJump = False
purple_idle_rect.bottom += player_gravity
if purple_idle_rect.bottom > 360:
purple_idle_rect.bottom = 360
pygame.display.update()
Clock.tick(FPS)
如果您不在评论中提问,也许您已经了解一切。希望我对你有所帮助,亚当
我真的很希望 purple_idle_rect 能够在空中跳跃,然后被重力拉下来。这里真正发生的是,一旦我跳跃,我就会被困在空中,然后一旦我再次按下跳跃按钮,我就会坠落。基本上,我需要按两次向上箭头键才能完全跳跃并降落在地面上。
import pygame
# import n stuff
import pygame
pygame.init()
# Screen and setup
Screen = pygame.display.set_mode((900, 500))
# Velocity
Velocity = 8
# Purple who sleep
purple_sleeping = pygame.transform.scale(pygame.image.load("purple_guy_sleeping.png"), (100, 100))
purple_sleeping_rect = purple_sleeping.get_rect(center=(450, 302))
# Purple who stand
purple_idle = pygame.transform.scale(pygame.image.load("purple_man_idle.png"), (100, 100))
purple_idle_rect = purple_idle.get_rect(midbottom = (100,360))
# The ground
ground = pygame.transform.scale(pygame.image.load("a2889b85d5c7122.png"), (800, 100))
# Main loop
Run = True
FPS = 60
Clock = pygame.time.Clock()
# All of our keys
pressed_keys = {"left": False, "right": False}
# Gravity
player_gravity = 0
isJump = False
while Run:
pygame.display.update()
Screen.fill("black")
Screen.blit(ground, (45, 350))
Screen.blit(purple_sleeping, purple_sleeping_rect)
Screen.blit(purple_idle, purple_idle_rect)
Clock.tick(FPS)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = True
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = True
if event.key == pygame.K_UP and purple_idle_rect.bottom == 360:
player_gravity -=20
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = False
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = False
if purple_idle_rect.bottom == 360:
isJump = False
if not isJump:
purple_idle_rect.bottom = 360
if purple_idle_rect.bottom < 360:
isJump = True
if isJump:
player_gravity += 20
if pressed_keys["left"]: # == True is implied here
purple_idle_rect.x -= 10
if pressed_keys["right"]:
purple_idle_rect.x += 10
purple_idle_rect.bottom += player_gravity
if purple_idle_rect.bottom > 360:
purple_idle_rect.bottom = 360
pygame.display.update()
我稍微改变了你的跳跃,这对我来说是这样的:
player_gravity = 20
isJump = False
while Run:
# pygame.display.update()
Screen.fill("black")
Screen.blit(ground, (45, 350))
Screen.blit(purple_sleeping, purple_sleeping_rect)
Screen.blit(purple_idle, purple_idle_rect)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = True
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = True
if event.key == pygame.K_UP and purple_idle_rect.bottom == 360:
isJump = True
player_gravity = -player_gravity
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = False
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = False
if pressed_keys["left"]: # == True is implied here
purple_idle_rect.x -= 10
if pressed_keys["right"]:
purple_idle_rect.x += 10
# Set gravity back
if isJump:
player_gravity += 1
if player_gravity >= 20:
isJump = False
purple_idle_rect.bottom += player_gravity
if purple_idle_rect.bottom > 360:
purple_idle_rect.bottom = 360
pygame.display.update()
Clock.tick(FPS)
如果您不在评论中提问,也许您已经了解一切。希望我对你有所帮助,亚当