Skip to content
Snippets Groups Projects
Commit d2a908f0 authored by Vesa Lappalainen's avatar Vesa Lappalainen :bicyclist:
Browse files

live17 loppu

parent 6ede2612
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
/// @author Vesa Lappalainen
/// @version 27.10.2012
/// <summary>
/// Esimerkkejä olioista
/// </summary>
public class ElaimetPerimalla
{
/// <summary>
/// Tutkitaan olioiden käyttäytymistä
/// </summary>
public static void Main()
{
Kissa miuku = new Kissa("Miuku", 900);
Kissa mirri = new Kissa("Mirri", 19000);
Koira musti = new Koira("Musti", 2700);
miuku.Aantele();
mirri.Aantele();
musti.Aantele();
Console.WriteLine(miuku); // kutsuu oikeasti miuku.ToString();
Console.WriteLine(miuku); // kutsuu oikeasti miuku.ToString();
Console.WriteLine(musti); // kutsuu oikeasti miuku.ToString();
List<Elain> elaimet = new List<Elain>();
elaimet.Add(mirri);
elaimet.Add(miuku);
elaimet.Add(musti);
foreach (Elain elain in elaimet)
elain.Aantele();
}
}
public class Elain
{
private readonly string nimi; // attribuutti
private readonly double paino;
protected Elain(string nimi, double paino) // Muodostaja
{
this.nimi = nimi;
this.paino = paino;
}
public virtual void Aantele() // metodi
{
Console.WriteLine("???");
}
/// <summary>
/// Elain tiedot merkkijonona
/// </summary>
/// <returns>Kissan tiedot merkkijonona</returns>
public override string ToString()
{
return nimi + " " + paino;
}
}
public class Kissa: Elain // luokka
{
private readonly double hannanPituus = 10;
public Kissa(string nimi, double paino): base(nimi, paino) // Muodostaja
{
}
public override void Aantele() // metodi
{
Console.WriteLine("Miau, minulla on " + hannanPituus + " cm häntä");
}
public override string ToString()
{
return base.ToString() + " " + hannanPituus + " cm häntä";
}
}
public class Koira: Elain // luokka
{
public Koira(string nimi, double paino): base(nimi, paino) // Muodostaja
{
}
public override void Aantele() // metodi
{
Console.WriteLine("Hau hau");
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
using Jypeli;
using Jypeli.Assets;
using Jypeli.Controls;
using Jypeli.Widgets;
// ReSharper disable VirtualMemberCallInConstructor
// ReSharper disable UnusedVariable
namespace Hirsipuu;
......@@ -14,11 +12,107 @@ namespace Hirsipuu;
/// </summary>
public class Hirsipuu : PhysicsGame
{
private static readonly string[] kirjaimet =
{
"QWERTYUIOPÅ",
"ASDFGHJKLÖÄ",
"ZXCVBNM -"
};
public override void Begin()
{
// Kirjoita ohjelmakoodisi tähän
string teksti = "LUMIUKKO";
Level.Background.CreateGradient(Color.Brown, Color.Black);
// Kirjain a = new Kirjain(this, 'A', new Vector(100, -100), new Vector(100, 100));
// Kirjain b = new Kirjain(this, 'B', new Vector(200, -100), new Vector(100, 100));
Aakkoset aakkoset = new Aakkoset(this, kirjaimet, new Vector(-200, 0), 800);
Sana sana = new Sana(this, new Vector(-200, 300), teksti, 100);
PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
}
}
public class Sana
{
// ReSharper disable once NotAccessedField.Local
private string teksti;
private readonly Vector position;
// ReSharper disable once ConvertToAutoProperty
// ReSharper disable once ArrangeAccessorOwnerBody
public Vector Position { get { return position; } }
// ReSharper disable once CollectionNeverQueried.Local
private readonly List<Kirjain> kirjaimet = new List<Kirjain>();
// ReSharper disable once NotAccessedField.Local
private PhysicsGame peli;
public Sana(PhysicsGame peli, Vector p, string teksti, double d)
{
this.teksti = teksti;
this.position = p;
Vector sarakesiirtyma = new Vector(d * 1.1, 0);
Vector paikka = p;
this.peli = peli;
Vector koko = new Vector(d, d);
foreach (var kirjain in teksti)
{
Kirjain palikka = new Kirjain(peli, kirjain, paikka, koko);
this.kirjaimet.Add(palikka);
paikka += sarakesiirtyma;
}
}
}
public class Aakkoset
{
public Aakkoset(PhysicsGame peli, string[] kirjaimet, Vector p, double w)
{
if (kirjaimet.Length == 0) return;
int n = kirjaimet[0].Length; // TODO: etsi pisin rivi
if (n == 0) return;
double d = w / n;
Vector koko = new Vector(d, d);
Vector rivisiirtyma = new Vector(d*0.2, -d*1.1);
Vector sarekesiirtyma = new Vector(d*1.1, 0);
for (int rivi = 0; rivi < kirjaimet.Length; rivi++)
{
Vector paikka = p + rivi * rivisiirtyma;
string jono = kirjaimet[rivi]; // QWERTYUIOPÅ
foreach (var kirjain in jono)
{
Kirjain palikka = new Kirjain(peli, kirjain, paikka, koko);
paikka += sarekesiirtyma;
}
}
}
}
public class Kirjain : PhysicsObject // IS-A Kissa on ELain, Has-A
{
private static readonly Image kuva = Game.LoadImage("puupalikka"); // vakio
private static readonly Font fontti = new Font(70, true);
public Kirjain(PhysicsGame peli, char k, Vector p, Vector koko, bool nakyy = true) : base(koko.X, koko.Y)
{
peli.Add(this);
Label label = new Label(""+k);
this.Add(label);
label.RelativePosition = Vector.Zero;
this.Position = p;
label.Font = fontti;
this.Image = kuva;
label.IsVisible = nakyy;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment