// Game1.cs using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Components; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; namespace Pong { /// /// This is the main type for your game /// partial class Game1 : Microsoft.Xna.Framework.Game { Texture2D t_paddle1, t_paddle2, t_ball; SpriteBatch spriteBatch; Paddle paddle1; Paddle paddle2; Ball ball; Random rand = new Random(); KeyboardState currentstate; public Game1() { InitializeComponent(); ResetGame(); } void ResetGame() { paddle1 = new Paddle(10, 200); paddle2 = new Paddle(770, 200); ball = new Ball(385, 285); } protected override void OnStarting() { base.OnStarting(); graphics.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset); LoadResources(); } void GraphicsDevice_DeviceReset(object sender, EventArgs e) { LoadResources(); } void LoadResources() { t_paddle1 = Texture.FromFile(graphics.GraphicsDevice, "paddle.png"); t_paddle2 = Texture.FromFile(graphics.GraphicsDevice, "paddle.png"); t_ball = Texture.FromFile(graphics.GraphicsDevice, "ball.png"); spriteBatch = new SpriteBatch(graphics.GraphicsDevice); } protected override void Update() { // The time since Update was called last float elapsed = (float)ElapsedTime.TotalSeconds; // TODO: Add your game logic here // Let the GameComponents update UpdateComponents(); } protected override void Draw() { // Make sure we have a valid device if (!graphics.EnsureDevice()) return; graphics.GraphicsDevice.Clear(Color.CornflowerBlue); graphics.GraphicsDevice.BeginScene(); // TODO: Add your drawing code here spriteBatch.Begin(SpriteBlendMode.AlphaBlend); spriteBatch.Draw(t_paddle1, new Rectangle(t_paddle1.pos.X, t_paddle1.pos.Y, t_paddle1.Width, t_paddle1.Height), Color.White); spriteBatch.Draw(t_paddle2, new Rectangle(t_paddle2.pos.X, t_paddle2.pos.Y, t_paddle2.Width, t_paddle2.Height), Color.White); spriteBatch.Draw(t_ball, new Rectangle(t_ball.pos.X, t_ball.pos.Y, t_ball.Width, t_ball.Height),Color.White); spriteBatch.End(); // Let the GameComponents draw DrawComponents(); graphics.GraphicsDevice.EndScene(); graphics.GraphicsDevice.Present(); } } } //Ball.cs using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; namespace Pong { class Ball { public Point pos; public int h_speed, v_speed; public Ball(int x, int y) { pos = new Point(x, y); Random rand = new Random(); h_speed = rand.Next(3 ,7); if (rand.Next(0, 2) == 0) h_speed *= -1; rand = new Random(); v_speed = rand.Next(3, 7); if (rand.Next(0,2) == 0) v_speed *=-1; } } } //Paddle.cs using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; namespace Pong { class Paddle { public Point pos; public int speed; public Paddle(int x, int y) { pos = new Point(x, y); speed = 3; } } } //Errors: /* Error 1 Cannot implicitly convert type 'Microsoft.Xna.Framework.Graphics.Texture' to 'Microsoft.Xna.Framework.Graphics.Texture2D'. An explicit conversion exists (are you missing a cast?) 56 21 Error 2 Cannot implicitly convert type 'Microsoft.Xna.Framework.Graphics.Texture' to 'Microsoft.Xna.Framework.Graphics.Texture2D'. An explicit conversion exists (are you missing a cast?) 57 21 Error 3 Cannot implicitly convert type 'Microsoft.Xna.Framework.Graphics.Texture' to 'Microsoft.Xna.Framework.Graphics.Texture2D'. An explicit conversion exists (are you missing a cast?) 58 18 Error 4 'Microsoft.Xna.Framework.Graphics.Texture2D' does not contain a definition for 'pos' 83 65 Error 5 'Microsoft.Xna.Framework.Graphics.Texture2D' does not contain a definition for 'pos' 83 82 Error 6 'Microsoft.Xna.Framework.Graphics.Texture2D' does not contain a definition for 'pos' 84 65 Error 7 'Microsoft.Xna.Framework.Graphics.Texture2D' does not contain a definition for 'pos' 84 82 Error 8 'Microsoft.Xna.Framework.Graphics.Texture2D' does not contain a definition for 'pos' 85 59 Error 9 'Microsoft.Xna.Framework.Graphics.Texture2D' does not contain a definition for 'pos' 85 73 */