书上的实例,照抄都出错了
新手正在照着《python编程 从入门到实践》上面的例子重复,但是到这里,怎么也进行不下去了。总是这样报错。
ship.py
import pygame
class Ship():
def __init__(self,screen):
"""初始化飞船并设定其位置"""
self.screen = screen
"""加载飞创并获取其外形"""
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.screen_rect = screen.get_rect()
def blitme(self):
self.screen.blit(self.image, self.rect)
另一个文件alien_invasion.py
import sysimport pygame
from setting import Setting
from ship import Ship
def run_game():
pygame.init()
ai_setting = Setting()
screen = pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height),0,0)
pygame.display.set_caption('Alien Invasion')
ship = Ship(screen)
bg_color = (230, 230, 230)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(ai_setting.bg_color)
ship.blitme()
pygame.display.flip()
run_game()
报错信息
Traceback (most recent call last):
File "C:/Users/jesse/Documents/pygame/alien_inversion.py", line 5, in <module>
from ship import Ship
File "C:UsersjesseDocumentspygameship.py", line 11
self.image = pygame.image.load(images\\ship.bmp) ^
SyntaxError: unexpected character after line continuation character
Process finished with exit code 1
回答:
self.image = pygame.image.load(images\ship.bmp)
你这一行,参数里面,地址是字符串,要用单引号或双引号括起来。
建议题主好好学完基础知识再上小项目。
以上是 书上的实例,照抄都出错了 的全部内容, 来源链接: utcz.com/p/937868.html