Python - Pygame A Very Simple Application For Beginners

In this tutorial we will create Simple Application using Pygame. Pygame is a cross-platform set of Python modules designed for creating games. It includes computer graphics and sound libraries designed to be used with the Python programming language. This is a good opportunity for beginners to learn Pygame first when it comes in developing some games. So let's now see about it.

Getting Started

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. After Python IDLE's is installed, open the command prompt then type "python -m pip install pygame", and hit enter. tut1 Wait for the Pygame to be downloaded and installed at the same time. When its completed type "python -m pygame.examples.oldaliens" and hit enter to check if Pygame is good to go. Note: A new window will pop up a windows that contains with the actual example of Pygame Game Application. Feel free to play it if you want to tut2

Importing Modules

After setting up the installation, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor
  1. import pygame
  2. import os  
  3. from pygame.locals import *

Assigning Variables

We will assign the a certain variables that we will need to declare later in the main function. To do that just kindly write this code inside your text editor
  1. #===================VARIABLES==========================
  2. titleFont = pygame.font.SysFont('arial', 36)
  3. subtitleFont = pygame.font.SysFont('times new roman', 36)
  4. BLACK = ( 0, 0, 0)
  5. WHITE = ( 255, 255, 255)
  6. GREEN = ( 0, 255, 0)
  7. RED = ( 255, 0, 0)
  8. LIGHTBLUE = (153, 204, 255)
  9. BROWN = (153, 76, 0)
  10. NAVYBLUE = (51, 102, 255)
  11. YELLOW = (255, 255, 0)
  12.  
  13. #Screen Width
  14. display_width = 800
  15.  
  16. #Screen Height
  17. display_height = 600
  18.  
  19. #Frame rate per second
  20. fps = 60
  21.  
  22. #Render the screen resolution
  23. display = pygame.display.set_mode((display_width, display_height))
  24.  
  25. #Display the tile of the application
  26. pygame.display.set_caption("Simple Game")
  27.  
  28. #Flag that start the loop
  29. progress = True
  30.  
  31. # The clock will be used to control how fast the screen updates
  32. clock = pygame.time.Clock()
  33.  
  34. title = titleFont.render('Sourcecodester', True, (0, 0, 0))
  35. subtitle = subtitleFont.render('Pygame', True, (0, 0, 0))

Creating The Main Function

This is the Main Code for the Pygame application. The code contains a several function that will render the objects of the application. To do that just write this code inside the IDLE text editor.
  1. #===========================MAIN FUNCTION=============================
  2. #Main progran loop
  3. while progress:
  4.  
  5.     #Main event loop
  6.     for event in pygame.event.get(): #User start
  7.         if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and (event.key == pygame.K_q or event.key == pygame.K_ESCAPE)): #User input
  8.             progress = False #Flag that stop the loop
  9.  
  10.             #Quit the application when exit
  11.             pygame.quit()
  12.             quit()
  13.            
  14.     #Create a white backgound
  15.     display.fill(WHITE)
  16.  
  17.     #Draw an object inside the application
  18.     pygame.draw.rect(display, GREEN, [0, 400, 800, 300], 0)
  19.     pygame.draw.rect(display, LIGHTBLUE, [0, 0, 800, 400], 0)
  20.     pygame.draw.rect(display, BROWN, [50, 200, 200, 200], 0)
  21.     pygame.draw.rect(display, WHITE, [60, 230, 60, 60], 0)
  22.     pygame.draw.rect(display, WHITE, [180, 230, 60, 60], 0)
  23.     pygame.draw.rect(display, NAVYBLUE, [180, 300, 60, 100], 0)
  24.     pygame.draw.polygon(display, RED, [[150, 100], [50, 200], [250, 200]], 0)
  25.     pygame.draw.circle(display, YELLOW, (800, 0), 180)
  26.     pygame.draw.circle(display, BLACK, (190, 350), 5 )
  27.     display.blit(title, (300, 420))
  28.     display.blit(subtitle, (350, 480))
  29.     pygame.display.update()
  30.      
  31.     #Limit the framerate
  32.     clock.tick(fps)
There you have it we just created a Simple Application using Pygame. I how that this tutorial help you understand about Pygame. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Tags

Add new comment