2015-11-07 53 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太

你只提供一个参数
相关问题