/
Kotopeska
/
RiseUpGame
Обзор
Документация
Войти
/
Kotopeska
/
RiseUpGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
view/GameView.cpp
292 строки
8 KB
Kotopeska
init
18 май 2025, 19:03
18 май 2025, 19:03
668dfef
Код
Авторство
О чём код?
#include "GameView.h" #include <FL/fl_draw.H> #include <sstream> #include <cmath> namespace View { GameView::GameView(int x, int y, int w, int h, Model::GameModel &model) : ViewBase(x, y, w, h, model), gameBox(std::make_unique<Fl_Box>(x, y, w, h)) { gameBox->box(FL_FLAT_BOX); add(gameBox.get()); } GameView::~GameView() { } void GameView::update() { redraw(); } void GameView::draw() { drawBackground(); drawObstacles(); drawBall(); drawShield(); drawScore(); if (model.getState() == Model::GameState::GAME_OVER) { drawGameOver(); } } void GameView::drawBackground() { ViewBase::drawBackground(); } void GameView::drawBall() { const auto &ball = model.getBall(); float centerX = ball.getX() + ball.getRadius(); float centerY = ball.getY() + ball.getRadius(); float radius = ball.getRadius(); fl_color(fl_rgb_color(64, 156, 255)); fl_pie(ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight(), 0, 360); fl_color(fl_rgb_color(128, 192, 255)); fl_pie(centerX - radius * 0.7f, centerY - radius * 0.7f, radius * 1.4f, radius * 1.4f, 0, 360); fl_color(FL_WHITE); fl_pie(centerX - radius * 0.3f, centerY - radius * 0.3f, radius * 0.6f, radius * 0.6f, 0, 360); } void GameView::drawObstacles() { const auto &obstacles = model.getObstacles(); for (const auto &obstacle : obstacles) { Fl_Color color; switch (obstacle.getColor()) { case Model::ObstacleColor::RED: color = fl_rgb_color(255, 64, 64); break; case Model::ObstacleColor::GREEN: color = fl_rgb_color(64, 255, 64); break; case Model::ObstacleColor::BLUE: color = fl_rgb_color(64, 64, 255); break; case Model::ObstacleColor::YELLOW: color = fl_rgb_color(255, 255, 64); break; case Model::ObstacleColor::PURPLE: color = fl_rgb_color(192, 64, 192); break; case Model::ObstacleColor::ORANGE: color = fl_rgb_color(255, 128, 0); break; case Model::ObstacleColor::CYAN: color = fl_rgb_color(0, 192, 192); break; default: color = fl_rgb_color(255, 255, 255); break; } fl_color(color); fl_push_matrix(); fl_translate(obstacle.getX(), obstacle.getY()); fl_translate(obstacle.getWidth() / 2, obstacle.getHeight() / 2); fl_rotate(obstacle.getRotationAngle()); fl_translate(-obstacle.getWidth() / 2, -obstacle.getHeight() / 2); switch (obstacle.getType()) { case Model::ObstacleType::RECTANGLE: { fl_begin_polygon(); fl_vertex(0, 0); fl_vertex(obstacle.getWidth(), 0); fl_vertex(obstacle.getWidth(), obstacle.getHeight()); fl_vertex(0, obstacle.getHeight()); fl_end_polygon(); fl_color(FL_BLACK); fl_begin_line(); fl_vertex(0, 0); fl_vertex(obstacle.getWidth(), 0); fl_vertex(obstacle.getWidth(), obstacle.getHeight()); fl_vertex(0, obstacle.getHeight()); fl_vertex(0, 0); fl_end_line(); break; } case Model::ObstacleType::CIRCLE: { float radius = obstacle.getWidth() / 2; float centerX = radius; float centerY = radius; fl_begin_polygon(); for (int i = 0; i < 360; i += 10) { float angle = i * M_PI / 180.0f; fl_vertex(centerX + radius * cos(angle), centerY + radius * sin(angle)); } fl_end_polygon(); fl_color(FL_BLACK); fl_begin_line(); for (int i = 0; i <= 360; i += 10) { float angle = i * M_PI / 180.0f; fl_vertex(centerX + radius * cos(angle), centerY + radius * sin(angle)); } fl_end_line(); break; } case Model::ObstacleType::TRIANGLE: { float halfW = obstacle.getWidth() / 2.0f; fl_begin_polygon(); fl_vertex(0, obstacle.getHeight()); fl_vertex(halfW, 0); fl_vertex(obstacle.getWidth(), obstacle.getHeight()); fl_end_polygon(); fl_color(FL_BLACK); fl_begin_line(); fl_vertex(0, obstacle.getHeight()); fl_vertex(halfW, 0); fl_vertex(obstacle.getWidth(), obstacle.getHeight()); fl_vertex(0, obstacle.getHeight()); fl_end_line(); break; } } fl_pop_matrix(); } } void GameView::drawShield() { const auto &shield = model.getShield(); if (shield.isActive()) { float centerX = shield.getX() + shield.getWidth() / 2.0f; float centerY = shield.getY() + shield.getHeight() / 2.0f; float radius = std::min(shield.getWidth(), shield.getHeight()) / 2.0f; fl_color(FL_WHITE); fl_line_style(FL_SOLID, 6); fl_arc(centerX - radius, centerY - radius, radius * 2, radius * 2, 0, 360); fl_line_style(FL_SOLID, 3); fl_arc(centerX - radius * 0.8f, centerY - radius * 0.8f, radius * 1.6f, radius * 1.6f, 0, 360); fl_line_style(FL_SOLID, 0); } } void GameView::drawScore() { fl_color(FL_WHITE); fl_font(FL_HELVETICA_BOLD, 30); std::stringstream ss; ss << "Score: " << model.getScore(); fl_draw(ss.str().c_str(), 20, 40); fl_font(FL_HELVETICA, 20); std::stringstream highScoreSS; highScoreSS << "High Score: " << model.getHighScore(); fl_draw(highScoreSS.str().c_str(), 20, 70); } void GameView::drawGameOver() { fl_color(FL_BLACK); fl_rectf(0, 0, w(), h()); fl_color(FL_WHITE); fl_font(FL_HELVETICA_BOLD, 40); const char *gameOverText = "GAME OVER"; int textWidth = fl_width(gameOverText); fl_draw(gameOverText, w() / 2 - textWidth / 2, h() / 3); fl_font(FL_HELVETICA, 30); std::stringstream ss; ss << "Score: " << model.getScore(); const char *scoreText = ss.str().c_str(); textWidth = fl_width(scoreText); fl_draw(scoreText, w() / 2 - textWidth / 2, h() / 2); const char *returnText = "Mouse click to return to menu"; textWidth = fl_width(returnText); fl_draw(returnText, w() / 2 - textWidth / 2, h() / 2 + 100); } int GameView::handle(int event) { switch (event) { case FL_PUSH: if (model.getState() == Model::GameState::PLAYING) { model.handleMouseDown(Fl::event_x(), Fl::event_y()); return 1; } else if (model.getState() == Model::GameState::GAME_OVER) { model.showMenu(); return 1; } break; case FL_RELEASE: if (model.getState() == Model::GameState::PLAYING) { model.handleMouseUp(Fl::event_x(), Fl::event_y()); return 1; } break; case FL_DRAG: if (model.getState() == Model::GameState::PLAYING) { model.handleMouseMove(Fl::event_x(), Fl::event_y()); return 1; } break; case FL_KEYDOWN: if (Fl::event_key() == ' ' || Fl::event_key() == 32) { if (model.getState() == Model::GameState::GAME_OVER) { model.showMenu(); return 1; } } break; } return ViewBase::handle(event); } } // namespace View