TypeError: unsupported operand type(s) for -:

TypeError: unsupported operand type(s) for -:

"""飞机的基础类
我方的飞机
敌方的飞机,包含大中小三种类型
"""
import pygame
import constants
from game.bullet import Bullet


class Plane(pygame.sprite.Sprite):
   """
   飞机的基础类
   """
   # 用来保存飞机的图片
   plane_images = []
   # 保存飞机爆炸的图片
   destroy_images = []
   # 坠毁的音乐地址
   down_sound_src = None
   # 飞机的状态: True--活的,False--死的
   active = True
   # 该飞机发射的子弹精灵组
   bullets = pygame.sprite.Group()

   def __init__(self, screen, speed=None):
       super().__init__()
       self.screen = screen
       # 加载静态资源
       self.img_list = []
       self._destroy_img_list = []
       self.down_sound = None
       self.load_src()

       # 飞机飞行的速度
       self.speed = speed or 10
       # 获取飞机的位置
       self.rect = self.img_list[0].get_rect()

       # 飞机的高、宽度
       self.plane_w = self.img_list[0].get_rect()
       self.plane_h = self.img_list[0].get_rect()

       # 游戏窗口的宽度和高度
       self.width = self.screen.get_size()
       self.height = self.screen.get_size()

       # 改变飞机的初始化位置,放在屏幕下方
       self.rect.left = int((self.width - self.plane_w) / 2)
       self.rect.top = int(self.height / 2)

   def load_src(self):
       """ 加载静态资源 """
       # 飞机图像
       for img in self.plane_images:
           self.img_list.append(pygame.image.load(img))
       # 飞机坠毁的图像
       for img in self.destroy_images:
           self._destroy_img_list.append(pygame.image.load(img))
       # 坠毁的音乐
       if self.down_sound_src is True:
           self.down_sound = pygame.mixer.Sound(self.down_sound_src)

   @property
   def image(self):
       return self.img_list[0]

   def blit_me(self):
       self.screen.blit(self.image, self.rect)

   def move_up(self):
       """ 飞机向上移动 """
       self.rect.top -= self.speed

   def move_down(self):
       """ 飞机向下移动 """
       self.rect.top += self.speed

   def move_left(self):
       """ 向左 """
       self.rect.left -= self.speed

   def move_right(self):
       """ 向右 """
       self.rect.left += self.speed

   def broken_down(self):
       """ 飞机坠毁效果 """
       # 播放坠毁音乐
       if self.down_sound:
           self.down_sound.play()
       # 播放坠毁的动画
       for img in self._destroy_img_list:
           self.screen.blit(img, self.rect)
       # 坠毁后
       self.active = False

   def shoot(self):
       """ 飞机发射子弹 """
       bullet = Bullet(self.screen, self, 20)
       self.bullets.add(bullet)


class OurPlane(Plane):
   """ 我方的飞机 """
   # 用来保存飞机的图片
   plane_images = constants.OUR_PLANE_IMG_LIST
   # 保存飞机爆炸的图片
   destroy_images = constants.OUR_DESTROY_IMG_LIST
   # 坠毁的音乐地址
   down_sound_src = None

   def update(self, frame):
       """ 更新飞机的动态内容 """
       if frame % 5:
           self.screen.blit(self.img_list[0], self.rect)
       else:
           self.screen.blit(self.img_list[1], self.rect)

   def move_up(self):
       """ 向上移动超出范围之后拉回来 """
       super().move_up()
       if self.rect.top <= 0:
           self.rect.top = 0

   def move_down(self):
       """ 向下移动超出范围之后拉回来 """
       super().move_down()
       if self.rect.top >= self.height - self.plane_h:
           self.rect.top = self.height - self.plane_h

   def move_left(self):
       super().move_left()
       if self.rect.left <= 0:
           self.rect.left = 0

   def move_right(self):
       super().move_right()
       if self.rect.left >= self.width - self.plane_w:
           self.rect.left = self.width - self.plane_w


运行的时候出现这样的错误:

Traceback (most recent call last):

  File "/Library/WebServer/Documents/python/code/imooc/Regular expression/Achieve Airplane Wars/mian.py", line 104, in <module>

    main()

  File "/Library/WebServer/Documents/python/code/imooc/Regular expression/Achieve Airplane Wars/mian.py", line 42, in main

    our_plane = OurPlane(screen)

  File "/Library/WebServer/Documents/python/code/imooc/Regular expression/Achieve Airplane Wars/game/plane.py", line 48, in __init__

    self.rect.left = int((self.width - self.plane_w) / 2)

TypeError: unsupported operand type(s) for -: 'tuple' and 'pygame.Rect'


Process finished with exit code 1


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学,你好。

1、在得到飞机的高和宽时应使用get_size(),并使用下标得到对应的宽和高进行赋值

http://img1.sycdn.imooc.com//climg/5ed0c87f09371bdb06570168.jpg

2、游戏窗口的宽和高使用下标得到对应的值

http://img1.sycdn.imooc.com//climg/5ed0c8ad09beb51c05830153.jpg


如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
1.Python零基础入门
  • 参与学习           人
  • 提交作业       2727    份
  • 解答问题       8160    个

想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师