//Tutorial de SDL .NET
//Capitulo 5: Sencillo Clon de Pong
//Por Dark-N
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing; //colores
using SdlDotNet;
using SdlDotNet.Graphics; //para sprites
using SdlDotNet.Core; //Eventos
using SdlDotNet.Graphics.Sprites; //textos
using SdlDotNet.Input; //keyborad
namespace Pong
{
class Pong: IDisposable
{
Sprite bola;
Sprite paleta1;
Sprite paleta2;
int velocidadBolaX = 1; //valocidad inicial
int velocidadBolaY = 1;
int puntajeJ1 = 0;
int puntajeJ2 = 0;
public Pong()
{
Video.WindowIcon();
Video.WindowCaption = "SDL.NET - Pong";
Video.SetVideoMode(400, 300);
bola = new Sprite(new Surface("../../imagenes/Ball.PNG"));
bola.TransparentColor = Color.Magenta;
bola.Transparent = true;
paleta1 = new Sprite(new Surface("../../imagenes/Paddle1.PNG"));
paleta1.Transparent = true;
paleta1.Center = new Point(10, Video.Screen.Height / 2);
paleta2 = new Sprite(new Surface("../../imagenes/Paddle2.PNG"));
paleta2.Transparent = true;
paleta2.Center = new Point(Video.Screen.Width-10, Video.Screen.Height / 2);
}
private void Quit(object sender, QuitEventArgs e)
{
Events.QuitApplication();
}
private void Teclado(object sender, KeyboardEventArgs e)
{
switch (e.Key)
{
case Key.Escape:
Events.QuitApplication();
break;
}
}
//ciclo de juego (game-loop)
private void Tick(object sender, TickEventArgs args)
{
//marcador
SdlDotNet.Graphics.Font fuente = new SdlDotNet.Graphics.Font("../../fuentes/comic.TTF", 20);
TextSprite texto = new TextSprite(puntajeJ1.ToString()+" - "+puntajeJ2.ToString(), fuente, Color.Yellow);
texto.Center = new Point(Video.Screen.Width / 2, 10);
//movimiento bola
bola.X += velocidadBolaX;
bola.Y += velocidadBolaY;
//movimiento paleta izq
if (Keyboard.IsKeyPressed(Key.UpArrow) && paleta1.Top >= 5)
paleta1.Y -= 5;
if (Keyboard.IsKeyPressed(Key.DownArrow) && paleta1.Bottom <= Video.Screen.Height - 5)
paleta1.Y += 5;
//movimiento paleta der
if (Keyboard.IsKeyPressed(Key.Keypad8) && paleta2.Top >= 5)
paleta2.Y -= 5;
if (Keyboard.IsKeyPressed(Key.Keypad2) && paleta2.Bottom <= Video.Screen.Height-5)
paleta2.Y += 5;
//rebote bola con paleta
if (paleta1.IntersectsWith(bola))
{
velocidadBolaX *= -1;
velocidadBolaX += 1;
}
if (paleta2.IntersectsWith(bola))
{
velocidadBolaX *= -1;
velocidadBolaX -= 1;
}
//hacer un punto Jugador 1 a Jugador 2
if (bola.X > Video.Screen.Width)
{
puntajeJ1++;
bola.X = 0;
bola.Y = 0;
velocidadBolaX = 1;
velocidadBolaY = 1;
}
if (bola.X < 0)
{
puntajeJ2++;
bola.X = 0;
bola.Y = 0;
velocidadBolaX = 1;
velocidadBolaY = 1;
}
//si bola choca arriba o abajo rebota
if (bola.Top < 0)
velocidadBolaY *= -1;
if (bola.Bottom > Video.Screen.Height)
velocidadBolaY *= -1;
// fondo negro
Video.Screen.Fill(Color.Black);
//texto en pantalla
Video.Screen.Blit(texto);
// pintamos la bola
Video.Screen.Blit(bola);
// pintamos las paletas
Video.Screen.Blit(paleta1);
Video.Screen.Blit(paleta2);
// se actualiza la pantalla
Video.Screen.Update();
}
public void Run()
{
Events.KeyboardDown += new EventHandler < KeyboardEventArgs >(this.Teclado);
Events.Tick += new EventHandler < TickEventArgs >(this.Tick);
Events.Quit += new EventHandler < QuitEventArgs >(this.Quit);
Events.Run();
}
static void Main(string[] args)
{
Pong game = new Pong();
game.Run();
}
#region IDisposable Members
private bool disposed;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (this.bola != null)
{
this.bola.Dispose();
this.bola = null;
}
}
this.disposed = true;
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
public void Close()
{
Dispose();
}
~Pong()
{
Dispose(false);
}
#endregion
}
} |