プログラミング演習の授業は大変だけど楽しい

モンゴル
この記事は約8分で読めます。

後期から、K高専の情報工学科2年生を対象に「Programming Experiments」の授業を週2回担当しています。
授業の開講にあたり、まず検討したのは 「どのプログラミング言語を使うか」「何を教えるか」 ということでした。前期にPythonの基礎を学んだとのことだったので、その延長としてPythonを使った演習授業を実施することにしました。

Pythonはシンプルで強力な言語ですが、GUIの実装が難しいと思っていました。いろいろインターネットで調査をして、マルチメディアアプリケーションやゲーム開発ができる Pygame というライブラリを導入することにしました。これを活用することで、アニメーションやマルチメディアを含むプログラムを学生が実際に作れるようになります。

授業はhttp://programarcadegames.com/ をベースに開発しました。この授業は英語で行うため、事前に何度も予行演習をして、90分の授業がスムーズに実施できるように準備しています。準備に多くの時間と労力を要しますが、学生の反応や習得度を見ながら教材を改善し、試行錯誤しながら進めているところです。
90分の授業が終わるとへとへとに疲れますが、授業後に学生に書いてもらったコメントに“The class is so fun!”“arigato teacher”というコメントを見ると、また次回もがんばろうと思います。大変なこともありますが、「楽しい」と感じてもらえることが一番のやりがい です。

今週はアニメーションのプログラムを作る演習を行いました。最初は エラーが出てなかなか動かなかった 学生がいましたが、少しアドバイスをしたところ、プログラムが動き、画面上でオブジェクトが動き出すと、学生は 「わぁ」と声をあげてうれしそうな顔をしました。私もうれしかったです。

次回の授業では、学生に 自由にアニメーションのプログラムを作ってもらう課題を出しています。どんなアイデアが飛び出すのか、どんなプログラムを作るのかとても楽しみです。

私が授業の教材として作った基本的なアニメーションを紹介します。
こんな短いプログラムで、アニメーション が作れます。


"""Created on Mon Dec  9 15:21:46 2024
@author: marikotagawa
"""
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing Rectangle")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Starting position of the rectangle
rect_x = 50
rect_y = 50
rect_x2 = 200
rect_y2 = 200
# Speed and direction of rectangle
rect_change_x = 2
rect_change_y = 2
rect_change_x2 = 2
rect_change_y2 = 2
# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    # Move the rectangle starting point
    rect_x += rect_change_x
    rect_y += rect_change_y
    rect_x2 += rect_change_x2
    rect_y2 += rect_change_y2
    # Bounce the ball if needed
    if rect_y > 450 or rect_y < 0:
        rect_change_y = rect_change_y * -1
    if rect_x > 650 or rect_x < 0:
        rect_change_x = rect_change_x * -1
    if rect_y2 > 450 or rect_y2 < 0:
        rect_change_y2 = rect_change_y2 * -1
    if rect_x2 > 650 or rect_x2 < 0:
        rect_change_x2 = rect_change_x2 * -1
    screen.fill(BLACK)
    # Draw the rectangle
    pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])
    pygame.draw.rect(screen, RED, [rect_x + 10, rect_y + 10, 30, 30])
    pygame.draw.rect(screen, WHITE, [rect_x2, rect_y2, 50, 50])
    pygame.draw.rect(screen, GREEN, [rect_x2 + 10, rect_y2 + 10, 30, 30])
    clock.tick(60)
    pygame.display.flip()
# Close everything down
pygame.quit()
import pygame
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PINK = (248,53,188)
PI = 3.141592653
size = (800, 800)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Professor Craven's Cool Game")
done = False
clock = pygame.time.Clock()
x_change = 0
y_change = 0
while not done:
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop
    x_change += 5
    screen.fill(BLACK)
    # Draw 2 circles and make a snowman
    for i in range(5):
        for j in range(5):
            if j %2:
                color = WHITE
            else:
                color = PINK
            pygame.draw.circle(screen, color, [i*200 + x_change, j*200], 20)
            pygame.draw.circle(screen, color, [i*200 + x_change, j*200+50], 40)
            if i == 3 and (i*200 + x_change) > 800:
                x_change = 0
    # Draw snowflake
    for i in range(5):
        x = random.randrange(0,800)
        y = random.randrange(0,800)
        pygame.draw.circle(screen, WHITE, [x, y], 10)   
    # Display Text
    font = pygame.font.SysFont('Calibri', 25, True, False)
    text = font.render("There are many Snowmen", True, RED)
    screen.blit(text, [200, 100])
    pygame.display.flip()
    clock.tick(60)
pygame.quit()

今日のモンゴル語
Оюутнууд хөтөлбөрт дуртай гэж хэлэхэд би баяртай байдаг.
(Oyuutnuud khötölbört durtai gej khelekhed bi bayartai baidag.)

生徒がプログラムの授業が楽しいと言ってくれるのがうれしいです。

コメント

タイトルとURLをコピーしました