Advertisement
nubideus

змейка

Mar 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. //rm snake;
  2. //c++ -Wall -pedantic -o snake snake.cpp -std=c++11 -pthread -Wno-deprecated-declarations -framework GLUT -framework OpenGL;
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <pthread.h>
  7.  
  8. #include <iostream>
  9. #include <termios.h>
  10.  
  11. #include <chrono>
  12. #include <thread>
  13. #include <mutex>
  14. #include <string>
  15. #include <string.h>
  16.  
  17. #include <sys/ioctl.h>
  18.  
  19. #include <cmath>
  20. #include <OpenGL/gl.h>
  21. #include <OpenGL/glu.h>
  22.  
  23. #if defined(linux) || defined(_WIN32)
  24. #include <GL/glut.h>
  25. #else
  26. #include <GLUT/GLUT.h>
  27. #endif
  28.  
  29.  
  30. using namespace std;
  31.  
  32. int* getTerminalSize(){
  33. struct winsize w;
  34. ioctl(0, TIOCGWINSZ, &w);
  35.  
  36. printf("lines %dcolumns %d\n", w.ws_row, w.ws_col);
  37.  
  38. return new int[2]{w.ws_col, w.ws_row};
  39. }
  40.  
  41. void clear(){
  42.  
  43. }
  44.  
  45. void setCaret(int x, int y){
  46. printf("\033[%d;%dH", x, y);
  47. }
  48.  
  49. void set_mode(int want_key){
  50.  
  51. }
  52.  
  53. int keys[0xFFFF] = {0};
  54.  
  55. int* size = new int[2]{0, 0};
  56. string* buffer = NULL;
  57.  
  58. void resize(int*& newSize){
  59. string* temp = buffer;
  60. buffer = new string[newSize[0] * newSize[1]];
  61.  
  62. if(temp != NULL){
  63. delete [] temp;
  64. }
  65.  
  66. size = newSize;
  67. for(int y = 0; y < size[1]; y++){
  68. for(int x = 0; x < size[0]; x++){
  69. buffer[y * size[0] + x] = '_';
  70. }
  71. }
  72. }
  73.  
  74. int FPS = 30;
  75. typedef struct{
  76. int x;
  77. int y;
  78. } Point;
  79.  
  80. struct{
  81. Point** body;
  82. int length;
  83. Point vel{0, 0};
  84. } snake;
  85.  
  86. int foodLength = 100;
  87. Point** food;
  88.  
  89. void GameThread(){
  90. while(1){
  91. //int* terminalSize = getTerminalSize();
  92. //resize(terminalSize);
  93.  
  94. Point* temp = snake.body[snake.length - 1];
  95. temp->x = snake.body[0]->x;
  96. temp->y = snake.body[0]->y;
  97. for(int i = snake.length - 1; i >= 0; i--){
  98. snake.body[i] = snake.body[i - 1];
  99. }
  100. snake.body[0] = temp;
  101.  
  102. Point& head = *snake.body[0];
  103. head.x += snake.vel.x;
  104. head.y += snake.vel.y;
  105. if(head.x < 0 ){
  106. head.x = size[0] - 1;
  107. }else if(head.x >= size[0]){
  108. head.x = 0;
  109. }
  110. if(head.y < 0 ){
  111. head.y = size[1] - 2;
  112. }else if(head.y >= size[1] - 1){
  113. head.y = 0;
  114. }
  115.  
  116. for(int i = 0; i < snake.length; i++){
  117. int x = snake.body[i]->x;
  118. int y = snake.body[i]->y;
  119. buffer[y * size[0] + x] = 'a';
  120. }
  121.  
  122. for(int i = 0; i < foodLength; i++){
  123. int x = food[i]->x;
  124. int y = food[i]->y;
  125.  
  126. if(head.x == x && head.y == y){
  127. free(food[i]);
  128. food[i] = food[foodLength - 1];
  129. foodLength--;
  130.  
  131. snake.length++;
  132. snake.body = (Point**) realloc(snake.body, sizeof(Point*) * snake.length);
  133. snake.body[snake.length - 1] = new Point{x, y};
  134. }
  135.  
  136. buffer[y * size[0] + x] = 'f';
  137. }
  138.  
  139. for(int y = 0; y < size[1] - 1; y++){
  140. string line = "";
  141. for(int x = 0; x < size[0]; x++){
  142. line += buffer[y * size[0] + x];
  143. }
  144. //cout<<line;
  145. }
  146.  
  147. if(snake.vel.x){
  148. if(keys[GLUT_KEY_UP]){
  149. snake.vel.y = -1;
  150. snake.vel.x = 0;
  151. }
  152. if(keys[GLUT_KEY_DOWN]){
  153. snake.vel.y = 1;
  154. snake.vel.x = 0;
  155. }
  156. }else{
  157. if(keys[GLUT_KEY_LEFT]){
  158. snake.vel.x = -1;
  159. snake.vel.y = 0;
  160. }
  161. if(keys[GLUT_KEY_RIGHT]){
  162. snake.vel.x = 1;
  163. snake.vel.y = 0;
  164. }
  165. }
  166.  
  167. //setCaret(0, 0);
  168. this_thread::sleep_for(chrono::nanoseconds(1000000000 / FPS));
  169. }
  170. }
  171.  
  172.  
  173. void startGame(){
  174. snake.vel.x = 1;
  175. snake.length = 30;
  176. snake.body = new Point*[snake.length];
  177.  
  178. int* terminalSize = getTerminalSize();
  179. resize(terminalSize);
  180.  
  181. for(int i = 0; i < snake.length; i++){
  182. snake.body[i] = new Point{terminalSize[0] - 1 - i, 10};
  183. }
  184.  
  185. food = new Point*[foodLength];
  186. for(int i = 0; i < foodLength; i++){
  187. food[i] = new Point{rand() % size[0], rand() % size[1]};
  188. }
  189.  
  190. Point** body = snake.body;
  191. body = (Point**) realloc(snake.body, sizeof(Point*) * snake.length);
  192. snake.body = body;
  193.  
  194. //system("setterm -cursor off");
  195.  
  196. thread thr(GameThread);
  197. thr.detach();
  198. }
  199.  
  200. float a = 0;
  201. void display(){
  202. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  203. glLoadIdentity();
  204.  
  205. glTranslatef(-1, 1, 0);
  206. glScaled((double) 2 / glutGet(GLUT_WINDOW_WIDTH), (double) -2 / glutGet(GLUT_WINDOW_HEIGHT), 1);
  207.  
  208. glPolygonMode(GL_FRONT, GL_LINE);
  209. glPolygonMode(GL_BACK, GL_FILL);
  210.  
  211. glTranslatef(50, 50, 0);
  212. glRotatef(a * 100, 0, 0, 1);
  213. glTranslatef(-5, -5, 0);
  214. a += 0.01;
  215. glBegin(GL_POLYGON);
  216. glColor3f(1.0, 1.0, 1.0);
  217.  
  218. glVertex3f(0.0, 0.0, 0);
  219. glVertex3f(10, 0.0, 0);
  220. glVertex3f(10, 0.2, 0);
  221. glVertex3f(5, 0.2, 0);
  222. glVertex3f(5, 0.8, 0);
  223. glVertex3f(10, 0.8, 0);
  224. glVertex3f(10, 10, 0);
  225. glVertex3f(0.0, 10, 0);
  226.  
  227. glEnd();
  228.  
  229. glTranslatef(5, 5, 0);
  230. glRotatef(a * -100, 0, 0, 1);
  231.  
  232. glBegin(GL_POLYGON);
  233. glColor3f(1.0, 1.0, 1.0);
  234.  
  235. glVertex3f(0.0, 0.0, 0);
  236. glVertex3f(10, 0.0, 0);
  237. glVertex3f(10, 10, 0);
  238. glVertex3f(0.0, 10, 0);
  239.  
  240. glEnd();
  241. glFlush();
  242.  
  243. glutSwapBuffers();
  244. glutPostRedisplay();
  245. }
  246.  
  247. void keyHandler(int key, int state, int x, int y){
  248. //cout<<key;
  249. switch(key) {
  250. case 27: // Escape key
  251. exit(0);
  252. break;
  253. case GLUT_KEY_UP:
  254. case GLUT_KEY_RIGHT:
  255. case GLUT_KEY_DOWN:
  256. case GLUT_KEY_LEFT:
  257. keys[key] = state;
  258. break;
  259. }
  260. }
  261.  
  262. void keyUpHandler(int key, int x, int y){
  263. keyHandler(key, 0, x, y);
  264. }
  265.  
  266. void keyDownHandler(int key, int x, int y){
  267. keyHandler(key, 0, x, y);
  268. }
  269. void mouseHandler(int button, int state, int x, int y){
  270. cout << button << "| " << state << "| " << x << "| " << y << "| " << endl;
  271. }
  272.  
  273. void motionHandler(int x, int y){
  274. //cout << x << "| " << y << "| " << endl;
  275. }
  276.  
  277. void initGL(){
  278. int argc = 0;
  279. char *argv[1] = {};
  280.  
  281. glutInit(&argc, argv);
  282.  
  283. //glutInitWindowPosition(200, 200);
  284. glutInitWindowSize(200, 200);
  285. glutInitDisplayMode(GLUT_RGBA | GLUT_MULTISAMPLE | GLUT_DOUBLE | GLUT_DEPTH);
  286. glutCreateWindow("snake");
  287. glutDisplayFunc(display);
  288. glutSpecialUpFunc(keyUpHandler);
  289. glutSpecialFunc(keyDownHandler);
  290. glutMouseFunc(mouseHandler);
  291. glutPassiveMotionFunc(motionHandler);
  292. glutMotionFunc(motionHandler);
  293.  
  294. glEnable(GL_DEPTH_TEST);
  295. glDepthFunc(GL_LESS);
  296.  
  297. glutMainLoop();
  298. }
  299.  
  300. int main() {
  301. //startGame();
  302. cout << "asdsdfasdfa sdfasdf \n\r";
  303. cout << "asd\n";
  304. initGL();
  305.  
  306. return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement