• 1
  • 新人请关照

can only concatenate tuple (not "int") to tuple 怎么这里就出现元组了?

Python编程 从入门到实践
中的实例,反复报错

ship.py

import pygame

class Ship():

    def __init__(self,ai_setting, screen):
        """初始化飞船并设定其位置"""
        self.screen = screen
        self.ai_setting = ai_setting
        """加载飞创并获取其外形"""
        self.image = pygame.image.load('images\ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        self.center = float(self.rect.centerx)
        self.moving_right = False
        self.moving_left = False
    def update(self):
        if self.moving_right:
            self.rect.center += self.ai_setting.ship_speed_factor
        if self.moving_left:
            self.rect.center -= self.ai_setting.ship_speed_factor
        self.rect.centerx = self.center
    def blitme(self):
        self.screen.blit(self.image, self.rect)

setting.py

class Setting():
    #存储《外星人入侵》的所有的设置的类

    def __init__(self):
        """初始化游戏的设置"""
        #屏幕的设置
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)
        self.ship_speed_factor = 1.5

报错信息
"C:Program FilesPython38python.exe" C:/Users/jesse/Documents/pygame/alien_invasion.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contri...
Traceback (most recent call last):
File "C:/Users/jesse/Documents/pygame/alien_invasion.py", line 19, in <module>

run_game()

File "C:/Users/jesse/Documents/pygame/alien_invasion.py", line 16, in run_game

ship.update()

File "C:UsersjesseDocumentspygameship.py", line 26, in update

self.rect.center += self.ai_setting.ship_speed_factor

TypeError: can only concatenate tuple (not "float") to tuple

Process finished with exit code 1

阅读 89
评论
    0 个回答
    撰写回答

    登录后参与交流、获取后续更新提醒