In the previous lesson, you used a predefined member variable named [code]position[/code]. In your projects, you will need to define your own variables. Imagine that you need to track the player's health in your game. They may start with [code]5[/code] health points. When taking a hit, the health should go down to [code]4[/code]. After another hit, it should be [code]3[/code]. And so on. To keep track of that, you can create a variable named [code]health[/code] to which you add and subtract points. The example below introduces the [code]print()[/code] function, which outputs its argument to the output box on the left. Click the [i]run()[/i] button to instantly run the entire function, and click the [i]step[/i] button to execute lines of code one by one. The [i]Debugger[/i] panel at the bottom shows the current value of the [code]health[/code] variable.
I föregående lektion använde du en fördefinierad medlems variabel som hette [code]position[/code] I dina egna projekt kommer du behöva definiera dina egna variabler. Föreställ dig att du måste hålla reda på spelarens hälsa i ditt spel. Dom startar kanske med [code]5[/code] hälsopoäng. När dom blir träffade av ett slag borde hälsan gå ner till [code]4[/code]. Efter ännu en träff borde hälsan vara [code]3[/code], och så vidare och så vidare... För att hålla reda på detta, kan du skapa en variabel som heter [code]health[/code] som gör att du kan lägga till eller ta bort hälsopoäng. [i]Exemplet nedan introducerar [code]print()[/code] funktionen, som skriver ut dess parameter till utmatnings boxen till vänster.[/i]
In your projects, you will need to define your own variables.
Imagine that you need to track the player's health in your game.
They may start with [code]5[/code] health points. When taking a hit, the health should go down to [code]4[/code]. After another hit, it should be [code]3[/code]. And so on.
To keep track of that, you can create a variable named [code]health[/code] to which you add and subtract points.
The example below introduces the [code]print()[/code] function, which outputs its argument to the output box on the left.
Click the [i]run()[/i] button to instantly run the entire function, and click the [i]step[/i] button to execute lines of code one by one.
The [i]Debugger[/i] panel at the bottom shows the current value of the [code]health[/code] variable.
I dina egna projekt kommer du behöva definiera dina egna variabler.
Föreställ dig att du måste hålla reda på spelarens hälsa i ditt spel.
Dom startar kanske med [code]5[/code] hälsopoäng. När dom blir träffade av ett slag borde hälsan gå ner till [code]4[/code]. Efter ännu en träff borde hälsan vara [code]3[/code], och så vidare och så vidare...
För att hålla reda på detta, kan du skapa en variabel som heter [code]health[/code] som gör att du kan lägga till eller ta bort hälsopoäng.
[i]Exemplet nedan introducerar [code]print()[/code] funktionen, som skriver ut dess parameter till utmatnings boxen till vänster.[/i]