如何在Pygame中实现无精灵跳跃?

问题描述:

我是新来的编程和Python以及Pygame。就这点而言,我对Pygame中的精灵并不舒服。我试图制作一个游戏,在空格键被按下时跳转块 - 类似于Mario。如何在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() 

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

我做你的代码的最小变化来获得该块反弹:

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坐标,以便我们可以在旧位置上绘制一个黑色正方形。我也可以通过按退出键退出程序。

我从头开始做这个,我希望这不是太令人恐惧!

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

希望它有帮助!