2015-10-05 77 views
0

我的玩家不會移動,我不明白爲什麼。我跟着一篇關於如何讓它移動的教程。我做了這個人做的事情,但對我而言並不動。爲什麼我的玩家不會在python中移動

這裏是我的代碼:

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

回答

0

你在你的gameloop有一個嵌套的問題。對事件動作的檢查需要位於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變量沒有被使用,並且,的確,您的播放器更新功能沒有水平運動的手段。

你需要做一些更新您的播放器類:

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) 

應幫幫我。

+0

似乎沒有解決它。只是向你展示我做過/看看我是否做得對http://prntscr.com/8o71ub – GeeZillion

+0

如果這沒有奏效,還必須有另一個問題。讓我再看一遍代碼。 –

+0

'movex'實際上並沒有用於任何事情,所以修改它不會導致任何操作。您需要將播放器的水平速度設置爲「movex」,但更新功能只使用「速度」,這是垂直運動。您需要向更新功能添加水平移動。 –