Please help me!

Tâm Nguyễn Thị Thanh
Posts
9
Karma
13
Tâm Nguyễn Thị Thanh posted this 07 July 2017

My game is "Destroy the ships from the bad guys!" and this is also my first game.

I checked it several times  but I can't figure out what's wrong with my code.

The idea is I when the sub bumpm into the ship, it will make the ship explode. When the ship see the oct., it will shoot the oct. once and then turn around. The red score is the number of died ship and blue score is died oct. If there are 5 died oct.s, then we're out. And if there are 2 died ships, then we're won.

Please help me!

Thanks for any help. smiley grinsmiley grinsmiley grin

0
Stephen Coy
Posts
170
Karma
216
Admin
Stephen Coy posted this 07 July 2017
Hi Tâm,

It looks like the problem is that the game ends right away.  Looking at the code the problem is in the

programming for the sub.

 

In the code it is looking for the red or blue scores to be equal to 0 to detemine a win or loss. At the
beginning of the game, they are both zero. This is why the game ends right away. This happens even
though your first 2 lines set those scores to non-zero values. The reason this happens is because of how
Kodu evaluates the rules each frame.

 

When the system runs the code for each frame it first evaluates all the WHEN sides of the rules and then
applies the DO sides for the rules where the WHEN is true. Since all the WHEN sides are evaluated before
the DO sides, this means that changes, such as setting a score, won't be visible until the next frame.

 

To fix you problem you need to use the Page Switch command.

 

On Page 1 have your initialization code:

 

Page 1
WHEN DO SetScore Red 2Points
WHEN DO SetScore Blue 5Points
WHEN DO Switch Page2

 

On page 2 have all the rest of the code:

 

Page 2
WHEN Keyboard WASD DO Move Quickly Quickly Quickly
...

 

When you run the game, on the very first frame it will set the scores and then switch to page 2.
On the next frame, page 2 will be evaluated. Since the scores are not set everything will work as expected.

 

I made this change and it seems to work although the Octopii die very quickly.

 

Let me know if there's anything else I can help with.

 

1
Tâm Nguyễn Thị Thanh
Posts
9
Karma
13
Tâm Nguyễn Thị Thanh posted this 08 July 2017

Thanks so much Stephen. smiley grinsmiley grinsmiley grinsmiley grinsmiley grin

I completed the game. Play and enjoy it!

Thanks, again.

1