не отталкивается мяч от верхней и нижней границы
и ещё не идет в случайную сторону когда не отбивается от ракетки
import pygame
import random
import time
pygame.init()
DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600
FPS = 30
PLAYER_1_WIDTH = 20
PLAYER_1_HEIGHT = 100
PLAYER_1_X = 50
PLAYER_1_Y = 300
PLAYER_2_HEIGHT = 100
PLAYER_2_WIDTH = 20
PLAYER_2_X = 730
PLAYER_2_Y = 300
BALL_RADIUS = 12
BALL_X = 400
BALL_Y = 300
BALL_SPEED_X = 12
BALL_SPEED_Y = random.randint(-12, 12)
display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("dino")
clock = pygame.time.Clock()
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
def run_game():
game = True
global PLAYER_1_Y, PLAYER_1_X, PLAYER_2_X, PLAYER_2_Y, BALL_X, BALL_Y, BALL_SPEED_X, BALL_SPEED_Y
while game:
# Контроль фпс
clock.tick(FPS)
# Обработка событий
for event in pygame.event.get():
#
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Обновление
keys = pygame.key.get_pressed()
if BALL_Y == 10:
BALL_SPEED_Y = -BALL_SPEED_Y
elif BALL_Y == 500:
BALL_SPEED_Y = -BALL_SPEED_Y
if keys[pygame.K_UP]:
PLAYER_2_Y -= 12
if PLAYER_2_Y <= 0:
PLAYER_2_Y = 0
elif keys[pygame.K_DOWN]:
PLAYER_2_Y += 12
if PLAYER_2_Y >= 500:
PLAYER_2_Y = 500
if keys[pygame.K_w]:
PLAYER_1_Y -= 12
if PLAYER_1_Y <= 0:
PLAYER_1_Y = 0
elif keys[pygame.K_s]:
PLAYER_1_Y += 12
if PLAYER_1_Y >= 550:
PLAYER_1_Y = 50
BALL_X += BALL_SPEED_X
BALL_Y += BALL_SPEED_Y
if (BALL_X < PLAYER_1_X) or (BALL_X > PLAYER_2_X):
BALL_X = 400
BALL_Y = 300
time.sleep(2)
BALL_X += BALL_SPEED_X
BALL_Y += BALL_SPEED_Y
# Рендеринг
display.fill((0, 0, 0))
player_1 = pygame.draw.rect(display, (103, 194, 95), (PLAYER_1_X, PLAYER_1_Y, PLAYER_1_WIDTH, PLAYER_1_HEIGHT))
player_2 = pygame.draw.rect(display, (103, 194, 95), (PLAYER_2_X, PLAYER_2_Y, PLAYER_2_WIDTH, PLAYER_2_HEIGHT))
ball = pygame.draw.circle(display, (103, 194, 95), (BALL_X, BALL_Y), BALL_RADIUS)
players = [player_1, player_2]
if ball.colliderect(players[1]):
BALL_SPEED_X = -BALL_SPEED_X
if ball.colliderect(players[0]):
BALL_SPEED_X = -BALL_SPEED_X
# Показ экрана пользователю
pygame.display.flip()
run_game()