Skip to content

Commit

Permalink
[LANGUAGE] Improve pressed command (hedyorg#4146)
Browse files Browse the repository at this point in the history
Fixes hedyorg#4138,

This PR changes how the _ifpressed_ command works. 
**From level 5 till level 15: it is required to use an else**

```
if x is pressed print "x!" else print "other one"
```

**From level 15: using it without else is allowed**
```
if x is pressed 
  print "x!"
```

**From level 17: using it with elif is possible (The drawing game !!)**
```
repeat 20 times
    if w is pressed:
        forward 20
    elif a is pressed:
        turn -90
    elif d is pressed:
        turn 90
    elif s is pressed:
        forward -20
```

I added an error when the user forgets to add the else in level 5 till 15, still need to figure out how to make translatable messages
![afbeelding](https://user-images.githubusercontent.com/48225550/226071609-860bc535-159f-458c-b5a1-223ce2425896.png)

**Still To-Do:**
- [x] Add/Fix tests
  • Loading branch information
ToniSkulj authored Apr 5, 2023
1 parent 6b1429b commit 7ef26ea
Show file tree
Hide file tree
Showing 75 changed files with 936 additions and 4,256 deletions.
3 changes: 3 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ def translate_error(code, arguments, keyword_lang):
'character_found',
'concept',
'tip',
'else',
'command',
'print',
'ask',
Expand All @@ -705,6 +706,7 @@ def translate_error(code, arguments, keyword_lang):
'variable',
'invalid_value',
'print',
'else',
'ask',
'echo',
'is',
Expand All @@ -727,6 +729,7 @@ def translate_error(code, arguments, keyword_lang):
arguments["print"] = "print"
arguments["ask"] = "ask"
arguments["echo"] = "echo"
arguments["else"] = "else"
arguments["repeat"] = "repeat"
arguments["is"] = "is"

Expand Down
56 changes: 0 additions & 56 deletions content/adventures/ar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -875,62 +875,6 @@ adventures:
story_text: "## Create a game of Blackjack\nBlackjack is a simple game of cards in which you have to get as close to 21 points as possible. You get two cards. Each card is worth their numeral value, and the face cards (Jack, Queen and King) are worth 10 points.\nThe Ace is worth either 1 or 11 points (you can choose). The dealer, your opponent, also gets two cards.\nIf you want, you can get another card, and its points will be added to your total. The dealer can also choose to take another card.\nBut be careful not to get more than 21 points, because if you do, you lose!\nThe player who gets closest to 21, without going over it, wins!\n\nHave fun!\n"
example_code: "```\n{print} 'BLACKJACK'\ncards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen','King', 'Ace']\npoints = 0\ndealer_points = 0\ncard_1 = cards[{random}]\ncard_2 = cards[{random}]\ncard_3 = cards [{random}]\ndealer_card_1 = cards[{random}]\ndealer_card_2 = cards[{random}]\ndealer_card_3 = cards[{random}]\n# Points for card 1\n{if} card_1 == 'Jack' {or} card_1 == 'Queen' {or} card_1 == 'King':\n points = points + 10\n{elif} card_1 == 'Ace':\n points = points + 11\n{else}:\n points = points + card_1\n# Points for card 2\n{if} card_2 == 'Jack' {or} card_2 == 'Queen' {or} card_2 == 'King':\n points = points + 10\n{elif} card_2 == 'Ace':\n points = points + 11\n{else}:\n points = points + card_2\n# Points for dealer card 1\n{if} dealer_card_1 == 'Jack' {or} dealer_card_1 == 'Queen' {or} dealer_card_1 == 'King':\n dealer_points = dealer_points + 10\n{elif} dealer_card_1 == 'Ace':\n dealer_points = dealer_points + 11\n{else}:\n dealer_points = dealer_points + dealer_card_1\n# Points for dealer card 2\n{if} dealer_card_2 == 'Jack' {or} dealer_card_2 == 'Queen' {or} dealer_card_2 == 'King':\n dealer_points = dealer_points + 10\n{elif} dealer_card_2 == 'Ace':\n dealer_points = dealer_points + 11\n{else}:\n dealer_points = dealer_points + dealer_card_2\n# Two Aces\n{if} card_1 == 'Ace' {and} card_2 == 'Ace':\n points = 12\n{if} dealer_card_1 == 'Ace' {and} dealer_card_2 == 'Ace':\n dealer_points = 12\n# Scoreboard\n{print} 'You have a ' card_1 ' and a ' card_2 ' (' points ' points)'\n{print} 'The dealer has a ' dealer_card_1 ' and a ' dealer_card_2 ' (' dealer_points ' points)'\n# Extra card for the player\nhit = {ask} 'Do you want an extra card?'\n{if} hit == 'yes':\n {if} card_3 == 'Jack' {or} card_3 == 'Queen' {or} card_3 == 'King':\n points = points + 10\n {elif} card_3 == 'Ace':\n {if} points > 11:\n points = points + 11\n {else}:\n points = points + 1\n {else}:\n points = points + card_3\n print 'You get an extra ' card_3 ' (' points ' points)'\n{else}:\n print 'No extra cards'\n# Winner\n{if} points > 21 {or} dealer_points > points {or} dealer_points == 21:\n {print} 'You lose'\n{elif} dealer_points < 17:\n {print} 'The dealer takes an extra card. It is a... ' dealer_card_3\n {if} dealer_card_3 == 'Jack' {or} dealer_card_3 == 'Queen' {or} dealer_card_3 == 'King':\n dealer_points = dealer_points + 10\n {elif} dealer_card_3 == 'Ace':\n {if} dealer_points < 11:\n dealer_points = dealer_points + 11\n {else}:\n dealer_points = dealer_points + 1\n {else}:\n dealer_points = dealer_points + dealer_card_3\n {print} 'The dealer has ' dealer_points ' points now'\n {if} dealer_points < 21 {and} dealer_points > points:\n {print} 'You lose'\n {else}:\n {print} 'You win'\n{elif} points > dealer_points {and} points < 21:\n {print} 'You win!'\n```\n"
start_code: '# place your code here'
pressit:
name: Press it!
description: Try linking a keyboard key to a command!
levels:
5:
story_text: "## اضغط عليه!\nفي البداية تم إخبارك عن الكلمة الرئيسية الجديدة! باستخدام ```pressed```! Using ```pressed```\nيمكن أن تجعل البرمجة أكثر فعالية ، ويمكنك التحكم مباشرة في ما يتم تنفيذه!\nفي المستويات القادمة سوف تتعلم كيف أن```pressed``` makes this possible and what uses ```pressed```\nلديها.\nإلى جانب طباعة النص ، هناك مجموعة متنوعة كاملة من الاستخدامات لـ ```pressed```\nعلى سبيل المثال ، يمكنك أيضًا ربط أوامر السلحفاة بالمفاتيح! جربها!\n"
example_code_2: "```\n{if} y {is} {pressed} {print} 'Amazing! You pressed the y key!'\n{else} {print} 'Oh no! You did not press the y key.'\n```\n"
start_code: '# place your code here'
example_code: "```\n{if} x {is} {pressed} {forward} 50\n```\n"
story_text_2: "## و الا اذا اضغط\nيمكنك أيضًا تعيين عبارة والا لـ ```pressed```. سيتم تنفيذ جملة والا\nعندما تضغط على مفتاح بخلاف المفتاح الذي قمت بتعيينه.\n"
6:
story_text: "هل تعلم أنه يمكنك أيضاً صنع آلة حاسبة بإستخدام الأمر```pressed```؟\nبإعطائك للآلة الحاسبة رقمين و الضغط على زر \"ج\" لتنفيذ عملية جمع.\nالآن ليس عليك أن تجمعهم بنفسك!\nجرب إستخدام أزرار أخرى من على لوحة المفاتيح وأنظر ماذا سيحدث! هل ستحصل على نتيجة مختلفة؟\n"
example_code: "```\nالأول = {ask} 'ما هو الرقم الأول؟'\nالثاني = {ask} 'ما هو الرقم الثاني؟'\nprint 'إضغط ج للجمع، ط للطرح، ق للقسمة، ض للضرب'\n{if} ج {is} {pressed} ناتج= الأول + الثاني\n{if} ط {is} {pressed} ناتج= الأول - الثاني\n{if} ق {is} {pressed} ناتج= الأول\\الثاني\n{if} ض {is} {pressed} ناتج= الأول * الثاني\n{print}'الناتج هو ' ناتج\n```\n"
start_code: '# أكتب الكود الخاص بك هنا'
7:
example_code: "```\n{repeat} 3 {times} {if} x {is} {pressed} forward 15\n```\n"
story_text: "You might have tried it in level 5 and 6, pressing the key linked to ```pressed``` multiple\ntimes. If you did, you noticed that this did nothing. but now that you have learned about repeat, we\ncan press keys multiple times! Make the turtle walk forward!\n"
start_code: '# place your code here'

9:
story_text: "Now that you know how to nest multiple statements, you can also do this with {pressed}!\nBy nesting a pressed in a {repeat} loop, you can repeatedly press buttons and make something happen.\nNow we know you could do this before, but by using the indents you have much more overview!\nTry it counting to 5, for example!\n"
example_code: "```\ntotal = 1\n{repeat} 5 times\n {if} x {is} {pressed}\n {print} total\n {print} 'keep pressing!'\n total = total + 1\n```\n"
start_code: '# place your code here'
10:
example_code: "```\ndistances = 100, 80, 60, 40, 20, 10\n{if} x {is} {pressed}\n for distance in distances\n {forward} distance\n {turn} -90\n{if} y {is} {pressed}\n for distance in distances\n {forward} distance\n {turn} 90\n```\n"
story_text: "In this level you can make the turtle draw you a figure. The turtle is raring to go.\nGive the turtle the starting signal by pressing the x or y key on your keyboard.\nCan you let the turtle draw you another figure?\n"
start_code: '# place your code here'
11:
story_text: "Now that you have learned of the ```for ... in range ... to ...``` rule, you can use it for ```pressed```\ntoo! for example, try moving the turtle! The turtle grows everytime you press a linked button.\nThis way the turtle can move further every step!\n"
example_code: "```\nleft = -90\nright = 90\naround = 180\nfor counter in range 1 to 15\n stepsize = counter * 5\n {if} w {is} {pressed}\n forward stepsize\n {if} s {is} {pressed}\n turn around\n {if} a {is} {pressed}\n turn left\n {if} d {is} {pressed}\n turn right\n```\n"
start_code: '# place your code here'
12:
story_text: "Are you familiar with videogames? You walk into tall grass and are suddenly approached by\na very suspicious mouse. What will you do?\nIn this level we will make a small menu with different kinds of options.\nStoring text into a variable is quite handy for repeated uses.\nOption, I choose you!\n"
example_code: "```\nchoose = 'You chose option: '\nprint 'A wild mouse appeared!'\nprint 'Choose an option and press first letter: (n)et, (c)at, (b)ag, (r)un'\n{if} n {is} {pressed}\n {print} choose + 'net'\n {print} 'You attempt to catch the mouse with a net..'\n sleep\n {print} 'Congratulations you did it!'\n{if} c {is} {pressed}\n {print} choose + 'cat'\n {print} 'You send out your cat'\n sleep\n {print} 'The mouse got scared and ran away!'\n{if} b {is} {pressed}\n {print} choose + 'bag'\n {print} 'The mouse jumped into you bag!'\n{if} r {is} {pressed}\n {print} choose + 'run'\n {print} 'You got away safely!'\n```\n"
start_code: '# place your code here'
14:
story_text: "Now that you learned how to use operators, we can use it with ```pressed```too!\nimagine the turtle being able to do a superjump forward. by pressing ```p``` the turtle can make a\nsuperjump! However, the turtle can only do this jump every 5 steps or every 4 turns\n"
example_code: "```\nstepsize = 15\nleft = -90\nright = 90\naround = 180\njumptime = 5\nspins = 4\nrepeat 40 times\n {if} w {is} {pressed}\n forward stepsize\n jumptime = jumptime - 1\n {if} s {is} {pressed}\n turn around\n {if} a {is} {pressed}\n turn left\n spins = spins - 1\n {if} d {is} {pressed}\n turn right\n spins = spins - 1\n {if} p {is} {pressed}\n {if} jumptime <= 0 or spins <= 0\n forward stepsize * 3\n jumptime = 5\n spins = 4\n```\n"
start_code: '# place your code here'
13:
example_code: "```\nstepsize = 10\nleft = -90\nright = 90\naround = 180\nlooked_left = 0\nlooked_right = 0\nrepeat 40 times\n {if} w {is} {pressed}\n forward stepsize\n {if} s {is} {pressed}\n turn around\n {if} a {is} {pressed}\n turn left\n looked_left = 1\n {if} d {is} {pressed}\n turn right\n looked_right = 1\n {if} p {is} {pressed}\n {if} looked_left is 1 and looked_right is 1\n forward 40\n looked_left = 0\n looked_right = 0\n```\n"
story_text: "Now that you learned how to use ```and``` and ```or```, we can use it with ```pressed```too!\nimagine the turtle being able to do a superjump forward. by pressing ```p``` the turtle can make a\nsuperjump! However, the turtle can only do this jump if he turns left and right first!\n"
start_code: '# place your code here'
16:
start_code: '# place your code here'
story_text: "Lets move a little with a game of Twister!\nWhen everyone is ready, press the x key for the next move!\n"
example_code: "```\nlimbs = ['Right Hand', 'Left Hand', 'Right Foot', 'Left Foot']\ncolours = ['Blue', 'Green', 'Red', 'Yellow']\nrepeat 20 times\n l = limbs[random]\n c = colours[random]\n {if} x {is} {pressed}\n {print} l + ' on ' + c + '!'\n```\n"
17:
story_text: "Now that you learned how to use ```elif```, we can use it with ```pressed```too! Be aware,\n```elif ... is pressed ``` is not possible! That is why we are going to nest some ```elif```\nstatements into the ```pressed``` options. We also had to add ```:``` to the code to make\neverything work! imagine the turtle being able to do a superjump forward. by pressing ```p``` the\nturtle can make a superjump! However, the turtle can only do this jump every 5 steps or every\n4 turns\n"
example_code: "```\nstepsize = 15\nleft = -90\nright = 90\naround = 180\njumptime = 5\nspins = 4\nrepeat 40 times\n {if} w {is} {pressed}:\n forward stepsize\n jumptime = jumptime - 1\n {if} s {is} {pressed}:\n turn around\n {if} a {is} {pressed}:\n turn left\n spins = spins - 1\n {if} d {is} {pressed}:\n turn right\n spins = spins - 1\n {if} p {is} {pressed}:\n {if} jumptime <= 0 or spins <= 0:\n forward stepsize * 3\n jumptime = 5\n spins = 4\n {elif} jumptime > 0:\n print \"you need to take more steps or turns before you can superjump!\"\n```\n"
start_code: '# place your code here'
15:
story_text: "Now that you have learned about the ```while``` loops, you can start using it on the turtle!\nThe turtle is now only allowed to take only 15 steps or turn 15 times, before he gets tired!\nTry to get as far as possible before the turtle gets tired! You can use your superjump after 2 turns\nor 5 steps.\n"
example_code: "```\nstepsize = 15\nleft = -90\nright = 90\naround = 180\njumptime = 5\nspins = 2\ntired_in = 15\nwhile tired_in > 0\n {if} w {is} {pressed}\n forward stepsize\n jumptime = jumptime - 1\n {if} s {is} {pressed}\n turn around\n {if} a {is} {pressed}\n turn left\n spins = spins - 1\n {if} d {is} {pressed}\n turn right\n spins = spins - 1\n {if} p {is} {pressed}\n {if} jumptime <= 0 or spins <= 0\n forward stepsize * 4\n jumptime = 5\n spins = 2\n tired_in = tired_in - 1\n```\n"
start_code: '# place your code here'
default_save_name: Pressit
years:
name: New Year's
description: Countdown to the new year!
Expand Down
Loading

0 comments on commit 7ef26ea

Please sign in to comment.