我尝试从“ python崩溃过程”(外来入侵)中执行第一个项目的代码,但是当我尝试执行文件时,它会先打开然后立即关闭。我该如何解决?
alien_invasion.py
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# Initialize pygame, settings, and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# make a ship
ship = Ship(screen)
# Start the main loop for the game.
while True:
gf.check_events(ship)
ship.update()
gf.update_screen(ai_settings, screen, ship)
run_game()
game_functions.py
import sys
import pygame
def check_events(ship):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left == False
def update_screen(ai_settings, screen, ship):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_color)
ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
ship.py
import pygame
class Ship():
def __init__(self, screen):
"""Initialize the ship and set its starting position."""
self.screen = screen
# Load the ship image and get its rect,
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get.rect()
self.screen_rect = screen.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# Movement flag
self.moving_right = False
self.moving_left = False
def update(self):
"""Update the ship's position based on the movement flag."""
if self.moving_right:
self.rect.centerx += 1
if self.moving_left:
self.rect.centerx -= 1
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
settings.py
class Settings():
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
VS代码给我这些错误:
在Alien_invasion.py中:
Module 'pygame' has no 'init' member
在game_functions.py中:
Module 'pygame' has no 'QUIT' member
Module 'pygame' has no 'KEYDOWN' member
Module 'pygame' has no 'K_RIGHT' member
Module 'pygame' has no 'K_LEFT' member
Module 'pygame' has no 'KEYUP' member
Module 'pygame' has no 'K_RIGHT' member
Module 'pygame' has no 'K_LEFT' member