import pygame
from pygame import image
pygame.init()
# Константы
SCREEN_WIDTH, SCREEN_HEIGHT = 600, 800
FPS = 30
BLUE_WIDTH, BLUE_HEIGHT = 50, 50
PUCK_WIDTH, PUCK_HEIGHT = 35, 35
# Цвета
WHITE = (255, 255, 255)
GRAY = (50, 50, 50)
GREEN = (0, 200, 0)
BROWN = (139, 69, 19)
BLUE = (0, 0, 255)
ZONE_WIDTH = 400
ZONE_HEIGHT = 600
GATE_WIDTH, GATE_HEIGHT = 100, 10
# Настройка экрана
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Air Hockey")
clock = pygame.time.Clock()
background = pygame.transform.scale(pygame.image.load("assets\\background.png"), (ZONE_WIDTH, ZONE_HEIGHT))
bluepad = pygame.transform.scale(pygame.image.load("assets\\bluepad.png"), (BLUE_WIDTH, BLUE_HEIGHT))
puck = pygame.transform.scale(pygame.image.load("assets\\puck.png"), (PUCK_WIDTH, PUCK_HEIGHT))
redpad = pygame.transform.scale(pygame.image.load("assets\\redpad.png"), (SCREEN_WIDTH, SCREEN_HEIGHT))
pos = pygame.mouse.get_pos()
STICK_RADIUS = 5
BALL_RADIUS = 10
bluepad_speed_y = 10
ball_speed_x = 2
ball_speed_y = 2
LOWER, UPPER = "lower", "upper"
HOME, AWAY = "Player 1", "Player 2"
START_SCORE = {HOME: 0, AWAY: 0}
score = [0, 0]
class Ball():
def __init__(self, x, y, radius):
self.dx = 0
self.dy = 0
self.friction = 0.95
self.rect = pygame.Rect(x, y, radius * 2, radius * 2)
def draw(self):
screen.blit(puck, self.rect.center)
def update(self):
# Добавляем инерцию
self.dx *= self.friction
self.dy *= self.friction
self.rect.x += self.dx
self.rect.y += self.dy
def check_zone_collision(self, zone):
if not self.rect.top > zone.rect.top:
self.rect.top = zone.rect.top
self.dy = -self.dy
elif not self.rect.bottom < zone.rect.bottom:
self.rect.bottom = zone.rect.bottom
self.dy = -self.dy
if not self.rect.left > zone.rect.left:
self.rect.left = zone.rect.left
self.dx = -self.dx
elif not self.rect.right < zone.rect.right:
self.rect.right = zone.rect.right
self.dx = -self.dx
class Gate():
def __init__(self, x, y):
self.x = x
self.y = y
self.rect = pygame.Rect(x, y, GATE_WIDTH, GATE_HEIGHT)
def draw(self):
pygame.draw.rect(screen, BROWN, self.rect)
class Stick():
def __init__(self, x, y, radius):
self.dragging = False
self.dx = 0
self.dy = 0
self.friction = 0.95
self.rect = pygame.Rect(x, y, radius * 2, radius * 2)
def draw(self):
pygame.draw.circle(screen, BROWN, self.rect.center, self.rect.width // 2)
screen.blit(bluepad, self.rect.center)
def update(self):
if not self.dragging:
# Добавляем инерцию
self.dx *= self.friction
self.dy *= self.friction
self.rect.x += self.dx
self.rect.y += self.dy
def check_zone_collision(self, zone):
if not self.rect.top > zone.rect.top:
self.rect.top = zone.rect.top
self.dy = -self.dy
elif not self.rect.bottom < zone.rect.bottom:
self.rect.bottom = zone.rect.bottom
self.dy = -self.dy
if not self.rect.left > zone.rect.left:
self.rect.left = zone.rect.left
self.dx = -self.dx
elif not self.rect.right < zone.rect.right:
self.rect.right = zone.rect.right
self.dx = -self.dx
class Bot():
def __init__(self, x, y, radius):
self.dragging = False
self.dx = 0
self.dy = 0
self.friction = 0.95
self.redpad = pygame.transform.scale(pygame.image.load("assets\\redpad.png"), (BLUE_WIDTH, BLUE_HEIGHT))
self.rect = pygame.Rect(x, y, radius * 2, radius * 2)
def draw(self):
screen.blit(self.redpad, self.rect.center)
pygame.draw.circle(screen, GREEN,
self.rect.center, self.rect.width // 2)
def move_bot(self):
if ball.rect.center > self.rect.center and ball.rect.centerx > 450:
self.dy += 1
if ball.rect.center < self.rect.center and ball.rect.centerx > 450:
self.dy -= 1
def update(self):
if not self.dragging:
# Добавляем инерцию
self.dx *= self.friction
self.dy *= self.friction
self.rect.x += self.dx
self.rect.y += self.dy
def check_zone_collision(self, zone):
if not self.rect.top > zone.rect.top:
self.rect.top = zone.rect.top
self.dy = -self.dy
elif not self.rect.bottom < zone.rect.bottom:
self.rect.bottom = zone.rect.bottom
self.dy = -self.dy
if not self.rect.left > zone.rect.left:
self.rect.left = zone.rect.left
self.dx = -self.dx
elif not self.rect.right < zone.rect.right:
self.rect.right = zone.rect.right
self.dx = -self.dx
class Zone():
def __init__(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height)
def draw(self):
screen.blit(background, self.rect)
stick = Stick(250, 500, STICK_RADIUS)
ball = Ball(275, 380, BALL_RADIUS)
zone = Zone(100, 100, ZONE_WIDTH, ZONE_HEIGHT)
bot = Bot(250, 250, STICK_RADIUS)
gate_enemy = Gate(250, 100)
gate_my = Gate(250, ZONE_HEIGHT + 100)
# Основной цикл игры
running = True
game_status = 'game'
bluepad_speed_y = 0
font_score = pygame.font.Font(None, 40)
while running:
screen.fill(BLUE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and stick.rect.collidepoint(event.pos):
stick.dragging = True
bluepad_speed_y -= 1
if game_status == 'menu':
game_status = 'game'
score = [0, 0]
elif event.type == pygame.MOUSEBUTTONUP:
stick.dragging = False
bluepad_speed_y += 1
elif event.type == pygame.MOUSEMOTION:
if stick.dragging:
dx, dy = event.rel
stick.rect.x += dx
stick.rect.y += dy
stick.dx = dx
stick.dy = dy
zone.draw()
gate_my.draw()
gate_enemy.draw()
stick.update()
stick.draw()
stick.check_zone_collision(zone)
ball.update()
ball.draw()
ball.check_zone_collision(zone)
bot.draw()
bot.move_bot()
if gate_enemy.rect.colliderect(ball.rect):
score[0] += 1
ball.rect = pygame.Rect(275, 380, BALL_RADIUS * 2, BALL_RADIUS * 2)
# running = False
if score[0] == 10:
running = False
print(f'\nКонец игры, забито 10 голов')
if gate_my.rect.colliderect(ball.rect):
running = False
if ball.rect.colliderect(stick.rect):
ball.dx = stick.dx
ball.dy = stick.dy
stick.dx = -stick.dx
stick.dy = -stick.dy
keys = pygame.key.get_pressed()
font_score.render(f"Счет {score}", True, WHITE)
message_score = font_score.render(f"Счет {score[0]} : {score[1]}", True, WHITE)
# message_rect = message_score.get_rect(center=(290, SCREEN_HEIGHT - 50))
message_rect = message_score.get_rect(center=(100, 60)) # +
screen.blit(message_score, message_rect)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()