密钥释放时如何自动实现重力?如何允许连续跳跃?

时间:2017-04-11 13:15:47

标签: pygame python-3.5

在我的代码中,当玩家的头像跳到某一点时,重力将其重新拉下来。但是,这仅在保持向上和空间按钮直到达到某个点时才有效。在释放按钮的情况下,我想使用以下代码将鸭子重力向下送回:

player.rect.move_ip(0, (player.speed) * -player.y_direction)

我不知道如何检查是否已发布,然后自动实施。

我想要帮助的另一个问题是,当鸭子落在地板上时,按钮必须被释放然后重新按下。我想更新代码,只要按下按钮,虚拟形象会跳回到地面然后它将自动返回跳跃状态按下空格和向上按钮。

以下是完整代码:     class Player(pygame.sprite.Sprite):         “”玩家控制着鸭子“”“

    def __init__(self, x, y, width, height):
        super(Player, self).__init__(x, y, width, height)

        # How many pixels the Player duck should move on a given frame.
        self.y_change = 0
        # How many pixels the spaceship should move each frame a key is pressed.
        self.y_dist = 150
        self.x_change = 0
        # How many pixels the spaceship should move each frame a key is pressed.
        self.x_dist = 30
        self.x_direction = 1
        # Positive = down, negative = up
        self.y_direction = -1
        # Current speed.
        self.speed = 13

duck = pygame.image.load('walkingduck.png')
player = Player(0, (window_height)-125, 120, 160) #Making player object(duck)

all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
move = False

while True:  # the main game loop
    if move: #Jumping process
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_UP] or key_pressed[pygame.K_SPACE]:
            if player.rect.y<=320:
                move=False
            else:
                player.rect.move_ip(0, player.speed * player.y_direction)
    if move==False and player.rect.y<=(window_height)-115:
        player.rect.move_ip(0, (player.speed) * -player.y_direction)

    for event in pygame.event.get(): #Closes game
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            move=True

            #Adds images and text
    screen.blit(BackGround.image, BackGround.rect)

    all_sprites_list.draw(screen)
    pygame.display.flip()
    pygame.display.update()
    clock.tick(FPS)

1 个答案:

答案 0 :(得分:0)

我会这样做:如果玩家正在飞行,请将y_change设置为负数,否则将GRAVITY值添加到它,以便它向下加速。然后移动矩形并夹紧它。 (我实际上将移动代码放入update类的Player方法中。)

import sys
import pygame


class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):
        super(Player, self).__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill((230, 200, 40))
        self.rect = self.image.get_rect(center=(x, y))
        self.y_change = 0


pygame.init()
GRAVITY = .9
window_height = 600
screen = pygame.display.set_mode((800, window_height))
player = Player(200, 50, 40, 30)
all_sprites_list = pygame.sprite.Group(player)
clock = pygame.time.Clock()
move = False
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            move = True
        if event.type == pygame.KEYUP:
            move = False

    if move:  # Fly upwards.
        player.y_change = -9
    else:  # Add the gravity to the y-velocity.
        player.y_change += GRAVITY
    player.rect.move_ip(0, player.y_change)
    # Clamp the rect.y value to the range 50 to 450-rect.h.
    # You could also use rect.clamp_ip((0, 50, 800, 450)).
    if player.rect.y < 50:
        player.rect.y = 50
        player.y_change = 0
    elif player.rect.bottom >= window_height-150:
        player.rect.bottom = window_height-150
        player.y_change = 0

    screen.fill((30, 100, 140))
    all_sprites_list.draw(screen)
    pygame.display.flip()
    pygame.display.update()
    clock.tick(30)

pygame.quit()
sys.exit()

以下是使用update方法和rect.clamp_ip的变体。

import sys
import pygame


class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):
        super(Player, self).__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill((230, 200, 40))
        self.rect = self.image.get_rect(center=(x, y))
        self.y_change = 0
        self.flying = False

    def update(self):
        if self.flying:
            self.y_change = -9
        self.y_change += GRAVITY
        self.rect.move_ip(0, self.y_change)
        self.rect.clamp_ip((0, 50, 800, 450))
        if self.rect.y <= 50:
            self.y_change = 1


pygame.init()
GRAVITY = .5
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
player = Player(200, 50, 40, 30)
all_sprites_list = pygame.sprite.Group(player)
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            player.flying = True
        if event.type == pygame.KEYUP:
            player.flying = False

    all_sprites_list.update()
    screen.fill((30, 100, 140))
    all_sprites_list.draw(screen)
    pygame.display.flip()
    clock.tick(30)

pygame.quit()
sys.exit()

编辑:这是连续跳跃而不是平滑向上移动的版本。

import sys
import pygame


class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):
        super(Player, self).__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill((230, 200, 40))
        self.rect = self.image.get_rect(center=(x, y))
        self.y_change = 0
        self.flying = False

    def update(self):
        if self.flying:
            # If the y_change is getting too big,
            # reset it to -13 to fly upwards again.
            if self.y_change > 5:
                self.y_change = -13
        # Accelerate downwards each frame.
        self.y_change += GRAVITY
        self.rect.move_ip(0, self.y_change)
        self.rect.clamp_ip((0, 50, 800, 450))
        if self.rect.y <= 50:
            self.y_change = 1


pygame.init()
GRAVITY = .9
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
player = Player(200, 50, 40, 30)
all_sprites_list = pygame.sprite.Group(player)
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            player.flying = True
        if event.type == pygame.KEYUP:
            player.flying = False

    all_sprites_list.update()
    screen.fill((30, 100, 140))
    all_sprites_list.draw(screen)
    pygame.display.flip()
    clock.tick(30)

pygame.quit()
sys.exit()