Contra Game Using Python with source code using Pygame

contra game using python

Introduction:

Remember the super fun Contra game where you fight aliens and jump through crazy levels? Well, guess what? You can make your own version of it using Python! Yup, Python can do some cool stuff with a library called Pygame.

In this blog, I’ll show you how to make your very own Contra game step by step. Don’t worry if you’re new to programming – I’ll keep it simple and easy to understand.

And guess what? I’ll give you all the code you need! So, you can play around with it, change things up, and make your own version of Contra. It’s going to be awesome!

So, let’s get started! Grab your computer, open up Python, and let’s make some gaming magic happen!

Game Instructions:

Controls:

  • A: Move Left
  • D: Move Right
  • S: Drop to a lower platform
  • R: Quick restart
  • SPACE: Jump
  • Left Mouse Button: Shoots in the direction determined by the mouse position
  • Right Mouse Button: Dash
  • Mouse Move: Direction of shooting

Powerups:

  • F: Resets cooldown of Dash
  • B: +1 Health
  • Bird: (Powerdown) Causes the player to drop down if taken when on a platform

Rules:

  • Get to the end of the Level and defeat the bot tanks
  • You will die if you lose all your health
  • Each bullet hit will decrease your Health by 1
  • Hitting an enemy unit will instantly kill you

Notes on mechanics:

  • Running soldiers will be difficult to see underwater. (Only their head is visible)
  • Jump, Right, and Dash for longer distance
  • The bird powerup will trigger only if you are walking on a platform while activating. Hence jumping directly on top of the powerup will not activate as you are not on a platform. This can be used to prevent unwanted drops to death
  • Trying to drop when on water will cause you to drown and die!!
  • Direction of shooting is determined by the mouse position. Mouse above/below the player shoots a bullet at an angle of +/- 45 degrees from the ground.
  • Right Mouse Button activates Dash when it is out of cooldown. Its cooldown is depicted in the top center of the screen. Use this ability to cross broken bridges, for example.
  • A random powerup drops in front of the player every 2 seconds. If it goes out of the screen behind the player, it disappears.
  • Snipers shoot only when the player is in front of them.
  • Soldiers who run across the screen are generated randomly every 3 seconds in front of the player.
  • All global settings are present in the ‘settings.py’. They can be changed by the player to make the game more challenging.
 

How to run the source Code:

To run the code, you’ll need Python 3 and pip installed on your system. Follow these steps:

  1. Open your command line interface (Terminal on macOS/Linux or Command Prompt on Windows).

  2. Navigate to the directory where the code files are located.

  3. Install the required dependencies by running the following command

pip install -r requirements.txt
  1. Once the dependencies are installed, you can start the game by running:
python main.py
  1. If you want to start the game in fullscreen mode, you can pass the “-f” argument like this:
 
python main.py -f

Required modules:

  • certifi==2019.6.16
  • pipenv==2018.11.26
  • pygame==1.9.6
  • virtualenv==16.7.2
  • virtualenv-clone==0.5.3
  1. certifi==2019.6.16:

    • Information: Certifi provides Mozilla’s CA Bundle.
    • Usage: It’s commonly used in Python projects to handle SSL certificate verification.
  2. pipenv==2018.11.26:

    • Information: Pipenv is a dependency manager for Python projects.
    • Usage: It simplifies the process of managing project dependencies and virtual environments.
  3. pygame==1.9.6:

    • Information: Pygame is a set of Python modules designed for writing video games.
    • Usage: It’s extensively used for game development, providing functions for graphics, sound, and input handling.
  4. virtualenv==16.7.2:

    • Information: Virtualenv is a tool used to create isolated Python environments.
    • Usage: It’s commonly used to keep project dependencies separated and to avoid conflicts between different projects’ dependencies.
  5. virtualenv-clone==0.5.3:

    • Information: Virtualenv-clone is a tool to clone virtual environments.
    • Usage: It’s useful when you want to replicate an existing virtual environment for another project, saving time on reinstalling dependencies.

Make sure you have Python 3 installed and that pip is configured correctly. Then, follow the steps above to run the game successfully. Enjoy!

Source Code:

Output:

contra game using python
contra game using python

More Projects:

More Python Projects
Get Huge Discounts

All Coding Handwritten Notes

Browse Handwritten Notes