-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2b47a3
commit bc92bb8
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include "application.h" | ||
#include <stdlib.h> | ||
#include <cinder/Color.h> | ||
#include <time.h> | ||
|
||
static const cinder::ivec2 WindowSize{640, 480}; | ||
|
||
Application::Application() | ||
{} | ||
|
||
void Application::prepareSettings(Settings* settings) | ||
{ | ||
settings->setWindowSize(WindowSize); | ||
settings->setFrameRate(35.0f); | ||
settings->setResizable(false); | ||
settings->setFullScreen(false); | ||
} | ||
|
||
void Application::keyDown(ci::app::KeyEvent keyEvent) | ||
{ | ||
if (keyEvent.getCode() == ci::app::KeyEvent::KEY_DOWN) | ||
{ | ||
mVerticalDirection = 1; | ||
mHorizontalDirection = 0; | ||
} | ||
else if (keyEvent.getCode() == ci::app::KeyEvent::KEY_UP) | ||
{ | ||
mVerticalDirection = -1; | ||
mHorizontalDirection = 0; | ||
} | ||
else if (keyEvent.getCode() == ci::app::KeyEvent::KEY_RIGHT) | ||
{ | ||
mVerticalDirection = 0; | ||
mHorizontalDirection = 1; | ||
} | ||
else if (keyEvent.getCode() == ci::app::KeyEvent::KEY_LEFT) | ||
{ | ||
mVerticalDirection = 0; | ||
mHorizontalDirection = -1; | ||
} | ||
} | ||
|
||
void Application::setup() | ||
{ | ||
mSnake.push_back(getWindowCenter()); | ||
updateFoodPos(); | ||
} | ||
|
||
void Application::draw() | ||
{ | ||
ci::gl::clear(); | ||
|
||
//Draw food | ||
ci::gl::color(cinder::Color(1, 1, 1)); | ||
ci::gl::drawSolidCircle(mFood, mFoodSize); | ||
|
||
//Draw snake | ||
ci::gl::color(cinder::Color(0, 1, 0)); | ||
for (auto const& item : mSnake) | ||
{ | ||
ci::gl::drawSolidCircle(item, mSnakeSize, mSnakeSize); | ||
} | ||
|
||
if (collisionDetected()) | ||
{ | ||
updateFoodPos(); | ||
increaseSnake(); | ||
mSpeed += 0.5f; | ||
} | ||
} | ||
|
||
void Application::increaseSnake() | ||
{ | ||
mSnake.push_back(glm::vec2()); | ||
} | ||
|
||
void Application::updateFoodPos() | ||
{ | ||
int x = rand() % 640 + 1; | ||
int y = rand() % 480 + 1; | ||
mFood = glm::vec2(x, y); | ||
} | ||
|
||
void Application::resetBoundaries(glm::vec2& pos) | ||
{ | ||
if (pos.x < getWindowBounds().x1) | ||
pos.x = getWindowBounds().x2; | ||
else if (pos.x > getWindowBounds().x2) | ||
pos.x = getWindowBounds().x1; | ||
else if (pos.y < getWindowBounds().y1) | ||
pos.y = getWindowBounds().y2; | ||
else if (pos.y > getWindowBounds().y2) | ||
pos.y = getWindowBounds().y1; | ||
} | ||
|
||
void Application::update() | ||
{ | ||
for (std::vector<glm::vec2>::size_type i = mSnake.size() - 1; i >= 1; i --) | ||
{ | ||
mSnake[i] = mSnake[i-1]; | ||
} | ||
mSnake[0] += glm::vec2(mHorizontalDirection * mSpeed, mVerticalDirection * mSpeed); | ||
resetBoundaries(mSnake[0]); | ||
} | ||
|
||
bool Application::collisionDetected() | ||
{ | ||
float headSnakeX = mSnake[0].x; | ||
float headSnakeY = mSnake[0].y; | ||
return abs(headSnakeX - mFood.x) < 20.0 && abs(headSnakeY - mFood.y) < 20.0; | ||
} | ||
|
||
CINDER_APP(Application, ci::app::RendererGl(ci::app::RendererGl::Options().msaa(16)), &Application::prepareSettings) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <cinder/app/App.h> | ||
#include <cinder/app/RendererGl.h> | ||
#include <cinder/gl/gl.h> | ||
#include <vector> | ||
|
||
class Application : public ci::app::App | ||
{ | ||
public: | ||
Application(); | ||
|
||
void keyDown(ci::app::KeyEvent) override; | ||
void setup() override; | ||
void draw() override; | ||
void update() override; | ||
|
||
static void prepareSettings(Settings*); | ||
|
||
bool collisionDetected(); | ||
void updateFoodPos(); | ||
void increaseSnake(); | ||
void resetBoundaries(glm::vec2& pos); | ||
|
||
private: | ||
std::vector<glm::vec2> mSnake; | ||
glm::vec2 mFood; | ||
|
||
int mHorizontalDirection = 1; | ||
int mVerticalDirection = 0; | ||
float mSpeed = 5.0f; | ||
int mSnakeSize = 40; | ||
int mFoodSize = 40; | ||
|
||
}; |