Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "play_state.hpp"
- #include "ai_snake.hpp"
- #include "player_snake.hpp"
- #include <iostream>
- const size_t MaxCircles {rand() % ((5 - 1) + 1) + 1};
- const size_t MaxSquares {rand() % ((5 - 1) + 1) + 1};
- const size_t MaxTriangles {rand() % ((5 - 1) + 1) + 1};
- const unsigned int MaxScale {5};
- const unsigned int MINIMUM_WIDTH = 148;
- const unsigned int MAXIMUM_WIDTH = 876;
- const unsigned int MINIMUM_HEIGHT = 20;
- const unsigned int MAXIMUM_HEIGHT = 748;
- const int mapwidth = 20;
- const int mapheight = 20;
- bool PlayState::onCreate()
- {
- snakes_.push_back(new AISnake);
- snakes_.back()->setPosition(Position(40,40));
- snakes_.push_back(new PlayerSnake);
- snakes_.back()->setPosition(Position(20,20));
- double x, y;
- for(unsigned shape = 0;shape < MaxCircles;shape++)
- {
- x = (double)((rand() % (MAXIMUM_WIDTH - MINIMUM_WIDTH)) + MINIMUM_WIDTH);
- y = (double)((rand() % (MAXIMUM_HEIGHT - MINIMUM_HEIGHT)) + MINIMUM_HEIGHT);
- shapes_.push_back(Circle({x, y}));
- }
- for(unsigned shape = 0;shape < MaxSquares;shape++)
- {
- x = (double)((rand() % (MAXIMUM_WIDTH - MINIMUM_WIDTH)) + MINIMUM_WIDTH);
- y = (double)((rand() % (MAXIMUM_HEIGHT - MINIMUM_HEIGHT)) + MINIMUM_HEIGHT);
- shapes_.push_back(Square({x, y}));
- }
- for(unsigned shape = 0;shape < MaxTriangles;shape++)
- {
- x = (double)((rand() % (MAXIMUM_WIDTH - MINIMUM_WIDTH)) + MINIMUM_WIDTH);
- y = (double)((rand() % (MAXIMUM_HEIGHT - MINIMUM_HEIGHT)) + MINIMUM_HEIGHT);
- shapes_.push_back(Triangle({x, y}));
- }
- return true;
- }
- bool PlayState::onDestroy()
- {
- return true;
- }
- void PlayState::onEntry()
- {
- prg::application.addKeyListener(*this);
- game_timer_.start();
- }
- void PlayState::onExit()
- {
- prg::application.removeKeyListener(*this);
- game_timer_.stop();
- }
- void PlayState::onUpdate()
- {
- }
- void PlayState::onRender(prg::Canvas& canvas)
- {
- const std::string text = "";
- canvas.blitFast(
- background_,
- canvas.getWidth() / 2 - background_.getWidth() / 2,
- canvas.getHeight() / 2 - background_.getHeight() / 2
- );
- prg::uint text_dims[2];
- prg::Font::MASSIVE.computePrintDimensions(text_dims, text);
- prg::Font::MASSIVE.print(
- canvas,
- prg::application.getScreenWidth() / 2 - text_dims[0] / 2,
- prg::application.getScreenHeight() / 2 - text_dims[1] / 2,
- prg::Colour::RED,
- text);
- for(const auto snake : snakes_) {
- snake->render(canvas);
- }
- for(Shape shapes : shapes_) {
- shapes.render(canvas);
- }
- }
- bool PlayState::onKey(const prg::IKeyEvent::KeyEvent& key_event)
- {
- if(key_event.key_state == KeyEvent::KB_DOWN) {
- switch(key_event.key) {
- case KeyEvent::KB_ESC_KEY:
- prg::application.exit();
- break;
- }
- }
- return true;
- }
- void PlayState::onTimer(prg::Timer& timer)
- {
- for(auto snake : snakes_) {
- snake->move();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment