import pygame
import sys
import random
DISPLAY_WIDTH=1500
DISPLAY_HEIGHT=720
COLOR_GREEN=(0,244,227)
PLAYER_HEIGHT=30
PLAYER_WIDTH=100
BALL_SIZE=30
Display=pygame.display.set_mode((DISPLAY_WIDTH,DISPLAY_HEIGHT))
def finish():
pygame.quit()
sys.exit()
clock=pygame.time.Clock()
def main():
#создаём прямоугольник буказываем в скобках откуда начать нарисовать и размеры
player=pygame.Rect(DISPLAY_WIDTH/2,DISPLAY_HEIGHT-40,PLAYER_WIDTH,PLAYER_HEIGHT)
ball=pygame.Rect(DISPLAY_WIDTH/2,DISPLAY_HEIGHT/2,BALL_SIZE,BALL_SIZE)
enemy=pygame.Rect(DISPLAY_WIDTH/2,40,PLAYER_WIDTH,PLAYER_HEIGHT)
x_ball_speed = 0
y_ball_speed = 0
while not x_ball_speed:
finish()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x > 0:
player.move_ip(-15, 0)
elif keys[pygame.K_RIGHT] and player.x < DISPLAY_WIDTH:
player.move_ip(15, 0)
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and enemy.x > 0:
enemy.move_ip(-15, 0)
elif keys[pygame.K_d] and enemy.x < DISPLAY_WIDTH:
enemy.move_ip(15, 0)
ball.move_ip(x_ball_speed, y_ball_speed)
if ball.x > DISPLAY_WIDTH - BALL_SIZE / 2:
x_ball_speed = random.randint(-10, -1)
if ball.x < BALL_SIZE / 2:
x_ball_speed = random.randint(1, 10)
if ball.y > DISPLAY_HEIGHT:
return
if ball.y < 0:
return
if ball.colliderect(enemy):
y_ball_speed = 10
if ball.colliderect(player):
y_ball_speed = -10
Display.fill(COLOR_GREEN)
pygame.draw.rect(Display,(0,10,255),player)
pygame.draw.rect(Display,(0,254,0),ball)
pygame.draw.rect(Display,(254,0,0),enemy)
pygame.display.update()
clock.tick(25)
main()