Anda di halaman 1dari 6

using using using using using using using using using using using using

System; System.Collections.Generic; System.Linq; Microsoft.Xna.Framework; Microsoft.Xna.Framework.Audio; Microsoft.Xna.Framework.Content; Microsoft.Xna.Framework.GamerServices; Microsoft.Xna.Framework.Graphics; Microsoft.Xna.Framework.Input; Microsoft.Xna.Framework.Media; Microsoft.Xna.Framework.Net; Microsoft.Xna.Framework.Storage;

namespace PFC { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; private Texture2D backgroundTexture; private Texture2D bolaaTexture; private Texture2D muroTexture; private SpriteBatch spriteBatch = null; private Bola player; private muro[] muros; private Vector2 position; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } private void Start() { if (player == null) { position = new Vector2(); player = new Bola(this, ref bolaaTexture); Components.Add(player); muros = new muro[7]; for (int i = 0; i < 7; i++) { muros[i] = new muro(this, ref muroTexture, i); Components.Add(muros[i]); } } } public void DoGameLogic() { position.Y = -1; bool hasCollision = false; Rectangle bolaRectangle = player.GetBounds(); foreach (GameComponent gc in Components) { if (gc is muro) { hasCollision = ((muro)gc).CheckCollision(bolaRectangle); if (hasCollision) {

position.Y = 2; } else { while (position.Y < 5) { position.Y = position.Y - 1; } } } } } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); backgroundTexture = Content.Load<Texture2D>("background"); bolaaTexture = Content.Load<Texture2D>("Bolaa"); muroTexture = Content.Load<Texture2D>("Muro"); Services.AddService(typeof(SpriteBatch), spriteBatch); } protected override void UnloadContent() { } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(SpriteBlendMode.AlphaBlend); spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, graphics.Gra phicsDevice.DisplayMode.Width, graphics.GraphicsDevice.DisplayMode.Height), Colo r.LightGray); base.Draw(gameTime); spriteBatch.End(); } protected override void Update(GameTime gameTime) { GamePadState gamepadstatus = GamePad.GetState(PlayerIndex.One); KeyboardState keyboard = Keyboard.GetState(); if ((gamepadstatus.Buttons.Back == ButtonState.Pressed) (keyboard .IsKeyDown(Keys.Escape))) { Exit(); } if (player == null) { Start();

} DoGameLogic(); base.Update(gameTime); } } } muro using using using using using using using using using using using using System; System.Collections.Generic; System.Linq; Microsoft.Xna.Framework; Microsoft.Xna.Framework.Audio; Microsoft.Xna.Framework.Content; Microsoft.Xna.Framework.GamerServices; Microsoft.Xna.Framework.Graphics; Microsoft.Xna.Framework.Input; Microsoft.Xna.Framework.Media; Microsoft.Xna.Framework.Net; Microsoft.Xna.Framework.Storage;

namespace PFC { public class muro : Microsoft.Xna.Framework.DrawableGameComponent { protected Texture2D texture; protected Rectangle spriteRectangle; protected Vector2 position; protected const int MUROHEIGHT = 30; protected const int MUROWIDTH = 200; protected Rectangle screenbounds; protected int sensibilidade = 3; protected Random random; protected int posY; protected int Yspeed; public muro(Game game, ref Texture2D theTexture, int i) : base(game) { texture = theTexture; position = new Vector2(); random = new Random(i * new Random().Next()); spriteRectangle = new Rectangle(0, 0, MUROWIDTH, MUROHEIGHT); posY = i; PutinStartPositon(); } public void PutinStartPositon() { position.X 4) - MUROWIDTH; if (position.X { position.X } if (posY < 99) { position.Y = random.Next(5) * (Game.Window.ClientBounds.Width / < 0) = 0;

= (posY + 1) * (MUROHEIGHT + 65);

posY = 99; } else { int a = Game.Window.ClientBounds.Height / (MUROHEIGHT + 65); position.Y = (a + 1) * (MUROHEIGHT + 65); } Yspeed = 2; } public override void Initialize() { base.Initialize(); } public override void Update(GameTime gameTime) { if ((position.Y+MUROHEIGHT < 0 )) { PutinStartPositon(); } position.Y -= Yspeed; base.Update(gameTime); } public override void Draw(GameTime gameTime) { SpriteBatch sbatch = (SpriteBatch)Game.Services.GetService(typeof(Sp riteBatch)); sbatch.Draw(texture, position, spriteRectangle, Color.White); base.Draw(gameTime); } public Rectangle GetBounds() { return new Rectangle((int)position.X, (int)position.Y, MUROWIDTH, MU ROHEIGHT); } public bool CheckCollision(Rectangle rect) { Rectangle spriterect = new Rectangle((int)position.X, (int)position. Y, MUROWIDTH, MUROHEIGHT); return spriterect.Intersects(rect); } } } player #region Using Statements using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; #endregion using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework.Audio;

using using using using using

Microsoft.Xna.Framework.Content; Microsoft.Xna.Framework.GamerServices; Microsoft.Xna.Framework.Media; Microsoft.Xna.Framework.Net; Microsoft.Xna.Framework.Storage;

namespace PFC { public class Bola : Microsoft.Xna.Framework.DrawableGameComponent { protected Texture2D texture; protected Rectangle spriteRectangle; protected Vector2 position; protected const int BOLAHEIGHT = 30; protected const int BOLAWIDTH = 30; protected Rectangle screenbounds; protected int sensibilidade = 3; public Bola(Game game, ref Texture2D theTexture) : base(game) { texture = theTexture; position = new Vector2(); spriteRectangle = new Rectangle(0, 0, BOLAWIDTH, BOLAHEIGHT); screenbounds = new Rectangle(0, 0, game.Window.ClientBounds.Width, g ame.Window.ClientBounds.Height); } public void PutinStartPositon() { position.X = 65; position.Y = 0; } public override void Initialize() { base.Initialize(); PutinStartPositon(); } public override void Update(GameTime gameTime) { KeyboardState keyboard = Keyboard.GetState(); if (keyboard.IsKeyDown(Keys.Up)) { position.Y -= sensibilidade; } if (keyboard.IsKeyDown(Keys.Down)) { position.Y += sensibilidade; } if (keyboard.IsKeyDown(Keys.Left)) { position.X -= sensibilidade; } if (keyboard.IsKeyDown(Keys.Right))

{ position.X += sensibilidade; } if (position.X < screenbounds.Left) { position.X = screenbounds.Left; } if (position.X > screenbounds.Width - BOLAWIDTH) { position.X = screenbounds.Width - BOLAWIDTH; } if (position.Y < screenbounds.Top) { position.Y = screenbounds.Top; } if (position.Y > screenbounds.Height - BOLAHEIGHT) { position.Y = screenbounds.Height - BOLAHEIGHT; } base.Update(gameTime); } public override void Draw(GameTime gameTime) { SpriteBatch sbatch = (SpriteBatch)Game.Services.GetService(typeof(Sp riteBatch)); sbatch.Draw(texture, position, spriteRectangle, Color.White); base.Draw(gameTime); } public Rectangle GetBounds() { return new Rectangle((int)position.X, (int)position.Y, BOLAWIDTH, BO LAHEIGHT); } } }

Anda mungkin juga menyukai