In previous post, we created an empty window for our game. In this post, let's add some images to our window.
In Pygame, Images are represented by a pygame object called as "surface". The Surface has a fixed
resolution and pixel format
In previous post, when we wrote a code that gave us an empty window which was looking like this.
Now we want to show some real picture on this window let's say something looking like this
for this we need to use two pygame modules those are pygame.surface and pygame.image
there is a method called pygame.image.load() which is used to load images and a blit() function of surface object to show one image on top of another.
please checkout followingsource code link that will give you a step by step guide to get your window displayed as
and yeah,we got how to display images on our empty game window that we created in previous post . Get the source code Here!
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.
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pygameis one of the most popular 2d game engines. It isSDLwrapper
for python. It is a powerful and very simple to use game engine. it is
specifically designed for game development so you get all the basic
elements of a game in it (eg.. sprites, timer, display functionality,
sound functionality, events etc..)
Pygame is highly portable means you can use it with nearly with every platform.
If
you want to teach kids programming, they find programming more
interesting when they make something like games. With pygame you can
write your full game code in a single file. You need not to create a big
and complicated file structure if you don't want to.
What you need to get started with Pygame?
The thing that you need to get started with pygame is python3 installed
on your system. Python 3.6.1 or greater is recommended by Pygame.
How to install?
The best way to install pygame is with the pip tool (which is what python uses to install packages).
when
you install python(newer versions), you get pip automatically
installed. We use the --user flag to tell it to install into the home
directory, rather than globally.
You need latest pip to install pygame. Try upgrading pip if pygame starts compiling from source and and fails to install.
Run the following commands to install pygame on your system:
Windows:-
py -m pip install -U pygame --user Linux:-
python3 -m pip install -U pygame --user
You get Pygame already installed with Raspberry Pi. You need not install it.
If you want to install on Mac or other platform, go through the installation processHere
Once you have installed Pygame, you are ready to GO......
Stay tuned for posts related to how to use the various elements of Pygame
In theNextpost we'll see how to create an empty game window using Pygame...