简体   繁体   English

Pygame卡在简单的跳跃

[英]Pygame stuck at simple jumping

I really tried to google everywhere and wound a lot of solutions, but I just don't understand how to make them work with my code.我真的试图到处搜索并找到很多解决方案,但我只是不明白如何让它们与我的代码一起使用。 I am trying to make a simple jump here, but after pressing UP key player gets stuck in vel_y position.我想在这里做一个简单的跳转,但是在按下 UP 键后,玩家会卡在 vel_y position 中。 Before this I tried to make player always go down but only when jumpin, but it did't work.在此之前,我试图让玩家总是 go 下降,但只有在跳跃时,但它没有用。 I am trying to build a simple jumping game.我正在尝试构建一个简单的跳跃游戏。

import pygame
import sys
from pygame.locals import *
pygame.init()


def game_settings():    
     width = 1280
     height = 800
     black = (0,0,0)
     red = (255,0,0)
     green = (0,255,0)
     blue = (0,0,255)
     gray = (127,127,127)

     base = pygame.display.set_mode((width,height))
     pygame.display.set_caption("F2 game")
     level = pygame.image.load("lvl1.png").convert()

     character = pygame.image.load("sprite.png").convert_alpha()

     floor = pygame.Surface((1280,50))
     floor.fill(gray)
     base.blit(floor, (0,200))



     pygame.display.flip()

     floorArea = floor.get_rect()
     floorArea.left = 0
     floorArea.top = 420

     characterArea = character.get_rect()
     characterArea.left = 0
     characterArea.top = 238


     vel_x = 1
     vel_y = 228

     jump = False

     while True:


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:     
            if event.key == K_ESCAPE: 
                pygame.quit() 
                sys.exit()

    userInput = pygame.key.get_pressed()
    if userInput[K_LEFT]:       
        characterArea.left -= vel_x
    if userInput[K_RIGHT]:
        characterArea.left += vel_x

        
    if characterArea.left <= 0:
        characterArea.left = 0
    elif characterArea.left >= 1207:
        characterArea.left = 1207


    if jump is False and userInput[K_UP]:
        jump = True
        characterArea.top = vel_y
        vel_y -= 1

        if vel_y < -10:
            characterArea.top = 238
            vel_y = 228


    
    pygame.time.delay(2)

base.blit(level, (0,0))
base.blit(character, characterArea)
base.blit(floor, floorArea)
pygame.display.flip()

game_settings()

You make jump = True , but you're missing the code to make jump = False when your character lands again:您使jump = True ,但是当您的角色再次着陆时,您缺少使jump = False的代码:

    if not jump and userInput[K_UP]:
        # start jumping
        jump = True
        vel_y = JUMP_START_VELOCITY

    if jump: # same thing as `if jump == True`
        # continue flying through the air
        vel_y -= JUMP_GRAVITY_INCREMENT

    if has_landed(): # up to you how to check this
        jump = False
        vel_y = 0

However you might want to look at the Game Programming Patterns chapter on State before going too far down this path!但是,在走得太远之前,您可能想查看 State 上的游戏编程模式一章! https://gameprogrammingpatterns.com/state.html https://gameprogrammingpatterns.com/state.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM