Skip to main content

Drawing shapes And Filling colours

Till now, we have written a python program to open a blank black screen and the QUIT event is handled.
In this post, let's see how to add colours to the game surface and draw shapes onto the screen.

Filling colours :

By default, pygame has no predefined colours. We have to define the colours by giving the RGB(red,green,blue) value of the colour. 

 import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display=pygame.display.set_mode((500,500))
game_loop=False
while not game_loop:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            game_loop=True
    display.fill(blue)
    pygame.display.update()
pygame.quit()
quit()

Here i have defined the basic colours by giving the rgb value of those colors. The values are given as a tuple here. Since white is a mixture of all the colours, it's rgb value is 255,255,255 .
Now that we have defined the colours, we can fill the game surface with the colour we want.
To fill the game surface, which is defined in the variable 'display' here, fill command is used.
                                 display.fill(white)
In pygame, we put all the things we want to display in the game surface and then render it because rendering requires more graphics. 
To render objects onto the screen, pygame.display.update() is used. This updates the game window with all the elements.

I have filled the screen with blue colour. So when the program runs, the output will be :




Drawing shapes :

We can draw rectangles onto the screen in pygame using pygame.draw.rect() function.

Now, let me first explain the parameters needed to be entered for the rect() function.

pygame.draw.rect(display,red,[100,100,250,250])

Here, display refers to the game surface where we want to render the rectangle. We want the rectangle to be red. so the second parameter is the colour of the rectangle. The third parameter is a list. 100,100 is the point of the top left pixel of the rectangle. It means the top left point of the rectangle is 100 pixels away from the left and 100 pixels away from the top of the game window.
The next two values 250,250 refers to the width and height of the rectangle respectively.

We can also use fill() to draw rectangles onto the screen. The advantage of using fill over draw.rect()
is that with fill(), the processing is faster compared to draw.rect().
The fill() function will be like :

display.fill(black,rect=[100,100,50,50])

And so, now our code looks like :

import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display=pygame.display.set_mode((500,500))
game_loop=False
while not game_loop:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            game_loop=True
    display.fill(blue)
    pygame.draw.rect(display,red,[100,100,50,100])
    display.fill(black,rect=[100,100,50,50])
    pygame.display.update()
pygame.quit()
quit()

The output of this code would be like :



As you can see, the black rectangle is drawn over the red rectangle. In this way, all the elements are drawn over and over on the window and then display is updated.

That's all for drawing shapes and filling colours in pygame. Thank you.

                                            >> Next : Moving Objects >>

Comments

Popular posts from this blog

Introducing Pygame

Hello Everyone! Let's get into the most interesting topic. We are going to see how we design games using python programming language, Let me introduce you to ' pygame' . Pygame is an open-source and free python library which is used for creating multimedia applications like games built on the top of an excellent SDL library. Pygame is highly portable and can be run on nearly any platform and operating system. First of all,We need to download the pygame library which can be downloaded from the official Pygame  website. Several versions of pygame compatible for the various versions of python are available. We need to select the appropriate version of pygame for our version of python and download and install it. Here i use Python-2.7. So i downloaded pygame-1.9.1 For using pygame library in a python program, like importing any other module, we must import pygame using the command.               import pygame You can also check if you ...

Introduction to Python

Introduction         Most of us would be very interested in game designing even before knowing what is it about. We get admired by the way the games we play are designed and that becomes the cause for many of us to have the wish to become a game designer. So this is the platform where I am going to share with everyone what I have learned. When it comes to game designing, everyone knows that there is some serious coding stuff done behind and behind every successful game in the market, there would have been coders and designers working very hard for nearly more than 5 years. So we need to learn  programming languages with great interest to get into game designing. Python is a latest language which is more convenient and easy to learn and work with.It is easy to learn it as it has built in capability for many features. We can perform the operations with a single line of code which requires several lines of code to be written in langu...