2017-06-18 1 views
0

Ich versuche, ein Tetris-Spiel zu machen, aber ich verstehe diesen Fehler nicht? Es scheint, Linie 34 zu sein:Python TypeError: schlechter Operandentyp für unary -: 'Tuple'

self.active_blk.move(-direction) 

Hier ist mein 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) 
+0

Wo wird 'move_block' genannt? – Ryan

+0

'Richtung' ist anscheinend ein Tupel. Und du kannst nicht '- (x, y)' –

+0

'stage.move_block ((0,1))' – Coder22

Antwort

2

Ihr direction Argument ist keine Zahl als negiert werden. Es ist vielmehr ein Tupel von zwei Zahlen. Tupel sind keine numerischen Typen, so dass das Inhalt negiert werden kann, das Tupel selbst nicht sein kann. Sie müssen die Teile selbst negieren, mit (-direction[0], -direction[1]).

Verwandte Themen