Created
February 25, 2012 13:01
-
-
Save LinuxDoku/1908404 to your computer and use it in GitHub Desktop.
Form1.cs
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.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Threading; | |
namespace Pong | |
{ | |
public partial class FormMain : Form | |
{ | |
Thread gameLogic; | |
public FormMain() | |
{ | |
InitializeComponent(); | |
this.KeyPreview = true; | |
} | |
private void FormMain_Shown(object sender, EventArgs e) | |
{ | |
this.reset(); | |
} | |
private void FormMain_Leave(object sender, EventArgs e) | |
{ | |
gameLogic.Abort(); | |
} | |
private void FormMain_KeyDown(object sender, KeyEventArgs e) | |
{ | |
if (e.KeyCode == Keys.M) | |
{ | |
if (panelPlayer.Top > 0) | |
panelPlayer.Top -= 10; | |
} | |
if (e.KeyCode == Keys.N) | |
{ | |
if (panelPlayer.Top < 345) | |
panelPlayer.Top += 10; | |
} | |
} | |
private void buttonNewGame_Click(object sender, EventArgs e) | |
{ | |
buttonNewGame.Enabled = false; | |
// start puk moving around, launch ki and colliders | |
gameLogic = new Thread(new ThreadStart(logic)); | |
gameLogic.Start(); | |
} | |
private void reset() | |
{ | |
// access gui thread | |
this.Invoke((MethodInvoker)delegate | |
{ | |
panelPlayground.BackColor = Color.LightGray; | |
panelPuk.Left = panelPlayground.Width / 2 - panelPuk.Width / 2; | |
panelPuk.Top = panelPlayground.Height / 2 - panelPuk.Height / 2; | |
panelKI.Top = panelPlayground.Height / 2 - panelKI.Height / 2; | |
panelPlayer.Top = panelPlayground.Height / 2 - panelPlayer.Height / 2; | |
buttonNewGame.Enabled = true; | |
}); | |
} | |
private void gameEnded(String loser) | |
{ | |
this.Invoke((MethodInvoker)delegate | |
{ | |
panelPlayground.BackColor = Color.Yellow; | |
}); | |
MessageBox.Show("Spiel beendet! Verlierer: "+loser); | |
this.reset(); | |
} | |
// game logic | |
private void logic() | |
{ | |
String lost = ""; | |
Random Random = new Random(); | |
int[] register = new int[] { -1, 1 }; | |
int minusLeft = register[Random.Next(0, 2)]; | |
Thread.Sleep(20); | |
int minusTop = register[Random.Next(0, 2)]; | |
while (lost == "") | |
{ | |
// access gui thread | |
this.Invoke((MethodInvoker)delegate | |
{ | |
panelPuk.Left -= minusLeft; | |
panelPuk.Top -= minusTop; | |
// ki | |
if(panelPuk.Left < panelPlayground.Width / 2) { | |
if (panelPuk.Top > panelKI.Top) | |
{ | |
panelKI.Top++; | |
} | |
if (panelPuk.Bottom < panelKI.Bottom) | |
{ | |
panelKI.Top--; | |
} | |
} | |
// player collider | |
if (panelPuk.Right >= 625 && panelPuk.Top >= panelPlayer.Top && panelPuk.Bottom <= panelPlayer.Bottom) | |
{ | |
// coliden! | |
// now rebuild coords | |
minusLeft = 1; | |
minusTop *= -1; | |
} | |
// ki collider | |
if (panelPuk.Left <= 15 && panelPuk.Top >= panelKI.Top && panelPuk.Bottom <= panelKI.Bottom) | |
{ | |
// coliden! | |
// now rebuild coords | |
minusLeft = -1; | |
minusTop *= -1; | |
} | |
// generate new directions | |
if (panelPuk.Top <= 0) | |
{ | |
minusTop *= -1; | |
} | |
else if (panelPuk.Bottom >= 400) | |
{ | |
minusTop *= -1; | |
} | |
// determine loser | |
if (panelPuk.Left <= 0) | |
{ | |
lost = "Computer"; | |
} | |
else if (panelPuk.Right >= 640) | |
{ | |
lost = "Du"; | |
} | |
}); | |
Thread.Sleep(10); | |
} | |
this.gameEnded(lost); | |
Thread.CurrentThread.Abort(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment