Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // fourthAttemptSnake.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <GL/glut.h>
- #include <vector> // лучше лист
- //using namespace System;
- //using namespace System::Collections::Generic;
- class Point
- {
- public:
- Point() { };
- Point(int inputx, int inputy)
- {
- this->SetFirstCoordinate(inputx);
- this->SetSecondCoordinate(inputy);
- };
- Point(Point & point)
- {
- x = point.x;
- y = point.y;
- };
- bool isEquals(Point input)
- {
- return ((this->GetFirstCoordinate()==input.GetFirstCoordinate())&&(this->GetSecondCoordinate() == input.GetSecondCoordinate()));
- }
- void SetFirstCoordinate(int input) { x = input; };
- void SetSecondCoordinate(int input) { y = input; };
- int GetFirstCoordinate() { return x; };
- int GetSecondCoordinate() { return y; };
- private:
- int x; int y;
- };
- // enum cellValue { Nothing, SnakeBodyBlock, Apple, WallBlock };
- //static cellValue map[Size1][Size2];
- class Game
- {
- public:
- Game()
- {
- /*for (int i = 0; i < Size1; i++)
- for (int j = 0; j < Size2; j++)
- *this->map[i, j] = Nothing;*/
- };
- public:
- static const int WindowSize1 = 800;
- static const int WindowSize2 = 600;
- static const int Size1 = 20;
- static const int Size2 = 20;
- static const int CellSize = WindowSize1 / Size1;
- enum direction { Up, Right, Left, Down };
- //static Graphic graph;
- };
- Point Apple;
- class Snake :public Game
- {
- public:
- class SnakeBodyElement// :public Snake
- {
- public:
- SnakeBodyElement() {};
- Point GetLocation()
- {
- return location;
- }
- void SetLocation(Point inputLocation)
- {
- location = inputLocation;
- };
- private:
- Point location;
- };
- int bodySize;//
- SnakeBodyElement body[5];
- Snake(int size = 4)//!!!!
- {
- bodySize = size;
- HeadDirection = Up;
- for (int i = 0; i < bodySize; i++)
- {
- body[i].SetLocation(Point(Size1 / 2*CellSize, (Size2 / 2 - i)*CellSize));
- }
- };
- void Move()// возможно вставить аргумент - нажатую клавишу
- {
- MoveHead();
- CheckHeadCell();
- BodyFollowHead();
- };
- void SetDirection(direction input)
- {
- HeadDirection = input;
- };
- private:
- Point lastHeadLocation;
- direction HeadDirection;//= Up;
- void BodyFollowHead()
- {
- for (int i = bodySize - 1; i > 1; i--)
- {
- auto location = body[i].GetLocation();
- body[i].SetLocation(body[i - 1].GetLocation()); //- не рбит правильно
- location = body[i].GetLocation();
- }
- //auto location = body[1].GetLocation();
- body[1].SetLocation(lastHeadLocation);
- };
- void CheckHeadCell()//
- { //либо здесь же вызывать функцию проигрыша\роста и т.д.
- auto location = body[0].GetLocation();
- if (Apple.isEquals(location))
- this->Grow();
- //this->Grow();
- //ограничение по стенам
- };
- void Grow()
- {
- //change Apple
- bodySize++;
- };
- void MoveHead()
- {
- Point location = body[0].GetLocation();
- lastHeadLocation = location;
- switch (HeadDirection)//все это убрать и напиисать foreach по ключам словаря
- {
- case Up:
- body[0].SetLocation(Point(location.GetFirstCoordinate(), location.GetSecondCoordinate() + CellSize));
- break;
- case Down:
- body[0].SetLocation(Point(location.GetFirstCoordinate(), location.GetSecondCoordinate() - CellSize));
- break;
- case Right:
- body[0].SetLocation(Point(location.GetFirstCoordinate() + CellSize, location.GetSecondCoordinate()));
- break;
- case Left:
- body[0].SetLocation(Point(location.GetFirstCoordinate() - CellSize, location.GetSecondCoordinate()));
- break;
- }
- };
- };
- class SnakeBodyElement :public Snake
- {
- public:
- SnakeBodyElement() {};
- Point GetLocation()
- {
- return location;
- }
- void SetLocation(Point inputLocation)
- {
- location = inputLocation;
- };
- private:
- Point location;
- };
- Snake snake;
- class Graphic :public Game
- {
- public:
- static void processSpecialKeys(int key, int x, int y) {
- switch (key) {
- case GLUT_KEY_LEFT:
- snake.SetDirection(Left);
- break;
- case GLUT_KEY_UP:
- snake.SetDirection(Up);
- break;
- case GLUT_KEY_DOWN:
- snake.SetDirection(Down);
- break;
- case GLUT_KEY_RIGHT:
- snake.SetDirection(Right);
- break;
- }
- }
- static void DrawLines()
- {
- //glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_LINES);
- glColor3f(0.2, 0.4, 0.6);
- for (int x = 0; x < WindowSize1; x += CellSize)
- {
- glVertex2i(x, 0);
- glVertex2i(x, WindowSize2);
- }
- for (int y = 0; y < WindowSize2; y += CellSize)
- {
- glVertex2i(0, y);
- glVertex2i(WindowSize1, y);
- }
- glEnd();
- }
- static void DrawSnake()
- {
- glBegin(GL_QUADS);
- glColor3f(0.2, 0.1, 0.7);//цвет змейки
- for (int i = 0; i< snake.bodySize; i++)
- {
- Point location = snake.body[i].GetLocation();
- glVertex2i(location.GetFirstCoordinate()+1, location.GetSecondCoordinate()+1);
- glVertex2i(location.GetFirstCoordinate() + CellSize-1 , location.GetSecondCoordinate()+1);
- glVertex2i(location.GetFirstCoordinate() + CellSize-1, location.GetSecondCoordinate() + CellSize-1);
- glVertex2i(location.GetFirstCoordinate()+1, location.GetSecondCoordinate() + CellSize -1);
- }
- glEnd;
- }
- static void DrawApple()
- {
- glBegin(GL_QUADS);
- glColor3f(0.4, 0.3, 0.6);
- glVertex2i(Apple.GetFirstCoordinate(), Apple.GetSecondCoordinate());
- glVertex2i(Apple.GetFirstCoordinate() + CellSize , Apple.GetSecondCoordinate());
- glVertex2i(Apple.GetFirstCoordinate() + CellSize, Apple.GetSecondCoordinate() + CellSize);
- glVertex2i(Apple.GetFirstCoordinate(), Apple.GetSecondCoordinate() + CellSize );
- glEnd;
- }
- static void Display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- //glClear();
- glClearColor(0.0, 0.0, 0.0, 0.0);
- DrawLines();
- DrawSnake();
- DrawApple();
- glutSwapBuffers();
- }
- void static Reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0, w, 0, h);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void static Step(int input )
- {
- // Reshape(WindowSize1, WindowSize2);
- Display();
- snake.Move();
- glutPostRedisplay();
- glutTimerFunc(500, Step, 1);
- // Step(0);
- }
- void GraphMain(int argc, char * argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); /*двойная буферизация и 4хкомпонентный цвет*/
- glutInitWindowSize(WindowSize1, WindowSize2);
- glutCreateWindow("Snake");
- glutSpecialFunc(processSpecialKeys);
- glClearColor(0, 0, 0,0);
- /*glMatrixMode(GL_PROJECTION);
- glLoadIdentity();*/
- glutReshapeFunc(Reshape);
- glutDisplayFunc(Display);
- glutTimerFunc(50, Step, 1);
- //glutTimerFunc()
- glutMainLoop();
- };
- };
- int main(int argc, char * argv[])
- {
- Game b;
- Graphic a;
- a.GraphMain(argc,argv);
- //*b.map[1, 2] = b.Nothing;
- //b.a.Move();
- //for (auto i = 0; i < b.Size2; i++)
- //{
- // for (auto j = 0; j < b.Size1; j++)
- // {
- // std::cout << *b.map[j, i];// что-то не так с картой
- // }
- //}
- return 0;
- };
Add Comment
Please, Sign In to add comment