Pygame tool

PYGAME

 

Aim: To study about Pygame tool

 

Introduction to Pygame

Python is the most popular programming language and has vast libraries for various fields such as Machine Learning (Numpy, Pandas, Matplotlib), Artificial intelligence (Pytorch, TensorFlow), and Game development (Pygame, Pyglet).

Pygame was officially written by Pete Shinners. Pygame is a cross-platform set of Python modules designed for writing video games. Pygame adds functionality on top of the SDL library. Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. Pygame, together with SDL, allows users to create fully featured games and multimedia programs in the python language. Pygame is highly portable and runs on nearly every platform and operating system.

 

Pygame core modules

Pygame modules provide abstract access to specific hardware on user system, as well as uniform methods to work with that hardware. Important Pygame Modules include

cursors

load cursor images, includes standard cursors

display

control the display window or screen

draw

draw simple shapes onto a Surface

event

manage events and the event queue

font

create and render TrueType fonts

image

save and load images

joystick

manage joystick devices

key

manage the keyboard

mouse

manage the mouse

sndarray

manipulate sounds with numpy

surfarray

manipulate images with numpy

time

control timing

transform

scale, rotate, and flip images

 

 

General steps to creating a Pygame based program

1.    Importing and Initializing PyGame

1.    import the pygame library and initialize all the functions in the pygame modules. This allows pygame to connect its abstractions to user specific hardware.

2.    Setting Up the Display

1.    Create a screen / surface object on which the graphical operations will be performed.

3.    Draw objects / Load images on the surface object

4.    Create a clock and set the framerate

5.    Sound Effects

1.    pygame provides mixer to handle all sound-related activities. classes and methods in mixer provides background music and sound effects for various actions. music sub-module is used to stream individual sound files in a variety of formats.

6.    Using .blit() and .flip()

1.    blit stands for Block Transfer. blit is the process to render the game object onto the surface. The .blit() takes two arguments – The Surface to draw and the location (coordinates) at which to draw it on the source Surface.

2.    flip() updates the entire screen with everything that’s been drawn since the last flip.

7.    Setting Up the Game Loop – The game loop performs four very important functions:

    1. Processing Events.
      1. Process user input (Key presses, mouse movements, joystick movements) using the pygame event system.
      2. All events in pygame are placed in the event queue, which can then be accessed and manipulated.
    2. Updates the state of all game objects in response to the user input
    3. Updates the display and audio output
    4. Maintains the speed of the game
      1. Every cycle of the game loop is called a frame. The number of frames handled each second is called the frame rate.
      2. Frames continue to occur until some condition to exit the game is met. Example – The player closes the window.
  1. Terminate the program.

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu