I changed it to get rid of the acceleration based control, now left moves it left and right moves it right. To apply the change replace game.cs with:
Code:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using XnaTetris.Graphics;
using System.Threading;
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;
Texture2D paddle;
Texture2D albumart;
Texture2D talbumart;
float p1x = 105;
int p1y = 0;
int score = 0;
float p1s = 0;
string error;
float p2x = 105;
int p2y = 275;
float p2s = 0;
float ballx = 120;
float bally = 160;
float ballsx = 1.0f;
float ballsy = 1.0f;
long numframes = 0;
string info;
bool changesong;
Thread albuminfoloader;
bool paused, pchange=false, volume=false;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
MediaPlayer.IsShuffled = true;
MediaLibrary ml = new MediaLibrary();
SongCollection s = ml.Songs;
MediaPlayer.Play(s);
MediaPlayer.Queue.MoveNext();
albuminfoloader = new Thread(new ThreadStart(LoadAlbumInfo));
albuminfoloader.Priority = ThreadPriority.Lowest;
albuminfoloader.Start();
}
/// <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();
p1x = 105;
p1y = 0;
p1s = 0;
p2x = 105;
p2y = 275;
p2s = 0;
ballx = 120;
bally = 160;
ballsx = 1.0f;
ballsy = 1.0f;
paused = false;
numframes = 0;
changesong = false;
error = "";
}
/// <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);
ball = Content.Load<Texture2D>("ball");
paddle = Content.Load<Texture2D>("paddle");
albumart = MediaPlayer.Queue.ActiveSong.Album.GetThumbnail(Content.ServiceProvider);
// TODO: use this.Content to load your game content here
}
/// <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)
{
MediaPlayer.Stop();
albuminfoloader.Abort();
this.Exit();
}
GamePadState gps = GamePad.GetState(PlayerIndex.One);
// TODO: Add your update logic here
if (!paused)
{
if (numframes > 100)
{
ballsy *= 1.0005f;
ballsx *= 1.0005f;
if (ballx + 10 > p1x && ballx < p1x + 30 && bally + ballsy < p1y + 5 && ballsy < 0)
{
ballsy = -ballsy;
ballsx -= .2f * p1s;
}
if (ballx + 10 > p2x && ballx < p2x + 30 && bally + ballsy + 10 > p2y && ballsy > 0)
{
ballsy = -ballsy;
ballsx -= .2f * p2s;
}
ballx += ballsx;
bally += ballsy;
}
p1x += p1s;
p2x += p2s;
if (bally < 0)
{
score++;
Initialize();
}
else if (bally > 270)
{
score--;
Initialize();
}
if (ballx < 0)
{
ballsx = -ballsx;
ballx = -ballx;
}
else if (ballx > 230)
{
ballx = 460 - ballx;
ballsx = -ballsx;
}
if (p1x < 0)
{
p1s = -p1s;
p1x = -p1x;
}
else if (p1x > 230)
{
p1x = 460 - p1x;
p1s = -p1s;
}
if (p2x < 0)
{
p2s = -p2s;
p2x = -p2x;
}
else if (p2x > 230)
{
p2x = 460 - p2x;
p2s = -p2s;
}
if (gps.DPad.Left == ButtonState.Pressed && p2s > -2-(.0005*numframes))
{
p2x -= 2.5f;
}
else if (gps.DPad.Right == ButtonState.Pressed && p2s < 2 + (.0005 * numframes))
{
p2x += 2.5f;
}
if (ballx < p1x)
{
p1x -= 2 + (.0005f * numframes);
}
else if (ballx > p1x + 30)
{
p1x += 2 + (.0005f * numframes);
}
numframes++;
}
if (gps.Buttons.A == ButtonState.Pressed && !pchange)
{
paused = !paused;
pchange = true;
}
if (gps.Buttons.A == ButtonState.Released)
{
pchange = false;
}
if (gps.DPad.Up == ButtonState.Pressed && !volume)
{
MediaPlayer.Volume+=.05f;
volume = true;
}
if (gps.DPad.Down == ButtonState.Pressed && !volume)
{
MediaPlayer.Volume -= .05f;
volume = true;
}
if (gps.DPad.Down == ButtonState.Released && gps.DPad.Up == ButtonState.Released)
{
volume = false;
}
base.Update(gameTime);
}
void LoadAlbumInfo()
{
bool album = false;
while (true)
{
GamePadState gps = GamePad.GetState(PlayerIndex.One);
info = (MediaPlayer.Queue.ActiveSong.Artist.ToString()) + ": " + MediaPlayer.Queue.ActiveSong.ToString();
if (gps.Buttons.B == ButtonState.Pressed && !album)
{
MediaPlayer.Queue.MoveNext();
changesong = true;
album = true;
}
else if (gps.Buttons.B == ButtonState.Released)
{
album = false;
}
if (gps.Buttons.Back == ButtonState.Pressed)
{
return;
}
}
}
/// <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.Black);
// TODO: Add your drawing code here
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
Rectangle r = new Rectangle((int)ballx, (int)bally, 10, 10);
spriteBatch.Draw(ball, r, Color.White);
r = new Rectangle((int)p1x, (int)p1y, 30, 5);
spriteBatch.Draw(paddle, r, Color.White);
r = new Rectangle((int)p2x, (int)p2y, 30, 5);
spriteBatch.Draw(paddle, r, Color.White);
if (changesong)
{
do
{
albumart = MediaPlayer.Queue.ActiveSong.Album.GetThumbnail(Content.ServiceProvider);
} while (albumart == null);
changesong = false;
}
r = new Rectangle(200, 280, 40, 40);
spriteBatch.Draw(albumart, r, Color.White);
spriteBatch.End();
TextureFont t =new TextureFont(GraphicsDevice, Content);
TextureFont.WriteText(30, 450, info);
t.WriteAll();
TextureFont.WriteText(30, 400, error);
t.WriteAll();
TextureFont.WriteText(30, 480, "Score: " + score);
t.WriteAll();
base.Draw(gameTime);
}
}
}
That's the only thing I changed around.
Besides that I'm having the freezing when changing songs thing it is a very cool little pong game. Add in a menu, ability to choose which songs you play (playlist or something) and a difficulty control and it would be epic.
Best game released so far
edit: yeah, the center thing was what I was doing. Why is everyone using the center button as pause? I keep hitting it by accident
Maybe add something that says PAUSED when paused and make the music pause when paused.
And instead of a pause before starting, maybe wait till you push a button
Zunepad support would be cool too