This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.
okay so i finaly did somthing worth while :) im gonna upload both the game and then the source code
The Game is the most basic you can get, no scoring no menu, just the bare basics
the ball bounces around and can bounce of the paddles
Controls,
Left Hand Paddle
Up -> W
Down -> S
Right Hand Paddle
Up -> Up Arrow
Down -> Down Arrow
Have Fun and see attachment for the game allready compiled
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
namespace Pong
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D Ball;
Vector2 positionball = new Vector2(400, 200);
Vector2 ballspeed = new Vector2(150, 150);
Texture2D Paddle;
Vector2 positionpaddle1 = new Vector2(50, 200);
Texture2D Paddle2;
Vector2 positionpaddle2 = new Vector2(750, 200);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 400;
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()
{
// TODO: Add your initialization logic here
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()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
Ball = Content.Load<Texture2D>("Ball");
Paddle = Content.Load<Texture2D>("Paddle");
Paddle2 = Content.Load<Texture2D>("Paddle2");
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
KeyboardState key = Keyboard.GetState();
//Paddle 1 Movement
if (key.IsKeyDown(Keys.W))
{
positionpaddle1.Y -= 3;
}
if (key.IsKeyDown(Keys.S))
{
positionpaddle1.Y += 3;
}
//Paddle 2 Movement
if (key.IsKeyDown(Keys.Up))
{
positionpaddle2.Y -= 3;
}
if (key.IsKeyDown(Keys.Down))
{
positionpaddle2.Y += 3;
}
Rectangle ballRect =
new Rectangle((int)positionball.X, (int)positionball.Y,
Ball.Width, Ball.Height);
Rectangle paddle1Rect =
new Rectangle((int)positionpaddle1.X, (int)positionpaddle1.Y,
Paddle.Width, Paddle.Height);
Rectangle paddle2Rect =
new Rectangle((int)positionpaddle2.X, (int)positionpaddle2.Y,
Paddle2.Width, Paddle2.Height);
if (ballRect.Intersects(paddle1Rect))
{
//increase ball speed
ballspeed.X -= 50;
if (ballspeed.Y < 0)
ballspeed.Y -= 50;
else
ballspeed.Y += 50;
// ball back
ballspeed.X *= -1;
}
if (ballRect.Intersects(paddle2Rect))
{
//increase ball speed
ballspeed.X += 50;
if (ballspeed.Y < 0)
ballspeed.Y -= 50;
else
ballspeed.Y += 50;
// ball back
ballspeed.X *= -1;
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)
{
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(Ball, positionball, Color.White);
spriteBatch.Draw(Paddle, positionpaddle1, Color.White);
spriteBatch.Draw(Paddle2, positionpaddle2, Color.White);
spriteBatch.End();