Ziomnexpl

game1

Feb 17th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5.  
  6. namespace gra1.Desktop
  7. {
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class Game1 : Game
  12. {
  13. GraphicsDeviceManager graphics;
  14. SpriteBatch spriteBatch;
  15. //zmienne gry
  16. private Texture2D tlo;
  17. private Vector2 pozycja_tlo;
  18.  
  19. private Texture2D p1;
  20. private Vector2 pozycja_p1;
  21.  
  22. private Texture2D ob1;
  23. private Vector2 pozycja_ob1;
  24. private Vector2 pozycja_ob1n2;
  25. private Vector2 pozycja_ob1n3;
  26.  
  27. private float obrot = 0;
  28. private float obrot2 = 0;
  29. private float obrot3 = 0;
  30.  
  31. public Game1()
  32. {
  33. graphics = new GraphicsDeviceManager(this);
  34. graphics.PreferredBackBufferWidth = 800;
  35. graphics.PreferredBackBufferHeight = 600;
  36. Content.RootDirectory = "Content";
  37. }
  38.  
  39. /// <summary>
  40. /// Allows the game to perform any initialization it needs to before starting to run.
  41. /// This is where it can query for any required services and load any non-graphic
  42. /// related content. Calling base.Initialize will enumerate through any components
  43. /// and initialize them as well.
  44. /// </summary>
  45. protected override void Initialize()
  46. {
  47. // TODO: Add your initialization logic here
  48.  
  49. base.Initialize();
  50. }
  51.  
  52. /// <summary>
  53. /// LoadContent will be called once per game and is the place to load
  54. /// all of your content.
  55. /// </summary>
  56. protected override void LoadContent()
  57. {
  58. // Create a new SpriteBatch, which can be used to draw textures.
  59. spriteBatch = new SpriteBatch(GraphicsDevice);
  60.  
  61. tlo = this.Content.Load<Texture2D>("lul0");
  62. p1 = this.Content.Load<Texture2D>("lup0");
  63. ob1 = this.Content.Load<Texture2D>("luo1");
  64. pozycja_p1.Y = 500;
  65. pozycja_p1.X = 500;
  66.  
  67. pozycja_ob1.Y = 0;
  68. pozycja_ob1.X = 200;
  69.  
  70. pozycja_ob1n2.Y = 0;
  71. pozycja_ob1n2.X = 200;
  72.  
  73. pozycja_ob1n3.Y = 0;
  74. pozycja_ob1n3.X = 200;
  75.  
  76.  
  77. // TODO: use this.Content to load your game content here
  78. }
  79.  
  80. /// <summary>
  81. /// UnloadContent will be called once per game and is the place to unload
  82. /// game-specific content.
  83. /// </summary>
  84. protected override void UnloadContent()
  85. {
  86. // TODO: Unload any non ContentManager content here
  87. }
  88.  
  89. /// <summary>
  90. /// Allows the game to run logic such as updating the world,
  91. /// checking for collisions, gathering input, and playing audio.
  92. /// </summary>
  93. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  94. protected override void Update(GameTime gameTime)
  95. {
  96. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  97. Exit();
  98.  
  99. // TODO: Add your update logic here
  100. KeyboardState klawisz = Keyboard.GetState();
  101. if (klawisz.IsKeyDown(Keys.Right))
  102. {
  103. pozycja_p1.X += 20;
  104. p1 = this.Content.Load<Texture2D>("lup1");
  105. }
  106. if (klawisz.IsKeyDown(Keys.Left))
  107. {
  108. pozycja_p1.X -= 20;
  109. p1 = this.Content.Load<Texture2D>("lup0");
  110. }
  111. if (klawisz.IsKeyDown(Keys.Up))
  112. {
  113. pozycja_p1.Y -= 2;
  114. }
  115. if (klawisz.IsKeyDown(Keys.Down))
  116. {
  117. pozycja_p1.Y += 2;
  118. }
  119. //przeskakiwanie przy krawędziach
  120. if (pozycja_p1.X < 0)
  121. {
  122. pozycja_p1.X = 800;
  123. }
  124. if (pozycja_p1.X > 800)
  125. {
  126. pozycja_p1.X = 0;
  127. }
  128. //pozycja banana
  129. pozycja_ob1.Y = pozycja_ob1.Y + 10;
  130. if (pozycja_ob1.Y > 600)
  131. {
  132. pozycja_ob1.Y = 0;
  133. Random losuj = new Random();
  134. pozycja_ob1.X = losuj.Next(0, 700);
  135.  
  136. }
  137. pozycja_ob1n2.Y = pozycja_ob1n2.Y + 20;
  138. if (pozycja_ob1n2.Y > 600)
  139. {
  140. pozycja_ob1n2.Y = 0;
  141. Random losuj = new Random();
  142. pozycja_ob1n2.X = losuj.Next(0, 700);
  143.  
  144. }
  145. pozycja_ob1n3.Y = pozycja_ob1n3.Y + 5;
  146. if (pozycja_ob1n3.Y > 600)
  147. {
  148. pozycja_ob1n3.Y = 0;
  149. Random losuj = new Random();
  150. pozycja_ob1n3.X = losuj.Next(0, 700);
  151.  
  152. }
  153. //obrot banana
  154. obrot += 0.01f;
  155. obrot2 += 0.02f;
  156. obrot3 += 0.006f;
  157.  
  158. base.Update(gameTime);
  159. }
  160.  
  161. /// <summary>
  162. /// This is called when the game should draw itself.
  163. /// </summary>
  164. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  165. protected override void Draw(GameTime gameTime)
  166. {
  167. GraphicsDevice.Clear(Color.CornflowerBlue);
  168. Vector2 kat = new Vector2(0, 0);
  169. Rectangle nowy = new Rectangle(0, 0, ob1.Width, ob1.Height);
  170.  
  171. spriteBatch.Begin();
  172. spriteBatch.Draw(tlo, pozycja_tlo, Color.White);
  173. spriteBatch.Draw(ob1, pozycja_ob1, nowy, Color.White, obrot, kat, 1.0f, SpriteEffects.None, 1);
  174. spriteBatch.Draw(ob1, pozycja_ob1n2, nowy, Color.White, obrot2, kat, 1.0f, SpriteEffects.None, 1);
  175. spriteBatch.Draw(ob1, pozycja_ob1n3, nowy, Color.White, obrot3, kat, 1.0f, SpriteEffects.None, 1);
  176. spriteBatch.End();
  177.  
  178. spriteBatch.Begin();
  179. spriteBatch.Draw(p1, pozycja_p1, Color.White);
  180. spriteBatch.End();
  181.  
  182.  
  183.  
  184. // TODO: Add your drawing code here
  185.  
  186. base.Draw(gameTime);
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment