Snowman from the command line + Jypeli

This task is related to the Demo 1 weekly task.

This task assumes that you have installed at least text editor and Rider (or Visual Studio) from the tools of the course .

This autumn 2024 exercise will use the dotnet command. If you want to compile with the csc command (or Mac/Linux mcs command), then see fall 2020 exercises.

This exercise works the same way on all Win/Mac/Linux systems.

1. Installing Jypeli project templates

If you have not yet entered the following command from the command line in previous installations, then enter it now and the Jypel project templates will be installed:

dotnet new install Jypeli.Templates

2. Creating a project

First, go to a directory such as /c/kurssit/ohj1/paate/paate1 (made in the HelloWorld task, micro class machine or Mac/Linux go to the directory where you made the HelloWorld task)

Windows:

cd /c/kurssit/ohj1/paate/paate1

Mac/Linux:

cd ~/kurssit/ohj1/paate/paate1

Create a new Lumiukko project:

dotnet new Fysiikkapeli -n Snowman

Go to the snowman directory

cd Snowman

See what files were created

ls -la

3. Edit file

Edit the file Snowman.cs with the text editor.

For example, if Visual Studio Code is installed, then

code Snowman.cs

or if no editor is installed, then

win:   notepad Snowman.cs
Mac:   open -e Snowman.cs
Linux: gedit Snowman.cs &

Copy the contents of the template into it. Note that you replace the underlying code completely with the code below.

// Otetaan käyttöön Jyväskylän yliopiston Jypeli-kirjasto
using Jypeli;

namespace Snowman;

/// @author  Antti-Jussi Lakanen, Vesa Lappalainen
/// @version 12.1.2012
///
/// <summary>
/// Class for drawing practice by adding circles to the screen
/// </summary>
public class Snowman: PhysicsGame
{
    /// <summary>
    /// Function, where circles are drawn and then zoomed into
    /// </summary>
    public override void Begin()
    {
        Camera.ZoomToLevel();
        Level.Background.Color = Color.Black;

        PhysicsObject p1 = new PhysicsObject(2 * 100.0, 2 * 100.0, Shape.Circle);
        p1.Y = Level.Bottom + 200.0;
        Add(p1);

        PhysicsObject p2 = new PhysicsObject(2 * 50.0, 2 * 50.0, Shape.Circle);
        p2.Y = p1.Y + 100 + 50;
        Add(p2);

        PhysicsObject p3 = new PhysicsObject(2 * 30.0, 2 * 30.0, Shape.Circle);
        p3.Y = p2.Y + 50 + 30;
        Add(p3);
    }
}

4. Compile and run the program

To compile and run a program, enter the command

dotnet run

See what files (and folders) were created during translation

ls -la
# color

5. Changing the colour and appearance of the Uko

NOTE! The following code snippets are written in a text editor in the program code (i.e. in a file Snowman.cs)! Try different colours for the cicles. For example

p2.Color = Color.HotPink;

The line can be added to any new row after the variable p2 has been introduced. } The best place is where the variable p2 is handled anyway (i.e. before p3 is introduced).

Compile and run the program as taught in the previous sections, and marvel at the change you have made.

You can see the available predefined colour names in Jypeli's instructions.

You can make any colour you like yourself by calling the Color-function:

  p3.Color = new Color(173, 255, 47, 50);

where the parameters are in the order Red, Green, Blue, Alpha. Each value can range from 0 to 255. Alpha denotes a transparency value, 0 fully transparent, 255 fully opaque.

Of course, you can also change the background colour to whatever you like - try it.

Try following the instructions in the library to change the bottom ball to twice as wide. Do not change the height of the "ball".

Draw eyes on the snowman.

If you try to insert pieces of physics inside other pieces of physics, you will find that they automatically move apart. That's why the following code can't be used to insert circles like before (you can try, of course)

Add(eye1);

because then silma1 would be added to the game and it would overlap with p3, because their coordinates are calculated to fall inside each other.

Strictly speaking, the above would be

this.Add(eye1)

i.e. the addition is made specifically inside this, the game itself.
The reference this refers to the game that is being made and if it is not explicitly written, is interpreted as if it were written.

Within the game, the physics engine does not allow objects to be nested.

However, you can join pieces (objects) together so that they behave like one and the same piece by placing an object as a child of another object(i.e. inserting an object inside another object, in this case p3, as follows:

PhysicsObject eye1= new PhysicsObject(2 * 10.0, 2 * 10.0, Shape.Circle);
eye1.Y = p3.Y + 0;
silma1.Color = Color.Black;
p3.Add(eye1);

Note above p3.Add(eye1), i.e. eye1 is added as a child of p3(i.e. explicitly inside p3, not inside the game).

So now eye1 is fixedly attached to p3 at its center (since 0 is added to Y, the y-coordinate of eye1 is the same as the y-coordinate of p3). Figure out for yourself how to change the x-coordinate. Since the x-coordinate is not given, it defaults to 0.

If the eye is exactly in the middle of p3, it does not seem to make any difference whether add(eye1) or p3.add(eye1). However, you will immediately notice the difference if the eye is not in the middle. The reason is that when in the middle of the "head", the forces of physics pull the eye equally in all directions if it is inserted into the game and not inside the circle p3.

6. Additional task: the power to play

Add a line to the end of the Begin method (remember that the Begin method starts with the bracket after the Begin line, and ends with the bracket that closes the method, so "end of subroutine" means before the bracket that closes it):

Gravity = new Vector(0, -400);
  • compile and run
  • try other figures for -400

7. Non-graphic programmes

Remember that using dotnet you can also make non-graphical programs, e.g. HelloWorld (which you probably already did in the previous section):

  1. Go to the directory under which you want the program

  2. Enter the command

dotnet new ConsoleMain -n HelloWorld
  1. Go to the program directory cd HelloWorld

  2. Edit the file HelloWorld.cs

  3. Compile and run:

dotnet run

These are the current permissions for this document; please modify if needed. You can always modify these permissions from the manage page.