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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
self.rect.center这里错了,要更新的是飞船的center值,而不是rect值,写成self.center就可以了