Advertisement
Guest User

myGame :P

a guest
Aug 5th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. namespace Game
  2. {
  3. public partial class Form1 : Form
  4. {
  5. bool left;
  6. bool right;
  7. bool jump;
  8. bool onObject = true;
  9. int G = 20;
  10. int force;
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15.  
  16. private void timer1_Tick(object sender, EventArgs e)
  17. {
  18. loadGameControls();
  19. RegisterBlocks();
  20. playerSkin();
  21. }
  22.  
  23. private void Form1_KeyDown(object sender, KeyEventArgs e)
  24. {
  25. if (e.KeyCode == Keys.Left)
  26. {
  27. left = true;
  28. player.Image = Image.FromFile("walk_l.gif");
  29. }
  30. if (e.KeyCode == Keys.Right)
  31. {
  32. right = true;
  33. player.Image = Image.FromFile("walk_r.gif");
  34. }
  35. if (!jump)
  36. {
  37. if (e.KeyCode == Keys.Up)
  38. {
  39. jump = true;
  40. force = G;
  41. player.Image = Image.FromFile("jump.png");
  42. }
  43. }
  44. }
  45. private void Form1_KeyUp(object sender, KeyEventArgs e)
  46. {
  47. if (e.KeyCode == Keys.Left){left = false;}
  48. if (e.KeyCode == Keys.Right){right = false;}
  49. }
  50. void playerSkin()
  51. {
  52. if (!jump && !right && !left)
  53. {
  54. player.Image = Image.FromFile("stand.png");
  55. }
  56. }
  57. void checkColision(PictureBox block)
  58. {
  59. //Colision
  60. //Right
  61. if (player.Left + player.Width >= block.Left && block.Left + player.Width > player.Left && player.Top < block.Top && player.Bottom > block.Bottom)
  62. {
  63. right = false;
  64. player.Left = block.Left - player.Width;
  65. }
  66. //Left
  67. if (block.Right + player.Width >= player.Right && block.Right + player.Width <= player.Right + block.Width && block.Bottom + block.Height >= player.Bottom + 5 && block.Top <= player.Top)
  68. {
  69. left = false;
  70. player.Left = block.Right;
  71. }
  72. //Top
  73. if (player.Left + player.Width > block.Left && player.Left < block.Left + block.Width && player.Top + player.Height >= block.Top && player.Top < block.Top)
  74. {
  75. player.Top = block.Top - player.Height;
  76. force = 0;
  77. onObject = true;
  78. jump = false;
  79. }
  80. else
  81. {
  82. onObject = false;
  83. }
  84. //Bottom
  85. if (player.Left + player.Width > block.Left && player.Left < block.Left + block.Width && player.Bottom > block.Bottom && player.Bottom <= block.Bottom + player.Height + 3)
  86. {
  87. force = -1;
  88. }
  89. }
  90. void RegisterBlocks()
  91. {
  92. checkColision(b3);
  93. checkColision(b1);
  94. checkColision(b2);
  95. checkColision(block);
  96. }
  97. void loadGameControls()
  98. {
  99. //Movement
  100. if (left)
  101. {
  102. player.Left -= 5;
  103. }
  104. if (right)
  105. {
  106. player.Left += 5;
  107. }
  108. if (jump)
  109. {
  110. player.Top -= force;
  111. force -= 1;
  112. }
  113. if ((player.Top + player.Height >= screen.Height))
  114. {
  115. player.Top = screen.Height - player.Height;
  116. jump = false;
  117.  
  118. } else if (!onObject)
  119. {
  120. player.Top += 5;
  121. }
  122. //Bounds
  123. if (player.Left <= 0 - player.Width)
  124. {
  125. player.Left += screen.Width;
  126. }
  127. if (player.Left >= screen.Width)
  128. {
  129. player.Left = 0 - player.Width;
  130. }
  131. }
  132.  
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement