当我使用方法时,我得到一个类型错误:“pygame”。表面的物体是不可调用的,

时间:2020-12-09 22:52:24

When I use the player method, it gives me am error:

当我使用玩家方法时,它会给我一个错误:

TypeError: 'pygame.Surface' object is not callable".

TypeError:“pygame。Surface的对象是不可调用的。

However, I don't have any problems when I use the same thing but then without using the method (see the row above). I don't know why. I am a newbie and could use some help.

但是,当我使用相同的东西时,我没有任何问题,但是在不使用方法的情况下(见上面的行)。我不知道为什么。我是新手,需要一些帮助。

Here is the whole code:

这是整个代码:

import pygame
from pygame.locals import *
from sys import exit

class Plane(object):
    def __init__(self,screen_temp):
        self.image = pygame.image.load('resources/image/shoot.png')
        self.rect = pygame.Rect(0,99,102,126)
        self.player = self.image.subsurface(self.rect)

        self.player_pos = [200,600]
        self.screen = screen_temp

    def player(self):
        self.screen.blit(self.player,self.player_pos)

    def key_press(self):
        key_press = pygame.key.get_pressed()
        if key_press[K_UP]:
            self.player_pos[1]-=5
        if key_press[K_DOWN]:
            self.player_pos[1]+=5
        if key_press[K_LEFT]:
            self.player_pos[0]-=5
        if key_press[K_RIGHT]:
            self.player_pos[0]+=5 


def main():
    screen = pygame.display.set_mode((480,800))
    pygame.display.set_caption("飞机大战") 

    background = pygame.image.load('resources/image/background.png')

    plane = Plane(screen)

    while True:
        screen.blit(background,(0,0))
        #plane.screen.blit(plane.player,plane.player_pos)  this can work
        plane.player() # this will get a TypeError

        pygame.display.update()
        plane.key_press()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

if __name__ == "__main__":
    main()

1 个解决方案

#1


0  

You are defining Plane.player twice:

你是定义平面。球员两次:

  • as a attribute self.player = self.image.subsurface(self.rect)
  • 自我作为一个属性。球员= self.image.subsurface(self.rect)
  • as a method def player(self):
  • 作为一种方法def player(self):

Those two are colliding and can be fixed by renaming one of them.

这两个是碰撞的,可以通过重命名一个来固定。

#1


0  

You are defining Plane.player twice:

你是定义平面。球员两次:

  • as a attribute self.player = self.image.subsurface(self.rect)
  • 自我作为一个属性。球员= self.image.subsurface(self.rect)
  • as a method def player(self):
  • 作为一种方法def player(self):

Those two are colliding and can be fixed by renaming one of them.

这两个是碰撞的,可以通过重命名一个来固定。