I have been playing with Pygame again recently because I have again decided to create a few games with this python Framework and then sell them to make some money on the online gaming store as part of the plan for me to support the yearly fees of this website and others. Before starting another brand new game I always like to refresh myself a little bit by reading through their homepage menu again and thus I think it is also a good idea for me to write something on this site about what I have learned so far from the Pygame official site!
In this article, I will create three rectangles and then display them on the Pygame display panel through a loop but will not update everything on the screen including the Pygame panel background screen itself! I will assign a blue color to all three rectangles but will leave the screen’s background dark which is the default color of it!
Here is the entire short pygame program which I have put together to create the below rectangles.
# Import and initialize the pygame library import pygame pygame.display.init() # set the caption on the panel pygame.display.set_caption("Three Rectangles") # Print this line on output panel if the initialize process is successful if(pygame.display.get_init() == True): print("Success initialize the game!") # Initialize a window or screen for display screen = pygame.display.set_mode([600, 600]) #create series of rectangle objects rec1 = pygame.Rect(10, 10, 30, 30) rec2 = pygame.Rect(100, 100, 30, 30) rec3 = pygame.Rect(150, 150, 50, 50) li = [rec1,rec2,rec3] # put the objects into a list # Run the game until the user asks to quit running = True while running: # If the user clicks on the 'x' button on pygame display window then set running to false for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Fill the rectangles with blue color screen.fill((0,0,255)) # update the three rectangles pygame.display.update(li) # Quit the game pygame.display.quit()
The codes above basically have all the details explaining to you everything that it is doing so I will leave them to you for further analysis.
Three RectanglesJust one thing to keep in mind, if no list is passed into the update method then this method will update the entire screen instead which means the entire background of the screen will get painted blue instead of just those three rectangles!