Python provides an environment which is so favourable for efficient programming because of it’s simple syntax, rapid development, enriched library, open source code and platform-independent nature. Same way for game development, python has some highly enriched library. In this tutorial we will see how to develop a game with pygame.
Pygame is a cross-platform library designed to make it easy to write multimedia software such as games in python. By installing pygame, you can have access to an environment which is favouriable for rapid development of 2d and 3d games. Pygame requires the Python language and SDL multimedia library.
First of all you need to have python installed on your system. Then you need to install pygame. You can download pygame from it’s website http://www.pygame.org
Develop your first game with pygame
Now we have pygame installed on our system. So, we can move forward to develop our first game with pygame. We will start with a simple game which do nothing than bouncing a ball when it runs. Let’s start our journey-
First you need to import pygame. We also need to import sys module because we need to use functionality of it.
import sys import pygame pygame.init()
Here we have imported all the available pygame modules and initializes these modules. Now we need to create a graphical window.
size=width, height=450, 280 speed=[1,1] color=1,1,1 screen=pygame.display.set_mode(size)
Now we need to load our image, which is a ball. The supported formats include jpg, png, tga and gif. Here is how to load the ball-
Pygame comes with object type named rect, which represents a rectangular area. We will create a variable with this object.
This time we will use infinite loop which will move the ball and check the user input. If QUIT event is occured, the program will be exit.
while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballAction = ballAction.move(speed) if ballAction.left < 0 or ballAction.right > width: speed = -speed if ballAction.top < 0 or ballAction.bottom > height: speed = -speed screen.fill(color) screen.blit(ball, ballAction) pygame.display.flip()
Here we have specified the current speed to the variable ballAction to move it. The ball is moved inside the screen and when it is moved to outside the screen, we have just reversed the direction. Then we have filled the screen with a black RGB color to erase the screen. This is done because to animate the movement of ball. Next we have drawn the ball image into the screen by ‘screen.blit()’ method. Finally we have updated the visible display by the method ‘pygame.display.flip()’.
Thats all. Don’t forget to put the image “ball.gif” in the same directory of your code.
So, take a look at the code-
import sys import pygame pygame.init() size=width, height=450, 280 speed=[1,1] color=1,1,1 screen=pygame.display.set_mode(size) ball=pygame.image.load(“ball.gif”) ballAction=ball.get_rect() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballAction = ballAction.move(speed) if ballAction.left < 0 or ballAction.right > width: speed = -speed if ballAction.top < 0 or ballAction.bottom > height: speed = -speed screen.fill(color) screen.blit(ball, ballAction) pygame.display.flip()
Hope you will enjoy it.