在pygame的落下力学(重力)(Falling mechanics (gravity) in py

2019-10-29 08:42发布

我怎么会得到一些力学上的空白落下时说,在互联网上很多答案补充重力,但我不明白他们怎么做,他们只是给我看了一堆公式。

此外,我将如何将图像设置为我的背景?

下面是我的源代码:

import pygame


pygame.init()

display_width = 2560
display_height = 1440

white = (255,255,255)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('RGB')
clock = pygame.time.Clock()

filler = pygame.image.load('filleraftergimp.png')


def fill(x,y):
    gameDisplay.blit(filler,(x,y))


x = (display_width * 0.45)
y = (display_height * 0.8)

x_change = 0
y_change = 0

diedorgameover = False
while not diedorgameover:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            diedorgameover = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                x_change = -5
            elif event.key == pygame.K_d:
                x_change = 5
            elif event.key == pygame.K_s:
                y_change = 5
            elif event.key == pygame.K_w:
                y_change = -5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or event.key == pygame.K_d:
                x_change = 0
            if event.key == pygame.K_s or event.key == pygame.K_w:
                y_change = 0

    x += x_change
    y += y_change
    gameDisplay.fill(white)   
    fill(x,y)


    pygame.display.update()
    clock.tick(60)


pygame.quit()
quit()

Answer 1:

为了实现在游戏中的重力(如在2D平台游戏),你可以增加y_change变量每一帧,让您移动每次物体快一点向下。 看看这个例子:

import pygame as pg


pg.init()

LIGHTBLUE = pg.Color('lightskyblue2')
DARKBLUE = pg.Color(11, 8, 69)

display = pg.display.set_mode((800, 600))
width, height = display.get_size()
clock = pg.time.Clock()

player_image = pg.Surface((30, 60))
player_image.fill(DARKBLUE)

x = width * 0.45
y = 0
x_change = 0
y_change = 0
on_ground = False

# A constant value that you add to the y_change each frame.
GRAVITY = .3

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_a:
                x_change = -5
            elif event.key == pg.K_d:
                x_change = 5
            elif event.key == pg.K_s:
                y_change = 5
            elif event.key == pg.K_w:
                if on_ground:  # Only jump if the player is on_ground.
                    y_change = -12
                    on_ground = False
        elif event.type == pg.KEYUP:
            if event.key == pg.K_a and x_change < 0:
                x_change = 0
            elif event.key == pg.K_d and x_change > 0:
                x_change = 0

    # Add the GRAVITY value to y_change, so that
    # the object moves faster each frame.
    y_change += GRAVITY
    x += x_change
    y += y_change
    # Stop the object when it's near the bottom of the screen.
    if y >= height - 130:
        y = height - 130
        y_change = 0
        on_ground = True

    # Draw everything.
    display.fill(LIGHTBLUE)
    pg.draw.line(display, (0, 0, 0), (0, height-70), (width, height-70))
    display.blit(player_image, (x, y))

    pg.display.update()
    clock.tick(60)

pg.quit()


文章来源: Falling mechanics (gravity) in pygame