タスクシステム(基本編)

http://www5f.biglobe.ne.jp/~kenmo/program/task/task2/task2.html
「タスクシステム」って、ゲームプログラムの基本のわりには、
難しい言葉で書かれていることが多いから、できるだけ簡単に解説しました。
 
で、

  • なぜタスクシステムなのか?
  • ゲームループとは?
  • なぜ関数ポインタなのか?
  • なぜリストなのか?

ということについて考えてみると、
「タスクシステム」ってホントに色々な要素が詰まっていて、
再発見がありますよね。

Pygameで画像処理1

画像処理でもなんでもない気がしますが…。
回転処理などを。

import pygame

_gSrc = None

# Image Ratation
def drawRote(buf, x, y, angle):
	srcSize = tBuf.get_size()
	tmpBuf = pygame.transform.rotate(tBuf, angle)
	dstSize = tmpBuf.get_size()
	x -= (srcSize[0] / 2)
	y -= (srcSize[1] / 2)
	p = ((srcSize[0] - dstSize[0]) / 2 + x, (srcSize[1] - dstSize[1]) / 2 + y)
	_gScr.blit(tmpBuf, p, tmpBuf.get_rect())

pygame.init()
pygame.display.set_mode((150, 150), 0, 32)
pygame.display.set_caption("画像回転")
_gScr = pygame.display.get_surface()
tBuf = pygame.image.load("kenmo.jpg").convert()
i = 0
while True:
	i += 1
	drawRote(tBuf, 75, 75, i)
	pygame.display.update()
	pygame.time.wait(10)

Pygameで画像処理2

ここからがホントの画像処理です。
で、超基本のグレースケールから、、。

import pygame

_gSrc = None

pygame.init()
pygame.display.set_mode((150, 150), 0, 32)
pygame.display.set_caption("グレースケール")
_gScr = pygame.display.get_surface()
tBuf = pygame.image.load("kenmo.jpg").convert()

# グレースケール変換
w, h = tBuf.get_size()
for j in range(h):
	for i in range(w):
		colorA = tBuf.get_at((i, j))
		gray = (colorA[0] + colorA[1] + colorA[2]) / 3
		tBuf.set_at((i, j), (gray, gray, gray, 255))

while True:
	_gScr.blit(tBuf, (0, 0), tBuf.get_rect())
	pygame.display.update()
	pygame.time.wait(10)

Pygameで画像処理3

次にセピア変換を。

import pygame

_gSrc = None

pygame.init()
pygame.display.set_mode((150, 150), 0, 32)
pygame.display.set_caption("セピア変換")
_gScr = pygame.display.get_surface()
tBuf = pygame.image.load("kenmo.jpg").convert()

# セピア変換
w, h = tBuf.get_size()
for j in range(h):
	for i in range(w):
		colorA = tBuf.get_at((i, j))
		gray = (colorA[0] + colorA[1] + colorA[2]) / 3
		tBuf.set_at((i, j), (gray * 1, gray * 0.8, gray * 0.6, 255))

while True:
	_gScr.blit(tBuf, (0, 0), tBuf.get_rect())
	pygame.display.update()
	pygame.time.wait(10)

Pygameで画像処理4

続いて、2値変換を。

import pygame

_gSrc = None

pygame.init()
pygame.display.set_mode((150, 150), 0, 32)
pygame.display.set_caption("2値変換")
_gScr = pygame.display.get_surface()
tBuf = pygame.image.load("kenmo.jpg").convert()

threshold = 127 # 閾値
# 2値変換変換
w, h = tBuf.get_size()
for j in range(h):
	for i in range(w):
		colorA = tBuf.get_at((i, j))
		gray = (colorA[0] + colorA[1] + colorA[2]) / 3
		if(threshold > gray):
			colorA = (255, 255, 255, 255)
		else:
			colorA = (0, 0, 0, 255)
		tBuf.set_at((i, j), colorA)

while True:
	_gScr.blit(tBuf, (0, 0), tBuf.get_rect())
	pygame.display.update()
	pygame.time.wait(10)

Pygameで画像処理5

ノリノリです。RGB抽出を。

import pygame

_gSrc = None

pygame.init()
pygame.display.set_mode((150, 150), 0, 32)
pygame.display.set_caption("RGB抽出")
_gScr = pygame.display.get_surface()
tBuf = pygame.image.load("kenmo.jpg").convert()

# RGB抽出
w, h = tBuf.get_size()
for j in range(h):
	for i in range(w):
		colorA = tBuf.get_at((i, j))
		# R成分抽出
		tBuf.set_at((i, j), (colorA[0], 0, 0, 255))

while True:
	_gScr.blit(tBuf, (0, 0), tBuf.get_rect())
	pygame.display.update()
	pygame.time.wait(10)

Pygameで画像処理6

RGB成分入れ替えです。

import pygame

_gSrc = None

pygame.init()
pygame.display.set_mode((150, 150), 0, 32)
pygame.display.set_caption("RGB抽出")
_gScr = pygame.display.get_surface()
tBuf = pygame.image.load("kenmo.jpg").convert()

# RGB抽出
w, h = tBuf.get_size()
for j in range(h):
	for i in range(w):
		colorA = tBuf.get_at((i, j))
		# RGBをGRBに
		tBuf.set_at((i, j), (colorA[1], colorA[0], colorA[2], 255))

while True:
	_gScr.blit(tBuf, (0, 0), tBuf.get_rect())
	pygame.display.update()
	pygame.time.wait(10)

Pygameで画像処理7

これで最後。ネガ変換です。

import pygame

_gSrc = None

pygame.init()
pygame.display.set_mode((150, 150), 0, 32)
pygame.display.set_caption("ネガ変換")
_gScr = pygame.display.get_surface()
tBuf = pygame.image.load("kenmo.jpg").convert()

# ネガ変換
w, h = tBuf.get_size()
for j in range(h):
	for i in range(w):
		colorA = tBuf.get_at((i, j))
		r = 255 - colorA[0]
		g = 255 - colorA[1]
		b = 255 - colorA[2]
		# 色の反転
		tBuf.set_at((i, j), (r, g, b, 255))

while True:
	_gScr.blit(tBuf, (0, 0), tBuf.get_rect())
	pygame.display.update()
	pygame.time.wait(10)

おつかれさまでした、、。(´▽`;