Python - Pygame A Very Simple Application For Beginners
Submitted by razormist on Wednesday, November 8, 2017 - 17:15.
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.
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!!!
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. 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 toImporting 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- import pygame
- import os
- 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- #===================VARIABLES==========================
- titleFont = pygame.font.SysFont('arial', 36)
- subtitleFont = pygame.font.SysFont('times new roman', 36)
- BLACK = ( 0, 0, 0)
- WHITE = ( 255, 255, 255)
- GREEN = ( 0, 255, 0)
- RED = ( 255, 0, 0)
- LIGHTBLUE = (153, 204, 255)
- BROWN = (153, 76, 0)
- NAVYBLUE = (51, 102, 255)
- YELLOW = (255, 255, 0)
- #Screen Width
- display_width = 800
- #Screen Height
- display_height = 600
- #Frame rate per second
- fps = 60
- #Render the screen resolution
- display = pygame.display.set_mode((display_width, display_height))
- #Display the tile of the application
- pygame.display.set_caption("Simple Game")
- #Flag that start the loop
- progress = True
- # The clock will be used to control how fast the screen updates
- clock = pygame.time.Clock()
- title = titleFont.render('Sourcecodester', True, (0, 0, 0))
- 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.- #===========================MAIN FUNCTION=============================
- #Main progran loop
- while progress:
- #Main event loop
- for event in pygame.event.get(): #User start
- if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and (event.key == pygame.K_q or event.key == pygame.K_ESCAPE)): #User input
- progress = False #Flag that stop the loop
- #Quit the application when exit
- pygame.quit()
- quit()
- #Create a white backgound
- display.fill(WHITE)
- #Draw an object inside the application
- pygame.draw.rect(display, GREEN, [0, 400, 800, 300], 0)
- pygame.draw.rect(display, LIGHTBLUE, [0, 0, 800, 400], 0)
- pygame.draw.rect(display, BROWN, [50, 200, 200, 200], 0)
- pygame.draw.rect(display, WHITE, [60, 230, 60, 60], 0)
- pygame.draw.rect(display, WHITE, [180, 230, 60, 60], 0)
- pygame.draw.rect(display, NAVYBLUE, [180, 300, 60, 100], 0)
- pygame.draw.polygon(display, RED, [[150, 100], [50, 200], [250, 200]], 0)
- pygame.draw.circle(display, YELLOW, (800, 0), 180)
- pygame.draw.circle(display, BLACK, (190, 350), 5 )
- display.blit(title, (300, 420))
- display.blit(subtitle, (350, 480))
- pygame.display.update()
- #Limit the framerate
- clock.tick(fps)
Add new comment
- 700 views