Skip to content

Instantly share code, notes, and snippets.

@httpsterio
Created February 27, 2020 20:31
Show Gist options
  • Save httpsterio/3a2c924c2af86fa3d364b54b19648b83 to your computer and use it in GitHub Desktop.
Save httpsterio/3a2c924c2af86fa3d364b54b19648b83 to your computer and use it in GitHub Desktop.
LahinPallo
using System;
using System.Collections.Generic;
using Jypeli;
using Jypeli.Assets;
using Jypeli.Controls;
using Jypeli.Widgets;
public class T4_5_LahinPallo : PhysicsGame
{
public override void Begin()
{
// Kirjoita ohjelmakoodisi tähän
SetWindowSize(800, 600);
Level.Size = new Vector(800,600);
PhysicsObject[] pallot = TeeSatunnaisetPallot(20, 30.0);
PhysicsObject sininenPallo = new PhysicsObject(30.0, 30.0, Shape.Circle);
sininenPallo.Color = Color.Blue;
sininenPallo.X = 100;
sininenPallo.Y = 100;
Add(sininenPallo);
PhysicsObject lahinPallo = LahinPallo(pallot, sininenPallo.Position);
PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
}
private PhysicsObject[] TeeSatunnaisetPallot(int montako, double koko)
{
PhysicsObject[] pallot = new PhysicsObject[montako];
for (int i = 0; i < montako; i++)
{
PhysicsObject p = new PhysicsObject(koko, koko, Shape.Circle);
pallot[i] = p;
p.Position = RandomGen.NextVector(Level.BoundingRect);
Add(p);
}
return pallot;
}
public PhysicsObject LahinPallo(PhysicsObject[] pallot, Vector piste)
{
double differenceX = 800;
double differenceY = 800;
double totalDifference = 1600;
int closest = 0;
for (int i = 0; i < pallot.Length; i++)
{
differenceX = Math.Abs(pallot[i].Position[x]) - Math.Abs(piste[x]);
differenceY = Math.Abs(pallot[i].Position[y]) - Math.Abs(piste[y]);
if (differenceY + differenceX < totalDifference)
{
closest = i;
}
}
return pallot[closest];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment