Saturday 23 February 2019

PyGame- Create a blank window for your game

How To Create an empty window for your game in Pygame


In last post we saw what is Pygame and how to install it. In this post let's start the first step for your game i.e. the display of your game. We are going to need a window in which you are going to place your other game objects. So let's get started....

To create our game window we are going to use pygame module named "display". This module creates a fully controlled display for our game.

Let's create a file named as GameScene.py and add the following code in it.

import pygame 
from pygame.locals import *

This'll import pygame in your file. Now for our window, we are going to need the dimensions  i.e. width and height. so let's define them also a color. These lines will define these things

winWidth=600
winHeight=480
color_CYAN=(0,255,255)

now we need to initialize the pygame and the display. The following lines will do it.

pygame.init()
Game_Window=pygame.display.set_mode((winWidth,winHeight))

Here we have created an object named as Game_Window, which is going to give us a rectangular surface with a  width of 600 pixels and a height of 480 pixels.

Let's save the file and run the code as follows:

$python GameScene.py

you'll see a blank window like this






Right now you'll see this window just once and it'll get disappeared. To keep it displayed continuously, we'll need to update the display continuously. Add the following lines in your GameScene.py


while True:

    pygame.display.update()


These lines will keep the window steady on your screen but you'll see the "X" button to close window will not work right now. To make it work we'll have to detect event of closing the window.
In your above while loop, add the following lines those are going to detect the events triggered on the display.

for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            exit()



Here we are getting all the events triggered  on our window and detecting the type of event. If we get a QUIT type of event, we are firing command pygame.quit() that is going to quit the window.

Now we have an empty window for our game. We can also change it's Title currently we see the title is "pygame window". Let's change it to "Empty Game Window"

add the following two lines after pygame.init()

caption="Empty Game Window"
pygame.display.set_caption(caption)



and you see title  of the window changed



Now let's change the background color of the window.
Add the following line after the Game_Window definition.

Game_Window.fill(color_CYAN)

And when we save and run this code, we'll see the output as:




And that's it!! Now you are ready to write your game with a resolution of 600X480 window.

The final code for the above window should look like:



In next post let's display some images on this empty window. 

            

No comments:

Post a Comment

Your comments and suggestions are valuable and are always welcome and will be helpful to me to create a good quality content. Please leave your thoughts