Gaming with Python

While Python is not the most used programming language for game development, as that title is held by C++, it is still a great language to get started on understanding the background of game development as it is said to be user-friendly. Python has many packages available for game development including PyGame.

PyGame is a great way to start with game development and can be used to create a lot of games such as Flappy Bird, Angry Bird etc. First, you have to start with the initialization of the package and then set it up. For the simplest use, you can create a screen set to a certain size just to get a visualization.

import pygame
 5 pygame.init()
 6
 7 # Set up the drawing window
 8 screen = pygame.display.set_mode([500, 500])
 9
10 # Run until the user asks to quit
11 running = True
12 while running:
18
19    # Fill the background with white
20    screen.fill((255, 255, 255))
21
22    # Draw a solid blue circle in the center
23    pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)
24
25    # Flip the display
26    pygame.display.flip()
27
28 # Done! Time to quit.
29 pygame.quit()

This is a basic example of how you might start creating a screen. First, you will initialize the package after importing and after some toggling with the values you can see what you want your screen to look like.

Right now all you have is a screen and there is no actual interaction between the user and the program. How do we add that? We can make it so our arrow keys control the little circle on line 23 that we had created.

from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
14)
15
16# Initialize pygame
17pygame.init()

Here we have imported some of the functionality provided by pygame. We are using the key buttons up, down, left, and right to control our blue ball. There will be more functionality added as we move along but it's a great place to start. This is just a look into how PyGame can help create games. With just a few lines of code, we can create a game very similar to what you might see Flappy Bird to be. There are many functionalities on how to set up the speed at which you want things to move, how an object might react when coming into contact with another structure, and even adding sound effects.

This is an example of how to create a game using PyGame and the link is a source with all the code needed to develop it on your computer.

https://thepythoncode.com/article/make-a-chess-game-using-pygame-in-python

This was just one available package from game development. We have something that allows the user ease of use for 3D gaming known as Panda3D. Panda3D is supported by Python and C++. Although Panda3D is a great tool it is also very complex and not beginner-friendly. Below is an example of how a game with Panda3d will differ from using something such as PyGame.

https://docs.panda3d.org/1.10/python/more-resources/samples/chessboard#chessboard

Ren'Py is another package to use when thinking of making a story-based game. This would be such as those following a character that makes decisions along the way. This type of game is more like a novel visualization.

Although Python is not the first choice for game development there are still ways to create games with it. Games will have multiple languages being used at a time and knowing how Python can help with that is a first step. Although not the first choice it is still capable of making loads of applications.