Explore

Close
Python Games Share

Create Fruit Ninja Game In Python With Code

Create Fruit Ninja Game In Python With Code

Want to create fruit ninja game in python, then you are at the right place today in this tutorial. I will show you how to create fruit ninja game in python and I will provide you with the fruit ninja python code, so read till the end.

The Fruit Ninja game is one of the most popular games. It is a game where fruits appear on the screen and you have to slice them by tapping them. You must have played this game, it is quite competitive game.

We will use the pygame library, which is used to develop games in python. It is one of the best python library for game development in python programming.

By the end of this tutorial, you will have a working fruit ninja game running on your computer. I will show you a step by step guide to create this python fruit ninja game, so follow me till the end.

Create Fruit Ninja Game In Python

The game is simple the fruits will start coming from the bottom the player has to slice it before they fall down on each slice they will get a point if the player slices a bomb by mistake then it is game over.

I will guide you till the end till you run the game so if you have python installed and setup then you can skip the first step now let’s start.

#1 Install and setup python 

The first step is to set up python on your computer so first you need to go to the python website and download the latest python version it will download the python installer.

After downloading you need to execute the python installer it will install and setup python on your computer, remember to check add python to path while installing.

Wait for installation to complete after that you will have python installed, you can check it by typing python in your command prompt or terminal.

#2 Copy and paste the fruit ninja python code


import pygame, sys
import os
import random

player_lives = 3                                                #keep track of lives
score = 0                                                       #keeps track of score
fruits = ['melon', 'orange', 'pomegranate', 'guava', 'bomb']    #entities in the game

# initialize pygame and create window
WIDTH = 800
HEIGHT = 500
FPS = 12                                                 #controls how often the gameDisplay should refresh. In our case, it will refresh every 1/12th second
pygame.init()
pygame.display.set_caption('Fruit-Ninja Game -- Pythondex')
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))   #setting game display size
clock = pygame.time.Clock()

# Define colors
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

background = pygame.image.load('back.jpg')                                  #game background
font = pygame.font.Font(os.path.join(os.getcwd(), 'comic.ttf'), 42)
score_text = font.render('Score : ' + str(score), True, (255, 255, 255))    #score display
lives_icon = pygame.image.load('images/white_lives.png')                    #images that shows remaining lives

# Generalized structure of the fruit Dictionary
def generate_random_fruits(fruit):
    fruit_path = "images/" + fruit + ".png"
    data[fruit] = {
        'img': pygame.image.load(fruit_path),
        'x' : random.randint(100,500),          #where the fruit should be positioned on x-coordinate
        'y' : 800,
        'speed_x': random.randint(-10,10),      #how fast the fruit should move in x direction. Controls the diagonal movement of fruits
        'speed_y': random.randint(-80, -60),    #control the speed of fruits in y-directionn ( UP )
        'throw': False,                         #determines if the generated coordinate of the fruits is outside the gameDisplay or not. If outside, then it will be discarded
        't': 0,                                 #manages the
        'hit': False,
    }

    if random.random() >= 0.75:     #Return the next random floating point number in the range [0.0, 1.0) to keep the fruits inside the gameDisplay
        data[fruit]['throw'] = True
    else:
        data[fruit]['throw'] = False

# Dictionary to hold the data the random fruit generation
data = {}
for fruit in fruits:
    generate_random_fruits(fruit)

def hide_cross_lives(x, y):
    gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))

# Generic method to draw fonts on the screen
font_name = pygame.font.match_font('comic.ttf')
def draw_text(display, text, size, x, y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    gameDisplay.blit(text_surface, text_rect)

# draw players lives
def draw_lives(display, x, y, lives, image) :
    for i in range(lives) :
        img = pygame.image.load(image)
        img_rect = img.get_rect()       #gets the (x,y) coordinates of the cross icons (lives on the the top rightmost side)
        img_rect.x = int(x + 35 * i)    #sets the next cross icon 35pixels awt from the previous one
        img_rect.y = y                  #takes care of how many pixels the cross icon should be positioned from top of the screen
        display.blit(img, img_rect)

# show game over display & front display
def show_gameover_screen():
    gameDisplay.blit(background, (0,0))
    draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)
    if not game_over :
        draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2)

    draw_text(gameDisplay, "Press a key to begin!", 64, WIDTH / 2, HEIGHT * 3 / 4)
    pygame.display.flip()
    waiting = True
    while waiting:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYUP:
                waiting = False

# Game Loop
first_round = True
game_over = True        #terminates the game While loop if more than 3-Bombs are cut
game_running = True     #used to manage the game loop
while game_running :
    if game_over :
        if first_round :
            show_gameover_screen()
            first_round = False
        game_over = False
        player_lives = 3
        draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
        score = 0

    for event in pygame.event.get():
        # checking for closing window
        if event.type == pygame.QUIT:
            game_running = False

    gameDisplay.blit(background, (0, 0))
    gameDisplay.blit(score_text, (0, 0))
    draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')

    for key, value in data.items():
        if value['throw']:
            value['x'] += value['speed_x']          #moving the fruits in x-coordinates
            value['y'] += value['speed_y']          #moving the fruits in y-coordinate
            value['speed_y'] += (1 * value['t'])    #increasing y-corrdinate
            value['t'] += 1                         #increasing speed_y for next loop

            if value['y'] <= 800:
                gameDisplay.blit(value['img'], (value['x'], value['y']))    #displaying the fruit inside screen dynamically
            else:
                generate_random_fruits(key)

            current_position = pygame.mouse.get_pos()   #gets the current coordinate (x, y) in pixels of the mouse

            if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \
                    and current_position[1] > value['y'] and current_position[1] < value['y']+60:
                if key == 'bomb':
                    player_lives -= 1
                    if player_lives == 0:
                        
                        hide_cross_lives(690, 15)
                    elif player_lives == 1 :
                        hide_cross_lives(725, 15)
                    elif player_lives == 2 :
                        hide_cross_lives(760, 15)
                    #if the user clicks bombs for three time, GAME OVER message should be displayed and the window should be reset
                    if player_lives < 0 :
                        show_gameover_screen()
                        game_over = True

                    half_fruit_path = "images/explosion.png"
                else:
                    half_fruit_path = "images/" + "half_" + key + ".png"

                value['img'] = pygame.image.load(half_fruit_path)
                value['speed_x'] += 10
                if key != 'bomb' :
                    score += 1
                score_text = font.render('Score : ' + str(score), True, (255, 255, 255))
                value['hit'] = True
        else:
            generate_random_fruits(key)

    pygame.display.update()
    clock.tick(FPS)      # keep loop running at the right speed (manages the frame/second. The loop should update afer every 1/12th pf the sec
                        

pygame.quit()

Create a new folder and open it in a code editor of your choice. Create a python file and paste the above python code for fruit ninja game. It is the only and main code file of our fruit ninja game.

Now if you try to run it will throw errors because it does not have image and other game assets, so now you have to download the game assets.

To download click the below button it will download a zip file extract it to your project folder it also contains the main python code file.

After downloading and extracting you should have all the files and folder like the below image

Fruit ninja game python folder structure

Now you are ready to run the game. Before that, I have said that we will use the pygame python library for this. Pygame comes installed with the python setup, so you don’t have to manually install it.

But if you get some errors that pygame module not found then you can use the below command to install pygame paste it in the command prompt


pip install pygame

So now we are ready to run the game, let’s see how to do that

#3 Run the fruit ninja game

So to run this game, you need to open your command prompt or terminal at the project folder location and paste the below command in the terminal


python main.py

It will start the game and below is the image of output how this fruit ninja game that we created will look like

Fruit ninja game python output

Above is the image output of how this game will look. I hope you ran the game successfully. This game was done with the help of data-flair website. You can check them out they have many game tutorials.

Summary 

This was the complete tutorial on creating a fruit ninja game in python programming. I hope you enjoyed this tutorial and you ran this game without any errors.

Here are some more python tutorials that might interest you:

I hope you found what you were looking for from this tutorial. Do share it with your friends if they are interested, and also join our Telegram Channel for more future guides like this.

Thanks for reading, have a nice day 🙂