Skip to main content

Posts

Showing posts from July, 2017

Moving Objects

Hi everyone ! In this post, let us see how to move objects on the game screen. In the previous post, we learnt how to draw objects and fill colors. Now we will see how to move those objects from one place to another. To move objects in a game, we need some event to occur. Let us make the object move on pressing the arrow buttons. 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 rect_x = 100 rect_y = 100 while not game_loop:     for event in pygame.event.get():         if event.type == pygame.QUIT:             game_loop = True         if event.type == pygame.KEYDOWN :             if event.key == pygame.K_LEFT :                 rect_x -= 10             if event.key...

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 o...