2015-11-07 54 views
0

有人可以幫我找出錯誤嗎?我不知道我做錯了什麼。有什麼不對? TypeError:__init __()需要3個參數(給出2個)

只有當我進入main.py並嘗試運行遊戲時纔會出現此錯誤。

主類:

import pygame 
from pygame.locals import * 
from player import * 
from blocks import * 

gravity = -4 

display_height, display_width = 640, 480 
display = pygame.display.set_mode ((display_height, display_width), 0, 16) 
pygame.display.set_caption("Pixie from Outerspace") 
clock = pygame.time.Clock() 
FPS = 30 

player = Player(display_height/2) 

levelOne = [ 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 1, 1, 1, 1, 1, 1] 
] 

blockList = [] 

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

moveX, moveY = 0, 0 

running = True 

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

       if (event.type == pygame.KEYDOWN): 
        if (event.key == pygame.K_RIGHT): 
         moveX = 3 
        elif (event.key == pygame.K_LEFT): 
         moveX = -3 

        elif (event.key == pygame.K_d): 
         moveX = 10 

        elif (event.key == pygame.K_UP): 
         player.jump() 

       if (event.type == pygame.KEYUP): 
        if (event.key == pygame.K_RIGHT): 
         moveX = 0 
        elif (event.key == pygame.K_LEFT): 
         moveX = 0 

        elif (event.key == pygame.K_d): 
         moveX = 0 


     display.fill(pygame.Color("lightblue3")) 

     for block in blockList: 
      block.render(display) 

     player.x += moveX 
     player.y -= moveY 

     player.update(gravity, blockList) 
     clock.tick(FPS) 
     pygame.display.flip() 

pygame.quit() 

Player類:

import pygame 

class Player(object): 

    def __init__(self, x, y): 
     self.x = x 
     self.y = y 
     self.width = 60 
     self.height = 56 
     self.velocity = 0 
     self.falling = True 
     self.onGround = False 
     self.pixie0 = pygame.image.load("pixie1.png") 
     self.pixie1 = pygame.image.load("pixie2.png") 
     self.timeTarget = 10 
     self.timeNumber = 0 
     self.currentImage = 0 
     self.cloud = pygame.image.load("cloud.png") 
     self.tree = pygame.image.load("tree.png") 
     self.bush = pygame.image.load("bush.png") 


    def jump(self): 
     if (self.onGround == False): 
      return 
     self.velocity = 30 
     self.onGround = False 


    def detectCollision(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.currentImage == 0): 
      self.currentImage += 1 
     else: 
      self.currentImage == 0 

     self.render() 


     if (self.velocity < 0): 
      self.falling = True 

     collision = False 

     blockX, blockY = 0, 0 

     for block in blockList: 
      collision = self.detectCollision(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): 
     display.blit (self.bush, (270, 367)) 
     display.blit (self.bush, (440, 367)) 
     display.blit (self.tree, (70, 232)) 
     display.blit (self.cloud, (150, 100)) 
     display.blit (self.cloud, (350, 50)) 
     display.blit (self.cloud, (450, 150)) 
     display.blit (self.cloud, (50, 50)) 

     if (self.currentImage == 0): 
      display.blit(self.pixie0, (self.x, self.y)) 
     else: 
      display.blit(self.pixie1, (self.x, self.y)) 
+0

你說'Player'需要'x'和'y',那麼你試着用一個數字來創建它。你期望什麼? – TigerhawkT3

回答

0

的這個錯誤告訴你到底是怎麼回事。 Player類在實例化時會期待兩個參數(以及自動傳遞的「self」):xy。但是你只是用display_height/2來實例化它,這只是一個參數。

您不應該發佈所有不相關的代碼,而應該查看回溯事件,告訴您哪一行發生了錯誤,如果您無法弄清楚發生了什麼,您應該只發布相關內容部分。

0

球員類的構造函數,它需要3個參數明確,一個是自己和另外兩個,但在初始化類的對象,您需要提供參數y太

你只提供一個參數
相關問題