2017-10-21 271 views
0

在空的空间中,我会如何获得一些力学机制,许多互联网上的答案表示会增加重力,但我无法理解他们是如何做到的,他们只是向我展示了一堆方程。在pygame中下降的力学(重力)

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

这里是我的源代码:

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

请一次只问一个问题。我最近解释了如何实现重力[here](https://stackoverflow.com/a/46659103/6220679)。在你的例子中,你可以每帧增加你的'y_change'来加速你的对象,例如'y_change + = 1'。 – skrx

+0

你能解释一下你在另一篇文章中做了什么,因为在检查后我无法弄清楚你做了什么;我也尝试改变我的y_change值,但它只是减缓了我的性格。你的意思是改变控件脚本部分中的y_value吧? –

+0

btw感谢您花时间回答我的问题 –

回答

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