Skip to content

Commit f5389df

Browse files
Added Fruit Ninja Game
1 parent 0c1da81 commit f5389df

20 files changed

+213
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## FRUIT NINJA GAME 🍉💥
2+
3+
# DESCRIPTION
4+
5+
This is a fruit ninja fun game designed using pygame, player has to slice the fruits and get scores but beware of bombs!!💣💥
6+
7+
## Aim & Purpose
8+
9+
This game is designed using pygame to apply the concept of random module and it is designed for entertainment purpose.
10+
11+
## Detailed Explaination Of Script
12+
13+
The player has to just run the program press any key on the keyboard and get ready to play the very popular game Fruit Ninja 🍐🍉. Player has to slice the fruits which will be randomly shown on the screen and there would be three lives with the person. As soon as, the lives are finished the game will stop and Game Over screen will splash.
14+
15+
The user has to slice the fruits simply by moving their mouse cursor over the fruits 🍉 and just be aware of bombs 💣 because if the cursor is over the bomb then it will explode 💥 and player will loose one life. The user can see the fruits sliced in two parts when they move cursor over it.
16+
17+
On the game screen in the top left corner you could see your scrore and on the top right corner you can see your remaining lives ❌❌❌. At maximum you will get 3 lives so use them safely. If the player loses all three lives then a screen will splash and you can see a message of game over. You can even replay by pressing any key on the keyboard.
18+
19+
## SCREENSHOTS:
20+
21+
First Screen 🍉💥
22+
23+
![Fruit Ninja Game](images/FirstScreen)
24+
25+
Game Screen 🍉💥❌
26+
27+
![Fruit Ninja Game](images/GameScreen)
28+
29+
Game Over Screen ❌💥
30+
31+
![Fruit Ninja Game](images/GameOverScreen)
32+
33+
Functioning
34+
35+
![Fruit Ninja Game](images/Functioning)
36+
37+
38+
## AUTHOR:
39+
KHUSHI SHARMA
40+
41+
42+
43+
44+
45+
46+
47+
48+
240 KB
Binary file not shown.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import pygame, sys
2+
import os
3+
import random
4+
5+
player_lives = 3 #keep track of lives
6+
score = 0 #keeps track of score
7+
fruits = ['melon','pear','orange','pomegranate','bomb'] #entities in the game
8+
9+
# initialize pygame and create window
10+
w=800
11+
h=600
12+
FPS = 12 #The game display will refresh every 1/12th second
13+
pygame.init()
14+
pygame.display.set_caption('Fruit-Ninja Game')
15+
gameDisplay = pygame.display.set_mode((w,h)) #sets game display size
16+
clock = pygame.time.Clock()
17+
18+
# Define colors
19+
WHITE = (255,255,255)
20+
BLACK = (0,0,0)
21+
RED = (255,0,0)
22+
GREEN = (0,255,0)
23+
BLUE = (0,0,255)
24+
25+
background = pygame.image.load('images/background.png') #game background
26+
font = pygame.font.Font(os.path.join(os.getcwd(),'fontdesign.ttf'),42) #font of game display
27+
score_text = font.render('Score : '+str(score),True,(255,255,255)) #score display
28+
29+
# Generalized structure of the fruit Dictionary
30+
def generate_random_fruits(fruit):
31+
fruit_path="images/"+fruit +".png"
32+
# 'x' determines where the fruit should be positioned on x-coordinate
33+
# 'speed_x' determines how fast the fruit should move in x direction.Controls the diagonal movement of fruits
34+
# 'speed_y' controls the speed of fruits in y-direction (UPWARD)
35+
# 'throw':False determines if the generated coordinate of the fruits is outside the gameDisplay or not.If outside,then it will be discarded.
36+
data[fruit]={
37+
'img': pygame.image.load(fruit_path),'x':random.randint(100,500),'y':800,
38+
'speed_x': random.randint(-10,10),'speed_y': random.randint(-80,-60),
39+
'throw': False,'t': 0,'hit': False
40+
}
41+
42+
#Return the next random floating point number in the range [0.0, 1.0) to keep the fruits inside the gameDisplay
43+
if (random.random() >= 0.75):
44+
data[fruit]['throw'] = True
45+
else:
46+
data[fruit]['throw'] = False
47+
48+
# Dictionary to hold the data the random fruit generation
49+
data = {}
50+
for fruit in fruits:
51+
generate_random_fruits(fruit)
52+
53+
def hide_cross_lives(x, y):
54+
gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))
55+
56+
# Generic method to draw fonts on the screen
57+
font_name = pygame.font.match_font('fontdesign.ttf')
58+
def draw_text(display, text, size, x, y):
59+
font = pygame.font.Font(font_name, size)
60+
text_surface = font.render(text, True, WHITE)
61+
text_rect = text_surface.get_rect()
62+
text_rect.midtop=(x,y)
63+
gameDisplay.blit(text_surface, text_rect)
64+
65+
# draw players lives
66+
def draw_lives(display, x, y, lives, image) :
67+
for i in range(lives) :
68+
img = pygame.image.load(image)
69+
img_rect = img.get_rect() #gets the (x,y) coordinates of the cross icons (lives on the the top rightmost side)
70+
img_rect.x = int(x + 35 * i) #position of lives icon (sets the next cross icon 35pixels awt from the previous one)
71+
img_rect.y = y #takes care of how many pixels the cross icon should be positioned from top of the screen
72+
display.blit(img, img_rect)
73+
74+
# show game over display & front display
75+
def show_gameover_screen():
76+
gameDisplay.blit(background, (0,0))
77+
draw_text(gameDisplay, "FRUIT NINJA!", 90, w/2,h/4)
78+
if not game_over :
79+
draw_text(gameDisplay,"Score : " + str(score), 50, w/2,h/2)
80+
draw_text(gameDisplay, "Game Over!!", 50,w/2,h*5/8)
81+
82+
draw_text(gameDisplay, "Press any key to play!", 64,w/2,h*3/4)
83+
pygame.display.flip()
84+
waiting = True
85+
while waiting:
86+
clock.tick(FPS)
87+
for event in pygame.event.get():
88+
if event.type == pygame.QUIT:
89+
sys.exit()
90+
91+
if event.type == pygame.KEYUP:
92+
waiting = False
93+
94+
# Game Loop
95+
first_round = True
96+
game_over = True #terminates the game while loop if more than 3-Bombs are cut
97+
game_running = True #used to manage the game loop
98+
while game_running :
99+
if game_over :
100+
if first_round :
101+
show_gameover_screen()
102+
first_round = False
103+
game_over = False
104+
player_lives = 3
105+
draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
106+
score = 0
107+
108+
for event in pygame.event.get():
109+
# checking for closing window
110+
if event.type == pygame.QUIT:
111+
game_running = False
112+
113+
gameDisplay.blit(background, (0, 0))
114+
gameDisplay.blit(score_text, (0, 0))
115+
draw_lives(gameDisplay, 690, 5, player_lives,'images/red_lives.png')
116+
117+
for key, value in data.items():
118+
if value['throw']:
119+
value['x'] += value['speed_x'] #moving the fruits in x-coordinates
120+
value['y'] += value['speed_y'] #moving the fruits in y-coordinate
121+
value['speed_y'] += (1 * value['t']) #increasing y-corrdinate
122+
value['t'] += 1 #increasing speed_y for next loop
123+
124+
if value['y'] <= 800:
125+
gameDisplay.blit(value['img'], (value['x'], value['y'])) #dynamic display of fruit inside display screen
126+
else:
127+
generate_random_fruits(key)
128+
129+
current_position=pygame.mouse.get_pos() #gets the current coordinate(x,y) in pixels of the mouse
130+
131+
if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \
132+
and current_position[1] > value['y'] and current_position[1] < value['y']+60:
133+
if key == 'bomb':
134+
player_lives -= 1
135+
if player_lives == 0:
136+
137+
hide_cross_lives(690, 15)
138+
elif player_lives == 1 :
139+
hide_cross_lives(725, 15)
140+
elif player_lives == 2 :
141+
hide_cross_lives(760, 15)
142+
#if the user clicks bombs for three time,GAME OVER message should be displayed and the window should be reset
143+
if (player_lives < 0):
144+
show_gameover_screen()
145+
game_over = True
146+
147+
half_fruit_path = "images/explosion.png"
148+
else:
149+
half_fruit_path = "images/" + "half_" + key + ".png"
150+
151+
value['img'] = pygame.image.load(half_fruit_path)
152+
value['speed_x'] += 10
153+
if key != 'bomb' :
154+
score += 1
155+
score_text = font.render('Score : ' + str(score), True, (255, 255, 255))
156+
value['hit'] = True
157+
else:
158+
generate_random_fruits(key)
159+
160+
pygame.display.update()
161+
clock.tick(FPS) #helps the loop to run at the right speed
162+
163+
164+
pygame.quit()
Loading
Binary file not shown.
Loading
Loading
Loading
2.76 KB
Loading
Loading

0 commit comments

Comments
 (0)