MakeCode Microbit 遊戲設計:簡易射擊遊戲(Microbit Game: Simple Shooting Game)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

本篇文章將講解如何製作一個簡易射擊遊戲。

一、遊戲功能如下(Game Design):
  • 砲台在畫面下方移動。The turret moves between the bottom of LED matrix.
  • 按A鍵砲台燈向左移動。 The turret moves to left when button A is pressed.
  • 按B鍵砲台燈向右移動。The turret moves to right when button B is pressed.
  • 按A+B鍵發射子彈。Firing when button A and button B are pressed.
  • 敵機隨機在最上方的位置出現。 The enemy moves between the top of LED matrix.
  • 子彈打到敵機時,得一分。Score 1 point when the enemy is hit by a bullet.

二、積木程式(Blocks Code)

遊戲啟動時(On Start):

敵機隨機在最上方的位置出現。 The enemy moves between the top of LED matrix.

砲台在畫面下方移動。The turret moves between the bottom of LED matrix.
按A鍵砲台燈向左移動。 The turret moves to left when button A is pressed.
按B鍵砲台燈向右移動。The turret moves to right when button B is pressed.

按A+B鍵發射子彈。Firing when button A and button B are pressed.
子彈打到敵機時,得一分,。Score 1 point when the enemy is hit by a bullet.


三、結果(The Results)
影片(Demo Video):



請自由發揮額外功能,例如玩家生命等。
More Ideas examples: player's life...etc. 

範例網址(Example Code):https://makecode.microbit.org/_EEAd7hdq8hav

Solutions to Lightbot

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

底下是 LightBot : Code Hour 的參考解法
The following pictures are possible solutions to LightBot : Code Hour.

Level 1-1:
Level 1-2:

Level 1-3:

Level 1-4:

Level 1-5:

Level 1-6:

Level 1-:7

Level 1-8:

Level 2-1:
Level 2-2:
Level 2-3:
Level 2-4:
Level 2-5:
Level 2-6:
Level 3-1:
Level 3-2:
Level 3-:3
Level 3-4:
Level 3-5:
Level 3-6:


C語言練習題:迴圈的魅力(C language exercise: Loop's charisma)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

練習一:奇數數列
使用迴圈輸出 1 到 100 之間的奇數。

Exercise 1: Odd Sequence
Print the odd numbers between 1 to 100.

練習一參考解法:
Exercise 1 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    // FOR loop 1
    for(i = 1; i <= 100; i += 2)
        printf("%d, ", i);

    printf("\n\n");

    // FOR loop 2
    for(i = 1; i <= 100; i++)
        if(i % 2)
            printf("%d, ", i);

    printf("\n\n");

    // WHILE loop
    i = 1;
    while( i <= 100)
    {
        if(i % 2)
            printf("%d, ", i);

        i++;
    }

    printf("\n\n");

    // DO-WHILE loop
    i = 1;
    do
    {
        if(i % 2)
            printf("%d, ", i);
        i++;
    } while(i <= 100);

    return 0;
}

練習二:範圍
輸出 300 到 500 之間可以被 3 與 11 整除的整數。

Exercise 2: Range 
Print the numbers between 300 to 500 which are divisible by 3 and 11.

練習二參考解法:
Exercise 2 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    // FOR loop
    for(i = 300; i <= 500; i++)
        if(i % 3 == 0 && i % 11 == 0)
            printf("%d, ", i);

    printf("\n\n");

    // WHILE loop
    i = 300;
    while( i <= 500)
    {
        if(i % 3 == 0 && i % 11 == 0)
            printf("%d, ", i);

        i++;
    }

    printf("\n\n");

    // DO-WHILE loop
    i = 300;
    do
    {
        if(i % 3 == 0 && i % 11 == 0)
            printf("%d, ", i);
        i++;
    } while(i <= 500);

    return 0;
}

練習三:星星三角形
設計一程式,輸出底下的星星三角形。

Exercise 3: Star Triangle
Design a C program that print the star triangle.


練習三參考解法:
Exercise 3 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j;

    for(i = 0; i <= 4; i++)
    {
        for(j = 0; j <= i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

練習四:乘法表
輸入一個整數,程式輸出此整數從 1 乘到 10 的結果,例如輸入整數 7 時,輸出結果會是
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
.
.
.
7 * 10 = 17 

Exercise 4: Multiplication table
Read an integer from user and print it's multiplication table up to 10. For example, if user give 7, the output will be look like the following:
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
.
.
.
7 * 10 = 17



練習四參考解法:
Exercise 4 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    printf("Enter an integer:");
    scanf("%d", &num);

    for(int i = 1; i <= 10; i++)
        printf("%d * %d = %d\n", num, i, (num*i));

    return 0;
}

練習五:等差數列加總
設計一程式可算出底下等差數列的總和。
1 + 4 + 7 + 10 + 13 + ... + 97 + 100

Exercise 5: Sum of an Arithmetic Series
Write a C program to print the summation of the following series:
1 + 4 + 7 + 10 + 13 + ... + 97 + 100

練習五參考解法:
Exercise 5 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, sum = 0;
    for(n = 1; n <= 100; n += 3)
        sum = sum + n;

    printf("Sum is %d\n", sum);
    return 0;
}

練習六:等比數列加總
設計一程式可算出底下等比數列的總和。
1 + 2 + 4 + 8 + 16 + ... + 524288 + 1048576

Exercise 6: Sum of a Geometric Series
Write a C program to print the summation of the following series:
1 + 2 + 4 + 8 + 16 + ... + 524288 + 1048576

練習六參考解法:
Exercise 6 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

int main()
{
    long n, sum = 0;
    for(n = 1; n <= 1048576; n *= 2)
        sum = sum + n;

    printf("Sum is %ld\n", sum);

    return 0;
}

練習七:星星三角形二
設計一程式,輸出底下的圖形。

Exercise 7:  Star Triangle 2
Design a C program that print the following star triangle.

練習七參考解法:
Exercise 7 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j;

    for(i = 0; i <= 4; i++)
    {
        for(j = 4; j >= i; j--)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

練習八:英文字母
使用迴圈來輸出英文的大小寫字母。

Exercise 8:  Alphabets
Write a C program that uses loop to display English alphabet.

練習八參考解法:
Exercise 8 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char letter;

    printf("Small letters: ");
    for(letter = 'a'; letter <= 'z'; letter++)
        printf("%c ", letter);

    printf("\n\n");
    printf("Large letters: ");
    for(letter = 'A'; letter <= 'Z'; letter++)
        printf("%c ", letter);

    return 0;
}

練習九:迴圈與條件判斷
設計一個程式可不斷讓使用者輸入一個整數,直到輸入 0 才結束程式。並判斷此整數是奇數或是偶數。

Exercise 9: Loop and conditions
Design a C program to determine whether an integer is odd or even repeatedly until 0 is entered. 

練習九參考解法:
Exercise 9 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number;

    do
    {
        printf("Please input an integer:");
        scanf("%d", &number);

        if( number % 2 )
            printf("%d is odd.\n", number);
        else
            printf("%d is even.\n", number);
    }while(number != 0);

    return 0;
}

練習十:費氏數列
設計可以輸入一個整數 N ,輸出到整數 N 的費氏數列。例如當 N = 22 時,程式輸出:0, 1, 1, 2, 3, 5, 8, 13, 21,

Exercise 10: Fibonacci sequence
Write a C program that gets an integer from user and display Fibonacci numbers up to N. If N is 22, the program should print: 0, 1, 1, 2, 3, 5, 8, 13, 21, 


練習十參考解法:
Exercise 10 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    printf("Please enter an integer:");
    scanf("%d", &n);

    int first = 0, second = 1, tmp;

    while(first <= n)
    {
        printf("%d, ", first);
        tmp = first + second;
        first = second;
        second = tmp;
    }

    return 0;
}

練習十一:數字圖案
設計一程式輸出下圖上的數字圖案。

Exercise 11: Number Pattern
Design a C program to display the number pattern in the below picture.

練習十一參考解法:
Exercise 11 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n = 9;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
            printf("%d ", i * j);
        printf("\n");
    }
    return 0;
}

練習十二:質數
設計可以輸入一個整數 N ,輸出到整數 N 的質數數列。例如當 N = 50 時,程式輸出:2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Exercise 12: Prime
Write a C program that gets an integer from user and display prime numbers up to N. If N is 50, the program should print: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

練習十二參考解法:
Exercise 12 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, isPrime;
    printf("Enter an integer:");
    scanf("%d", &n);

    for(int p = 2; p <= n; p++)
    {
        isPrime = 1;
        for(int i = 2; i * i <= p; i++)
        {
            if(p % i == 0)
            {
                isPrime = 0;
                break;
            }
        }

        if(isPrime == 1)
            printf("%d, ", p);
    }

    return 0;
}


C語言練習題:條件判斷(C language exercise: If Condition)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

練習一:入學許可
如果您的在校成績有高於80分,就可以申請自己第一志願的大學。現在請設計一程式可讀取使用者的在校成績,若使用者可以申請第一志願的大學就輸出「YES」,否則輸出「NO」。

Exercise 1: College Admission
If you got GPA higher than 4.0 in transcript then you can apply for  your first choice. Now write a C program read GPA from the keyboard and print "YES" if you can apply otherwise print "NO".

練習一參考解法:
Exercise 1 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float gpa;
    printf("Please enter your GPA:");
    scanf("%f", &gpa);

    if(gpa < 4.0)
        printf("NO\n");
    else
        printf("YES\n");

    return 0;
}


練習二:APCS成績級分
撰寫一個可輸入APCS成績的C語言程式,此程式會依據下表輸出對應的級分。

級分

分數範圍

90~100

70~89

50~69

30~49

0~29

Exercise 2: C Programming GPA

Write a C program that gets C Programming score from the user. Then print the grade depending on the given scale(See the following table).

Grade

Scale

A

90~100

B

70~89

C

50~69

D

30~49

F

0~29


練習二參考解法:
Exercise 2 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int apcsScore;
    printf("Enter your C programming score:");
    scanf("%d", &apcsScore);

    printf("Grade:");
    if(apcsScore >= 90)
        printf("A");
    else if(apcsScore >= 70)
        printf("B");
    else if(apcsScore >= 50)
        printf("C");
    else if(apcsScore >= 30)
        printf("D");
    else if(apcsScore >= 0)
        printf("F");
    printf(".\n");

    return 0;
}

練習三:成年了嗎?
每個國家都會規定幾歲以上才是成年,那我們就來寫個程式來判斷年齡是否有成年吧。此練習題是18歲以上算成年喔!成年後就可以做好多事啊!

Exercise 3: Are you an adult?
In some countries, men and women come of age at 18. Now let's write a C program to determine is someone an adult. There are many things that adults can do.

練習三參考解法:
Exercise 3 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Enter your age:");
    scanf("%d", &age);

    if(age >= 18)
        printf("Congratulation!\n");
    else
        printf("There are %d years left to be an adult.\n", 18 - age);
    return 0;
}

練習四:奇數或偶數
設計一個可以判斷一個整數是奇數或是偶數的程式。

Exercise 4: Odd or even
Design a C program to determine whether an integer is odd or even. 

練習四參考解法:
Exercise 4 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number;
    printf("Please input an integer:");
    scanf("%d", &number);

    if( number % 2 )
        printf("%d is odd.\n", number);
    else
        printf("%d is even.\n", number);

    return 0;
}

練習五:可整除嗎?
寫一個可以判斷一個數是否可以同時被 3 與 11 整除。 

Exercise 5: Divisible 
Take an integer from user and determine whether it is divisible by 3 and 11.

練習五參考解法:
Exercise 5 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number;
    printf("Please input an integer:");
    scanf("%d", &number);

    if( number % 3 == 0 && number % 11 == 0 )
        printf("%d is divisible by 3 and 11.\n", number);
    else
        printf("%d isn't divisible by 3 and 11.\n", number);

    return 0;
}

練習六:最大值
輸入三個整數,程式輸出最大值。

Exercise 6: Maximum
Take three integers from user and print the maximum one.

練習六參考解法一:
Exercise 6 solution 1:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, c;
    printf("Enter three integer:");
    scanf("%d%d%d", &a, &b, &c);

    printf("From %d, %d, %d, the maximum is ", a, b, c);

    if(a > b && a > c)
        printf("%d.\n", a);
    else if(b > c)
        printf("%d.\n", b);
    else
        printf("%d.\n", c);

    return 0;
}

練習六參考解法二:
Exercise 6 solution 2:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, c;
    printf("Enter three integer:");
    scanf("%d%d%d", &a, &b, &c);

    int max;
    if(a > b)
        max = a;
    else
        max = b;

    if(max < c)
        max = c;

    printf("From %d, %d, %d, the maximum is %d.\n", a, b, c, max);

    return 0;
}

練習七:母音?子音?
設計一個程式可以判斷英文字母是母音或子音。

Exercise 7: Vowel or Consonant
Design a C program to print whether a letter alphabet is Vowel or Consonant

練習七參考解法:
Exercise 7 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char letter;
    printf("Enter a letter:");
    scanf(" %c", &letter);

    if( letter == 'a' || letter == 'A' ||
        letter == 'e' || letter == 'E' ||
        letter == 'i' || letter == 'I' ||
        letter == 'o' || letter == 'O' ||
        letter == 'u' || letter == 'U' )
            printf("%c is Vowel.\n", letter);
    else
        printf("%c is Consonant.\n", letter);

    return 0;
}

練習八:月份英文名稱
設計一程式可讀取 [1 - 12] 的整數,輸出對應月份的英文名稱。

Exercise 8: Months Name
Write a C program that gets an integer [1 - 12] from user. And print the corresponding month name. 

練習八參考解法:
Exercise 8 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m;
    printf("Input an integer [1 - 12]:");
    scanf("%d", &m);

    if(m == 1)
    {
        printf("January\n");
    }
    else if(m == 2)
    {
        printf("February\n");
    }
    else if(m == 3)
    {
        printf("March\n");
    }
    else if(m == 4)
    {
        printf("April\n");
    }
    else if(m == 5)
    {
        printf("May\n");
    }
    else if(m == 6)
    {
        printf("June\n");
    }
    else if(m == 7)
    {
        printf("July\n");
    }
    else if(m == 8)
    {
        printf("August\n");
    }
    else if(m == 9)
    {
        printf("September\n");
    }
    else if(m == 10)
    {
        printf("October\n");
    }
    else if(m == 11)
    {
        printf("November\n");
    }
    else if(m == 12)
    {
        printf("Decemember\n");
    }
    else
    {
        printf("Invalid input.\n");
    }


    return 0;
}

練習九:第幾象限
設計一個可以判斷(x, y)座標在第幾象限的程式。

Exercise 9: Coordinate
Design a C program that print the quadrant of the given point(x, y).

練習九參考解法:
Exercise 9 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x, y;
    printf("Enter the value of (X Y):");
    scanf("%d%d", &x, &y);

    if(x > 0 && y > 0)
    {
        printf("First");
    }
    else if(x < 0 && y > 0)
    {
        printf("Second");
    }
    else if(x < 0 && y < 0)
    {
        printf("Third");
    }
    else if(x > 0 && y < 0)
    {
        printf("Fourth");
    }

    printf(" Quadrant.\n");
    return 0;
}