Someguy384728

Code

Jun 23rd, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. #include "wx/wx.h"
  2. #include "wx/dcbuffer.h"
  3. #include <string>
  4. using namespace std;
  5.  
  6. class MyFrame : public wxFrame{
  7. public:
  8.  
  9. wxRect player;
  10. wxRect leftEye, rightEye;
  11. wxRect shot;
  12. wxRect enemy;
  13. bool shotMoving;
  14. wxPoint shotSpeed;
  15. wxPoint playerSpeedUpLeft, playerSpeedDownRight;
  16. wxTimer timer;
  17. wxColor black;
  18. wxBrush blackBrush;
  19. wxColor red;
  20. wxBrush redBrush;
  21. wxColor blue;
  22. wxBrush blueBrush;
  23. const static int UP{0}, DOWN{1}, LEFT{2}, RIGHT{3};
  24. int facing;
  25. const static int playerSpeed{7}, shotVelocity{7}, eyeOffset{5},
  26. playerMajorDimension{60},playerMinorDimension{40};
  27.  
  28. MyFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title, wxPoint(50,50), wxSize(700,550)){
  29. SetBackgroundStyle(wxBG_STYLE_PAINT);
  30. facing = RIGHT;
  31. player = wxRect((GetSize().GetWidth() - playerMinorDimension)/2, (GetSize().GetHeight() - playerMajorDimension)/2,
  32. playerMinorDimension, playerMajorDimension);
  33.  
  34.  
  35. leftEye.SetSize(wxSize(10,10));
  36. rightEye.SetSize(wxSize(10,10));
  37. leftEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - leftEye.GetWidth());
  38. rightEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - rightEye.GetWidth());
  39. int yCenter = player.GetY() + player.GetHeight()/2;
  40. leftEye.SetY(yCenter - eyeOffset - leftEye.GetHeight());
  41. rightEye.SetY(yCenter + eyeOffset);
  42.  
  43.  
  44. shot = wxRect(wxPoint(-100,-100), wxSize(5,5));
  45. shotMoving = false;
  46.  
  47.  
  48. shotSpeed = wxPoint(0,0);
  49. playerSpeedUpLeft = wxPoint(0,0); playerSpeedDownRight = wxPoint(0,0);
  50.  
  51.  
  52. timer.SetOwner(this);
  53. black = wxColor(0,0,0);
  54. blackBrush = wxBrush(black);
  55. red = wxColor(255,0,0);
  56. redBrush= wxBrush(red);
  57. blue = wxColor(0,0,255);
  58. blueBrush = wxBrush(blue);
  59.  
  60. Bind(wxEVT_CLOSE_WINDOW, &MyFrame::OnClose, this);
  61. Bind(wxEVT_KEY_DOWN, &MyFrame::OnKeyDown, this);
  62. Bind(wxEVT_KEY_UP, &MyFrame::OnKeyUp, this);
  63. Bind(wxEVT_PAINT, &MyFrame::OnPaint, this);
  64. Bind(wxEVT_TIMER, &MyFrame::OnTimer, this);
  65.  
  66. timer.Start(30);
  67. }
  68.  
  69. void OnClose(wxCloseEvent &event){
  70. Destroy();
  71. }
  72.  
  73.  
  74. void updateplayerAndEyes(){
  75. if (facing == LEFT || facing == RIGHT){
  76. player.SetWidth(playerMinorDimension);
  77. player.SetHeight(playerMajorDimension);
  78. int yCenter = player.GetY() + player.GetHeight()/2;
  79. leftEye.SetY(yCenter - eyeOffset - leftEye.GetHeight());
  80. rightEye.SetY(yCenter + eyeOffset);
  81. if (facing == LEFT){
  82. leftEye.SetX(player.GetX() + eyeOffset);
  83. rightEye.SetX(player.GetX() + eyeOffset);
  84. }
  85. else{
  86. leftEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - leftEye.GetWidth());
  87. rightEye.SetX(player.GetX() + player.GetWidth() - eyeOffset - rightEye.GetWidth());
  88. }
  89. }
  90. else{
  91. player.SetWidth(playerMajorDimension);
  92. player.SetHeight(playerMinorDimension);
  93. int xCenter = player.GetX() + player.GetWidth()/2;
  94. leftEye.SetX(xCenter - eyeOffset - leftEye.GetWidth());
  95. rightEye.SetX(xCenter + eyeOffset);
  96. if (facing == UP){
  97. leftEye.SetY(player.GetY() + eyeOffset);
  98. rightEye.SetY(player.GetY() + eyeOffset);
  99. }
  100. else{
  101. leftEye.SetY(player.GetY() + player.GetHeight() - eyeOffset - leftEye.GetHeight());
  102. rightEye.SetY(player.GetY() + player.GetHeight() - eyeOffset - rightEye.GetHeight());
  103. }
  104. }
  105. }
  106.  
  107.  
  108. void OnKeyDown(wxKeyEvent &event){
  109. char key = static_cast<char>(event.GetUnicodeKey());
  110. if (key == 'W'){
  111. playerSpeedUpLeft.y = playerSpeed;
  112. facing = UP;
  113. }
  114. else if (key == 'S'){
  115. playerSpeedDownRight.y = playerSpeed;
  116. facing = DOWN;
  117. }
  118. else if (key == 'A'){
  119. playerSpeedUpLeft.x = playerSpeed;
  120. facing = LEFT;
  121. }
  122. else if (key == 'D'){
  123. playerSpeedDownRight.x = playerSpeed;
  124. facing = RIGHT;
  125. }
  126.  
  127. else if (event.GetKeyCode() == WXK_SPACE && !shotMoving){
  128. if (facing == UP){
  129. shot.SetX(player.GetX() + (player.GetWidth() - shot.GetWidth())/2);
  130. shot.SetY(player.GetY() - shot.GetWidth()/2);
  131. shotSpeed = wxPoint(0, -shotVelocity);
  132. }
  133. if (facing == DOWN){
  134. shot.SetX(player.GetX() + (player.GetWidth() - shot.GetWidth())/2);
  135. shot.SetY(player.GetY() + player.GetHeight() - shot.GetWidth()/2);
  136. shotSpeed = wxPoint(0, shotVelocity);
  137. }
  138. if (facing == RIGHT){
  139. shot.SetX(player.GetX() + player.GetWidth() - shot.GetWidth()/2);
  140. shot.SetY(player.GetY() + (player.GetHeight()- shot.GetHeight())/2);
  141. shotSpeed = wxPoint(shotVelocity, 0);
  142. }
  143. if (facing == LEFT){
  144. shot.SetX(player.GetX() - shot.GetWidth()/2);
  145. shot.SetY(player.GetY() + (player.GetHeight()- shot.GetHeight())/2);
  146. shotSpeed = wxPoint(-shotVelocity, 0);
  147. }
  148. shotMoving = true;
  149. }
  150. updateplayerAndEyes();
  151. }
  152.  
  153.  
  154. void OnKeyUp(wxKeyEvent &event){
  155. char key = static_cast<char>(event.GetUnicodeKey());
  156. if (key == 'W'){
  157. playerSpeedUpLeft.y = 0;
  158. if (playerSpeedDownRight.y > 0){
  159. facing = DOWN;
  160. }
  161. else if (playerSpeedDownRight.x > 0){
  162. facing = RIGHT;
  163. }
  164. else if (playerSpeedUpLeft.x > 0){
  165. facing = LEFT;
  166. }
  167. }
  168. if (key == 'S'){
  169. playerSpeedDownRight.y = 0;
  170. if (playerSpeedUpLeft.y > 0){
  171. facing = UP;
  172. }
  173. else if (playerSpeedDownRight.x > 0){
  174. facing = RIGHT;
  175. }
  176. else if (playerSpeedUpLeft.x > 0){
  177. facing = LEFT;
  178. }
  179. }
  180. if (key == 'A'){
  181. playerSpeedUpLeft.x = 0;
  182. if (playerSpeedDownRight.x > 0){
  183. facing = RIGHT;
  184. }
  185. else if (playerSpeedDownRight.y > 0){
  186. facing = DOWN;
  187. }
  188. else if (playerSpeedUpLeft.y > 0){
  189. facing = UP;
  190. }
  191. }
  192. if (key == 'D'){
  193. playerSpeedDownRight.x = 0;
  194. if (playerSpeedUpLeft.x > 0){
  195. facing = LEFT;
  196. }
  197. else if (playerSpeedDownRight.y > 0){
  198. facing = DOWN;
  199. }
  200. else if (playerSpeedUpLeft.y > 0){
  201. facing = UP;
  202. }
  203. }
  204. updateplayerAndEyes();
  205. }
  206.  
  207.  
  208. void OnPaint(wxPaintEvent &event){
  209. wxAutoBufferedPaintDC dc(this);
  210. dc.SetBackground(*wxWHITE_BRUSH);
  211. dc.Clear();
  212. dc.SetPen(*wxBLACK_PEN);
  213. dc.SetBrush(blackBrush);
  214. dc.DrawEllipse(player);
  215. dc.SetBrush(redBrush);
  216. dc.DrawEllipse(leftEye);
  217. dc.DrawEllipse(rightEye);
  218. dc.SetBrush(blackBrush);
  219. dc.DrawEllipse(shot);
  220. dc.SetBrush(blueBrush);
  221. dc.DrawEllipse(enemy);
  222. }
  223.  
  224.  
  225. void OnTimer(wxTimerEvent &event){
  226. player.SetPosition(player.GetPosition() + playerSpeedDownRight - playerSpeedUpLeft);
  227. leftEye.SetPosition(leftEye.GetPosition() + playerSpeedDownRight - playerSpeedUpLeft);
  228. rightEye.SetPosition(rightEye.GetPosition() + playerSpeedDownRight - playerSpeedUpLeft);
  229. if (shotMoving){
  230. shot.SetPosition(shot.GetPosition() + shotSpeed);
  231. if (shot.GetX() + shot.GetWidth() < 0 || shot.GetX() > GetSize().GetWidth() ||
  232. shot.GetY() + shot.GetHeight() < 0 || shot.GetY() > GetSize().GetHeight()){
  233. shotSpeed = wxPoint(0,0);
  234. shotMoving = false;
  235. }
  236. }
  237.  
  238. Refresh();
  239. }
  240. };
  241.  
  242. class MyApp : public wxApp{
  243. public:
  244. virtual bool OnInit(){
  245. MyFrame *frame = new MyFrame("Final Game");
  246. frame->Show(true);
  247. return true;
  248. }
  249. };
  250.  
  251. IMPLEMENT_APP(MyApp);
  252.  
Advertisement
Add Comment
Please, Sign In to add comment