Python TypeError:一元操作数类型错误-:“tuple”

2024-06-28 20:54:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个俄罗斯方块游戏,但我不明白这个错误? 好像是第34行:

self.active_blk.move(-direction)

这是我的代码:

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)

Tags: importselfsizemoveifdefcellrandom
1条回答
网友
1楼 · 发布于 2024-06-28 20:54:18

您的direction参数不是可以求反的数字。而是两个数字的二元组。元组不是数字类型,因此即使其内容可以取反,元组本身也不能。你需要自己用(-direction[0], -direction[1])来否定这些片段。

相关问题 更多 >