c++ - Snake game border collision -
c++ - Snake game border collision -
i'm wondering if can help me code. below have code controllable circle (representing snake head)
what modify/add below code when snake collides border/boundaries, repeats opposite side?
i have set game resolution 1024 x 768
any help appreciated :)
snake.cpp
#include "snake.hpp" #include <cstdlib> void snake::move() { switch(direction_){ case direction::north: position_.y += 10; break; case direction::east: position_.x += 10; break; case direction::south: position_.y -= 10; break; case direction::west: position_.x -= 10; } } void snake::render(prg::canvas& canvas) const { canvas.drawcircle(getposition().x, getposition().y,20,prg::colour::white); } void snake::changedirection(direction new_direction) { direction_ = new_direction; }
play_state.cpp
#include "play_state.hpp" #include "ai_snake.hpp" #include "player_snake.hpp" #include <iostream> const size_t maxshapes {5}; const unsigned int maxscale {5}; bool playstate::oncreate() { snakes_.push_back(new aisnake); snakes_.back()->setposition(position(100,100)); snakes_.push_back(new playersnake); snakes_.back()->setposition(position(50,50)); double x, y; for(unsigned shape = 0;shape < maxshapes;shape++) { x = (double)(rand() % prg::application.getscreenwidth()); y = (double)(rand() % prg::application.getscreenheight()); shapes_.push_back(square({x, y})); } homecoming true; } bool playstate::ondestroy() { homecoming 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; } } homecoming true; } void playstate::ontimer(prg::timer& timer) { for(auto snake : snakes_) { snake->move(); } }
play_state.hpp
#if !defined play_state_hpp #define play_state_hpp #include <prg_interactive.hpp> #include "snake.hpp" #include "square.hpp" #include <list> //example of forwards declaration of snake class class snake; class playstate final : public prg::iappstate, public prg::ikeyevent, public prg::itimerevent { public: playstate() = default; bool oncreate() override; bool ondestroy() override; void onentry() override; void onexit() override; void onupdate() override; void onrender(prg::canvas& canvas) override; bool onkey(const prg::ikeyevent::keyevent& key_event) override; void ontimer(prg::timer& timer) override; private: //snake* snakes_[2] {nullptr,nullptr}; std::list<snake*> snakes_; prg::timer game_timer_ {0, 1000 / 30, *this}; const prg::image background_ {prg::imagefile("resources/images/border.bmp").load()}; std::vector<shape> shapes_; }; #endif // play_state_hpp
this goes boundary of 1024x768:
if (position_.x < 0) position_.x = 1023; else if (position_.x > 1023) position_.x = 0; if (position_.y < 0) position_.y = 767; else if (position_.y > 767) position_.y = 0;
note width of 1024 means x between 0 , 1023!
judging code, utilize blocks of 10x10. if that's case, should customize code above needs.
edit:
from comments , answer, merged code can follow:
#include "snake.hpp" #include <cstdlib> void snake::move() { switch(direction_){ case direction::north: position_.y += 1; break; case direction::east: position_.x += 1; break; case direction::south: position_.y -= 1; break; case direction::west: position_.x -= 1; } if (position_.x < 0) position_.x = 63; else if (position_.x > 63) position_.x = 0; if (position_.y < 0) position_.y = 47; else if (position_.y > 47) position_.y = 0; } void snake::render(prg::canvas& canvas) const { canvas.drawcircle(getposition().x * 16, getposition().y * 16,16,prg::colour::white); } void snake::changedirection(direction new_direction) { direction_ = new_direction; }
this 64x48 grid of 16x16 pixels , results in 1024x768 resolution.
have fun!
c++ codeblocks
Comments
Post a Comment