Create a Pygame window with the corresponding input:
import sys import pygame def run_game(): #Initialize the game and create screen objects pygame.init() #Check if pygame package is complete screen=pygame.display.set_mode((1200,800))#Create display window pygame.display.set_caption("Alien Invasion") #Start the main cycle of the game while True: #Monitor mouse and keyboard events for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() #Make the most recently drawn screen visible pygame.display.flip() run_game()
The pygame.init() method checks if the pygame package is complete.
The pyGame. display.flip() method makes the recently drawn screen visible. Each time you move the game's elements, display.flip() will constantly update the screen to show the latest location of the elements.
Set the background color:
import sys import pygame def run_game(): #Initialize the game and create screen objects pygame.init() screen=pygame.display.set_mode((1200,800)) pygame.display.set_caption("Alien Invasion") #Set screen color bg_color=(230,230,230) #Start the main cycle of the game while True: #Monitor mouse and keyboard events for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() #Redraw the screen every time you cycle screen.fill(bg_color) #Make the most recently drawn screen visible pygame.display.flip() run_game()
screen.fill() takes a color parameter and fills the screen with that color.
To create a settings class:
Store the settings in the game in one place to make function calls easier.
Setting class:
class Setting(): def __init__(self): self.screen_width=1200 self.screen_height=800 self.bgcolor=(230,230,230)
Modified main program:
import sys import pygame #Import settings class from setting import Setting def run_game(): #Initialize the game and create screen objects pygame.init() ai_setting=Setting() screen=pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height)) pygame.display.set_caption("Alien Invasion") #Start the main cycle of the game while True: #Monitor mouse and keyboard events for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() #Redraw the screen every time you cycle screen.fill(ai_setting.bgcolor) #Make the most recently drawn screen visible pygame.display.flip() run_game()