pygame は2次元ゲームのフレームワーク.Python で動く. このWebページでは,pygame の次の機能を試してみる.
ここではPythonを使う.
※ Python プログラムを動かすために, Windows では,「python」コマンドを使う. Ubuntu では「python3」コマンドを使う.
開発環境や Python コンソール(Jupyter Qt Console,spyder,PyCharm,PyScripter など)も便利である.
import os, sys
import pygame
from pygame.locals import *
class PyManMain:
def __init__(self, width=640,height=480):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width, self.height))
def MainLoop(self):
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
MainWindow = PyManMain()
MainWindow.MainLoop()
■ 演習問題: spyder を起動し、上の手順を行いなさい.
※ Python プログラムを動かすために, Windows では,「python」コマンドを使う. Ubuntu では「python3」コマンドを使う.
開発環境や Python コンソール(Jupyter Qt Console,spyder,PyCharm,PyScripter など)も便利である.
import os, sys
import pygame
from pygame.locals import *
class PyManMain:
def __init__(self, width=640,height=480):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width, self.height))
def MainLoop(self):
pygame.draw.line(self.screen, (0,95,0), (0,0), (80,80), 5)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
MainWindow = PyManMain()
MainWindow.MainLoop()
■ 演習問題: 「(0,95,0)」を書き換えて、色を変えてみなさい. 実行ボタンを押して確認すること
■ 演習問題: 「(0,0), (80,80)」を書き換えて、形を変えてみなさい.実行ボタンを押して確認すること
※ Python プログラムを動かすために, Windows では,「python」コマンドを使う. Ubuntu では「python3」コマンドを使う.
開発環境や Python コンソール(Jupyter Qt Console,spyder,PyCharm,PyScripter など)も便利である.
import os, sys
import pygame
from pygame.locals import *
class PyManMain:
def __init__(self, width=640,height=480):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width, self.height))
def MainLoop(self):
pygame.draw.rect(self.screen,(0,80,0),Rect(10,10,80,50),5)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
MainWindow = PyManMain()
MainWindow.MainLoop()
■ 演習問題: 「(0,80,0)」を書き換えて、色を変えてみなさい.実行ボタンを押して確認すること
■ 演習問題: 「(10,10,80,50)」を書き換えて、形を変えてみなさい.実行ボタンを押して確認すること
エディタの画面にコピー&ペーストして,実行ボタンをクリック.
import os, sys
import pygame
from pygame.locals import *
class PyManMain:
def __init__(self, width=640,height=480):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width, self.height))
def MainLoop(self):
pygame.draw.ellipse(self.screen,(0,100,0),(50,50,200,100))
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
MainWindow = PyManMain()
MainWindow.MainLoop()
■ 演習問題: 「(0,100,0)」を書き換えて、色を変えてみなさい.実行ボタンを押して確認すること
■ 演習問題: 「(50,50,200,100)」を書き換えて、形を変えてみなさい.実行ボタンを押して確認すること
エディタの画面にコピー&ペーストして,実行ボタンをクリック.
import os, sys
import pygame
from pygame.locals import *
class PyManMain:
def __init__(self, width=640,height=480):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width, self.height))
def MainLoop(self):
font = pygame.font.Font(None, 55)
text = font.render("hello, world", True, (255,255,255))
self.screen.blit(text, [20, 100])
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
MainWindow = PyManMain()
MainWindow.MainLoop()
■ 演習問題: 「(255,255,255)」を書き換えて、色を変えてみなさい.実行ボタンを押して確認すること
■ 演習問題: 「hello,world」を書き換えて、表示されるテキストを変えてみなさい.実行ボタンを押して確認すること
エディタの画面にコピー&ペーストして,実行ボタンをクリック.
import os, sys
import pygame
from pygame.locals import *
class PyManMain:
def __init__(self, width=640,height=480):
pygame.init()
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width, self.height))
def MainLoop(self):
x = 100
y = 200
while 1:
pygame.display.update()
pygame.time.wait(30)
pygame.draw.circle(self.screen, (0, 200, 0), (x, y), 5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT:
x = x - 5
if event.key == K_RIGHT:
x = x + 5
if event.key == K_UP:
y = y - 5
if event.key == K_DOWN:
y = y + 5
MainWindow = PyManMain()
MainWindow.MainLoop()
■ 演習問題: 上の手順を行いなさい.
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ) ![]()