import random
import pgzero
import pgzrun
from pgzhelper import *
from pgzero.actor import Actor
WIDTH = 800
HEIGHT = 600
TITLE = "Fruit Ninja"
background = Actor("back1")
hp = 3
score = 0
blade = []
fruits = []
def create_fruit():
fruit_types = ['banana', 'apple', 'mango']
if random.random() < 0.001:
fruit_type = 'bomb'
left_image = right_image = 'bomb'
else:
fruit_type = random.choice(fruit_types)
left_image = fruit_type + "_left"
right_image = fruit_type + "_left"
fruit = Actor(fruit_type)
fruit.scale = 0.1
fruit.x = random.randint(100, WIDTH - 100)
fruit.y = HEIGHT + 50
fruit.speed = random.randint(2, 5)
fruit.type = fruit_type
fruit.sliced = False
fruit.radius = 50
fruit.image_left = left_image
fruit.image_right = right_image
fruit.angle = random.uniform(0, 360)
fruit.rotation_speed = random.uniform(-3, 3)
return fruit
def update_fruit(fruit):
"""Обновляет позицию и вращение фрукта"""
if not fruit.sliced:
fruit.y -= fruit.speed
fruit.angle = (fruit.angle + fruit.rotation_speed) % 360 # обновляем угол
else:
if fruit.type == 'bomb':
pass # Бомба не двигается после взрыва
else:
fruit.y += fruit.speed * 0.8
fruit.x += fruit.speed * 0.5 * (1 if random.random() > 0.5 else -1)
return fruit
def is_off_screen(fruit):
"""Проверяет, вышел ли фрукт за пределы экрана"""
return fruit.y < -50 or fruit.y > HEIGHT + 50
def draw():
background.draw()
for i in range(1, len(blade)):
screen.draw.line(blade[i-1], blade[i], "white")
for fruit in fruits:
if not fruit.sliced:
fruit.draw()
else:
# Рисуем две половинки
left_half = Actor(fruit.image_left)
right_half = Actor(fruit.image_left)
left_half.scale = 0.5
right_half.scale = 0.5
left_half.pos = (fruit.x - 15, fruit.y)
right_half.pos = (fruit.x + 15, fruit.y)
left_half.angle = (fruit.angle - 10) % 360
right_half.angle = (fruit.angle + 10) % 360
left_half.draw()
right_half.draw()
screen.draw.text(f"HP: {hp}", (15, 15), fontsize=30, color="red")
screen.draw.text(f"score: {score}", (WIDTH - 150, 15), fontsize=30, color="red")
def update():
global score
if random.random() < 0.01:
fruits.append(create_fruit())
# Двигаем фрукты вверх
for fruit in list(fruits):
update_fruit(fruit)
# Удаляем фрукты, улетевшие за экран
if is_off_screen(fruit):
if not fruit.sliced and fruit.type != 'bomb':
sounds.critical.play()
fruits.remove(fruit)
if len(blade) > 1:
for fruit in list(fruits):
if not fruit.sliced:
for i in range(1, len(blade)):
if (abs(blade[i][0] - fruit.x) < fruit.radius and
abs(blade[i][1] - fruit.y) < fruit.radius):
fruit.sliced = True
if fruit.type == 'bomb':
sounds.device_error.play()
pass
else:
score += 1
sounds.asterisk.play()
break
def on_mouse_move(pos):
blade.append(pos)
if len(blade) > 2:
blade.pop(0)
pgzrun.go()