Python Typeerror: Bad Operand Type for Unary -: 'Tuple'

Python Typeerror: Bad Operand Type for Unary -: 'Tuple'

I am trying to make a tetris game, but I don't understand this error? It seems to be line 34:

self.active_blk.move(-direction)

Here is my code:

import pygame
import random
from Block import Block

class Stage():
def __init__(self,cell_size,h_cells,v_cells):
    self.cell_size=cell_size
    self.width=h_cells
    self.height=v_cells
    self.blocks=[]
    self.active_blk=self.add_block()

def add_block(self):
    blk=Block(0,self.cell_size,(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
    self.blocks.append(blk)
    return blk

def move_block(self,direction):
    self.active_blk.move(direction)

    obstacle=False
    for cell in self.active_blk.cells:
        if(cell.y>=self.height or 
           cell.x<0 or 
           cell.x>= self.width): obstacle=True

    for blk in self.blocks:
        if(blk is self.active_blk): continue
        if(blk.collide_with(self.active_blk)):
            obstacle=True
            break;

    if(obstacle):
        self.active_blk.move(-direction)

def draw(self,screen):
    screen.fill((0,0,0))  
    for blk in self.blocks:
        blk.draw(screen)
3

1 Answer

Your direction argument is not a number than can be negated. Rather, it's a tuple of two numbers. Tuples are not numeric types, so even though its contents can be negated, the tuple itself cannot be. You need to negate the pieces yourself, with (-direction[0], -direction[1]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.