Skip to content

Instantly share code, notes, and snippets.

@Hakkadaikon
Last active October 11, 2023 18:52
Show Gist options
  • Save Hakkadaikon/47504ddb5bc1796f3f00c115f54a5c42 to your computer and use it in GitHub Desktop.
Save Hakkadaikon/47504ddb5bc1796f3f00c115f54a5c42 to your computer and use it in GitHub Desktop.
IkuradonGame
using System.Diagnostics;
namespace IkuradonGame
{
public partial class MainForm : Form
{
private const int MAX_BALL = 30;
private Rectangle riceRect = new Rectangle();
private Rectangle[] ballRects = new Rectangle[MAX_BALL];
private Point[] diffs = new Point[MAX_BALL];
private int[] xDirections = new int[MAX_BALL];
private int[] yDirections = new int[MAX_BALL];
private int[] ballSpeeds = new int[MAX_BALL];
private bool[] hits = new bool[MAX_BALL];
private Image rice = Image.FromFile("./img/rice.png");
private Image ball = Image.FromFile("./img/ball.png");
private Random random = new Random();
private int riceRadius = 10;
private int riceMaxRadius = 240;
private int ballDiameter = 30;
private int riceSpeed = 30;
private Stopwatch stopwatch = new Stopwatch();
public MainForm()
{
InitializeComponent();
this.riceRect.X = (this.Width / 2) - (this.rice.Width / 2);
this.riceRect.Y = (this.Width / 2) - (this.rice.Height / 2);
this.riceRect.Width = this.riceMaxRadius;
this.riceRect.Height = this.riceMaxRadius;
for (int ii = 0; ii < this.ballRects.Count(); ii++)
{
this.ballRects[ii] = new Rectangle(
(this.Width / 2) - (this.ballDiameter / 2),
(this.Height / 2) - (this.ballDiameter / 2),
this.ballDiameter,
this.ballDiameter
);
this.ballSpeeds[ii] = DecisionBallSpeed();
this.xDirections[ii] = DecisionDirection();
this.yDirections[ii] = DecisionDirection();
this.hits[ii] = false;
this.diffs[ii] = new Point(0, 0);
}
}
private void MainForm_Shown(object sender, EventArgs e)
{
this.LblStopWatch.Text = this.stopwatch.Elapsed.ToString();
this.LblIkura.Text = "Ikura:0/" + MAX_BALL.ToString();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(this.rice, this.riceRect);
for (int ii = 0; ii < this.ballRects.Count(); ii++)
{
var rect = this.ballRects[ii];
g.DrawImage(this.ball, rect);
}
g.Dispose();
}
private void DrawTimer_Tick(object sender, EventArgs e)
{
if (GetHitCount() != MAX_BALL)
{
this.LblStopWatch.Text = this.stopwatch.Elapsed.ToString();
}
for (int ii = 0; ii < this.ballRects.Count(); ii++)
{
ref var ballRect = ref this.ballRects[ii];
ref var diff = ref this.diffs[ii];
ref var ballSpeed = ref this.ballSpeeds[ii];
ref var xDirection = ref this.xDirections[ii];
ref var yDirection = ref this.yDirections[ii];
ref var hit = ref this.hits[ii];
if (hits[ii])
{
ballRect.X = this.riceRect.X + diff.X;
ballRect.Y = this.riceRect.Y + diff.Y;
continue;
}
ballRect.X += xDirection * ballSpeed;
if (
ballRect.X < ballRect.Width ||
ballRect.X > this.Width - ballRect.Width
)
{
xDirection = -xDirection;
ballSpeed = DecisionBallSpeed();
}
ballRect.Y += yDirection * ballSpeed;
if (
ballRect.Y < ballRect.Height ||
ballRect.Y > this.Height - ballRect.Height
)
{
yDirection = -yDirection;
ballSpeed = DecisionBallSpeed();
}
var ballX = ballRect.X + (ballRect.Width / 2);
var ballY = ballRect.Y + (ballRect.Height / 2);
var ballRadius = (ballRect.Width / 2);
// rice center point
var riceX = this.riceRect.X + (this.riceRect.Width / 2);
var riceY = this.riceRect.Y + (this.riceRect.Height / 2) - 30;
var a = ballX - riceX;
var b = ballY - riceY;
var c = Math.Sqrt(a * a + b * b);
if (c <= ballRadius + this.riceRadius / 2)
{
this.riceRadius += 1;
hit = true;
diff.X = ballRect.X - this.riceRect.X;
diff.Y = ballRect.Y - this.riceRect.Y;
}
}
this.LblIkura.Text =
"Ikura:" + GetHitCount().ToString() +
"/" +
MAX_BALL.ToString();
this.Refresh();
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.H:
riceRect.X -= this.riceSpeed;
break;
case Keys.J:
riceRect.Y += this.riceSpeed;
break;
case Keys.K:
riceRect.Y -= this.riceSpeed;
break;
case Keys.L:
riceRect.X += this.riceSpeed;
break;
}
}
private int DecisionBallSpeed()
{
return this.random.Next(2, 40);
}
private int DecisionDirection()
{
return (this.random.NextDouble() >= 0.5) ? (1) : (-1);
}
private void BtnStart_Click(object sender, EventArgs e)
{
this.DrawTimer.Enabled = true;
this.BtnStart.Visible = false;
this.Focus();
this.stopwatch.Start();
}
private int GetHitCount()
{
int count = 0;
foreach (var hit in this.hits)
{
if (hit) count++;
}
return count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment