Загрузка данных
import pgzrun
import random
from pgzhelper import *
WIDTH = 800
HEIGHT = 600
TITLE = "Fruit Ninja"
explosions = Actor("explosion")
background = Actor("back1")
lives = 3
score = 0
game_over = False
game_begin = True
blade = []
fruits = []
def create_fruit():
fruit_types = ['banana', 'apple', 'mango']
if random.random() < 0.1:
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.5
fruit.x = random.randint(100, WIDTH - 100)
fruit.y = HEIGHT + 50
fruit.speed = random.randint(2, 7)
fruit.sliced = False
fruit.radius = 50
fruit.type = fruit_type
fruit.image_left = left_image
fruit.image_right = right_image
fruit.angle = random.uniform(0, 360) # начальный угол в градусах
fruit.rotation_speed = random.uniform(-3, 30) # скорость вращения в градусах
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 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:
if fruit.type == 'bomb':
# Рисуем взрыв
explosion = Actor("explosion")
explosion.pos = (fruit.x, fruit.y)
explosion.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: {lives}", (15, 15), fontsize=30, color="red")
screen.draw.text(f"SCORE: {score}", (WIDTH - 150, 15), fontsize=30, color="red")
if game_begin:
screen.draw.text("Fruit Ninja", center=(WIDTH // 2, HEIGHT // 2 - 50), fontsize=60,
color="black")
screen.draw.text("Нажмите ПРОБЕЛ чтобы начать",
center=(WIDTH // 2, HEIGHT // 2 + 50), fontsize=30, color="black")
if game_over:
screen.draw.text("ИГРА ОКОНЧЕНА", center=(WIDTH // 2, HEIGHT // 2), fontsize=60, color="red")
screen.draw.text("Нажмите ПРОБЕЛ чтобы начать заново",
center=(WIDTH // 2, HEIGHT // 2 + 50), fontsize=30, color="black")
def is_off_screen(fruit):
return fruit.y < -50 or fruit.y > HEIGHT + 50
def update():
global score,lives, game_over
if game_over:
return
if random.random() < 0.1:
fruits.append(create_fruit())
# Двигаем фрукты вверх
for fruit in list(fruits):
if not fruit.sliced:
fruit.draw()
else :
if fruit.type == 'bomb':
explosion.draw()
explosion.scale = 0.1
explosion.pos = (fruit.x, fruit.y)
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()
# Постепенно теряем жизнь, чтобы добавить напряжения
# Удаляем фрукты, улетевшие за экран
if is_off_screen(fruit):
if not fruit.sliced and fruit.type != 'bomb':
sounds.balloon.play()
lives -= 0.1 # Уменьшаем жизни со временем
if lives <= 0:
game_over = True
fruits.remove(fruit)
if len(blade) > 1:
for i in fruits:
if not i.sliced:
for k in range(1, len(blade)):
if abs(blade[k][0] - i.x) < i.radius and abs(blade[k][1] - i.y) < i.radius:
i.sliced = True
if i.type == "bomb":
sounds.device_error.play()
lives -= 1
if lives <= 0:
game_over = True
pass
else:
score += 10
sounds.asterisk.play()
break
def on_mouse_move(pos):
global game_begin, blade
game_begin = False
blade.append(pos)
if len(blade) == 2:
sounds.uac.play()
if len(blade) > 20:
blade.pop(0)
def on_key_down(key):
global game_over, score, lives, blade
if key == keys.SPACE and game_over:
game_over = False
score = 0
lives = 3
fruits = []
blade.clear()
pgzrun.go()