Python Forum
Window Color/Adding a ship
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Window Color/Adding a ship
#1
I am able to edit the window size. However, when attempting to change the window background color I am having issues. Doesn't matter what numbers I enter the window background stays black. I decided to try and add a ship to the code to see if anything on my background would change. Nothing has changed.

import sys
import pygame
from settings import Settings
from ship import Ship

class AlienInvasion:

	"""Overall class to manage game assets and behavior."""

	def __init__(self):
		"""Initialize the game, and greate 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)

	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()
class Settings:
	"""A class to store all settings for Alien Invasion."""

	def __init__(self):
		# Screen Settings
		self.screen_width = 600
		self.screen_height = 500
		self.bg_color = (230, 230, 230)
import pygame

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

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

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

		# Start each new ship at the bottom enter 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)
Reply
#2
The indentation needs to be fixed so it updates all your graphics each frame.


    def run_game(self):
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
            self.screen.fill(self.settings.bg_color)
         
            self.ship.blitme()
 
            pygame.display.flip()
Reply
#3
Okay thanks! I thought I tried that originally, but I may have been one whitespace off. I'll have to make sure I extra check these indentations. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting a ship to move in pygame djwilson0495 2 3,631 Dec-09-2020, 11:03 AM
Last Post: djwilson0495
  Python BattleShips Random Ship placement FnaticPutin 2 7,758 Oct-13-2017, 11:33 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020