My own (newbie) snake c++


SUBMITTED BY: Guest

DATE: Dec. 7, 2013, 11:54 a.m.

FORMAT: C++

SIZE: 3.8 kB

HITS: 2753

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <windows.h>
  4. #include <ctime>
  5. using namespace std;
  6. int main()
  7. {
  8. int hBound = 23;
  9. int wBound = 45;
  10. //Ball's X and Y
  11. int bX, bY;
  12. int dX, dY;
  13. //Player's X and Y
  14. int pY;
  15. int tmp;
  16. //Stats
  17. int life, score;
  18. //Init start
  19. bX = 21; bY = rand() % (23 - 19 + 1) + 19;
  20. dX = -1 ; dY = -1;
  21. pY = 22;
  22. life = 3; score = 0;
  23. srand(time(0));
  24. //Set window size
  25. system ("MODE 55,30");
  26. while (life != -1)
  27. {
  28. string s = "";
  29. system("cls");
  30. if (life > 0)
  31. {
  32. cout << "Score: " << score << "\n";
  33. s += "Life: ";
  34. for (int i = 1; i <= life; i++)
  35. {
  36. s = s + (char)3 + " ";
  37. }
  38. s += "\n";
  39. for (int i = 0; i < hBound; i++)
  40. {
  41. for (int j = 0; j < wBound; j++)
  42. {
  43. if (i == 0 || i == hBound-1)
  44. s += "=";
  45. else if ((i != 0 || i != hBound-1) && (j == 0 || j == wBound-1))
  46. s += "|";
  47. else if (i == 3 && (j == 3 || j == 5 || j == 7 || j == 9))
  48. s += (char)178;
  49. //Ball Printout
  50. else if (i == bX && j == bY)
  51. s += (char)2;
  52. //Player's Bumper
  53. else if (i == hBound-2 && (j == pY || j == pY-1 || j == pY-2 || j == pY+1 || j == pY+2))
  54. s += "X";
  55. else if (i == hBound-2 && j == pY-3)
  56. s += "[";
  57. else if (i == hBound-2 && j == pY+3)
  58. s += "]";
  59. else
  60. s += " ";
  61. }
  62. //cout << s << "\n";
  63. //s = "";
  64. s += "\n";
  65. }
  66. cout << s;
  67. //Check user input
  68. if (GetAsyncKeyState(VK_LEFT) && pY >= 5)
  69. pY -= 1;
  70. else if (GetAsyncKeyState(VK_RIGHT) && pY <= wBound-6)
  71. pY += 1;
  72. //Decrease Life
  73. if (bX + dX == hBound)
  74. {
  75. life -= 1;
  76. bX = 21;
  77. bY = rand() % (23 - 19 + 1) + 19;
  78. pY = 22;
  79. dX *= -1;
  80. dY *= -1;
  81. Sleep(1000);
  82. }
  83. //Check bounce
  84. tmp = hBound-1;
  85. if (bX + dX == 0)
  86. dX *= -1;
  87. else if (bX + dX == hBound-2 && (bY == pY || bY == pY-1 || bY == pY-2 || bY == pY-3 ||
  88. bY == pY-4 || bY == pY+1 || bY == pY+2 || bY == pY+3 || bY == pY+4))
  89. dX *= -1;
  90. if (bY + dY == 0 || (bY + dY == wBound-1) )
  91. {
  92. dY *= -1;
  93. }
  94. bX += dX;
  95. bY += dY;
  96. Sleep(20);
  97. }
  98. else
  99. {
  100. life = -1;
  101. }
  102. }
  103. for (int i = 1; i <= 3; i++)
  104. {
  105. system("cls");
  106. Sleep(500);
  107. if (life <= 0)
  108. cout << "\n\n YOU LOSE!!!\n";
  109. Sleep(500);
  110. }
  111. cout << " Your Score: " << score << "\n\n";
  112. return 0;
  113. }

comments powered by Disqus