Ошибка, в которой говорится, что я не могу вызвать объект кортежа. - PullRequest
1 голос
/ 25 мая 2020

Я следил за технологиями с видео Тима о боковом скроллере. Я следил за его уроками, но есть одно место, где отображается эта ошибка: TypeError: объект 'tuple' не вызывается, что странно, потому что этот формат кода вызывается несколько раз раньше и не вызывает никаких ошибок

'' '

import pygame
from pygame.locals import *
import os
import math
import sys
import random

pygame.init()
window = pygame.display.set_mode((800, 447))
pygame.display.set_caption('Side Scroller')
bg = pygame.image.load('images/bg.png').convert()
bgX = 0
bgX2 = bg.get_width() 

clock = pygame.time.Clock()

window.blit(bg, (0, 0))
pygame.display.update()


class player():
    run = [pygame.image.load(os.path.join('images', str(x) + '.png')) for x in range(8, 16)]
    jump = [pygame.image.load(os.path.join('images', str(x) + '.png')) for x in range(2, 8)]
    slide = [pygame.image.load(os.path.join('images', 'S1.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S3.png')), pygame.image.load(os.path.join('images', 'S4.png')), pygame.image.load(os.path.join('images', 'S5.png'))]
    jumpList = [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1,
                -1, -1, -1, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
                -3, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4]
    fall = [pygame.image.load(os.path.join('images', '0.png'))]

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.runCount = 0
        self.jumpCount = 0
        self.slideCount = 0
        self.jumping = False
        self.sliding = False
        self.slideUp = False
        self.falling = False

    def draw(self, window):
        if self.jumping:
            self.y -= self.jumpList[self.jumpCount] * 1.5
            window.blit(self.jump[round(self.jumpCount // 18)], (self.x, round(self.y)))
            self.jumpCount += 1
            if self.jumpCount > 100:
                self.jumpCount = 0
                self.runCount = 0
                self.jumping = False
            self.hitbox = (self.x + 4, self.y, self.width - 24, self.height - 10)
        elif self.sliding or self.slideUp:
            if self.slideCount < 20:
                self.y += 1
            elif self.slideCount == 80:
                self.y -= 19
                self.sliding = False
                self.slideUp = True

            elif self.slideCount > 20 and self.sliding < 80:

Очевидно, ошибка происходит из этого раздела: я не уверен, почему я не могу вызвать self.hitbox

          self.hitbox(self.x, self.y + 3, self.width - 8, self.height - 35)

            if self.slideCount >= 110:
                self.slideCount = 0
                self.slideUp = False
                self.runCount = 0
                self.hitbox = (self.x + 4, self.y, self.width - 24, self.height - 10)
            window.blit(self.slide[(self.slideCount // 10)], (round(self.x), round(self.y)))
            self.slideCount += 1

        elif self.falling:
            window.blit(self.fall, (self.x, self.y + 30, self.width, self.height))
        else:
            if self.runCount > 42:
                self.runCount = 0

            window.blit(self.run[(self.runCount // 6)], (round(self.x), round(self.y)))
            self.runCount += 1
            self.hitbox = (self.x + 4, self.y, self.width - 24, self.height - 10)
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)




class saw():
    img = [pygame.image.load(os.path.join('images', 'SAW' + str(x) + '.png')) for x in range(0, 4)]

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.hitbox = (x, y, width, height)
        self.count = 0

    def draw(self, window):
        self.htibox = (self.x + 5, self.y + 5, self.width - 10, self.height - 5)
        if self.count >= 8:
            self.count = 0
        window.blit(pygame.transform.scale(self.img[round(self.count//2)], (64, 64)), (self.x, self.y))
        self.count += 1
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)


class spike(saw):
    img = pygame.image.load('images/spike.png')

    def draw(self, window):
        self.hitbox = (self.x + 10, self.y, 28, 315)
        window.blit(self.img, (self.x, self.y))
        pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)


def redrawWindow():
    window.blit(bg, (bgX, 0))
    window.blit(bg, (bgX2, 0))
    runner.draw(window)
    for objectt in objects:
        objectt.draw(window)
    pygame.display.update()


objects = []

runner = player(200, 313, 64, 64)
pygame.time.set_timer(USEREVENT + 1, 500)
pygame.time.set_timer(USEREVENT + 2, random.randrange(2000, 3500))
speed = 30
run = True

while run:
    clock.tick(speed)

    redrawWindow()

    for objectt in objects:
        objectt.x -= 2
        if objectt.x < objectt.width * -1:
            objects.pop(objects.index(objectt))

    bgX -= 2
    bgX2 -= 2
    if bgX < bg.get_width() * -1:
        bgX = bg.get_width()
    if bgX2 < bg.get_width() * -1:
        bgX2 = bg.get_width()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()
        if event.type == USEREVENT + 1:
            speed += 1

        if event.type == USEREVENT + 2:
            r = random.randrange(0, 2)
            if r == 0:
                objects.append(saw(810, 310, 64, 64))
            else:
                objects.append(spike(810, 0, 48, 320))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE] or keys[pygame.K_UP]:
        if not runner.jumping:
            runner.jumping = True

    if keys[pygame.K_DOWN]:
        if not runner.sliding:
            runner.sliding = True

1 Ответ

2 голосов
/ 25 мая 2020

Предположительно, именно эта строка выдает ошибку:

self.hitbox(self.x, self.y + 3, self.width - 8, self.height - 35)

Когда у вас есть идентификатор, за которым следует (*args), это означает, что вы вызываете его как функцию. self.hitbox - это не функция, это кортеж.

Вы хотели ввести это вместо этого?

self.hitbox = (self.x, self.y + 3, self.width - 8, self.height - 35)
...