using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using Microsoft.DirectX.DirectInput; using Microsoft.DirectX.DirectSound; namespace SpriteClass { public partial class Form1 : Form { public Form1() { InitializeComponent(); InitializeGraphics(); InitializeInput(); InitializeSound(); } //Graphics Microsoft.DirectX.Direct3D.Device device = null; Sprite spriteHandler; //Input Microsoft.DirectX.DirectInput.Device inputDevice = null; //Sound Microsoft.DirectX.DirectSound.Device soundDevice = null; SecondaryBuffer bgMusic; SecondaryBuffer hit; SecondaryBuffer explosion; SecondaryBuffer fire; SecondaryBuffer heliSound; //Objects and Variables SpriteClass.player player1 = new SpriteClass.player(); SpriteClass.player player2 = new SpriteClass.player(); Apache.entity p1hp = new Moloy.entity(); Apache.entity p2hp = new Moloy.entity(); Apache.entity helpImage = new Moloy.entity(); Apache.entity p1text = new Moloy.entity(); Apache.entity p2text = new Moloy.entity(); Apache.entity bg = new Moloy.entity(); Apache.entity p1Win = new Moloy.entity(); Apache.entity p2Win = new Moloy.entity(); float p1Health = 100; float p2Health = 100; //Methods #region Initialization //---------------------------------------------------------------------------- // Method: initSystem // Description: Initializes GUI components //---------------------------------------------------------------------------- private void initSystem() { // Help Init helpImage.x = 1000; helpImage.y = 1000; helpImage.loadTexture("./art/help.png", device); //Background Init bg.x = 0; bg.y = 0; bg.loadTexture("./art/bg.png", device); //Player 1 win init p1Win.x = 1000; p1Win.y = 1000; p1Win.loadTexture("./art/p1win.png", device); //Player 2 win init p2Win.x = 1000; p2Win.y = 1000; p2Win.loadTexture("./art/p2win.png", device); } //FIXME: initPlayer1 & initPlayer2 should be moved to a seperate class //---------------------------------------------------------------------------- // Method: initPlayer1 // Description: Initializes player 1 object and variables //---------------------------------------------------------------------------- private void initPlayer1() { //Player 1 Init player1.x = 50; player1.y = 500; player1.setWidth(95); player1.setHeight(70); player1.velX = 5; player1.velY = 0; player1.loadTexture("./art/heli.png", device); player1.columns = 3; player1.curFrame = 0; player1.totalFrames = 3; player1.animCount = 3; player1.animDelay = 5; player1.rotationAngle = 3.5f; //player 1 health init p1hp.x = 10; p1hp.y = 540; p1hp.height = 10; p1hp.width = 100; p1hp.loadTexture("./art/hp2.png", device); p1hp.columns = 5; p1hp.curFrame = 0; p1hp.totalFrames = 5; p1text.x = 10; p1text.y = 520; p1text.loadTexture("./art/p1.png", device); //player2 bullet player1.bullet.x = player1.x; player1.bullet.y = player1.y; player1.bullet.setWidth(player1.width); player1.bullet.setHeight(player1.height); player1.bullet.RotationPoint.X = player1.bullet.x + player1.bullet.width / 2; player2.bullet.RotationPoint.Y = player2.bullet.y + player2.bullet.height / 2; player1.bullet.length = -10; player1.bullet.alive = false; player1.bullet.loadTexture("./art/missle.png", device); } //---------------------------------------------------------------------------- // Method: initPlayer2 // Description: Initializes player 2 object and variables //---------------------------------------------------------------------------- private void initPlayer2() { //Player 2 init player2.x = 700; player2.y = 40; player2.setWidth(95); player2.setHeight(70); player2.velX = 5; player2.velY = 0; player2.loadTexture("./art/heli2.png", device); player2.columns = 3; player2.curFrame = 0; player2.totalFrames = 3; player2.animCount = 3; player2.animDelay = 5; //Player 2 health init p2hp.x = 680; p2hp.y = 540; p2hp.height = 10; p2hp.width = 100; p2hp.loadTexture("./art/hp2.png", device); p2hp.columns = 5; p2hp.curFrame = 0; p2hp.totalFrames = 5; p2text.x = 680; p2text.y = 520; p2text.loadTexture("./art/p2.png", device); //Player2 bullet player2.bullet.x = player2.x; player2.bullet.y = player2.y; player2.bullet.setWidth(player2.width); player2.bullet.setHeight(player2.height); player2.bullet.length = -10; player2.bullet.alive = false; player2.bullet.loadTexture("./art/missle.png", device); } //---------------------------------------------------------------------------- // Method: InitializeAudio // Description: Initializes Audio Device //---------------------------------------------------------------------------- public void InitializeAudio() { Microsoft.DirectX.DirectSound.Device audioDevice = new Microsoft.DirectX.DirectSound.Device(); } //---------------------------------------------------------------------------- // Method: Initialize Graphics // Description: Initializes Graphic Device and sets Presentation Parameters //---------------------------------------------------------------------------- public void InitializeGraphics() { //set the presentation perametors PresentParameters pp = new PresentParameters(); pp.Windowed = true; pp.SwapEffect = SwapEffect.Copy; pp.AutoDepthStencilFormat = DepthFormat.D16; //pp.BackBufferHeight = 600; //pp.BackBufferWidth = 800; //create the device device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pp); spriteHandler = new Sprite(device); spriteHandler.Transform = Matrix.Identity; initSystem(); initPlayer1(); initPlayer2(); } //---------------------------------------------------------------------------- // Method: InitializeInput // Description: Aquires the input device //---------------------------------------------------------------------------- public void InitializeInput() { inputDevice = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard); CooperativeLevelFlags coopFlags; //coopFlags = CooperativeLevelFlags.Foreground; //coopFlags |= CooperativeLevelFlags.NonExclusive; //inputDevice.SetCooperativeLevel(this, coopFlags); try { inputDevice.Acquire(); } catch (Exception) { MessageBox.Show("Error Acquiring Keyboard"); } } //---------------------------------------------------------------------------- // Method: InitializeSound // Description: Sets up the sounds uses in the game //---------------------------------------------------------------------------- public void InitializeSound() { soundDevice = new Microsoft.DirectX.DirectSound.Device(); soundDevice.SetCooperativeLevel(this, CooperativeLevel.Priority); bgMusic = new SecondaryBuffer("./sound/e1m1.wav", soundDevice); heliSound = new SecondaryBuffer("./sound/heli2.wav", soundDevice); fire = new SecondaryBuffer("./sound/fire.wav", soundDevice); hit = new SecondaryBuffer("./sound/hit.wav", soundDevice); explosion = new SecondaryBuffer("./sound/explosion.wav", soundDevice); } #endregion //---------------------------------------------------------------------------- // Method: checkKeyboard // Description: Checks the state of the keyboards keys and updates logic // accordingly. //---------------------------------------------------------------------------- private void checkKeyboard() { KeyboardState state = inputDevice.GetCurrentKeyboardState(); //Player 2 Controls player2.velX = 0; player2.velY = 0; if (state[Key.Down]) player2.length += 0.05f; if (state[Key.Up]) player2.length -= 0.05f; if (state[Key.Right]) player2.setRotationAngle(player2.rotationAngle + 0.05f); if (state[Key.Left]) player2.setRotationAngle(player2.rotationAngle - 0.05f); //firing if (state[Key.RightControl] && player2.alive) { player2.bullet.alive = true; fire.Play(0, BufferPlayFlags.Default); } //Player 1 Controls player1.velX = 0; player1.velY = 0; if (state[Key.W]) player1.length -= 0.05f; if (state[Key.S]) player1.length += 0.05f; if (state[Key.A]) player1.setRotationAngle(player1.rotationAngle - 0.05f); if (state[Key.D]) player1.setRotationAngle(player1.rotationAngle + 0.05f); if (state[Key.Tab] && player1.alive) { player1.bullet.alive = true; fire.Play(0, BufferPlayFlags.Default); } // Help helpImage.x = 1000; helpImage.y = 1000; if (state[Key.H]) { helpImage.x = 100; helpImage.y = 100; } if (state[Key.R]) { Application.Restart(); } //Quit Game if (state[Key.Escape]) { if (Application.AllowQuit) Application.Exit(); } } //---------------------------------------------------------------------------- // Method: checkCollision // Description: Checks if objects collide and updates logic accordingly //---------------------------------------------------------------------------- public void checkCollision() { if (player1.alive && player2.alive) { //FIXME: Players continue to collide until one is defeated. (sean) /*if (boxCollision(player2, player1)) { p1Health -= 20; p1hp.curFrame++; player1.x += 50; player1.y += 50; p2Health -= 20; p2hp.curFrame++; player2.x -= 50; player2.y -= 50; hit.Play(0, BufferPlayFlags.Default); }*/ if (player2.bullet.alive) { //player1 collide with player2's bullet if (boxCollision(player1, player2.bullet.x + 30, player2.bullet.y + 30, player2.bullet.getX2() - 30, player2.bullet.getY2()-30)) { p1Health -= 20; p1hp.curFrame++; player2.bullet.alive = false; hit.Play(0, BufferPlayFlags.Default); } } if (player1.bullet.alive) { //player2 collids with player1's bullet if (boxCollision(player2, player1.bullet.x + 30, player1.bullet.y + 30, player1.bullet.getX2() - 30, player1.bullet.getY2() - 30)) { p2Health -= 20; p2hp.curFrame++; player1.bullet.alive = false; hit.Play(0, BufferPlayFlags.Default); } } } } //---------------------------------------------------------------------------- // Method: checkPlayerLife // Description: checks players life and updates logic accordingly //---------------------------------------------------------------------------- public void checkPlayerLife() { if (p1Health == 0) { p1Health--; player1.totalFrames = 8; player1.curFrame = 4; player1.firstFrame = 4; player1.animDelay = 30; player1.alive = false; p2Win.x = 100; p2Win.y = 100; explosion.Play(0, BufferPlayFlags.Default); } if (p2Health == 0) { p2Health--; player2.totalFrames = 8; player2.curFrame = 4; player2.firstFrame = 4; player2.animDelay = 30; player2.alive = false; p1Win.x = 100; p1Win.y = 100; explosion.Play(0, BufferPlayFlags.Default); } } //---------------------------------------------------------------------------- // Method: boxCollision (overloaded) // Description: Box Collision (overloaded) //---------------------------------------------------------------------------- public bool boxCollision(Moloy.entity defender, Moloy.entity attacker) { //checks to see if the "attacker" is NOT within defender if (defender.getY2() < attacker.y) return false; if (defender.y > attacker.getY2()) return false; if (defender.getX2() < attacker.x) return false; if (defender.x > attacker.getX2()) return false; //else returns true return true; } //---------------------------------------------------------------------------- // Method: boxCollision (overloaded) // Description: Box Collision (overloaded) //---------------------------------------------------------------------------- public bool boxCollision(Moloy.entity defender, float x, float y, float x2, float y2) { //checks to see if the "attacker" is NOT within defender if (defender.getY2() < y) return false; if (defender.y > y2) return false; if (defender.getX2() < x) return false; if (defender.x > x2) return false; //else returns true return true; } //---------------------------------------------------------------------------- // Method: OnPaintBackground // Description: Main game loop //---------------------------------------------------------------------------- protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e) { //background sounds bgMusic.Play(0, BufferPlayFlags.Looping); heliSound.Play(0, BufferPlayFlags.Looping); checkKeyboard(); //player1 Updating player1.update(); //bounderys if (player1.x > 800 - player1.width) player1.x = 800 - player1.width; if (player1.x < 0) player1.x = 0; if (player1.y > 600 - player1.height - 100) player1.y = 600 - player1.height - 100; if (player1.y < 0) player1.y = 0; // Length boundry to limit speed if (player1.length < -5.0f) player1.length = -5.0f; if (player1.length > 5.0f) player1.length = 5.0f; //player2 Updating player2.update(); if (player2.x > 800 - player2.width) player2.x = 800 - player2.width; if (player2.x < 0) player2.x = 0; if (player2.y > 600 - player2.height - 100) player2.y = 600 - player2.height - 100; if (player2.y < 0) player2.y = 0; // Length boundry to limit speed if (player2.length < -5.0f) player2.length = -5.0f; if (player2.length > 5.0f) player2.length = 5.0f; checkCollision(); //player1 bullets updating if (player1.bullet.alive == false) { player1.bullet.x = (player1.x + player1.width / 2) - 50; player1.bullet.y = (player1.y + player1.height / 2) - 25; player1.bullet.rotationAngle = player1.rotationAngle - 0.40f; } else { player1.bullet.update(); } if (player1.bullet.x < -10 || player1.bullet.x > 810) player1.bullet.alive = false; if (player1.bullet.y < -10 || player1.bullet.y > 610) player1.bullet.alive = false; //player2 bullet updating if (player2.bullet.alive == false) { player2.bullet.x = (player2.x + player2.width / 2) - 50; player2.bullet.y = (player2.y + player2.height / 2) - 25; player2.bullet.rotationAngle = player2.rotationAngle - 0.40f; } else { player2.bullet.update(); } if (player2.bullet.x < -10 || player2.bullet.x > 810) player2.bullet.alive = false; if (player2.bullet.y < -10 || player2.bullet.y > 610) player2.bullet.alive = false; checkPlayerLife(); //animate if (p1Health <= 0) player1.animateOnce(); else player1.animate(); if (p2Health <= 0) player2.animateOnce(); else player2.animate(); device.Clear(ClearFlags.Target, Color.DarkSeaGreen, 1.0f, 0); device.BeginScene(); spriteHandler.Begin(SpriteFlags.AlphaBlend); // Draw bg.draw(spriteHandler); if (player1.bullet.alive) player1.bullet.drawFrame(spriteHandler); if (player2.bullet.alive) player2.bullet.drawFrame(spriteHandler); player1.drawFrame(spriteHandler); player2.drawFrame(spriteHandler); p1hp.drawFrame(spriteHandler); p2hp.drawFrame(spriteHandler); p1text.draw(spriteHandler); p2text.draw(spriteHandler); helpImage.draw(spriteHandler); p1Win.draw(spriteHandler); p2Win.draw(spriteHandler); spriteHandler.End(); device.EndScene(); device.Present(); this.Invalidate(); } } }