FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
1.8k views
in Python Programming by Goeduhub's Expert (3.1k points)
PyGame Python  Tutorial
by (100 points)
Pygame is totally a new feature I learned in this module.

4 Answers

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

PyGame

PyGame in a python library to develop games.

Before starting with pygame we have to install pygame and we must have some programming knowledge (Python Preferred).

pip install is simplest way 

Getting Started with PyGame

As we know, to play any game we first need a window.So we will first create a window in the PyGame 

#Importing pygame

import pygame

pygame.init()

#Setting name of the window 

pygame.display.set_caption("pygame window")

#Creating a window 

window = pygame.display.set_mode((400, 300))

done = False

#Loop to control our window 

while not done:

        for event in pygame.event.get():

                if event.type == pygame.QUIT:

                        done = True

        pygame.display.flip()

#Quit pygame 

pygame.quit()

Output

Let's see the functionality of our code

  1. First we imported Pygame library 
  2. Initializing all modules of pygame (pygame.init())
  3. pygame.display.set_caption , used to display name of the window.
  4. After that we have created a window. (pygame.display.set_mode(),  is a function in pygame to create window)
  5. The loop that is placed to stop our windows on the screen.(If we do not loop, our window will be created but will not stop)
  6. Inside the event, we handle all the actions that occur above the window.
  7. Like here we have given the message that if we quit the window (event.type == pygame.QUIT) means we close the window, then done become true means our window is closed. 
  8. pygame.display.flip(),It allows only a portion of the screen to updated, instead of the entire area.

Interactivity or action on keys 

Image Loading using Pygame

Let's see it with an example 

import pygame

pygame.init()

window = pygame.display.set_mode((500, 500))

done = False

clock = pygame.time.Clock()

while not done:

        for event in pygame.event.get():

                if event.type == pygame.QUIT:

                        done = True

        screen.fill((255, 255, 255))

        image = pygame.image.load(r'hhh.jpg')  

        window.blit(image, (20, 20))

        pygame.display.flip()

        clock.tick(60)

pygame.quit()

Output

 

Let's see the functionality of our code

  1. What's new for us here,that is  pygame.image.load() function, which is used here to load the image.
  2. And next is window.blit(),This function says take the image and draw it onto the window and position it at (x,y).
  3. As you can see from the output, our image is larger than our window size, this means that we do not need to take the image size smaller than the window size (window width and window height)
  4. (x,y) this is our coordinates where we draw the images on the window.

Font Style and size in  Pygame

import pygame

pygame.init()

window = pygame.display.set_mode((300, 300))

clock = pygame.time.Clock()

done = False

font = pygame.font.SysFont(None,50)

text = font.render("Goeduhub", True, (255, 0, 0))

while not done:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            done = True

    window.fill((255, 255, 255))

    window.blit(text,

        (150 - text.get_width() // 2, 

         140 - text.get_height() // 2))

    pygame.display.flip()

    clock.tick(60)

pygame.quit()

Output

Let's see the functionality of our code

  1. The pygame.font.SysFont(text_style,text_size) ,function is used in pygame which describes the style of a text and the size of the text.Here we passed None in as style of text means, it will take default system text style.
  2.  The font.render (text,True , color)- Create a Text surface object i.e.surface object in which Text is drawn on it.
  3. window.fill(color), it just fill our window with color which we defined in it , by default our window is black , here we filled it by white color

Sound and Music in pygame

Snake Game in Python with Pygame


Python Tutorial 

Machine Learning Tutorial 

AI Tutorial

Free Online Tutorials

by (413 points)
What is work of tick() and blit() functions
by Goeduhub's Expert (3.1k points)
blit(image OR Text, (coordinate of position ))-- Basically a blit method or function used to  Draw the image/text to the screen at the given position.
Time is a module in pygame (pygame.time) to manage time in a game.
We can understand from an example. When we play a game of snake in mobile , snake  speed increases as the level of game increases.
Actually, this is not the speed of the snake. This happens because with the help of the clock, we increase the frame per seconds  (FPS).
clock. tick(40) means that for every second at most 40 frames should pass.
by (294 points)
Python pygame is the very important library to developing for the game using python language
by (422 points)
It  tells  us how to  create game with the help of pygame.
by (100 points)
pygame is a library that is used for develope games in python. I learned about creating window, loop to control window, quit function, loading an image etc.
0 like 0 dislike
by (391 points)

Date: 02/05/2020

Topic: Python Pygame 

Pygame is to develop the games. here, i have learned how to create an window, load an image, code to set the size and few knowledge of programme.

0 like 0 dislike
by (232 points)
Topic-Pygame python

I learned about pygame, basically pygame in use for developing a game
0 like 0 dislike
by (294 points)
In this tutorial I learned about game library in python pygame library

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...