【问题标题】:Pygame - Gravity MethodsPygame - 重力方法
【发布时间】:2016-03-05 21:49:26
【问题描述】:

我正在用 pygame 制作一个简单的游戏。它看起来是一个平台游戏角色扮演游戏。但这既不是最终的,也不是这个问题的相关内容。到目前为止,我在游戏中的功能很少。如果那样的话,它在这一点上只是一个骨架。我的问题有两个方面:

  • 在 pygame 中向类添加重力的最佳方式(就性能和灵活性而言)是什么?
  • 一般来说,添加重力的最佳做法是什么?例如,您是否只是简单地执行“如果 keyPressed == k_W 则从 player-y 中减去 2 个像素,持续 20 刻”或在向上或负 y 方向上的速度?

我看过其他关于事后为游戏添加重力的帖子,在这些帖子中,在最初的开发过程中确实没有考虑过添加重力。我想尽早添加它,这样我就可以将其他东西添加到重力中,而不是向其他事物添加重力。我将继续阅读此内容,因此,如果您希望向我指出一些在线资源的方向,我也将不胜感激!

【问题讨论】:

    标签: python pygame gravity


    【解决方案1】:

    快速免责声明:我不知道合并重力的多种方法,所以我不能说哪个是“最好的”。但是,如果您在 Python 中打性能战,那么您可能打错了仗。

    对于重力,您可以使用矢量系统。假设一个角色跳离地面并且具有 [5, -15] 的初始速度(负 y,因为正 y 向下!),您可以每帧以该速度移动角色的矩形以模拟运动。要将重力投入其中,您需要在每秒的 y 速度分量值上添加 9.8。所以 1 秒后,速度将约为 [5, -5]。这将使您的角色缓慢停止,然后开始向下移动。

    对于按键移动,我建议使用布尔值。例如,按下 k_U 后,表示您正在向上移动的变量变为 True。然后,如果此变量为 True,则移动他,例如 [0, -5]。在 keyup 时,将变量设置为 false。对北/东/南/西执行此操作,然后您将拥有一个 4 个方向的移动系统,当您按住键时会移动您。

    【讨论】:

      【解决方案2】:

      我在这段代码中使用了 1/2 mg^2 方程,它有雪一样的效果:

      import math, sys, random, time
      import pygame, inputbox
      from pygame.locals import *
      
      class flake:
      
          def __init__(self, xpos, ypos, mass, color, drift):
              self.xpos = xpos
              self.ypos = ypos
              self.mass = mass
              self.rect = pygame.Rect(xpos, ypos, 2, 2)
              self.checked = False
              self.color = color
              self.drift = drift
      
      size = width, height = 510, 700
      
      BLACK = (0,0,0)
      WHITE = (255, 255, 255)
      GREY = (128,128,128)
      DARKGREY = (169,169,169)
      SILVER = (192,192,192)
      LIGHTGREY = (211,211,211)
      LIGHTESTGREY = (220,220,220)
      
      pygame.init()     
      
      screen = pygame.display.set_mode(size)
      background = pygame.Surface(screen.get_size())
      background = background.convert()
      background.fill(BLACK)
      
      def init():
      
          global theSnow, snowColours, clock, startrange
      
          theSnow = []
          snowColours = []
      
          snowColours.append(WHITE)
          snowColours.append(GREY)
          snowColours.append(DARKGREY)
          snowColours.append(SILVER)
          snowColours.append(LIGHTGREY)
          snowColours.append(LIGHTESTGREY)
      
          for c in range(2000):
              mass = 0.0
              mass = float(random.randint(1,8) / 100.0)
              xpos = random.randint(0,width)
              ypos = random.randint(0,5)
              ypos = -ypos
              drift = ypos/10.0
      
              colour = snowColours[random.randint(0,5)]
      
              f = flake(xpos, ypos, mass, colour, drift)
              theSnow.append(f)       
              print "flake x = " + str(f.xpos) + "   y = " + str(f.ypos) + "   mass = " + str(f.mass)             
      
          startrange = 200
          clock = pygame.time.Clock() 
      
      def run():
      
          global theSnow, clock
          global startrange
      
          newrange = 0
      
          while True:
              events = pygame.event.get()
              for event in events:
                  if event.type == pygame.QUIT:
                      sys.exit()
              keys=pygame.key.get_pressed()
      
              if keys[K_q]:
                  return
      
              g = 3
      
              for count in range(startrange):
      
                  yinc = 0.0
                  yuncertainty = float(random.randint(1,5)/10.0)
                  yinc = float(0.5 * theSnow[count].mass * (g*g)) + yuncertainty
                  theSnow[count].ypos += yinc
      
                  xuncertainty = random.randint(1,10)
                  if xuncertainty > 4:
                      theSnow[count].xpos += theSnow[count].drift
                  else:
                      theSnow[count].xpos -= theSnow[count].drift
      
                  theSnow[count].rect = pygame.Rect(theSnow[count].xpos, theSnow[count].ypos, 2,2)
      
                  if not theSnow[count].checked:
                      if theSnow[count].ypos > 30:
                          for c in range(newrange, startrange):
                              print " c= " + str(c)
                              theSnow[c].checked = True
      
                          if startrange < 2000:
                              startrange += 100
                              newrange = startrange - 100
                              print " newrange = " + str(newrange)
                              print " startrange = " + str(startrange)
      
              update()
              pygame.time.wait(10)
          #clock.tick(10)
      
      def update():
      
          global theSnow, startrange
          background.fill(BLACK)
      
          for count in range(startrange):
              pygame.draw.rect(background, theSnow[count].color, theSnow[count].rect)
      
          screen.blit(background, (0, 0))
          pygame.display.flip()
      
      
      if __name__ == "__main__":
          init()
          run()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多