So I'm going through the Alien Invasion project, and I haven't encountered anything I couldn't work out apart from this. I'm trying to input from the book and make a button that overlays transparently at the start of the game. When I do, the game begins with a black screen instead. I've tested the program in Vs Code and Sublime, it does the same thing in both.
The button is there, I tested it by using code from the next chapter. I can tell its there because when I move the mouse to the middle of the screen it triggers the game as normal. It's really strange.
I've been double checking the code repeatedly, but I can't find any issues with input. I'll be checking input from earlier chapters too.
Any help would be great, as this seems really dumb to get stuck on.
Note: I've only included the relevant methods. If you want to see code for others please ask.
alien_invasion.py
import sys
import pygame
from time import sleep
from settings import Settings
from game_stats import GameStats
from ship import Ship
from bullet import Bullet
from alien import Alien
from button import Button
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game and create game resources."""
pygame.init()
self.settings = Settings()
#window settings
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
#windowed settings
#fullscreen settings
#self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
#self.settings.screen_width = self.screen.get_rect().width
#self.settings.screen_height = self.screen.get_rect().height
#fullscreen settings
pygame.display.set_caption("Alien Invasion")
self.stats = GameStats(self)
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
self._create_fleet()
#Make the Play button.
self.play_button = Button(self, "Play")
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
if self.stats.game_active:
self.ship.update()
self._update_bullets()
self._update_screen()
self._update_aliens()
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)
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
self._check_play_button(mouse_pos)
def _ship_hit(self):
"""Respond to the ship being hit by an alien."""
if self.stats.ships_left > 0:
#Decrement ships_left.
self.stats.ships_left -= 1
#Get rid of any remaining aliens and bullets.
self.aliens.empty()
self.bullets.empty()
#Create a new fleet and center the ship.
self._create_fleet()
self.ship.center_ship()
#Pause
sleep(0.5)
else:
self.stats.game_active = 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()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.aliens.draw(self.screen)
#Draw the play button if the game is inactive.
if not self.stats.game_active:
self.play_button.draw_button()
pygame.display.flip()
if __name__ == '__main__':
#Make a game instance and run the game.
ai = AlienInvasion()
ai.run_game()
button.py
import pygame.font
class Button:
def __init__(self, ai_game, msg):
"""Initialize button attributes."""
self.screen = ai_game.screen
self.screen_rect = self.screen.get_rect()
#Set the dimensions and properties of the button.
self.width, self.height = 200, 50
self.button_color = (0, 255, 0)
self.text_color = (255, 255, 255)
self.font = pygame.font.SysFont(None, 48)
#Build the button's rect object and center it.
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = self.screen_rect.center
#The buttone needs to be prepped only once.
self._prep_msg(msg)
def _prep_msg(self, msg):
"""Turn msg into a rendered image and center text on the button."""
self.msg_image = self.font.render(msg, True, self.text_color, self.button_color)
self.msg_image_rect = self.msg_image.get_rect()
self.msg_image_rect.center = self.rect.center
def draw_button(self):
#Draw a blank button then draw message
self.screen.fill(self.button_color, self.rect)
self.screen.blit(self.msg_image, self.msg_image_rect)
gamestats.py
class GameStats:
def __init__(self, ai_game):
"""Initialise statistics."""
self.settings = ai_game.settings
self.reset_stats()
#Start Alien Invasion in an inactive state.
self.game_active = False
def reset_stats(self):
"""Initialise statistics that can change during the game."""
self.ships_left = self.settings.ship_limit