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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…