Загрузка данных
import pgzrun
import random
WIDTH = 800
HEIGHT = 600
TITLE = "Fruit Ninja"
# Цвета
BACKGROUND = (135, 206, 235)
BLADE_COLOR = (255, 255, 255)
fruits = []
blade = []
score = 0
game_over = False
game_begin = True
lives = 3
blade_active = False
background = 'back1.png'
explosion = Actor('explosion')
def reset_game():
global score, lives, game_over, game_begin, fruits, blade
score = 0
lives = 3
game_over = False
game_begin = False
fruits = []
blade.clear()
def create_fruit():
fruit_types = ['banana', 'mango', 'apple']
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 = left_image
fruit = Actor(fruit_type)
fruit.scale = 0.5
fruit.x = random.randint(100, WIDTH - 100)
fruit.y = HEIGHT + 50
fruit.speed = random.randint(5, 10)
fruit.type = fruit_type
fruit.sliced = False
fruit.radius = 30
fruit.angle = random.uniform(0, 360)
fruit.rotation_speed = random.uniform(-3, 3)
fruit.image_left = left_image
fruit.image_right = right_image
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():
screen.blit(background, (0, 0))
if len(blade) > 1:
for i in range(1, len(blade)):
screen.draw.line(blade[i - 1], blade[i], BLADE_COLOR)
for fruit in fruits:
if not fruit.sliced:
fruit.draw()
else:
if fruit.type == 'bomb':
explosion.scale = 0.1
explosion.pos = (fruit.x, fruit.y)
explosion.draw()
else:
left_half = Actor(fruit.image_left)
right_half = Actor(fruit.image_right)
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"Счет: {score}", (10, 10), fontsize=40, color="white")
screen.draw.text(f"Жизни: {'+' * lives}", (WIDTH - 150, 10), fontsize=40, 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 update():
global score, lives, game_over
if game_begin or game_over:
return
for fruit in list(fruits):
update_fruit(fruit)
if is_off_screen(fruit):
if not fruit.sliced and fruit.type != 'bomb':
sounds.critical.play()
lives -= 1
if lives <= 0:
game_over = True
blade.clear()
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()
lives -= 1
else:
score += 10
sounds.asterisk.play()
break
if random.random() < 0.03:
fruits.append(create_fruit())
def on_mouse_down(pos, button):
global blade_active, game_begin
if button == mouse.LEFT:
blade_active = True
if game_begin or game_over:
reset_game()
blade.clear()
blade.append(pos)
def on_mouse_up(pos, button):
global blade_active
if button == mouse.LEFT:
blade_active = False
blade.clear()
def on_mouse_move(pos):
if not blade_active:
return
if game_begin or game_over:
return
blade.append(pos)
if len(blade) == 2:
sounds.print_complete.play()
if len(blade) > 20:
blade.pop(0)
def on_key_down(key):
global score, lives, game_over, game_begin, fruits
if key == keys.SPACE and game_over:
score = 0
lives = 3
game_over = False
game_begin = False
fruits = []
blade.clear()
pgzrun.go()