在pygame中下降机制(重力)

时间:2017-10-21 22:27:09

标签: python background pygame physics

在空旷的地方我怎么会得到一些摔倒的机制,互联网上的许多答案都说增加了引力但我无法理解他们是怎么做的,他们只是向我展示了一堆方程式。

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

这是我的源代码:

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()

1 个答案:

答案 0 :(得分: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()