Advertisement
Guest User

Untitled

a guest
Mar 30th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.18 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Audio.hpp>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <iostream>
  7.  
  8. /* GIMP RGBA C-Source image dump (gem.c) */
  9. static const struct {
  10. unsigned int width;
  11. unsigned int height;
  12. unsigned int bytes_per_pixel; /* 3:RGB, 4:RGBA */
  13. unsigned char pixel_data[32 * 32 * 4 + 1];
  14. } gem_icon = {
  15. 32, 32, 4,
  16. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  17. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  18. """2d\310\3772d\310\3772d\310\3772d\310\3772d\310\3772d\310\3772d\310\377"
  19. "2d\310\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  20. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  21. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  22. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0""2d"
  23. "\310\377\353\357\372\377\353\357\372\377\353\357\372\377\353\357\372\377"
  24. "\353\357\372\377\255\303\361\377\353\357\372\377\255\303\361\3772d\310\377"
  25. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  26. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  27. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  28. "\34B\223\377\34B\223\377\34B\223\3772d\310\377\353\357\372\377\353\357\372"
  29. "\377\353\357\372\377\255\303\361\377\255\303\361\377\255\303\361\377\353"
  30. "\357\372\377\255\303\361\377\255\303\361\377x\232\336\3772d\310\377\34B\223"
  31. "\377\34B\223\377\34B\223\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  32. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  33. "\0\377\0\377\0\377\0\377\0""2d\310\3772d\310\3772d\310\377s\226\335\377s"
  34. "\226\335\377\353\357\372\377\353\357\372\377\353\357\372\377\255\303\361"
  35. "\377\353\357\372\377\353\357\372\377\353\357\372\377\255\303\361\377\255"
  36. "\303\361\377x\232\336\377x\232\336\377x\232\336\377s\226\335\3772d\310\377"
  37. "2d\310\3772d\310\3772d\310\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  38. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0""2d\310"
  39. "\377\255\303\361\377s\226\335\377f\214\332\377f\214\332\377s\226\335\377"
  40. "s\226\335\377\255\303\361\377\255\303\361\377\353\357\372\377\255\303\361"
  41. "\377\255\303\361\377\255\303\361\377x\232\336\377x\232\336\377x\232\336\377"
  42. "s\226\335\377s\226\335\377f\214\332\377s\226\335\377s\226\335\377s\226\335"
  43. "\377s\226\335\3772d\310\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  44. "\0\377\0\377\0\377\0\377\0""2d\310\3772d\310\377\255\303\361\377s\226\335"
  45. "\377f\214\332\377f\214\332\377s\226\335\377s\226\335\3772d\310\377s\226\335"
  46. "\377\255\303\361\377\255\303\361\377\255\303\361\377x\232\336\377x\232\336"
  47. "\377x\232\336\377x\232\336\377x\232\336\377s\226\335\3772d\310\377s\226\335"
  48. "\377\255\303\361\377f\214\332\377f\214\332\377s\226\335\377s\226\335\377"
  49. "2d\310\3772d\310\377\377\0\377\0\377\0\377\0\377\0\377\0""2d\310\377s\226"
  50. "\335\377\255\303\361\377s\226\335\377f\214\332\377s\226\335\377s\226\335"
  51. "\377s\226\335\377s\226\335\377s\226\335\377\255\303\361\3772d\310\377s\226"
  52. "\335\377s\226\335\3772d\310\377s\226\335\3772d\310\3772d\310\377s\226\335"
  53. "\377\255\303\361\3772d\310\377s\226\335\377s\226\335\377s\226\335\377f\214"
  54. "\332\377f\214\332\377f\214\332\377s\226\335\377s\226\335\3772d\310\377\377"
  55. "\0\377\0""2d\310\3772d\310\3772d\310\377s\226\335\377f\214\332\377s\226\335"
  56. "\377s\226\335\377s\226\335\377s\226\335\377\353\357\372\377\255\303\361\377"
  57. "\255\303\361\377s\226\335\377x\232\336\377\255\303\361\377x\232\336\377x"
  58. "\232\336\377x\232\336\377x\232\336\377s\226\335\377\353\357\372\377\255\303"
  59. "\361\377\255\303\361\3772d\310\377\255\303\361\377s\226\335\377s\226\335"
  60. "\377f\214\332\377s\226\335\3772d\310\3772d\310\3772d\310\3772d\310\377\255"
  61. "\303\361\377\255\303\361\3772d\310\3772d\310\377s\226\335\377s\226\335\377"
  62. "s\226\335\377\353\357\372\377\255\303\361\377\255\303\361\377\255\303\361"
  63. "\377\255\303\361\377s\226\335\377s\226\335\377\255\303\361\377s\226\335\377"
  64. "s\226\335\3772d\310\377\353\357\372\377\255\303\361\377\255\303\361\377\255"
  65. "\303\361\377\255\303\361\377s\226\335\377s\226\335\377s\226\335\3772d\310"
  66. "\3772d\310\377\255\303\361\377\255\303\361\3772d\310\3772d\310\3772d\310"
  67. "\377\255\303\361\377\255\303\361\377\255\303\361\377x\232\336\377x\232\336"
  68. "\377\255\303\361\377\255\303\361\377\255\303\361\377\255\303\361\377\255"
  69. "\303\361\377\255\303\361\377\255\303\361\3772d\310\377s\226\335\377s\226"
  70. "\335\377s\226\335\377\353\357\372\377\353\357\372\377\255\303\361\377\255"
  71. "\303\361\377\255\303\361\377\255\303\361\377\255\303\361\377x\232\336\377"
  72. "x\232\336\377\255\303\361\377\255\303\361\377\255\303\361\3772d\310\3772"
  73. "d\310\3772d\310\377x\232\336\3772d\310\3772d\310\377\255\303\361\377x\232"
  74. "\336\377\353\357\372\377x\232\336\377x\232\336\377x\232\336\3772d\310\377"
  75. "2d\310\3772d\310\377\34B\223\377\34B\223\377\34B\223\377\34B\223\377\34B"
  76. "\223\377\34B\223\3772d\310\3772d\310\3772d\310\377x\232\336\377x\232\336"
  77. "\377x\232\336\377\353\357\372\377x\232\336\377\255\303\361\3772d\310\377"
  78. "2d\310\377Y\204\333\3772d\310\3772d\310\377x\232\336\377Y\204\333\377Y\204"
  79. "\333\377x\232\336\377\353\357\372\377\353\357\372\377\353\357\372\377\353"
  80. "\357\372\377\353\357\372\3772d\310\377s\226\335\377s\226\335\377s\226\335"
  81. "\377Lx\320\377Lx\320\377Lx\320\377Lx\320\377s\226\335\377s\226\335\377s\226"
  82. "\335\3772d\310\377\353\357\372\377\353\357\372\377\353\357\372\377\353\357"
  83. "\372\377\353\357\372\377x\232\336\377Y\204\333\377x\232\336\377Y\204\333"
  84. "\3772d\310\377\377\0\377\0""2d\310\377x\232\336\377Y\204\333\377Y\204\333"
  85. "\377x\232\336\377x\232\336\377\353\357\372\377\353\357\372\3772d\310\377"
  86. "s\226\335\377s\226\335\377s\226\335\377s\226\335\377s\226\335\377Lx\320\377"
  87. "Lx\320\377s\226\335\377s\226\335\377s\226\335\377s\226\335\377s\226\335\377"
  88. "2d\310\377\353\357\372\377\353\357\372\377x\232\336\377x\232\336\377\34B"
  89. "\223\377x\232\336\377Y\204\333\3772d\310\377\377\0\377\0\377\0\377\0""2d"
  90. "\310\377x\232\336\377Y\204\333\377Y\204\333\377\34B\223\377Lx\320\377x\232"
  91. "\336\377x\232\336\3772d\310\377Lx\320\377Lx\320\3772d\310\377Lx\320\3772"
  92. "d\310\377\34B\223\377\34B\223\3772d\310\377Lx\320\3772d\310\377Lx\320\377"
  93. "2d\310\3772d\310\377x\232\336\377x\232\336\377Lx\320\377\34B\223\377\34B"
  94. "\223\377x\232\336\377Y\204\333\3772d\310\377\377\0\377\0\377\0\377\0\377"
  95. "\0\377\0""2d\310\377Y\204\333\377Y\204\333\377\34B\223\377\34B\223\377x\232"
  96. "\336\377x\232\336\377\34B\223\377x\232\336\377x\232\336\377Lx\320\377Lx\320"
  97. "\377Lx\320\377\34B\223\377\34B\223\377x\232\336\377x\232\336\377x\232\336"
  98. "\377Lx\320\377Lx\320\377\34B\223\377x\232\336\377Lx\320\377\34B\223\377L"
  99. "x\320\377\34B\223\377Y\204\333\3772d\310\377\377\0\377\0\377\0\377\0\377"
  100. "\0\377\0\377\0\377\0\377\0\377\0""2d\310\377Y\204\333\377\34B\223\377\34"
  101. "B\223\377x\232\336\377Lx\320\377\34B\223\377x\232\336\377Lx\320\377Lx\320"
  102. "\377Lx\320\377Lx\320\377\34B\223\377\34B\223\377x\232\336\377Lx\320\377L"
  103. "x\320\377Lx\320\377Lx\320\377\34B\223\377x\232\336\377Lx\320\377\34B\223"
  104. "\377Lx\320\377\34B\223\3772d\310\377\377\0\377\0\377\0\377\0\377\0\377\0"
  105. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0""2d\310\377\34B\223\377"
  106. "Lx\320\377\34B\223\377Lx\320\377Lx\320\377\34B\223\377Lx\320\377Lx\320\377"
  107. "Lx\320\377\34B\223\377Lx\320\377!M\245\377\34B\223\377Lx\320\377Lx\320\377"
  108. "Lx\320\377\34B\223\377Lx\320\377Lx\320\377\34B\223\377Lx\320\377!M\245\377"
  109. "\34B\223\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  110. "\377\0\377\0\377\0\377\0\377\0\377\0""2d\310\377\34B\223\377!M\245\377Lx"
  111. "\320\377\34B\223\377Lx\320\377\34B\223\377Lx\320\377Lx\320\377Lx\320\377"
  112. "\34B\223\377Lx\320\377!M\245\377\34B\223\377Lx\320\377Lx\320\377Lx\320\377"
  113. "\34B\223\377Lx\320\377\34B\223\377Lx\320\377!M\245\377!M\245\377\34B\223"
  114. "\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  115. "\0\377\0\377\0\377\0\377\0\377\0\377\0\34B\223\377!M\245\377!M\245\377Lx"
  116. "\320\377\34B\223\377\34B\223\377Lx\320\377Lx\320\377Lx\320\377\34B\223\377"
  117. "!M\245\377!M\245\377\34B\223\377Lx\320\377Lx\320\377Lx\320\377\34B\223\377"
  118. "\34B\223\377Lx\320\377!M\245\377!M\245\377\34B\223\377\377\0\377\0\377\0"
  119. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  120. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\34B\223\377!M\245\377Lx\320\377"
  121. "\34B\223\377Lx\320\377\34B\223\377Lx\320\377\34B\223\377Lx\320\377!M\245"
  122. "\377!M\245\377!M\245\377\34B\223\377Lx\320\377\34B\223\377Lx\320\377\34B"
  123. "\223\377!M\245\377!M\245\377\34B\223\377\377\0\377\0\377\0\377\0\377\0\377"
  124. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  125. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\34B\223\377!M\245\377"
  126. "!M\245\377\34B\223\377\34B\223\377Lx\320\377\34B\223\377!M\245\377!M\245"
  127. "\377!M\245\377!M\245\377\34B\223\377Lx\320\377\34B\223\377\34B\223\377!M"
  128. "\245\377!M\245\377\34B\223\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  129. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  130. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\34B\223\377\20\"F"
  131. "\377\20\"F\377\20\"F\377\20\"F\377\34B\223\377!M\245\377Lx\320\377!M\245"
  132. "\377!M\245\377!M\245\377!M\245\377\34B\223\377\20\"F\377\20\"F\377\20\"F"
  133. "\377\20\"F\377\34B\223\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  134. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  135. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\20\"F\377"
  136. "\27""3k\377\27""3k\377\27""3k\377\34B\223\377Lx\320\377!M\245\377!M\245\377"
  137. "!M\245\377!M\245\377!M\245\377\34B\223\377\27""3k\377\27""3k\377\27""3k\377"
  138. "\20\"F\377\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  139. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  140. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\20\""
  141. "F\377\27""3k\377\27""3k\377\34B\223\377Lx\320\377!M\245\377!M\245\377!M\245"
  142. "\377!M\245\377!M\245\377\34B\223\377\27""3k\377\27""3k\377\20\"F\377\377"
  143. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  144. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  145. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  146. "\0\377\0\20\"F\377\27""3k\377\27""3k\377\25/c\377!M\245\377!M\245\377!M\245"
  147. "\377!M\245\377\25/c\377\27""3k\377\27""3k\377\20\"F\377\377\0\377\0\377\0"
  148. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  149. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  150. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  151. "\377\0\20\"F\377\27""3k\377\27""3k\377\25/c\377!M\245\377!M\245\377!M\245"
  152. "\377!M\245\377\25/c\377\27""3k\377\27""3k\377\20\"F\377\377\0\377\0\377\0"
  153. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  154. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  155. "\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0"
  156. "\377\0\377\0\377\0\20\"F\377\27""3k\377\25/c\377!M\245\377!M\245\377!M\245"
  157. "\377!M\245\377\25/c\377\27""3k\377\20\"F\377\377\0\377\0\377\0\377\0\377"
  158. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  159. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  160. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  161. "\0\377\0\377\0\377\0\377\0\377\0\20\"F\377\27""3k\377\25/c\377!M\245\377"
  162. "!M\245\377\25/c\377\27""3k\377\20\"F\377\377\0\377\0\377\0\377\0\377\0\377"
  163. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  164. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  165. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  166. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\20\"F\377\25/c\377!M\245"
  167. "\377!M\245\377\25/c\377\20\"F\377\377\0\377\0\377\0\377\0\377\0\377\0\377"
  168. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  169. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  170. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  171. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\20\"F\377\27""3"
  172. "k\377\25/c\377\25/c\377\27""3k\377\20\"F\377\377\0\377\0\377\0\377\0\377"
  173. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  174. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  175. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  176. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  177. "\0\377\0\20\"F\377\25/c\377\25/c\377\20\"F\377\377\0\377\0\377\0\377\0\377"
  178. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  179. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  180. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  181. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  182. "\0\377\0\377\0\377\0\377\0\377\0\25/c\377\25/c\377\377\0\377\0\377\0\377"
  183. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  184. "\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377\0\377"
  185. "\0\377\0\377\0",
  186. };
  187.  
  188. bool isColliding (sf::IntRect rectA, sf::IntRect rectB)
  189. {
  190. if (rectA.Left < rectB.Left + rectB.Right &&
  191. rectA.Left + rectA.Right > rectB.Left &&
  192. rectA.Top < rectB.Top + rectB.Bottom &&
  193. rectA.Top + rectA.Right > rectB.Top)
  194. {
  195. return true;
  196. }
  197. return false;
  198. }
  199.  
  200. sf::IntRect getRegionCoordinates(sf::Vector2f pos, int width, int height)
  201. {
  202. sf::IntRect temp;
  203. temp.Left = pos.x;
  204. temp.Right = width;
  205. temp.Top = pos.y;
  206. temp.Bottom = height;
  207.  
  208. return temp;
  209. }
  210.  
  211. int main()
  212. {
  213. sf::RenderWindow window(sf::VideoMode(640, 480, 32), "Pickin' Gems");
  214. window.SetFramerateLimit(30);
  215. window.SetIcon(gem_icon.width, gem_icon.height, gem_icon.pixel_data);
  216. sf::Image grassImage;
  217. grassImage.LoadFromFile("grass.png");
  218. grassImage.SetSmooth(false);
  219. sf::Sprite grass;
  220. grass.SetImage(grassImage);
  221.  
  222. sf::Image gemImage;
  223. gemImage.LoadFromFile("gem.png");
  224. gemImage.CreateMaskFromColor(sf::Color(255, 0, 255));
  225. gemImage.SetSmooth(false);
  226. sf::Sprite gem;
  227. gem.SetImage(gemImage);
  228.  
  229. sf::Image player1Image;
  230. player1Image.LoadFromFile("ayne.png");
  231. player1Image.CreateMaskFromColor(sf::Color(255, 0, 255));
  232. player1Image.SetSmooth(false);
  233. sf::Sprite player1(player1Image);
  234.  
  235. sf::Image player2Image;
  236. player2Image.LoadFromFile("elliot.png");
  237. player2Image.CreateMaskFromColor(sf::Color(255, 0, 255));
  238. player2Image.SetSmooth(false);
  239. sf::Sprite player2(player2Image);
  240.  
  241. sf::Image pressEnterToStartImage;
  242. pressEnterToStartImage.LoadFromFile("pressEnterToStart.png");
  243. pressEnterToStartImage.SetSmooth(false);
  244. sf::Sprite pressEnterToStart(pressEnterToStartImage);
  245.  
  246. sf::Image humanSelectedImage;
  247. humanSelectedImage.LoadFromFile("humanSelected.png");
  248. humanSelectedImage.SetSmooth(false);
  249. sf::Sprite humanSelectedSprite(humanSelectedImage);
  250.  
  251. sf::Image computerSelectedImage;
  252. computerSelectedImage.LoadFromFile("computerSelected.png");
  253. computerSelectedImage.SetSmooth(false);
  254. sf::Sprite computerSelectedSprite(computerSelectedImage);
  255.  
  256. sf::Image gameOverScreenImage;
  257. gameOverScreenImage.LoadFromFile("gameOverScreen.png");
  258. gameOverScreenImage.SetSmooth(false);
  259. sf::Sprite gameOverScreen(gameOverScreenImage);
  260.  
  261. sf::Music backgroundMusic;
  262. backgroundMusic.OpenFromFile("background.ogg");
  263. backgroundMusic.SetLoop(true);
  264.  
  265. sf::SoundBuffer gemPickupBuffer;
  266. gemPickupBuffer.LoadFromFile("gemCollect.wav");
  267. sf::Sound gemPickup;
  268. gemPickup.SetBuffer(gemPickupBuffer);
  269.  
  270. sf::Font font;
  271. font.LoadFromFile("Halo11.ttf");
  272. sf::String player1ScoreToPrint;
  273. sf::String player2ScoreToPrint;
  274. sf::String timerPrint;
  275. sf::String gameOverPrint;
  276. player1ScoreToPrint.SetFont(font);
  277. player1ScoreToPrint.SetSize(24);
  278. player2ScoreToPrint.SetFont(font);
  279. player2ScoreToPrint.SetSize(24);
  280. timerPrint.SetFont(font);
  281. timerPrint.SetSize(24);
  282. gameOverPrint.SetFont(font);
  283. gameOverPrint.SetSize(24);
  284.  
  285. float player1Frame = 1;
  286. float player2Frame = 1;
  287. int player1Score = 0;
  288. int player2Score = 0;
  289. char buffer1[256];
  290. char buffer2[256];
  291. char buffer3[256];
  292. char buffer4[256];
  293. sf::Vector2f player1Pos;
  294. sf::Vector2f player2Pos;
  295. enum Direction {DOWN = 0, UP = 1, LEFT = 2, RIGHT = 3};
  296. enum State {MAIN_MENU = 0, PLAYER_SELECT = 1, GAME = 2, GAME_OVER = 3};
  297. State gameState = MAIN_MENU;
  298. bool humanSelected = true;
  299. bool computerSelected = false;
  300. bool enterPressed = false;
  301. bool downPressed = false;
  302. bool upPressed = false;
  303. bool backgroundMusicPlaying = true;
  304. Direction player1Direction = DOWN;
  305. Direction player2Direction = DOWN;
  306. bool player1Moving = false;
  307. bool player2Moving = false;
  308. bool player1Animating = false;
  309. bool player2Animating = false;
  310. sf::Vector2f gemPos;
  311. srand(time(NULL));
  312. gemPos.x = (int)rand() % (640 - 32);
  313. gemPos.y = (int)rand() % (480 - 32);
  314. int time = 1800;
  315. bool gameOver = false;
  316. bool fullscreen = false;
  317. while (window.IsOpened())
  318. {
  319. sf::Event event;
  320. while (window.GetEvent(event))
  321. {
  322. if (event.Type == sf::Event::Closed)
  323. {
  324. window.Close();
  325. }
  326. if ((event.Type == sf::Event::KeyPressed) &&
  327. (event.Key.Code == sf::Key::Escape))
  328. {
  329. window.Close();
  330. }
  331. if ((event.Type == sf::Event::KeyPressed) &&
  332. (event.Key.Code == sf::Key::Return))
  333. {
  334. enterPressed = true;
  335. }
  336. if ((event.Type == sf::Event::KeyPressed) &&
  337. (event.Key.Code == sf::Key::Up))
  338. {
  339. upPressed = true;
  340. }
  341. if ((event.Type == sf::Event::KeyPressed) &&
  342. (event.Key.Code == sf::Key::Down))
  343. {
  344. downPressed = true;
  345. }
  346. }
  347. switch (gameState)
  348. {
  349. case MAIN_MENU:
  350. window.Draw(pressEnterToStart);
  351. if (enterPressed)
  352. {
  353. enterPressed = false;
  354. gameState = PLAYER_SELECT;
  355. }
  356. break;
  357. case PLAYER_SELECT:
  358. if (humanSelected)
  359. {
  360. window.Draw(humanSelectedSprite);
  361. }
  362. else if (computerSelected)
  363. {
  364. window.Draw(computerSelectedSprite);
  365. }
  366. if (upPressed)
  367. {
  368. if (computerSelected)
  369. {
  370. computerSelected = false;
  371. humanSelected = true;
  372. }
  373. else if (humanSelected)
  374. {
  375. humanSelected = false;
  376. computerSelected = true;
  377. }
  378. upPressed = false;
  379. }
  380. else if (downPressed)
  381. {
  382. if (humanSelected)
  383. {
  384. humanSelected = false;
  385. computerSelected = true;
  386. }
  387. else if (computerSelected)
  388. {
  389. computerSelected = false;
  390. humanSelected = true;
  391. }
  392. downPressed = false;
  393. }
  394. if (enterPressed)
  395. {
  396. enterPressed = false;
  397. gameState = GAME;
  398. }
  399. break;
  400. case GAME:
  401. {
  402. --time;
  403. if (backgroundMusicPlaying)
  404. {
  405. backgroundMusicPlaying = false;
  406. backgroundMusic.Play();
  407. }
  408. for (int i = 0; i < 640 / 32; i++)
  409. {
  410. for (int j = 0; j < 480 / 32; j++)
  411. {
  412. grass.SetPosition(i * 32,
  413. j * 32);
  414. window.Draw(grass);
  415. std::cout << grass.GetPosition().x << " " << grass.GetPosition().y << "\n";
  416. }
  417. }
  418. gem.SetPosition(gemPos);
  419.  
  420. if (window.GetInput().IsKeyDown(sf::Key::Left))
  421. {
  422. player1Direction = LEFT;
  423. player1Animating = true;
  424. player1.Move(-3, 0);
  425. }
  426. else if (window.GetInput().IsKeyDown(sf::Key::Right))
  427. {
  428. player1Direction = RIGHT;
  429. player1Animating = true;
  430. player1.Move(3, 0);
  431. }
  432. else if (window.GetInput().IsKeyDown(sf::Key::Up))
  433. {
  434. player1Direction = UP;
  435. player1Animating = true;
  436. player1.Move(0, -3);
  437. }
  438. else if (window.GetInput().IsKeyDown(sf::Key::Down))
  439. {
  440. player1Direction = DOWN;
  441. player1Animating = true;
  442. player1.Move(0, 3);
  443. }
  444. if (humanSelected)
  445. {
  446. if (window.GetInput().IsKeyDown(sf::Key::A))
  447. {
  448. player2Direction = LEFT;
  449. player2Animating = true;
  450. player2.Move(-3, 0);
  451. }
  452. else if (window.GetInput().IsKeyDown(sf::Key::D))
  453. {
  454. player2Direction = RIGHT;
  455. player2Animating = true;
  456. player2.Move(3, 0);
  457. }
  458. else if (window.GetInput().IsKeyDown(sf::Key::W))
  459. {
  460. player2Direction = UP;
  461. player2Animating = true;
  462. player2.Move(0, -3);
  463. }
  464. else if (window.GetInput().IsKeyDown(sf::Key::S))
  465. {
  466. player2Direction = DOWN;
  467. player2Animating = true;
  468. player2.Move(0, 3);
  469. }
  470. }
  471. else if (computerSelected)
  472. {
  473. if ((int)player2Pos.x < (int)gemPos.x)
  474. {
  475. if (gemPos.x - player2Pos.x < 2)
  476. {
  477. player2.Move(1, 0);
  478. }
  479. else
  480. {
  481. player2.Move(2, 0);
  482. }
  483. player2Direction = RIGHT;
  484. player2Animating = true;
  485. }
  486. else if ((int)player2Pos.y >
  487. (int)gemPos.y)
  488. {
  489. if (player2Pos.y - gemPos.y < 2)
  490. {
  491. player2.Move(0, -1);
  492. }
  493. else
  494. {
  495. player2.Move(0, -2);
  496. }
  497. player2Direction = UP;
  498. player2Animating = true;
  499. }
  500. else if ((int)player2Pos.x >
  501. (int)gemPos.x)
  502. {
  503. if (player2Pos.x - gemPos.x < 2)
  504. {
  505. player2.Move(-1, 0);
  506. }
  507. else
  508. {
  509. player2.Move(-2, 0);
  510. }
  511. player2Direction = LEFT;
  512. player2Animating = true;
  513. }
  514. else if ((int)player2Pos.y <
  515. (int)gemPos.y)
  516. {
  517.  
  518. if (gemPos.y - player2Pos.y < 2)
  519. {
  520. player2.Move(0, 1);
  521. }
  522. else
  523. {
  524. player2.Move(0, 2);
  525. }
  526. player2Direction = DOWN;
  527. player2Animating = true;
  528. }
  529. else if ((int)player2Pos.x >
  530. (int)gemPos.x)
  531. {
  532. if (player2Pos.x - gemPos.x < 2)
  533. {
  534. player2.Move(-1, 0);
  535. }
  536. else
  537. {
  538. player2.Move(-2, 0);
  539. }
  540. player2Direction = LEFT;
  541. player2Animating = true;
  542. }
  543. }
  544. if (player1Animating)
  545. {
  546. player1Frame += 0.25;
  547. if (player1Frame > 3)
  548. {
  549. player1Frame = 0;
  550. }
  551. player1Animating = false;
  552. }
  553.  
  554. if (player2Animating)
  555. {
  556. player2Frame += 0.25;
  557. if (player2Frame > 3)
  558. {
  559. player2Frame = 0;
  560. }
  561. player2Animating = false;
  562. }
  563. player1Pos = player1.GetPosition();
  564. player2Pos = player2.GetPosition();
  565.  
  566. if (player1Pos.x < 0)
  567. {
  568. player1.SetX(0);
  569. }
  570. else if (player1Pos.x + 32 > 640)
  571. {
  572. player1.SetX(640 - 32);
  573. }
  574. if (player1Pos.y < 0)
  575. {
  576. player1.SetY(0);
  577. }
  578. else if (player1Pos.y + 48 > 480)
  579. {
  580. player1.SetY(480 - 48);
  581. }
  582. if (player2Pos.x < 0)
  583. {
  584. player2.SetX(0);
  585. }
  586. else if (player2Pos.x + 32 > 640)
  587. {
  588. player2.SetX(640 - 32);
  589. }
  590. if (player2Pos.y < 0)
  591. {
  592. player2.SetY(0);
  593. }
  594. else if (player2Pos.y + 48 > 480)
  595. {
  596. player2.SetY(480 - 48);
  597. }
  598. if (isColliding(getRegionCoordinates(gemPos, 32, 32),
  599. getRegionCoordinates(player1Pos, 32,
  600. 48)))
  601. {
  602. gemPos.x = (int)rand() % (640 - 32);
  603. gemPos.y = (int)rand() % (480 - 32);
  604. gemPickup.Play();
  605. player1Score += 10;
  606. }
  607. if (isColliding(getRegionCoordinates(gemPos, 32 + 3, 32 + 3),
  608. getRegionCoordinates(player2Pos, 32,
  609. 48)))
  610. {
  611. gemPos.x = (int)rand() % (640 - 32);
  612. gemPos.y = (int)rand() % (480 - 32);
  613. gemPickup.Play();
  614. player2Score += 10;
  615. }
  616. sprintf(buffer1, "Player 1's Score: %d", player1Score);
  617. sprintf(buffer2, "Player 2's Score: %d", player2Score);
  618. sprintf(buffer3, "Time left: %d", time / 30);
  619. player1ScoreToPrint.SetText(buffer1);
  620. player2ScoreToPrint.SetText(buffer2);
  621. timerPrint.SetText(buffer3);
  622. player1ScoreToPrint.SetColor(sf::Color(0, 0, 0));
  623. player2ScoreToPrint.SetColor(sf::Color(0, 0, 0));
  624. timerPrint.SetColor(sf::Color(0, 0, 0));
  625. player1ScoreToPrint.SetX(0);
  626. player1ScoreToPrint.SetY(0);
  627. player2ScoreToPrint.SetX(0);
  628. player2ScoreToPrint.SetY(24);
  629. timerPrint.SetX(0);
  630. timerPrint.SetY(48);
  631.  
  632. int player1SpritesheetX = (int)player1Frame * 32;
  633. int player1SpritesheetY = player1Direction * 48;
  634. int player2SpritesheetX = (int)player2Frame * 32;
  635. int player2SpritesheetY = player2Direction * 48;
  636.  
  637. player1.SetSubRect(sf::IntRect(player1SpritesheetX,
  638. player1SpritesheetY,
  639. player1SpritesheetX + 32,
  640. player1SpritesheetY + 48));
  641.  
  642. player2.SetSubRect(sf::IntRect(player2SpritesheetX,
  643. player2SpritesheetY,
  644. player2SpritesheetX + 32,
  645. player2SpritesheetY + 48));
  646.  
  647. window.Draw(gem);
  648. window.Draw(player2);
  649. window.Draw(player1);
  650. window.Draw(player1ScoreToPrint);
  651. window.Draw(player2ScoreToPrint);
  652. window.Draw(timerPrint);
  653. if (time / 30 == 0)
  654. {
  655. gameState = GAME_OVER;
  656. }
  657. break;
  658. }
  659. case GAME_OVER:
  660. {
  661. if (player1Score < player2Score)
  662. {
  663. sprintf(buffer4, "Congratulations, Player 2! You win!");
  664. }
  665. else if (player1Score > player2Score)
  666. {
  667. sprintf(buffer4, "Congratulations, Player 1! You win!");
  668. }
  669. else
  670. {
  671. sprintf(buffer4, "It's a tie!");
  672. }
  673. gameOverPrint.SetText(buffer4);
  674. gameOverPrint.SetColor(sf::Color(0, 0, 0));
  675. sf::FloatRect rect(gameOverPrint.GetRect());
  676. sf::Vector2f pos(window.GetWidth() / 2 - rect.GetWidth() / 2,
  677. window.GetHeight() / 2 - rect.GetHeight() / 2);
  678. gameOverPrint.SetPosition(pos);
  679. window.Draw(gameOverScreen);
  680. window.Draw(gameOverPrint);
  681. if (enterPressed)
  682. {
  683. enterPressed = false;
  684. gameState = MAIN_MENU;
  685. time = 1800;
  686. player1Score = 0;
  687. player2Score = 0;
  688. player1Frame = 1;
  689. player2Frame = 1;
  690. player1.SetPosition(0, 0);
  691. player2.SetPosition(0, 0);
  692. player1Direction = DOWN;
  693. player2Direction = DOWN;
  694. upPressed = false;
  695. downPressed = false;
  696. humanSelected = true;
  697. }
  698. break;
  699. }
  700. }
  701. window.Display();
  702. window.Clear();
  703. }
  704.  
  705. return 0;
  706. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement