0% found this document useful (0 votes)
18 views12 pages

Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Game1.

cs
using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

public class Game1 : Game

private GraphicsDeviceManager _graphics;

private SpriteBatch _spriteBatch;

private SpriteFont _font;

private Texture2D _playerTexture;

private Texture2D _logoTexture;

private Texture2D _treeTexture1;

private Texture2D _treeTexture2;

private Texture2D _snowLayerTexture;

private Texture2D _skyBG;

private Texture2D _signTexture;

private Texture2D _barrierTexture1;

private Texture2D _barrierTexture2;

private Texture2D _tableTexture;

private Texture2D _coffeeTexture;

private Texture2D _drillTexture;

private Texture2D _foregroundTexture;

private Texture2D _floorTexture;

private Vector2 _playerPosition;

private Vector2 _targetPosition; // Nueva variable para la posición objetivo


private string _playerName;

private string _input;

private bool _isNameEntered;

private MouseState _previousMouseState;

private KeyboardState _previousKeyboardState; // Estado anterior del teclado

private float _playerScale; // Variable para el tamaño del sprite

private float _moveSpeed; // Nueva variable para la velocidad de movimiento

private Song _backgroundMusic; // Variable para la canción de fondo

private Vector2 _logoPosition = new Vector2(-1, -1); // Logo

private Vector2 _skyLayerPosition = new Vector2(-1, -1); // Cielo

private Vector2 _treePosition1 = new Vector2(12, 75);

private Vector2 _treePosition2 = new Vector2(660, 55);

private Vector2 _signPosition1 = new Vector2(640, 275);

private Vector2 _barrierPosition1 = new Vector2(670, 320);

private Vector2 _barrierPosition2 = new Vector2(115, 361);

private Vector2 _tablePosition = new Vector2(260, 230);

private Vector2 _coffeePosition = new Vector2(280, 135);

private Vector2 _drillPosition = new Vector2(237, 160);

private Vector2 _foregroundPosition = new Vector2(-1, 386);

private Vector2 _floorPosition = new Vector2(1, 164);

private Vector2 _snowLayerPosition = new Vector2(-1, 75); // Capa de nieve


cubriendo toda la pantalla

private Song _menuMusic; // Variable para la música del menú

public Game1()

{
_graphics = new GraphicsDeviceManager(this);

Content.RootDirectory = "Content";

IsMouseVisible = true;

_isNameEntered = false;

_input = string.Empty;

_playerScale = 0.5f; // Establecer el tamaño del sprite al 50%

_moveSpeed = 100f; // Establecer la velocidad de movimiento

// Establecer la resolución de pantalla

_graphics.PreferredBackBufferWidth = 759; // Ancho de la pantalla

_graphics.PreferredBackBufferHeight = 456; // Alto de la pantalla

_graphics.ApplyChanges(); // Aplicar los cambios

protected override void Initialize()

base.Initialize();

protected override void LoadContent()

_spriteBatch = new SpriteBatch(GraphicsDevice);

_font = Content.Load<SpriteFont>("Arial");

_logoTexture = Content.Load<Texture2D>("logo");

_playerTexture = Content.Load<Texture2D>("player");

_skyBG = Content.Load<Texture2D>("sky");

_treeTexture1 = Content.Load<Texture2D>("tree1");

_treeTexture2 = Content.Load<Texture2D>("tree2");
_signTexture = Content.Load<Texture2D>("sign");

_barrierTexture1 = Content.Load<Texture2D>("barrier");

_barrierTexture2 = Content.Load<Texture2D>("barrier2");

_tableTexture = Content.Load<Texture2D>("table");

_coffeeTexture = Content.Load<Texture2D>("coffee");

_drillTexture = Content.Load<Texture2D>("drill");

_foregroundTexture = Content.Load<Texture2D>("foreground");

_floorTexture = Content.Load<Texture2D>("floor");

_snowLayerTexture = Content.Load<Texture2D>("snowLayer");

_playerPosition = new Vector2(GraphicsDevice.Viewport.Width / 2,


GraphicsDevice.Viewport.Height / 2);

_targetPosition = _playerPosition; // Inicializar la posición objetivo

_menuMusic = Content.Load<Song>("menuMusic"); // Cargar la canción del


menú

MediaPlayer.Play(_menuMusic); // Reproducir la música del menú

MediaPlayer.IsRepeating = true; // Hacer que se repita

_backgroundMusic = Content.Load<Song>("backgroundMusic"); // Cargar la


canción

protected override void Update(GameTime gameTime)

if (Keyboard.GetState().IsKeyDown(Keys.Escape))

Exit();

KeyboardState currentKeyboardState = Keyboard.GetState();

if (!_isNameEntered)

{
foreach (var key in currentKeyboardState.GetPressedKeys())

if (!_previousKeyboardState.IsKeyDown(key)) // Detectar solo la pulsación


inicial de la tecla

if (key == Keys.Back && _input.Length > 0)

_input = _input.Remove(_input.Length - 1);

else if (key == Keys.Enter && _input.Length > 0)

_playerName = _input;

_isNameEntered = true;

// Detener la música del menú y reproducir música de fondo

MediaPlayer.Stop();

MediaPlayer.Play(_backgroundMusic);

MediaPlayer.IsRepeating = true;

else if (key >= Keys.A && key <= Keys.Z) // Filtrar solo teclas de letras

if (currentKeyboardState.IsKeyDown(Keys.LeftShift) ||
currentKeyboardState.IsKeyDown(Keys.RightShift))

_input += key.ToString(); // Añadir letra mayúscula

else

_input += key.ToString().ToLower(); // Añadir letra minúscula


}

else if (key >= Keys.D0 && key <= Keys.D9) // Filtrar solo teclas de
números

_input += key.ToString().Substring(1); // Añadir número

else

MouseState mouseState = Mouse.GetState();

if (mouseState.LeftButton == ButtonState.Pressed &&


_previousMouseState.LeftButton == ButtonState.Released)

_targetPosition = new Vector2(mouseState.X, mouseState.Y);

_previousMouseState = mouseState;

// Calcular la dirección y la distancia del movimiento

Vector2 direction = _targetPosition - _playerPosition;

if (direction.Length() > _moveSpeed *


(float)gameTime.ElapsedGameTime.TotalSeconds)

direction.Normalize();

_playerPosition += direction * _moveSpeed *


(float)gameTime.ElapsedGameTime.TotalSeconds;

}
else

_playerPosition = _targetPosition;

_previousKeyboardState = currentKeyboardState; // Actualizar el estado


anterior del teclado

base.Update(gameTime);

protected override void Draw(GameTime gameTime)

GraphicsDevice.Clear(Color.CornflowerBlue);

_spriteBatch.Begin();

if (!_isNameEntered)

// Calcular la posición centrada del logo

Vector2 logoPosition = new Vector2((GraphicsDevice.Viewport.Width -


_logoTexture.Width) / 2,

50); // Ajusta la altura según sea necesario

// Dibujar el logo

_spriteBatch.Draw(_logoTexture, logoPosition, Color.White);


// Calcular la posición centrada del texto para ingresar el nombre

string instructionText = "Por favor, ingresa un nombre para tu oso: " + _input;

Vector2 instructionSize = _font.MeasureString(instructionText);

Vector2 instructionPosition = new Vector2((GraphicsDevice.Viewport.Width -


instructionSize.X) / 2,

logoPosition.Y + _logoTexture.Height + 20); // Espacio


entre el logo y el texto

// Dibujar la instrucción para ingresar el nombre

_spriteBatch.DrawString(_font, instructionText, instructionPosition,


Color.Black);

else

// Calcular la posición centrada del nombre del jugador

Vector2 nameSize = _font.MeasureString(_playerName);

Vector2 namePosition = _playerPosition + new Vector2(-nameSize.X / 2,


_playerTexture.Height * _playerScale / 2 + 10);

// Dibuja la capa del cielo

_spriteBatch.Draw(_skyBG, _skyLayerPosition, Color.White);

_spriteBatch.DrawString(_font, "Esta es una prueba de desarrollo.", new


Vector2(10, 10), Color.Black);

// Dibuja el piso

_spriteBatch.Draw(_floorTexture, _floorPosition, Color.White);

// Dibuja la capa de nieve

_spriteBatch.Draw(_snowLayerTexture, _snowLayerPosition, Color.White);

// Dibuja el foreground

_spriteBatch.Draw(_foregroundTexture, _foregroundPosition, Color.White);


// Dibuja los árboles

_spriteBatch.Draw(_treeTexture1, _treePosition1, Color.White);

_spriteBatch.Draw(_treeTexture2, _treePosition2, Color.White);

// Dibuja los carteles

_spriteBatch.Draw(_signTexture, _signPosition1, Color.White);

// Dibuja la mesa

_spriteBatch.Draw(_tableTexture, _tablePosition, Color.White);

// Dibuja cafetera

_spriteBatch.Draw(_coffeeTexture, _coffeePosition, Color.White);

// Dibuja taladro

_spriteBatch.Draw(_drillTexture, _drillPosition, Color.White);

// Dibuja las barreras

_spriteBatch.Draw(_barrierTexture1, _barrierPosition1, Color.White);

_spriteBatch.Draw(_barrierTexture2, _barrierPosition2, Color.White);

_spriteBatch.DrawString(_font, _playerName, namePosition, Color.Black);

_spriteBatch.Draw(_playerTexture, _playerPosition, null, Color.White, 0f, new


Vector2(_playerTexture.Width / 2, _playerTexture.Height / 2), _playerScale,
SpriteEffects.None, 0f);

_spriteBatch.End();

base.Draw(gameTime);
}

MainMenu.cs
using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

public class MainMenu : Game

private GraphicsDeviceManager _graphics;

private SpriteBatch _spriteBatch;

private SpriteFont _font;

private string _gameTitle = "CODENAME NEVISCA";

private bool _isStartGameSelected;

public MainMenu()

_graphics = new GraphicsDeviceManager(this);

Content.RootDirectory = "Content";

IsMouseVisible = true;

_isStartGameSelected = false;

protected override void Initialize()

base.Initialize();

}
protected override void LoadContent()

_spriteBatch = new SpriteBatch(GraphicsDevice);

_font = Content.Load<SpriteFont>("Arial");

protected override void Update(GameTime gameTime)

if (Keyboard.GetState().IsKeyDown(Keys.Enter))

_isStartGameSelected = true;

if (_isStartGameSelected)

using (var game = new Game1())

game.Run();

Exit();

base.Update(gameTime);

protected override void Draw(GameTime gameTime)

GraphicsDevice.Clear(Color.CornflowerBlue);

_spriteBatch.Begin();

_spriteBatch.DrawString(_font, _gameTitle, new Vector2(10, 10), Color.White);


_spriteBatch.DrawString(_font, "Presiona ENTER para comenzar", new
Vector2(10, 50), Color.White);

_spriteBatch.End();

base.Draw(gameTime);

You might also like