
Originally Posted by
My C# Work
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Starfight
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
static float aspectRatio;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
ship p1ship = new ship();
ship p2ship = new ship();
plasma p1plasma = new plasma();
plasma p2plasma = new plasma();
static Vector3 cameraPosition = new Vector3(0.0f, 250.0f, 500.0f);
Texture2D bgpic;
static int leftbound = -15000;
static int rightbound = 15000;
static int topbound = 15000;
static int bottombound = -15000;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
p1ship.resetposition = new Vector3(5000.0f, 0.0f, 2000.0f);
p2ship.resetposition = new Vector3(-5000.0f, 0.0f, 2000.0f);
p1ship.reset();
p2ship.reset();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
bgpic = Content.Load<Texture2D>("Textures\\background");
p1ship.model = Content.Load<Model>("Models\\p1_wedge");
p2ship.model = Content.Load<Model>("Models\\p2_pencil");
p1plasma.model = Content.Load<Model>("Models\\bfg_proj");
p2plasma.model = Content.Load<Model>("Models\\bfg_proj");
aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height;
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape) == true) this.Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Left) == true) p1ship.rotate(1, gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.Right) == true) p1ship.rotate(2, gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.Up) == true) p1ship.thrust(gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.Down) == true) p1plasma.launch(p1ship);
if (Keyboard.GetState().IsKeyDown(Keys.A) == true) p2ship.rotate(1, gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.D) == true) p2ship.rotate(2, gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.W) == true) p2ship.thrust(gameTime);
if (Keyboard.GetState().IsKeyDown(Keys.S) == true) p2plasma.launch(p2ship);
p1ship.Update(gameTime);
p2ship.Update(gameTime);
p1plasma.Update(gameTime);
p2plasma.Update(gameTime);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue );
//plasma class
public class plasma
{
public Model model;
public Vector3 position = Vector3.Zero;
public Vector3 speed = Vector3.Zero;
public float rotation = 0.0f;
public int time;
public float scale = 0.025f;
public bool visible = false;
public void launch(ship shooting)
{
position = shooting.position;
rotation = shooting.rotation;
speed.X = shooting.speed.X + (float)Math.Cos((rotation + Math.PI / 2)) * 25.0f;
speed.Z = shooting.speed.Z - (float)Math.Sin((rotation + Math.PI / 2)) * 25.0f;
time = 1500;
visible = true;
}
public void Update(GameTime gametime)
{
position.X += speed.X * (float)gametime.ElapsedGameTime.TotalMilliseconds;
position.Y += speed.Y * (float)gametime.ElapsedGameTime.TotalMilliseconds;
position.Z += speed.Z * (float)gametime.ElapsedGameTime.TotalMilliseconds;
if (position.X < leftbound) position.X = rightbound;
if (position.X > rightbound) position.X = leftbound;
if (position.Z < bottombound) position.Z = topbound;
if (position.Z > topbound) position.Z = bottombound;
time -= (int)gametime.ElapsedGameTime.TotalMilliseconds;
if (time <= 0) visible = false;
}
public void Draw()
{
if (visible == true)
{
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index];
effect.World *= Matrix.CreateRotationY(rotation);
effect.World *= Matrix.CreateTranslation(position);
effect.World *= Matrix.CreateScale(scale);
effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToR adians(45.0f), aspectRatio, 1.0f, 10000.0f);
}
mesh.Draw();
}
}
}
public void checkcollision(ship target)
{
if (visible == true)
{
BoundingSphere plasmasphere = new BoundingSphere();
plasmasphere.Center = position;
plasmasphere.Radius = 500;
BoundingSphere shipsphere = new BoundingSphere();
shipsphere.Center = target.position;
shipsphere.Radius = 500;
if (plasmasphere.Intersects(shipsphere))
{
visible = false;
target.reset();
}
}
}
}
//ship class
public class ship
{
public Model model;
public Vector3 position = Vector3.Zero;
public Vector3 resetposition = Vector3.Zero;
public Vector3 speed = Vector3.Zero;
public float rotation = 0.0f;
public float handling = 0.35f;
public float acceleration = 0.01f;
public float scale = 0.025f;
public void Draw()
{
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index];
effect.World *= Matrix.CreateRotationY(rotation);
effect.World *= Matrix.CreateTranslation(position);
effect.World *= Matrix.CreateScale(scale);
effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToR adians(45.0f), aspectRatio, 1.0f, 10000.0f);
}
mesh.Draw();
}
}
public void thrust(GameTime gametime)
{
speed.X += (float)Math.Cos((rotation + Math.PI / 2)) * acceleration * (float)gametime.ElapsedGameTime.TotalMilliseconds;
speed.Z -= (float)Math.Sin((rotation + Math.PI / 2)) * acceleration * (float)gametime.ElapsedGameTime.TotalMilliseconds;
}
public void rotate(int direction, GameTime gametime)
{
if (direction == 1) rotation += (float)gametime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(handling);
else rotation -= (float)gametime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(handling);
}
public void Update(GameTime gametime)
{
position.X += speed.X * (float)gametime.ElapsedGameTime.TotalMilliseconds;
position.Y += speed.Y * (float)gametime.ElapsedGameTime.TotalMilliseconds;
position.Z += speed.Z * (float)gametime.ElapsedGameTime.TotalMilliseconds;
if (position.X < leftbound) position.X = rightbound;
if (position.X > rightbound) position.X = leftbound;
if (position.Z < bottombound) position.Z = topbound;
if (position.Z > topbound) position.Z = bottombound;
}
public void checkcollision(ship othership)
{
BoundingSphere thisshipsphere = new BoundingSphere();
thisshipsphere.Center = position;
thisshipsphere.Radius = 500;
BoundingSphere othershipsphere = new BoundingSphere();
othershipsphere.Center = othership.position;
othershipsphere.Radius = 500;
if (thisshipsphere.Intersects(othershipsphere))
{
speed.X = -speed.X;
speed.Z = -speed.Z;
}
}
public void reset()
{
position = resetposition;
speed = Vector3.Zero;
}
}
base.Draw(gameTime);
}
}
}
Yay for almost weekend time! One project I'm going to tackle is changing the transmission/transfer case gear lube in my Subaru this long weekend. Hopefully there's enough of a gap in the rain...
Is It Just Me? v233893843