Created
March 2, 2020 09:49
-
-
Save httpsterio/f91529a5e79566456deb12d526565ab4 to your computer and use it in GitHub Desktop.
This file contains 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 Jypeli; | |
using Jypeli.Assets; | |
using Jypeli.Controls; | |
using Jypeli.Widgets; | |
public class T6_hiiripallo : 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); | |
Mouse.IsCursorVisible = true; | |
hiiriPallo(); | |
//Mouse.Listen( MouseButton.Left, ButtonState.Pressed, SiirraPallo, "Luo uusi pallo"); | |
//PhysicsObject lahinPallo = LahinPallo(pallot, LuoHiiriPallo().Position); | |
//lahinPallo.Color = Color.Red; | |
PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); | |
Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); | |
} | |
public PhysicsObject hiiriPallo() | |
{ | |
bool exists = false; | |
Vector paikkaruudulla = Mouse.PositionOnScreen; | |
PhysicsObject pallo = new PhysicsObject(30.0, 30.0); | |
if (exists == false) | |
{ | |
pallo.Color = Color.Aqua; | |
pallo.Shape = Shape.Circle; | |
pallo.X = paikkaruudulla.X; | |
pallo.Y = paikkaruudulla.Y; | |
Add(pallo); | |
exists = true; | |
} | |
else | |
{ | |
pallo.X = paikkaruudulla.X; | |
pallo.Y = paikkaruudulla.Y; | |
} | |
return pallo; | |
} | |
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 totalDifference = Vector.Distance(pallot[0].Position, piste); | |
int closest = 0; | |
for (int i = 0; i < pallot.Length; i++) | |
{ | |
if (Vector.Distance(pallot[i].Position, piste) < totalDifference) | |
{ | |
closest = i; | |
totalDifference = Vector.Distance(pallot[i].Position, piste); | |
} | |
} | |
return pallot[closest]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment