继续在第27行出现错误:TypeError:-:'int'和'builtin_function_or_method'不受支持的操作数类型

时间:2019-06-18 18:55:41

标签: python

import pygame, sys

clock = pygame.time.Clock()

from pygame.locals import *
pygame.init()

pygame.display.set_caption('Platformer')

WINDOW_SIZE = (400, 400)

screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)

player_image = pygame.image.load('player.png')

moving_right = False
moving_left = False

player_location = [50, 50]
player_y_momentum = 0

while True:
    screen.fill((146, 200, 255))

    screen.blit(player_image, player_location)

    if player_location[1] > WINDOW_SIZE[1]-player_image.get_height: #line 27
        player_y_momentum = -player_y_momentum
    else:
        player_y_momentum += 0.2
    player_location[1] += player_y_momentum


    if moving_right == True:
        player_location[0] += 4
    if moving_left == True:
        player_location[0] -= 4

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False

    pygame.display.update()
    clock.tick(60)

2 个答案:

答案 0 :(得分:1)

我从未使用过pygame,但我认为您可以通过将第27行更改为:

if player_location[1] > WINDOW_SIZE[1]-player_image.get_height(): #line 27

我假设get_height()是一个函数,而不是player_image对象的元素,因此应该附上括号。

此外,由于您在此处执行减法运算,因此两个元素都应为数字。由于

player_image.get_height

不会返回数值,您将得到不支持的操作数类型错误。

答案 1 :(得分:1)

将此player_image.get_height更改为player_image.get_height()。您指的是函数,而不是获取函数的返回值。