Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

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('imagesship.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...