Created
July 3, 2016 04:47
-
-
Save pigeonhands/b7cb8da8afdaaf2013b0da18ec9c106e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Windows.Forms; | |
public delegate void OnStringGet(string s); | |
public class RandomStringInvader : Control | |
{ | |
private bool _PauseOnMouseExit = false; | |
public bool PauseOnMouseExit | |
{ | |
get | |
{ | |
return _PauseOnMouseExit; | |
} | |
set | |
{ | |
_PauseOnMouseExit = value; | |
if(_PauseOnMouseExit) | |
{ | |
if (boardX == int.MinValue) | |
Stop(); | |
} | |
else | |
{ | |
Start(); | |
} | |
} | |
} | |
Ship[] shooters; | |
Timer drawTimer; | |
List<Collectable> FallingCollectables = new List<Collectable>(); | |
Random r = new Random(); | |
int boardX = 0; | |
int boardY = 0; | |
int boardWidth = 0; | |
int boardHeight = 6; | |
bool Stopped = false; | |
public event OnStringGet OnStringGet; | |
public string CharMap { get; set; } | |
struct Ship | |
{ | |
public bool reverse; | |
public int X; | |
public Size Size; | |
public void Draw(Graphics g) | |
{ | |
g.FillRectangle(Brushes.White, new Rectangle(new Point(X, 0), Size)); | |
g.FillPolygon(Brushes.White, new Point[] { new Point(X, Size.Height / 2), new Point(X + Size.Width, Size.Height / 2), new Point(X + (Size.Width / 2), Size.Height + 3) }); | |
} | |
} | |
struct Collectable | |
{ | |
public string Letter; | |
public int X; | |
public int Y; | |
} | |
public RandomStringInvader() | |
{ | |
CharMap = "1234567890-=qwertyuiopasdfghjklzxcvbnm,./;'[]"; | |
BackColor = Color.Black; | |
MinimumSize = new Size(100, 100); | |
drawTimer = new Timer(); | |
drawTimer.Interval = 50; | |
drawTimer.Tick += DrawTimer_Tick; | |
drawTimer.Start(); | |
Setup(); | |
} | |
private void DrawTimer_Tick(object sender, EventArgs e) | |
{ | |
for (int i = 0; i < shooters.Length; i++) | |
{ | |
Ship shooter = shooters[i]; | |
int newX = shooter.X; | |
if (shooter.reverse) | |
{ | |
newX -= this.Width / 10; | |
if ((newX - (shooter.Size.Width / 2)) < 0) | |
{ | |
newX = newX - (shooter.Size.Width / 2); | |
shooter.reverse = false; | |
} | |
} | |
else | |
{ | |
newX += this.Width / 10; | |
if (newX > this.Width - shooter.Size.Width) | |
{ | |
newX = this.Width - shooter.Size.Width; | |
shooter.reverse = true; | |
} | |
} | |
shooter.X = newX; | |
shooters[i] = shooter; | |
var newc = new Collectable(); | |
newc.Letter = CharMap[r.Next(CharMap.Length)].ToString(); | |
newc.X = shooter.X + (shooter.Size.Width / 2); | |
newc.Y = shooter.Size.Height + 3; | |
FallingCollectables.Add(newc); | |
} | |
for (int i = 0; i < FallingCollectables.Count; i++) | |
{ | |
Collectable c = FallingCollectables[i]; | |
c.Y += 10; | |
if (c.Y > this.Height + 10) | |
FallingCollectables.RemoveAt(i); | |
else | |
FallingCollectables[i] = c; | |
} | |
Invalidate(); | |
} | |
private void Setup() | |
{ | |
shooters = new Ship[] | |
{ | |
new Ship() | |
{ | |
Size = new Size(this.Width / 10, this.Height / 10), | |
reverse = false | |
}, | |
new Ship() | |
{ | |
Size = new Size(this.Width / 10, this.Height / 10), | |
reverse = true, | |
X = this.Width | |
}, | |
new Ship() | |
{ | |
Size = new Size(this.Width / 10, this.Height / 10), | |
reverse = false, | |
X = this.Width / 2 | |
} | |
}; | |
boardX = int.MinValue; | |
boardY = this.Height - (this.Height / 5); | |
boardWidth = this.Width / 10; | |
} | |
protected override void OnPaint(PaintEventArgs e) | |
{ | |
var g = e.Graphics; | |
if(boardX != int.MinValue) | |
g.FillRectangle(Brushes.Green, new Rectangle(boardX, boardY, boardWidth, boardHeight)); | |
foreach (Ship s in shooters) | |
s.Draw(g); | |
for (int i = 0; i < FallingCollectables.Count; i++) | |
{ | |
Collectable c = FallingCollectables[i]; | |
if ((c.X > boardX && c.X < boardX + boardWidth) && (c.Y > boardY)) | |
{ | |
FallingCollectables.RemoveAt(i); | |
var callback = OnStringGet; | |
if (callback != null) | |
callback(c.Letter); | |
} | |
else | |
{ | |
g.DrawString(c.Letter, this.Font, Brushes.Red, new Point(c.X, c.Y)); | |
} | |
} | |
base.OnPaint(e); | |
} | |
protected override void OnSizeChanged(EventArgs e) | |
{ | |
base.OnSizeChanged(e); | |
Setup(); | |
} | |
protected override void OnMouseMove(MouseEventArgs e) | |
{ | |
base.OnMouseMove(e); | |
if(!Stopped) | |
boardX = e.X; | |
} | |
protected override void OnMouseLeave(EventArgs e) | |
{ | |
boardX = int.MinValue; | |
if (PauseOnMouseExit) | |
Stop(); | |
base.OnMouseLeave(e); | |
} | |
protected override void OnMouseEnter(EventArgs e) | |
{ | |
if (PauseOnMouseExit) | |
Start(); | |
base.OnMouseEnter(e); | |
} | |
public void Stop() | |
{ | |
Stopped = true; | |
boardX = int.MinValue; | |
drawTimer.Stop(); | |
} | |
public void Start() | |
{ | |
Stopped = false; | |
drawTimer.Start(); | |
} | |
public void ClearCharacters() | |
{ | |
FallingCollectables.Clear(); | |
Invalidate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment