Pygame Python Cra sh Курс Alien Invaders стр. 246 Ошибка трассировки? - PullRequest
0 голосов
/ 30 марта 2020

Проблема с игрой Alien Invasion на стр. 246 из Python Cra sh Курс: Eri c Matthes? и у меня есть 3 ошибки отслеживания, хотя я и сделал все, что было указано в книге, прежде чем я начну кодировать маркеры, мне нужна помощь в получении, хотя ошибка приведена ниже. Я не могу заставить что-либо всплыть, и наконец-то я скачал Pygame после нескольких часов борьбы с этим. Любая помощь высоко ценится! Помогите мне понять, что я сделал не так.

Инопланетное вторжение

import sys

import pygame

from settings import Settings
from ship import Ship 

class AlienInvasion:
    """Overall class to manange game assests and behavior."""

    def __init__(self):
        """Initialize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        self.ship = Ship(self)

        #Set the background color.
        self.bg_color = (230, 230, 230)

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()
            self.ship.update()
            self._update_screen()
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # Redraw the screen during each pass through the loop.
            self.screen.fill(self.settings.bg_color)
            self.ship.blitme()

    def _check_events(self):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

                #Move th ship to the right
                self.ship.rect.x += 1  

    def _check_keydown_events(self, event):
        """Respond to keypresses."""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.k_q:
            sys.exit()        



    def _check_keyup_events(self, event):
        """Responds to releases.""" 
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False    



    def _update_screen(self):
        """Update images on the screen, and flip to the new screen."""
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()            



        # Make the most recently drawn screen availible.
        pygame.display.flip()

if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()

НАСТРОЙКИ

class Settings:
    """A Class to store all settings for Alien Invasion."""

    def __init__(self):
        """Initializing the game's settings."""
        #Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)

        #Ship settings
        self.ship_speed = 1.5

КОРАБЛЬ

import pygame

class Ship:
    """A class to manage the ship."""

    def __init__(self, ai_game):
        """Initiate the ship and set its starting position."""
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()

        #Load the ship and imagine its rect.
        self.image = pygame.image.load('downloads/ship.bmp')
        self.rect = self.image.get_rect()

        #Start each new ship at the bottom of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

        #Store a decimal value for the ship's horizontal position.
        self.x = float(self.rect.x)

        # Movement flags
        self.moving_right = False
        self.moving_left = False

    def update(self):
        """Update the ship's position based on movement flags."""
        # Update the ship's x value, not the rect.
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.ship_speed 
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.ship_speed

        # Update rect object from self.x.
        self.rect.x = self.x        

    def blitme(self):
        """Draw the ship at its current location."""
        self.screen.blit(self.image, self.rect)

Ошибка

Messagepygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/Users/EzbKreativeG/Desktop/git_practice/alien_invasion/alien_invasion.py", line 85, in <module>
    ai = AlienInvasion()
  File "/Users/EzbKreativeG/Desktop/git_practice/alien_invasion/alien_invasion.py", line 20, in __init__
    self.ship = Ship(self)
  File "/Users/EzbKreativeG/Desktop/git_practice/alien_invasion/ship.py", line 13, in __init__
    self.image = pygame.image.load('downloads/ship.bmp')
pygame.error: Couldn't open downloads/ship.bmp
[Finished in 0.6s with exit code 1]
...