简体   繁体   English

为什么我的播放器不会在python中移动

[英]why wont my player move in python

My player won't move and I don't understand why. 我的播放器不会移动,我也不知道为什么。 I followed a tutorial on how to make it move. 我遵循了有关如何使其移动的教程。 I did what the person did but it doesn't move for me. 我做了那个人所做的事情,但对我来说并没有动。

Here is my code: 这是我的代码:

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

You have a nesting issue in your gameloop. 您的游戏循环中存在嵌套问题。 The checks for the event actions need to be inside the for loop, so it checks each event during the frame. 对事件操作的检查需要在for循环内,因此它将检查框架中的每个事件。 It is only checking the last event as it currently is coded. 它仅检查当前已编码的最后一个事件。

Indent your event checks like so: 缩进事件检查,如下所示:

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

Edit: 编辑:

In addition to the above, the movex variable is not being used, and, indeed, your player update function has no means of horizontal movement in it. 除上述内容外,未使用movex变量,并且实际上,您的播放器更新功能中没有水平移动的手段。

You'll need to make a few update to your Player class: 您需要对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.

Then, in your game loop, update the player's xVelocity according to the input: 然后,在游戏循环中,根据输入更新玩家的xVelocity:

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

That should help. 那应该有帮助。

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

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