Getting Started with Pygame

  • Matplotlib- It is a numerical plotting library that helps in data analysis.
  • Pandas- It is an open-source Python package, It is most generally used for data science/data analysis and machine learning tasks.
  • Pygame- It is an open-source module for the Python programming language. It is used to help you make games and other multimedia applications.
  • It is a set of Python modules for writing video games.
  • It was officially developed by Pete Shinners.
  • It is highly portable and runs on every platform and OS.

Pygame Installation-

Step 1- Importing and initializing the Pygame Library

import pygame
pygame.init()

Step 2- Defining the Colors

  • RED = (255, 0, 0)
  • GREEN = (0, 255, 0)
  • BLUE = (0, 0, 255)
  • red + green = yellow; [YELLOW = (255, 255, 0)]
  • green + blue = cyan; [CYAN = (0, 255, 255)]
  • blue + red = magenta; [MAGENTA = (255, 0, 255)]
  • red + green + blue = white; [WHITE = (255, 255, 255)]
  • no colors = black; [BLACK = (0, 0, 0)]

Step 3- Operations on background-color

#Importing the library
import pygame
#Initializing Pygame modules
pygame.init()
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
#Initializing gameDisplay
gameDisplay = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Pygame')
gameExit = Falsewhile not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True

#Changing the color
gameDisplay.fill(red)
pygame.display.update()
pygame.quit()
quit()

Step 4- Main Program Loop

  • Capturing Events: It is used to continually “listen” to user inputs and react to those. It could be when the user uses the keyboard or the mouse to capture events.
  • Implementing the Game Logic: What happens while the game is running? Are cars moving forward, aliens dropping from the sky, ghosts chasing you, etc?
  • Refresh the screen by redrawing the stage and sprites.
# Import the pygame library and initializing pygame modules
import pygame
pygame.init()
# Defining colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
# Open a new window
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My First Game")
# The loop will move on till the user exit the game (e.g. clicks a close button).
carryOn = True
# The clock will be managed to control that how fast the screen updates.
clock = pygame.time.Clock()
# Main Program Loop
while carryOn:
# Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # user clicked close
carryOn = False
# Game logic should go here
# Drawing code should go here
# First, clear the screen to white.
screen.fill(WHITE)

# Then you can draw different shapes, lines and add text to the background stage.
pygame.draw.rect(screen, RED, [55, 200, 100, 70], 0)
pygame.draw.line(screen, GREEN, [0, 0], [100, 100], 5)
pygame.draw.ellipse(screen, BLACK, [20, 20, 250, 100], 2)
# update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
# Once we have exit the main program loop we can quit the game engine:
pygame.quit()

Step 5- Changing the window caption note

# Import the pygame library and initializing pygame modules
import pygame
height = 500
width = 500
def game():
pygame.init()
screen = pygame.display.set_mode((height, width))
pygame.display.set_caption("Counter Strike")
game_exit = True
while game_exit:
for event in pygame.event.get():
print(event)
pygame.quit()
quit()
if __name__ == '__main__':
game()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store