Advertisement
EddieTheLiar

Untitled

Mar 24th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Cowboy_Zombaliens
  12. {
  13. public partial class Form4 : Form
  14. {
  15. //set up movement booleans. if the boolean is true, the player moves, if the boolean is false it will not.
  16. bool goLeft;
  17. bool goRight;
  18. bool goUp;
  19. bool goDown;
  20.  
  21. // set up a vlaue for the scoring
  22. int score = 0;
  23.  
  24. private Point pt;
  25.  
  26. public Form4()
  27. {
  28. InitializeComponent();
  29. }
  30.  
  31. private void keyisdown(object sender, KeyEventArgs e)
  32. {
  33. if (e.KeyCode == Keys.A)
  34. {
  35. goLeft = true;
  36. }
  37.  
  38. if (e.KeyCode == Keys.D)
  39. {
  40. goRight = true;
  41. }
  42.  
  43. if (e.KeyCode == Keys.W)
  44. {
  45. goUp = true;
  46. }
  47. if (e.KeyCode == Keys.S)
  48. {
  49. goDown = true;
  50. }
  51.  
  52. if (e.KeyCode == Keys.R)
  53. {
  54. new Form4().Show();
  55. this.Hide();
  56. }
  57. //Returns to the main menu when pressing escape
  58. if (e.KeyCode == Keys.Escape)
  59. {
  60. new Form2().Show();
  61. this.Hide();
  62. }
  63.  
  64. }
  65.  
  66. public void AIMove(int x, int y, float speed)
  67. {
  68. float tx = x - pt.X;
  69. float ty = y - pt.Y;
  70. float length = (float)Math.Sqrt(tx * tx + ty * ty);
  71. if (length > speed)
  72. {
  73.  
  74. // move towards the goal
  75. pt.X = (int)(pt.X + speed * tx / length);
  76. pt.Y = (int)(pt.Y + speed * ty / length);
  77. }
  78. else
  79. {
  80. // already there
  81. pt.X = x;
  82. pt.Y = y;
  83. }
  84. }
  85.  
  86. private void keyisup(object sender, KeyEventArgs e)
  87. {
  88. //Get users input
  89. if (e.KeyCode == Keys.A)
  90. {
  91. goLeft = false;
  92. }
  93.  
  94. if (e.KeyCode == Keys.D)
  95. {
  96. goRight = false;
  97. }
  98.  
  99. if (e.KeyCode == Keys.W)
  100. {
  101. goUp = false;
  102. }
  103.  
  104. if (e.KeyCode == Keys.S)
  105. {
  106. goDown = false;
  107. }
  108.  
  109. }
  110.  
  111. private void timer1_Tick(object sender, EventArgs e)
  112. {
  113. if (Player.Bounds.IntersectsWith(pbxEnemy.Bounds))
  114. {
  115. timer1.Stop(); //stops the timer to prevent game from running
  116. MessageBox.Show("A Cowboy Zombalien has eaten your brain.\nYour final score is " + score, "GAME OVER!"); //displays game over menu
  117. new Form2().Show();
  118. this.Hide();
  119. }
  120.  
  121. //get the current player location before movement occurs
  122. int CurrentPlayerLocationX = Player.Top;
  123. int CurrentPlayerLocationY = Player.Left;
  124.  
  125. //player moves based on which key is pressed
  126. if (goLeft)
  127. {
  128. Player.Left -= 5;
  129. }
  130. if (goRight)
  131. {
  132. Player.Left += 5;
  133. }
  134. if (goUp)
  135. {
  136. Player.Top -= 5;
  137. }
  138. if (goDown)
  139. {
  140. Player.Top += 5;
  141. }
  142.  
  143. //Get the players new position
  144. int NewPlayerLocationX = Player.Location.X;
  145. int NewPlayerLocationY = Player.Location.Y;
  146.  
  147. foreach (Control x in this.Controls)
  148. {
  149. //tests if the player is touching a wall
  150. if (x is PictureBox && x.Tag == "wall")
  151. {
  152. if (Player.Bounds.IntersectsWith(x.Bounds))
  153. {
  154. //moves the player out of the wall
  155. Player.Top = CurrentPlayerLocationX;
  156. Player.Left = CurrentPlayerLocationY;
  157. }
  158. }
  159. //Checks if the player has picked up a coin
  160. }
  161. foreach (Control x in this.Controls)
  162. {
  163.  
  164. if (x is PictureBox && x.Tag == "coin")
  165. {
  166. if (Player.Bounds.IntersectsWith(x.Bounds))
  167. {
  168. Random random = new System.Random();
  169. int RandomXLocation = random.Next(10, 1550); //returns a random X co-ord
  170. int RandomYLocation = random.Next(10, 850); //returns a random Y co-ord
  171.  
  172. x.Top = RandomYLocation; //moves coin to the random co-ords above
  173. x.Left = RandomXLocation; //moves coin to the random co-ords
  174.  
  175. score = score + 10;
  176.  
  177. }
  178. }
  179. //Checks if the player has picked up a diamond
  180. if (x is PictureBox && x.Tag == "diamond")
  181. {
  182. if (Player.Bounds.IntersectsWith(x.Bounds))
  183. {
  184. Random random = new System.Random();
  185. int RandomXLocation = random.Next(10, 1550); //returns a random X co-ord
  186. int RandomYLocation = random.Next(10, 850); //returns a random Y co-ord
  187.  
  188. x.Top = RandomYLocation; //moves the diamond to the random co-ords above
  189. x.Left = RandomXLocation; //moves the diamond to the random co-ords above
  190.  
  191. score = score + 125; //increases score
  192. }
  193. }
  194.  
  195. lblScore.Text = "Score: " + score;
  196. }
  197. pbxEnemy.AIMove(100, 100, 0.5f);
  198. }
  199.  
  200.  
  201. }
  202. }
  203.  
  204. //private void Form4_Load(object sender, EventArgs e)
  205. //{
  206.  
  207. //}
  208.  
  209. //private void lblScore_Click(object sender, EventArgs e)
  210. //{
  211.  
  212. //}
  213.  
  214. //private void Player_Click(object sender, EventArgs e)
  215. //{
  216.  
  217. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement