【问题标题】:How to implement jump in Pygame without sprites?如何在没有精灵的情况下在 Pygame 中实现跳转?
【发布时间】:2017-04-30 12:34:53
【问题描述】:

我是编程新手,不熟悉 Python 和 Pygame。因此,我还不习惯 Pygame 中的精灵。我正在尝试制作一个游戏,只要按下空格键就会跳块 - 类似于马里奥。

我的代码不能按预期工作,因为每当按下空格键时,块就会逐渐向上移动(我添加了一个重力组件),而不是“跳跃”。

import pygame

pygame.init()
game_display = pygame.display.set_mode((800, 800))


# fixed variables at the start
x_pos = 400
y_pos = 400
current_speed = 15

def jump_coords(y_position, speed):
    if speed >= 0:
        #to move up, reduce the y-coordinate
        y_position -= speed
    return y_position

game_exit = False

# main loop
while not game_exit: 
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                y_pos = jump_coords(y_pos, current_speed)
                # 1 represents gravity value
                current_speed -= 1

    rect_one = pygame.Rect(x_pos, y_pos, 10, 10)  
    pygame.draw.rect(game_display, (255, 0, 0), rect_one)
    pygame.display.update()

我知道我必须在speed >= 0 期间以某种方式使 y_pos 在 while 循环中不断更新,但我不知道如何实现它。

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    我对您的代码进行了最小的更改以使块反弹:

    import pygame
    
    pygame.init()
    game_display = pygame.display.set_mode((800, 800))
    
    # fixed variables at the start
    x_pos = 400
    y_pos = 400
    x_old = x_pos
    y_old = y_pos
    current_speed = 15
    
    def jump_coords(y_position, speed):
        # to move up, reduce the y-coordinate
        y_position -= speed
        if y_position > 400:
            y_position = 400
            global jump_flag
            jump_flag = False
            global current_speed
            current_speed = 15
        return y_position
    
    game_exit = False
    jump_flag = False
    
    # main loop
    while not game_exit:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    jump_flag = True
                elif event.key == pygame.K_ESCAPE:
                    exit(0)
    
        if jump_flag:
            x_old = x_pos
            y_old = y_pos
            y_pos = jump_coords(y_pos, current_speed)
            # 1 represents gravity value
            current_speed -= 1
    
        rect_old = pygame.Rect(x_old, y_old, 10, 10)
        pygame.draw.rect(game_display, (0, 0, 0), rect_old)
        rect_one = pygame.Rect(x_pos, y_pos, 10, 10)
        pygame.draw.rect(game_display, (255, 0, 0), rect_one)
        pygame.display.update()
    

    最重要的变化是删除了速度大于零的检查。如果块要回落,则速度必须为负。下一个更改是保存旧的 x 和 y 坐标,以便我们可以在旧位置上绘制一个黑色方块。我还可以通过按 Escape 键退出程序。

    【讨论】:

      【解决方案2】:

      这是我从头开始制作的,希望不会太令人生畏!

      import pygame,sys
      
      pygame.init()
      screen = pygame.display.set_mode((800, 800))
      
      tm = 20 # Terminal Velocity
      gravity = 1
      
      class Player:
          def __init__(self,speed,x,y):
              self.speed = speed
              self.x = x; self.y = y
              self.yVelocity = 0
              self.xVelocity = 0
          def getKeys(self):
              key = pygame.key.get_pressed()
      
              if key[pygame.K_a]: self.xVelocity -= self.speed
              if key[pygame.K_d]: self.xVelocity += self.speed
      
              if key[pygame.K_SPACE]:
                  if isGround(self.x,self.y):
                      self.yVelocity -= 20
      
          def move(self,dt):
              if self.x < 0:
                  self.x = 0
              if self.x > 800-15:
                  self.x = 800-15
              if self.y < 0:
                  self.y = 0
              if self.y > 800-10:
                  self.y = 800-10
              self.x += self.xVelocity
              self.y += self.yVelocity
              if self.xVelocity != 0:
                  self.xVelocity /= 70*dt
      
              if self.yVelocity < tm and not isBlocking(self.x,self.y+self.yVelocity):
                  self.yVelocity += gravity
      
              if isBlocking(self.x,self.y):
                  self.yVelocity = 0
      
          def draw(self):
              screen.fill((255,0,0),(self.x,self.y,10,10))
      
      def isBlocking(x,y):
          if x < 0 or x > 800 or y < 0 or y > 800:
              return True
          elif y >= 400:
              return True
          else:
              return False
      
      def isGround(x,y):
          if y >= 400:
              return True
          else:
              return False
      
      player = Player(1,400,400)
      
      clock = pygame.time.Clock()
      
      while True:
          dt = clock.tick(60)/1000 # limit to 60 FPS.
          screen.fill((0,0,0))
      
          if pygame.event.poll().type == pygame.QUIT: pygame.quit(); sys.exit()
      
          player.getKeys()
          player.move(dt)
          player.draw()
      
          pygame.display.flip()
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多