当前位置: 首页 > 知识库问答 >
问题:

玩家不会倒下

乐正远
2023-03-14

问题是:

所以我在制作这个2D平台游戏时遇到了一个问题。当玩家跳到一个平台上并离开它时,重力不会影响它,它不会掉下来,直到你再次按下跳跃键,就好像它认为它仍然在地面上,直到你更新他。我已经把问题缩小到重力或碰撞,但找不到问题。有人能帮忙吗?我在下面附上代码。

代码:

import pygame, sys, time, random, math
from pygame.locals import *

BACKGROUNDCOLOR = (255, 255, 255)

WINDOWW = 800 
WINDOWH = 600
PLAYERW = 66
PLAYERH = 22
FPS = 60
MOVESPEED = 3
YACCEL = 0.13
GRAVITY = 2
BLOCKSIZE = 30

pygame.init()
screen = pygame.display.set_mode((WINDOWW, WINDOWH), 0, 32)
mainClock = pygame.time.Clock()

testLevel = [
            (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,),
            (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,)]

def createblock(length, height, color):
    tmpblock = pygame.Surface((length, height))
    tmpblock.fill(color)
    tmpblock.convert()
    return tmpblock

def terminate(): # Used to shut down the software
    pygame.quit()
    sys.exit()

def add_level(lvl, bSize): # Creates the level based on a map (lvl) and the size of blocks
    bList = [] # List of every block
    bListDisp = [] # List of every block to display    
    bTypeList = [] # List with corresponding type of block(wall, air, etc.)

    for y in range(len(lvl)): 
        for x in range(len(lvl[0])):

            if lvl[y][x] == 0: # If the block type on lvl[y][x] is '0', write "air" down in the type list
                bTypeList.append("air")
            elif lvl[y][x] == 1: # If the block type on lvl[y][x] is '1', write "wall" down in the type list
                bTypeList.append("solid")

            bList.append(pygame.Rect((bSize * x), (bSize * y), bSize, bSize)) #Append every block that is registered
            bListDisp.append(pygame.Rect((bSize * x), (bSize * y), bSize, bSize)) #Append every block to display that is registered

    return bList, bListDisp, bTypeList

player = pygame.Rect((WINDOWW/2), (WINDOWH - BLOCKSIZE*3), PLAYERW, PLAYERH)
wallblock = createblock(BLOCKSIZE, BLOCKSIZE,(20,0,50))

lastTime = pygame.time.get_ticks()
isGrounded = False

vx = 0
vy = 0

allLevels = [testLevel] # A list containing all lvls(only one for now)
maxLevel = len(allLevels) # Checks which level is the last
currLevel = allLevels[0] # Current level(start with the first lvl)
blockList, blockListDisp, blockTypeList = add_level(currLevel, BLOCKSIZE) # A list with every block and another list with the blocks types

thrusters = True
jumping = False
falling = True
while True:
    """COLLISION"""
    for i in range(len(blockTypeList)): # Go through every block...
        if blockTypeList[i] == "solid": # ...and check what kind of block it is
            if player.colliderect(blockList[i]): #Apply necessary influences from the block(e.g. a solid block prevents the player from moving into it)
                if vx > 0 and not falling:
                    player.right = blockListDisp[i].left
                    print 'Collide Right'
                if vx < 0 and not falling:
                    player.left = blockListDisp[i].right
                    print 'Collide Left'
                if vy > 0:
                    player.bottom = blockListDisp[i].top
                    isGrounded = True
                    falling = False
                    vy = 0
                    print 'Collide Bottom'
                if vy < 0:
                    player.top = blockListDisp[i].bottom
                    print 'Collide Top'
    # Input
    pressedKeys = pygame.key.get_pressed() # Checks which keys are being pressed
    timeDiff = pygame.time.get_ticks() - lastTime # Calculates time difference 
    lastTime +=  timeDiff # Last time checked reset to current time

    # Shut-down if the ESC-key is pressed or the window is "crossed down"
    for event in pygame.event.get():
        if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
            terminate()    

    """X-axis control"""
    if pressedKeys[ord('a')]:
        vx = -MOVESPEED
    if pressedKeys[ord('d')]:
        vx = MOVESPEED
    if not pressedKeys[ord('d')] and not pressedKeys[ord('a')]:
        vx = 0

    """Y-axis control"""
    # Controls for jumping
    if pressedKeys[ord('w')] and thrusters == True:
            vy -= YACCEL * timeDiff; # Accelerate along the y-xis when "jumping", but not above/below max speed
            if vy <= -4:
                vy = -4
            isGrounded = False # You are airborne
            jumping = True # You are jumping

    if event.type == KEYUP: # If you let go of the "jump"-button, stop jumping
        if event.key == ord('w') and vy < 0 and not isGrounded:
            jumping = False
            falling = True

    player.x += vx
    player.y += vy

    # Gravity
    if not isGrounded or falling:
        vy += 0.3
        if vy > 80:
            vy = 80

    screen.fill(BACKGROUNDCOLOR)

    for i in range(len(blockTypeList)):
        if blockTypeList[i] == "solid":
            screen.blit(wallblock, (blockListDisp[i].x, blockListDisp[i].y)) #blit the wall-block graphics

    pygame.draw.rect(screen, (0, 0, 0), player)

    pygame.display.update()
    mainClock.tick(FPS)

共有1个答案

国斌斌
2023-03-14

一份清单,可能会对您有所帮助。

创建自我.最小_高度:

  • min_height初始化为可能的最低级别(例如,楼层)
  • 如果玩家没有与任何东西碰撞,那么min_height仍然是地板
  • 如果玩家与平台碰撞,那么他的min_height就变成了平台的min_height
  • 如果distance_between(min_height,current_y)接近JUMP_SENSITIVITY,玩家可以跳跃(你可能需要一种方法来确定他是否会很快相撞,但留待以后再说)
  • 只要未达到min_height,玩家就会继续下降。

编辑:这可能会帮助你摆脱跳跃/下降的旗帜

 类似资料:
  • 刚开始使用pygame,当你按住箭头键时,试图让一个简单的点在屏幕上移动。目前,它只在你按键时移动,但你必须重复按键。 此外,我很感激你对我目前的代码有任何建议,可以改进或改变,使之更有效。

  • 你好,我目前正在开发一款生存射击游戏,我目前对一些事情感到沮丧。一个是我的游戏动画不起作用。我这里有播放器类的代码: 如果您想了解我的任何其他问题/挫折,请说出来。任何帮助将不胜感激。谢谢。

  • 我正在编程我的第一个实际2D游戏(吃豆人)。我认为比赛看起来不错,但我有一个大问题--碰撞。对象仍然存在,我被困住了,所以我决定寻求有丰富经验的真正程序员的帮助。(我当然做了一些研究,但我不想做复制粘贴之类的事情,因为我没搞懂)。就像我说的,游戏差不多完成了,我所需要做的就是让帕克曼继续前进。像例子一样,我画大的白色矩形作为平台。我希望有人能帮助我。在这个项目中,我学到了很多东西,碰撞是我理解的东

  • 对于一个Minecraft项目,我想让玩家逐渐面对(0,60,0)。到目前为止,当玩家在(0,60,0)周围移动超过720°时,我尝试的一切似乎都失败了。 有人知道如何让相机无缝移动到(0,60,0)吗? 非常感谢。 这是我到目前为止的代码(切换时循环运行): 这段没有if语句的代码可以正常工作。偏航和俯仰变量以度为单位。 我遇到的问题是,每当我转过身(0,60,0)几次,屏幕就会突然360°旋转

  • 我知道你可以得到损坏的原因 在实体损坏事件中,但是我还没有找到返回造成损坏的实体的方法。我需要找出这一点,这样我就可以检查玩家的库存中是否有某个物品。

  • 我在使用我创建的bot为服务器中的不协调用户分配角色时遇到问题。 我编写的代码是用于与FaceIT交互的,它工作得很好,但我希望能够根据一个用户进行了多少次匹配来分配角色。 通过我的代码,我知道discord用户的ID,因为它们存储在配置文件中,配置被加载到一个名为 运行代码时,出现以下错误: 不一致外部命令。错误。CommandInvokeError:命令引发异常:AttributeError: