'Pygame window jumps out and disappears immediately
im very new to python, and im currently at chapter 12 on Python Crash Course.
it asks me to build an alien invasion game, and im stuck at this point where I wrote exactly what it said on the book, and tried exactly what YouTube Travis Pugh said in his tutorial on this chapter (his code works fine in the video) (https://youtu.be/WB8i9cseVT8), my pygame window jumps out and disappear immediately.
could anybody please help me ?
here's Travis Pugh's code(there's no changes to the class Settings):
(first file is ship.py)
import pygame
class Ship:
def __init__(self, ai_game):
"""initialize the ship, set its starting location"""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
#load the ship image, get its rectangle
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
#start each new ship at the bottom center of the screen
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""draw the ship at its current location"""
self.screen.blit(self.image, self.rect)
(second file is alien_invasion.py)
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
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 background color
self.bg_color = (230,230,230)
def run_game(self):
"""start the main loop for the game"""
while True:
# 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()
#make the most recently drawn screen visible
pygame.display.flip()
if __name__ == '__main__':
#make a game instance, and run the game
ai = AlienInvasion()
ai.run_game()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|