Загрузка данных
import pgzrun
import random
WIDTH = 800
HEIGHT = 400
TITLE = 'Volleyball'
player = Actor('player', (100, HEIGHT//2))
bot = Actor('player', (WIDTH - 100, HEIGHT//2))
ball = Actor('ball', (WIDTH//2, HEIGHT//2))
ball_speed_x = 3
ball_speed_y = 2
bot_speed = 4
player_score = 0
bot_score = 0
is_smashing = False
is_blocking = False
is_jumping = False
action_timer = 0
jump_height = 0
original_y = HEIGHT//2
def reset_ball():
global ball_speed_x, ball_speed_y
ball.center = (WIDTH//2, HEIGHT//2)
ball_speed_x = 3 * (1 if random.random() > 0.5 else -1)
ball_speed_y = 2 * (1 if random.random() > 0.5 else -1)
def draw():
screen.blit('beachbkgo', (0, 0))
screen.blit('beachbkglso', (WIDTH//2, 0))
player.draw()
bot.draw()
ball.draw()
screen.draw.text(f"{player_score} : {bot_score}",
center=(WIDTH//2, 30),
fontsize=50,
color="white",
shadow=(1,1))
screen.draw.text("W/S - двигаться | X - удар | C - блок | V - прыжок",
(10, HEIGHT-30),
color="white",
fontsize=20)
if is_smashing:
screen.draw.text("SMASH!", (player.x - 30, player.y - 40), color="red", fontsize=25)
elif is_blocking:
screen.draw.text("BLOCK!", (player.x - 30, player.y - 40), color="blue", fontsize=25)
elif is_jumping:
screen.draw.text("JUMP!", (player.x - 30, player.y - 40), color="yellow", fontsize=25)
def update():
global ball_speed_x, ball_speed_y, player_score, bot_score
global is_smashing, is_blocking, is_jumping, action_timer, jump_height, original_y
if action_timer > 0:
action_timer -= 1
if action_timer <= 0:
is_smashing = False
is_blocking = False
is_jumping = False
player.image = 'player'
if player.y != original_y:
player.y = original_y
if is_jumping:
jump_height += 4
if jump_height <= 30:
player.y = original_y - jump_height
else:
player.y = original_y - (60 - jump_height)
if jump_height >= 60:
is_jumping = False
jump_height = 0
player.y = original_y
if ball.y < bot.y - 10:
bot.y -= bot_speed
elif ball.y > bot.y + 10:
bot.y += bot_speed
if bot.top < 0:
bot.top = 0
if bot.bottom > HEIGHT:
bot.bottom = HEIGHT
ball.x += ball_speed_x
ball.y += ball_speed_y
if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_speed_y = -ball_speed_y
if ball.colliderect(player) and ball_speed_x < 0:
ball_speed_x = -ball_speed_x
if is_smashing:
ball_speed_x = min(ball_speed_x * 1.5, 8)
ball_speed_y = min(ball_speed_y * 1.3, 7)
is_smashing = False
action_timer = 0
if is_blocking:
ball_speed_x = ball_speed_x * 1.2
is_blocking = False
action_timer = 0
if is_jumping:
offset = (ball.y - player.y) / 15
ball_speed_y += offset * 2
is_jumping = False
jump_height = 0
player.y = original_y
offset = (ball.y - player.y) / 20
ball_speed_y += offset
ball_speed_x = max(-8, min(8, ball_speed_x))
ball_speed_y = max(-6, min(6, ball_speed_y))
if ball.colliderect(bot) and ball_speed_x > 0:
ball_speed_x = -ball_speed_x
offset = (ball.y - bot.y) / 20
ball_speed_y += offset
ball_speed_y = max(-5, min(5, ball_speed_y))
if ball.left <= 0:
bot_score += 1
reset_ball()
if ball.right >= WIDTH:
player_score += 1
reset_ball()
def on_key_down(key):
global is_smashing, is_blocking, is_jumping, action_timer, jump_height, original_y
if key == keys.W:
animate(player, duration=0.1, y=max(0, player.y - 50))
elif key == keys.S:
animate(player, duration=0.1, y=min(HEIGHT, player.y + 50))
elif key == keys.X:
if not is_blocking and not is_jumping:
is_smashing = True
is_blocking = False
is_jumping = False
action_timer = 10
player.image = 'player'
elif key == keys.C:
if not is_smashing and not is_jumping:
is_blocking = True
is_smashing = False
is_jumping = False
action_timer = 8
player.image = 'player'
elif key == keys.V:
if not is_smashing and not is_blocking and not is_jumping:
is_jumping = True
is_smashing = False
is_blocking = False
jump_height = 0
original_y = player.y
def on_key_up(key):
pass
pgzrun.go()