在网上查了下错误的原因,主要是说要补全路径或把bmp图片放在.py文件相同的目录下,我各种方式都试了,但还是报错...
以下是代码:
主程序:
import sys
import pygame
from settings import Settings
from ship import Ship
def run_game():
initialize game and create a window obj
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
creat a ship
ship = Ship(screen)
begin main cycle of game
while True:
monitor key and shubiao
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
draw a new screen while a new cycle begin
screen.fill(ai_settings.bg_color)
ship.blitme()
show the near screen
pygame.display.flip()
run_game()
模块1:ship.py
import pygame
class Ship():
def init(self,screen):
"""initialize ship and set location of ship"""
self.screen = screen
load image of ship and get the bounding box of the ship
self.image = pygame.image.load(r'images\ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
place each ship into center of bottom of screen
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
"""drawing the ship in the specific place"""
self.screen.blit(self.image,self.rect)