text stringlengths 0 2.87M |
|---|
leftPaddleState = IDLE; |
} |
// Обработка ввода для правой ракетки |
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { |
rightPaddleState = UP; |
} |
else if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) { |
rightPaddleState = DOWN; |
} |
else { |
rightPaddleState = IDLE; |
} |
} |
// Функция для обновления игровых объектов |
void UpdateGameObjects(float deltaTime) { |
// Обновление мяча |
ball.x += ball.velocityX * deltaTime; |
ball.y += ball.velocityY * deltaTime; |
// Обновление левой ракетки |
switch (leftPaddleState) { |
case UP: |
leftPaddle.velocityY = -PADDLE_VELOCITY; |
break; |
case DOWN: |
leftPaddle.velocityY = PADDLE_VELOCITY; |
break; |
default: |
leftPaddle.velocityY = 0; |
break; |
} |
leftPaddle.y += leftPaddle.velocityY * deltaTime; |
// Обновление правой ракетки |
switch (rightPaddleState) { |
case UP: |
rightPaddle.velocityY = -PADDLE_VELOCITY; |
break; |
case DOWN: |
rightPaddle.velocityY = PADDLE_VELOCITY; |
break; |
default: |
rightPaddle.velocityY = 0; |
break; |
} |
rightPaddle.y += rightPaddle.velocityY * deltaTime; |
// Обработка столкновений со стенами |
if (ball.y < BALL_RADIUS || ball.y > WINDOW_HEIGHT - BALL_RADIUS) { |
ball.velocityY = -ball.velocityY; |
} |
// Обработка столкновений с ракетками |
if (ball.x < leftPaddle.x + leftPaddle.width + BALL_RADIUS && ball.x > leftPaddle.x + leftPaddle.width - COLLISION_THRESHOLD && ball.y > leftPaddle.y - BALL_RADIUS && ball.y < leftPaddle.y + leftPaddle.height + BALL_RADIUS) { |
float relativeIntersectY = (leftPaddle.y + leftPaddle.height / 2) - ball.y; |
float normalizedRelativeIntersectY = relativeIntersectY / (leftPaddle.height / 2); |
float bounceAngle = normalizedRelativeIntersectY * BALL_MAX_ANGLE; |
ball.velocityX = BALL_VELOCITY * cosf(bounceAngle); |
ball.velocityY = BALL_VELOCITY * sinf(bounceAngle); |
} |
if (ball.x > rightPaddle.x - BALL_RADIUS && ball.x < rightPaddle.x + COLLISION_THRESHOLD && ball.y > rightPaddle.y - BALL_RADIUS && ball.y < rightPaddle.y + rightPaddle.height + BALL_RADIUS) { |
float relativeIntersectY = (rightPaddle.y + rightPaddle.height / 2) - ball.y; |
float normalizedRelativeIntersectY = relativeIntersectY / (rightPaddle.height / 2); |
float bounceAngle = normalizedRelativeIntersectY * BALL_MAX_ANGLE; |
ball.velocityX = -BALL_VELOCITY * cosf(bounceAngle); |
ball.velocityY = BALL_VELOCITY * sinf(bounceAngle); |
} |
// Обработка вылета мяча за пределы поля |
if (ball.x < 0 || ball.x > WINDOW_WIDTH) { |
std::cout << "Game over!" << std::endl; |
glfwSetWindowShouldClose(window, true); |
} |
} |
// Функция для отрисовки игровых объектов |
void DrawGameObjects() { |
// Очистка экрана |
glClear(GL_COLOR_BUFFER_BIT); |
// Отрисовка мяча |
glPushMatrix(); |
glTranslatef(ball.x, ball.y, 0.0f); |
glColor3f(1.0f, 1.0f, 1.0f); |
glBegin(GL_TRIANGLE_FAN); |
for (int i = 0; i < 360; i++) { |
float theta = i * 3.1415926f / 180.0f; |
glVertex2f(BALL_RADIUS * cosf(theta), BALL_RADIUS * sinf(theta)); |
} |
glEnd(); |
glPopMatrix(); |
// Отрисовка левой ракетки |
glPushMatrix(); |
glTranslatef(leftPaddle.x, leftPaddle.y, 0.0f); |
glColor3f(1.0f, 1.0f, 1.0f); |
glBegin(GL_QUADS); |
glVertex2f(0.0f, 0.0f); |
glVertex2f(leftPaddle.width, 0.0f); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.