Game Development with Python

Game Development

Game development is the process of creating video games or interactive digital experiences. It involves designing, programming, and producing games for various platforms, such as computers, consoles, mobile devices, and virtual reality systems.

Game development is a multidisciplinary field that involves collaboration among programmers, artists, designers, writers, and sound engineers.

It requires creativity, technical expertise, problem-solving skills, and a deep understanding of player engagement to create successful and enjoyable games.

Game development is a dynamic and exciting industry that continues to evolve as technology advances, opening up new possibilities for innovative and immersive gaming experiences.

 

Setting up the Environment

Let’s set up the environment first. Follow the steps.

  1. First, add the beetle sprite from PictoBlox’s sprite library.
    New Sprite
  2. Delete the Tobi Sprite.
  3. Add the backdrop as Maze.

The stage is all set.

Let’s write the code for the beetle. Since we’re controlling it using the keyboard’s arrow keys, the beetle must move in the direction we push the key. We will use the sprite’s move() and setdirection() functions to make it work.

Below is the code of the beetle’s movement:

sprite = Sprite('Beetle')

while 1:
  if (sprite.iskeypressed('up arrow')):
    sprite.setdirection(0)
    sprite.move(5)
    
  if (sprite.iskeypressed('down arrow')):
    sprite.setdirection(180)
    sprite.move(5)
    
  if (sprite.iskeypressed('left arrow')):
    sprite.setdirection(-90)
    sprite.move(5)
    
  if (sprite.iskeypressed('right arrow')):
    sprite.setdirection(90)
    sprite.move(5)

Check the code:

Set Beetle Start Position

We want the beetle to start from -150, -150 with 50% size and direction facing up. This we can do with the following code:

sprite.setdirection(0)
sprite.setsize(50)
sprite.gotoxy(-150, -150)

Note: This code will be added to the same program and above the while loop.

Programing the Game Conditions

Every game has certain rules and conditions to be followed, then only there is real fun in playing the game. In our game also, let us provide certain simple conditions to make it more fun!

  1. The beetle is required to move inside the created maze, if it tries to move such that it is touching any line of the maze, the beetle would return to its original position, which is the starting position irrespective of its present position inside the maze.
  2. When the beetle touches the Apple, the game gets over.

To do this, we will use istouchingcolor((R, G, B)) function for the sprite. This function test whether the sprite is touching the specified color or not.

Touching Walls

The wall color is black which in RGB value is (0, 0, 0). So we will add the condition inside the while loop to check if the beetle touches the wall.

  if (sprite.istouchingcolor((0, 0, 0))):

If the condition is true, the beetle should go to the initial position.

  if (sprite.istouchingcolor((0, 0, 0))):
    sprite.setdirection(0)
    sprite.gotoxy(-150, -150)

Test the code till now.

Touching Apple

The apple color is red which in RGB value is (255, 0, 0). So we will add the condition inside the while loop to check if the beetle touches the apple.

  if (sprite.istouchingcolor((255, 0, 0))):

If the condition is true, the game is over.

  if (sprite.istouchingcolor((255, 0, 0))):
    sprite.say('Game Over', 2)
    break

Test the code:

Complete Code

The full code is:

sprite = Sprite('Beetle')

sprite.setdirection(0)
sprite.setsize(50)
sprite.gotoxy(-150, -150)

while 1:
  if (sprite.iskeypressed('up arrow')):
    sprite.setdirection(0)
    sprite.move(5)
    
  if (sprite.iskeypressed('down arrow')):
    sprite.setdirection(180)
    sprite.move(5)
    
  if (sprite.iskeypressed('left arrow')):
    sprite.setdirection(-90)
    sprite.move(5)
    
  if (sprite.iskeypressed('right arrow')):
    sprite.setdirection(90)
    sprite.move(5)
    
  if (sprite.istouchingcolor((0, 0, 0))):
    sprite.setdirection(0)
    sprite.gotoxy(-150, -150)
    
  if (sprite.istouchingcolor((255, 0, 0))):
    sprite.say('Game Over', 2)
    break