"> ");
100%
LESSONS & TOPICS

Εκτέλεση ενεργειών πολλές φορές

Εκτέλεση ενεργειών πολλές φορές

Video – Εκτέλεση ενεργειών πολλές φορές

#accordions-6025{display: none;}
#accordions-6025 {
text-align: left;
}
#accordions-6025{
background: url() repeat scroll 0 0;
padding: 0;
}
#accordions-6025 .accordions-head{
background:rgba(221,153,51, 1) none repeat scroll 0 0;
margin:1px;
padding:10px;
}
#accordions-6025 .accordions-head-title{
color:#ffffff;
font-size:14px;
font-family:;
}
#accordions-6025 .accordions-head-title-toogle{
color:#ffffff;
font-size:14px;
}
#accordions-6025 .accordions-head:hover .accordions-head-title{
color:#000;
}
#accordions-6025 .ui-state-active{
background: #4b8fe3;
}
#accordions-6025 .accordion-content{
background:rgba(255,255,255,1) none repeat scroll 0 0;
color:#333333;
font-size:13px;
font-family:;
margin:0;
padding:10px;
}
#accordions-6025 .accordion-icons{
color:#565656;
font-size:16px;
}
#accordions-6025 .accordions-head:hover .accordion-icons{
color:#999;
}
#accordions-6025{}
#accordions-6025 .accordions-head{}
#accordions-6025 .accordion-content{}

Video Transcript

Καλησπέρα.

 

3η στην σειρά αλλά εξίσου σημαντική με τις προηγούμενες 2 προγραμματιστικές δομές είναι η δομή επανάληψης.

 

Αν επιθυμούμε κάποιες ενέργειες να εκτελούνται πολλές φορές τότε θα πρέπει να μπορούμε να χρησιμοποιούμε εντολές της συγκεκριμένης δομής.

 

Ας ακούσουμε τον Steve σε ένα εξαιρετικά ενδιαφέρον βίντεο.

Γεια σας,

Πολλές φορές, ως προγραμματιστές θέλουμε κάποιες εντολές να εκτελούνται ξανά και ξανά. Να επαναλαμβάνονται δηλαδή αρκετές φορές.

Ας δούμε ένα πολύ απλό πρόγραμμα που δημιούργησα για να σας δείξω ακριβώς αυτό.

Αρχικά ας τρέξουμε το σενάριο για να δούμε το αποτέλεσμα του στο σκηνικό μας.

Βλέπετε πως ο χαρακτήρας μας επαναλαμβάνει κάποιες ενέργειες.

Ας ρίξουμε μια ματιά στον κώδικα. Πράγματι βλέπουμε πως αυτό το μπλοκ εντολών επαναλαμβάνεται 3 φορές.

Μπορούμε να επαναλάβουμε αυτές τις γραμμές κώδικα όσες φορές θέλουμε. Με αυτόν τον τρόπο όμως δημιουργούμε πολύ μεγάλα σενάρια τα οποία είναι δύσκολο να διαβαστούν και να αλλαχτούν αν κάτι τέτοιο χρειάζεται.

Αντί να γράφουμε ξανά και ξανά τις ίδιες εντολές μπορούμε να ρυθμίσουμε εκτελούνται όσες φορές θέλουμε αν οι εντολές αυτές ενταχθούν μέσα σε ένα μπλοκ επανέλαβε.

Για δείτε…

Ένα μπλοκ με εντολές μπορεί επίσης να επαναλαμβάνεται ακόμη και αν δεν γνωρίζουμε τον ακριβή αριθμό των επαναλήψεων. Μπορεί να επαναλαμβάνεται μέχρις ότου να συμβεί κάτι.

Στην οθόνη μου θα δείτε το ίδιο σενάριο με πριν, μόνο που τώρα η επανάληψη γίνεται με την εντολή «Επανέλαβε ώσπου»

Δείτε τις ίδιες γραμμές κώδικα. Επαναλαμβάνονται μέχρις ότου η μεταβλητή counter να γίνει ίση με 10.

Προσέξτε την μεταβλητή counter η οποία αρχικά παίρνει την τιμή 1 και μετά την εκτέλεση του μπλοκ επανάληψης η τιμή της ανεβαίνει κατά 1. Αυτό επαναλαμβάνεται μέχρις ότου η τιμή της μεταβλητής να γίνει 10.

Η επανάληψη χρησιμοποιείται πάρα πολύ συχνά στον προγραμματισμό. Χρησιμοποιείται θα λέγαμε πάντα όταν θα πρέπει να δημιουργήσουμε παιχνίδια με προγραμματισμό.

Σε όλα τα παιχνίδια, τις περισσότερες φορές, θέλουμε καθ’ όλη την διάρκεια του παιχνιδιού να συμβαίνει κάτι. Για παράδειγμα, στο παιχνίδι που έχουμε συζητήσει ξανά στο προηγούμενο μάθημα που διαθέτει τους χαρακτήρες μαϊμού, μπανάνα και λιοντάρι, σε όλη την διάρκεια του παιχνιδιού όταν η μαϊμού ακουμπάει μπανάνα θέλουμε να ανεβαίνει το score και το τσαμπί να μετακινείται σε άλλο σημείο του σκηνικού.

Αυτά θέλουμε να γίνονται σε όλη την διάρκεια του παιχνιδιού. Οι εντολές που εκτελούν αυτές τις ενέργειες πρέπει να βρίσκονται εντός επανάληψης.

Επειδή όμως δεν ξέρουμε πόσο θα διαρκέσει το παιχνίδι για να θέσουμε τον ακριβή αριθμό των επαναλήψεων ούτε μπορούμε να ορίσουμε ένα συμβάν που οι εντολές αυτές θα εκτελούνται μέχρι την πραγματοποίηση του εύκολα, χρησιμοποιούμε την εντολή

«Για πάντα».

Η εντολή “για πάντα” είναι μια εντολή επανάληψης που χρησιμοποιείται πάρα πολύ συχνά στον προγραμματισμό παιχνιδιών με Scratch.

Στο παιχνίδι που αναφέραμε ξανά προηγουμένως και θα δημιουργήσουμε στο επόμενο μάθημα, χρησιμοποιείται σε κάθε αντικείμενο. Ας ρίξουμε μια ματιά.

Μουσική παρακαλώ…

 

 

 

 

Εκτέλεση ενεργειών πολλές φορές "> ");
100%
LESSONS & TOPICS

Perform actions multiple times

Perform actions multiple times

 

Video – Perform actions multiple times

 

 

 

#accordions-596{display: none;}
#accordions-596 {
text-align: left;
}
#accordions-596{
background: url() repeat scroll 0 0;
padding: 0;
}
#accordions-596 .accordions-head{
background:rgba(129,215,66, 1) none repeat scroll 0 0;
margin:1px;
padding:10px;
}
#accordions-596 .accordions-head-title{
color:#ffffff;
font-size:14px;
font-family:;
}
#accordions-596 .accordions-head-title-toogle{
color:#ffffff;
font-size:14px;
}
#accordions-596 .accordions-head:hover .accordions-head-title{
color:#000;
}
#accordions-596 .ui-state-active{
background: #4b8fe3;
}
#accordions-596 .accordion-content{
background:rgba(255,255,255,1) none repeat scroll 0 0;
color:#333333;
font-size:13px;
font-family:;
margin:0;
padding:10px;
}
#accordions-596 .accordion-icons{
color:#565656;
font-size:16px;
}
#accordions-596 .accordions-head:hover .accordion-icons{
color:#999;
}
#accordions-596{}
#accordions-596 .accordions-head{}
#accordions-596 .accordion-content{}

Video Transcript

Good evening.

3rd in a row but equally important with the previous 2 programming structures is the repetition structure.

If we want some actions to be executed many times then we should be able to use commands of that particular structure.

Let’s listen to Steve on an extremely interesting video.

 

Hello,

Often, as developers, we want some instructions to run again and again. Repeat them several times.

Let’s look at a very simple program I’ve created to show you just that.

First let’s run the script and see its effect in our setting.

You see how our sprite repeats some actions.

Let’s take a look at the code. Indeed, we see that this command block is repeated three times.

We can repeat these code lines as many times as we want.

In this way, however, we create very large scenarios that are difficult to read and change if needed.

Instead of writing again and again the same commands we can set up running as many times as we want if these commands enter a block of repeats.

Let’s have a look…

A block of commands can also be repeated even if we do not know the exact number of repetitions. It can be repeated until something happens.

It can be repeated until something happens.

On my screen you will see the same scenario as before, only now the repetition is done with the command “Repeat until”.

See the same lines of code. Repeat until the variable counter is equal to 10.

Note that the variable “counter” which initially gets the value 1 and after executing the repeat block its value rises by 1.

This keeps repeating until the value of the variable is 10.

Repetition is used too often in computer programming.

It is always used when we have to create games by programming.

In all games, we always want to be happening.

For example, remember the game we  discussed in the previous lesson.

Remember that throughout the game, when the monkey touches a banana the score raises and the bananas moves to another point of the stage.

We want action like this throughout the whole game.

The instructions that perform these actions must be in a repeat mode.

Because we do not know how long the game will take in order to set the exact number of repeats, nor can we define an event that these instructions will run until it is done easily, the instruction FOREVER.

The “forever” instruction is a repeat command that is used very often in scratch game programming.

In the game we mentioned earlier and we will create in the next lesson, it is used in every sprite.

Let’s have a look.

Music please..