Python Fruit Ninja Game Tutorial Using Pygame
This tutorial walks through creating a simple Fruit Ninja‑style game in Python with pygame, covering package imports, window setup, random fruit generation, drawing text and lives, handling game‑over screens, and implementing the main game loop with scoring and bomb detection.
This article demonstrates how to build a simple Fruit Ninja‑style game in Python using the pygame library.
It begins by importing required modules and configuring the display window, colors, fonts, and background image:
import pygame, sys, os, random
WIDTH = 800
HEIGHT = 500
FPS = 15
pygame.init()
pygame.display.set_caption('水果忍者')
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
background = pygame.image.load('背景.jpg')
font = pygame.font.Font(os.path.join(os.getcwd(),'comic.ttf'),42)
score_text = font.render('Score : '+str(score),True,WHITE)Functions are defined to generate random fruit positions, draw text, display player lives, and show the game‑over screen, each illustrated with full source code:
def generate_random_fruits(fruit):
fruit_path = "images/"+fruit+".png"
data[fruit] = {
'img': pygame.image.load(fruit_path),
'x': random.randint(100,500),
'y': 800,
'speed_x': random.randint(-10,10),
'speed_y': random.randint(-80,-60),
'throw': False,
't': 0,
'hit': False
}
def draw_text(display, text, size, x, y):
font_name = pygame.font.match_font('comic.ttf')
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)
display.blit(text_surface, text_rect)
def draw_lives(display, x, y, lives, image):
for i in range(lives):
img = pygame.image.load(image)
img_rect = img.get_rect()
img_rect.x = int(x + 35 * i)
img_rect.y = y
display.blit(img, img_rect)
def show_gameover_screen():
gameDisplay.blit(background, (0,0))
draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH/2, HEIGHT/4)
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 = FalseThe main game loop manages the game state, processes input events, updates fruit positions, detects cuts and bomb collisions, updates the score, and renders all elements at a fixed frame rate:
first_round = True
game_over = True
game_running = True
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():
if event.type == pygame.QUIT:
game_running = False
# Update fruit positions, handle collisions, draw everything
pygame.display.update()
clock.tick(FPS)
pygame.quit()Additional details such as handling explosions, updating life icons, and restarting the game after a game‑over are also covered, providing a complete, runnable example for beginners interested in game development with Python.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.