Pygame入門 マウスポインターの座標を取得する

マウスポインターの座標を取得して、画面に数値で表示する。

import pygame
from pygame.locals import *
import sys

SCREEN_SIZE = (400, 400)

def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)   # 画面の大きさを設定する
    pygame.display.set_caption('mouse')   # 画面のタイトルを設定する
    font = pygame.font.Font(None, 60)

    while True:
        pygame.display.update()
        mouseX, mouseY = pygame.mouse.get_pos()
        text = font.render(f'{mouseX}, {mouseY}', True, (255, 0, 0))
        screen.fill((255, 255, 255))
        screen.blit(text, [0, 0])

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main()

Pygame入門 図形(四角)を行列に並べて表示する

import pygame
from pygame.locals import *   # 定数QUITを使うため。importしない場合は、pygame.QUITとする。
import sys

SCREEN_WIDTH = 400
SCREEN_HEIGHT = 300
SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)

ROW = 3 # 表示する四角の行数
COLUMN = 5 # 表示する四角の列数
MARGIN = 10 # 表示する四角の間隔
RECT_WIDTH = (SCREEN_WIDTH - (MARGIN * (COLUMN + 1))) / COLUMN # 表示する四角の幅
RECT_HEIGHT = 20 # 表示する四角の高さ

def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)   # 画面の大きさを設定する
    pygame.display.set_caption('figure')   # 画面のタイトルを設定する

    while True:
        screen.fill((255, 255, 255))   # 画面を白く塗りつぶす

        # 赤色の四角を行列に並べて描く
        for r in range(ROW):
            for c in range(COLUMN):
                x = MARGIN * (c + 1) + RECT_WIDTH * c
                y = MARGIN * (r + 1) + RECT_HEIGHT * r
                pygame.draw.rect(screen, (255, 0, 0), Rect(x, y, RECT_WIDTH, RECT_HEIGHT))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main()

Pygame入門 図形(四角)を表示する

import pygame
from pygame.locals import *   # 定数QUITを使うため。importしない場合は、pygame.QUITとする。
import sys

SCREEN_SIZE = (400, 300)

def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)   # 画面の大きさを設定する
    pygame.display.set_caption('figure')   # 画面のタイトルを設定する

    while True:
        screen.fill((255, 255, 255))   # 画面を白く塗りつぶす

        # 赤色の四角を描く 左上の座標が(10, 10)、長さと高さが(50, 20)
        pygame.draw.rect(screen, (255, 0, 0), Rect(10, 10, 50, 20))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main()

Pygame入門 日本語文字列を表示する

IPAexゴシックフォントを使いました。

プログラムを実行する前に、
IPAのサイトからフォントファイルをダウンロードして、
カレントディレクトリ(Pythonスクリプトファイルが置かれているディレクトリ)に配置してください。

import pygame
from pygame.locals import *   # 定数QUITを使うため。importしない場合は、pygame.QUITとする。
import sys

SCREEN_SIZE = (400, 300)

def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)   # 画面の大きさを設定する
    pygame.display.set_caption('display')   # 画面のタイトルを設定する
    font = pygame.font.Font('ipaexg.ttf', 50)
    text = font.render('日本語', True, (0, 0, 0))

    while True:
        screen.fill((255, 255, 255))   # 画面を白く塗りつぶす
        screen.blit(text, (10, 10))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main()

Pygame入門 文字列を表示する

Pygameでは文字列を直接画面に表示することはできないそうです。
なので、下記のように3つの手順を実行します。
font = pygame.font.Font(None, 55) # フォントを設定する
text = font.render('abc', True, (255, 255, 255)) # 表示する文字列を作る
screen.blit(text, [50, 100]) # 画面に文字列を貼り付ける

renderの2つ目の引数にTrueを指定すると、
文字の角が滑らかに表示されるそうです。

import pygame
from pygame.locals import *   #pygameで設定された定数を使うため
import sys

def main():
    pygame.init()
    screen = pygame.display.set_mode((400, 300))   # 画面の大きさを設定する
    pygame.display.set_caption('text')   # 画面のタイトルを設定する
    font = pygame.font.Font(None, 55)   # フォントを設定する

    while True:
        screen.fill((0, 0, 0))   # 画面を黒く塗りつぶす
        text = font.render('abc', True, (255, 255, 255))   # 表示する文字列を作る
        screen.blit(text, [50, 100])   # 画面に文字列を貼り付ける
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main()

Pygame入門 画面を表示する

import pygame

# 定数QUITを使うため。importしない場合は、pygame.QUITとする。
from pygame.locals import *

import sys

def main():
    pygame.init()
    screen = pygame.display.set_mode((400, 300))   # 画面の大きさを設定する
    pygame.display.set_caption('display')   # 画面のタイトルを設定する

    while True:
        screen.fill((0, 0, 0))   # 画面を黒く塗りつぶす
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

if __name__ == '__main__':
    main()

プログラミング言語のScratchをLinux(Debian)にインストールしてオフラインで使う

$ sudo apt install git
$ sudo apt install nodejs npm
$ git clone --depth=1 https://github.com/LLK/scratch-gui.git
$ cd scratch-gui
$ npm install
$ npm run-script build

「build」ディレクトリの中の「index.html」をブラウザで開く