Alien invasion python ошибка

Обновлено: 06.07.2024

Я делал игру по книге. И столкнулся с проблемой.
Вот программа:

import sys
import pygame

from settings import Settings
from ship import Ship


class AlienInvasion:
def __init__(self):
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")

bg_color = (230, 230, 230)

def run_game(self):
while True:
self._check_events()
self._update_screen()

I am new in learning Python from Python Crash Course by Eric Matthes. In the Alien Invasion project I encountered trouble. I investigated so much, compared the book line by line, but I couldn't find the problem.

Here in the last columns, I didn't get the logic of writing if case?

alien_invasion.py

And in ship.py could someone explain to me why we write ai_game in initial module.

ship.py

settings.py

When I run alien_invasion.py I get the following error:


7,064 17 17 gold badges 30 30 silver badges 48 48 bronze badges Try move self.ship = Ship(self) in your AlienInvasion init down a few more lines as the AlienInvation screen attribute is not create and initialize yet.

1 Answer 1

doesn't do what you expect it to do. pygame.quit() is a function and it uninitialize all pygame modules. The function returns None and so the condition fails. The code runs through and crashes at the next instruction which tries to access a pygame module.

The .type property of a pygame.event.Event object contains the event type identifier. pygame.QUIT is an enumerator constant which identifies the quit event. See the documentation of pygame.event .


157k 23 23 gold badges 80 80 silver badges 125 125 bronze badges

Linked

Related

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

I am doing the Alien Invasion project in the Python Crash Course book. When I test the code to see if the ship comes on screen the screen starts then shuts down.

I have been looking through the code for hours now without finding out why.

This is the error that comes up

10.7k 2 2 gold badges 27 27 silver badges 40 40 bronze badges I guess you should replace pygame.quit() with something like pygame.QUIT . I suppose pygame.quit() is actual call to stop pygame engine. Also I would replace sys.exit() with just break and add pygame.quit() somewhere after while-loop. Try this way. Add another version of the code you try ( run_game function) and corresponding error message.

1 Answer 1

You simply need two switch these two lines so they are in order, from:

The reason for this is that you create a Ship object which wants to access the AlienInvasion 's screen , so you need to define it before the Ship , otherwise it's undefined.

Here in the last columns, I didn't get the logic of writing if case?

if __name__ == '__main__' is a special construct in Python. It means the following block will only be executed if you run the script directly, rather than importing the script inside some other script. It isn't really needed for a game but it's good practice to get used to it anyway.

And in ship.py could someone explain to me why we write ai_game in initial module.

The Ship basically want to have access to the original AlienInvasion object because it holds information related to the game, like in this case the screen to draw on. The AlienInvasion object then gets passed as using Ship(self) which calls Ship.__init__(self, ai_game) (The left self turns into the right ai_game ).

I am doing the Alien Invasion project in the Python Crash Course book. When I test the code to see if the ship comes on screen the screen starts then shuts down.

I have been looking through the code for hours now without finding out why.

This is the error that comes up

10.7k 2 2 gold badges 27 27 silver badges 40 40 bronze badges I guess you should replace pygame.quit() with something like pygame.QUIT . I suppose pygame.quit() is actual call to stop pygame engine. Also I would replace sys.exit() with just break and add pygame.quit() somewhere after while-loop. Try this way. Add another version of the code you try ( run_game function) and corresponding error message.

1 Answer 1

doesn't do what you expect it to do. pygame.quit() is a function and it uninitialize all pygame modules. The function returns None and so the condition fails. The code runs through and crashes at the next instruction which tries to access a pygame module.

The .type property of a pygame.event.Event object contains the event type identifier. pygame.QUIT is an enumerator constant which identifies the quit event. See the documentation of pygame.event .


157k 23 23 gold badges 80 80 silver badges 125 125 bronze badges

Linked

Related

Hot Network Questions

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

606b2da6deeb7921817735.jpg

Здравствуйте. Пытаюсь написать первую игру используя pygame и столкнулся с такой проблемой. При запуске чёрный экран, как на на картинке. При нажимании на выделенное место игра запускается, т.к. там кнопка.

Ниже привожу фрагменты кода
Код кнопки


Функции игры связанные с кнопкой


Код отвечающий за запуск игры в неактивном состоянии

Читайте также: