【问题标题】:why wont my player move in python为什么我的玩家不能在 python 中移动
【发布时间】:2015-10-05 23:58:03
【问题描述】:

我的播放器不会移动,我不明白为什么。我遵循了有关如何使其移动的教程。我做了那个人做的事,但对我没有动静。

这是我的代码:

import pygame


pygame.init()


screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("use arows")





class player:

    def __init__(self ,x, y):

        self.x = x
        self.y = y
        self.width = 32
        self.height = 32
        self.velocity = 0
        self.falling = False
        self.onGround = False

    def detectCollisions(self,x1,y1,w1,h1,x2,y2,w2,h2):

        if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):

            return True

        elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):

            return True

        elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):

            return True

        elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):

            return True

        else:

            return False

    def update(self, gravity, blockList):
        if (self.velocity < 0):
            self.falling = True

        collision = False
        blockX,blockY =  0,0
        for block in blockList:

            collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height )
            if collision == True:
                blockx = block.x
                blocky = block.y
                break


        if(collision == True):
            if (self.falling == True):
                self.falling == False
                self.onGround== True
                self.velocity = 0
                self.y = blocky - self.height

        if (self.onGround == False):
            self.velocity += gravity
        self.y -= self.velocity

    def render(self,screen):
        pygame.draw.rect(screen,(0,0,0),(self.x, self.y, self.width, self.height))


class Block:
    def __init__ (self, x, y):
       self.x = x
       self.y = y
       self.width = 32
       self.height = 32

    def render(self,screen):
        pygame.draw.rect(screen,(0,0,0),(self.x, self.y, self.width, self.height))



gravity = -0.5

black = (0,0,0)
white = (255,255,255)
blue = (50,60,200)

clock = pygame.time.Clock()

player = player(0,0)

# 25 colums and 19 rows
level1 = [
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,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]
]

blockList = []

for y in range (0,len(level1)):
    for x in range (0,len(level1[y])):
        if (level1[y][x] == 1):
            blockList.append(Block(x*32, y*32))

movex,movey=0,0

gameloop = True
while gameloop:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameloop = False

    if(event.type == pygame.KEYDOWN):
        if (event.key == pygame.K_RIGHT):
            movex = 5

        elif (event.key == pygame.K_LEFT):
            movex = -5


    if(event.type == pygame.KEYUP):
        if (event.key == pygame.K_RIGHT):
            movex = 0

        elif (event.key == pygame.K_LEFT):
           movex = 0

    screen.fill(blue)

    for block in blockList:
        block.render(screen)

    player.update(gravity, blockList)
    player.render(screen)
    clock.tick(60)

    pygame.display.update()

pygame.quit()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您的游戏循环中有嵌套问题。对事件动作的检查需要在 for 循环内,因此它会在帧中检查每个事件。它只检查当前编码的最后一个事件。

    像这样缩进你的事件检查:

    gameloop = True
    while gameloop:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameloop = False
    
            if(event.type == pygame.KEYDOWN):
                if (event.key == pygame.K_RIGHT):
                    movex = 5
    
                elif (event.key == pygame.K_LEFT):
                    movex = -5
    
    
            if(event.type == pygame.KEYUP):
                if (event.key == pygame.K_RIGHT):
                    movex = 0
    
                elif (event.key == pygame.K_LEFT):
                    movex = 0
    

    编辑:

    除了上述之外,movex 变量没有被使用,事实上,你的播放器更新函数没有水平移动的方法。

    您需要对 Player 类进行一些更新:

    def __init__(self ,x, y):
    
        self.x = x
        self.y = y
        self.width = 32
        self.height = 32
        self.yVelocity = 0
        self.xVelocity = 0 #Add xVelocity variable to track horizontal speed.
        self.falling = False
        self.onGround = False
    
    
    def update(self, gravity, blockList):
        if (self.yVelocity < 0):
            self.falling = True
    
        collision = False
        blockX,blockY =  0,0
        for block in blockList:
    
            collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height )
            if collision == True:
                blockx = block.x
                blocky = block.y
                break
    
    
        if(collision == True):
            if (self.falling == True):
                self.falling == False
                self.onGround== True
                self.yVelocity = 0
                self.xVelocity = 0 #On a collision, stop all movement.
                self.y = blocky - self.height
    
        if (self.onGround == False):
            self.yVelocity += gravity
        self.y -= self.yVelocity
        self.x -= self.xVelocity #Modify position according to velocity.
    

    然后,在您的游戏循环中,根据输入更新玩家的 xVelocity:

    player.xVelocity = movex #Update horizontal velocity.
    player.update(gravity, blockList)
    player.render(screen)
    clock.tick(60)
    

    这应该会有所帮助。

    【讨论】:

    • 这似乎没有解决它。只是告诉你我做到了/看看我做得对吗prntscr.com/8o71ub
    • 如果这不起作用,那么肯定还有另一个问题。给我一秒钟再看一遍代码。
    • movex 实际上并没有被用于任何事情,因此修改它不会导致任何操作。您需要将播放器的水平速度设置为movex,但更新功能仅使用velocity,用于垂直运动。您需要向更新函数添加水平移动。
    • 好的,所以我添加了 player.x = move x 并且 move x = 0 但是现在当我移动时,它只移动了大约 1 个像素,然后它停止了,当我放开我的钥匙时(右键)它回到它开始的地方,所以我将播放器的起点更改为 400,0,它仍然从 0,0 开始,然后跌到底部并做了同样的事情
    • 在教程上他没有做任何他只是添加了 player.x = move.x
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    相关资源
    最近更新 更多