o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
こんにちは。テトリスを実現するソースコードを書いて下さい
- User
こんにちは。テトリスを実現するソースコードを書いて下さい
- Assistant
こんにちは。テトリスを実現するソースコードを書いて下さい
こんにちは。テトリスを実現するための簡単なPythonコードの例を提供します。このコードはPygameライブラリを使用していますので、事前にインストールしておいてください。
import pygame
import sys
import random
# 定数の定義
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 600
BLOCK_SIZE = 30
COLUMNS = SCREEN_WIDTH // BLOCK_SIZE
ROWS = SCREEN_HEIGHT // BLOCK_SIZE
# 色の定義
colors = [
(0, 0, 0 ), # 背景色
(255, 0, 0 ), # 赤
(0, 255, 0 ), # 緑
(0, 0, 255), # 青
(255, 255, 0 ), # 黄
(255, 165, 0 ), # オレンジ
(128, 0, 128), # 紫
(0, 255, 255) # シアン
]
# テトリミノの形状
tetriminoes = [
[[1, 1, 1],
[0, 1, 0]],
[[0, 2, 2],
[2, 2, 0]],
[[3, 3, 0],
[0, 3, 3]],
[[4, 0, 0],
[4, 4, 4]],
[[0, 0, 5],
[5, 5, 5]],
[[6, 6, 6, 6]],
[[7, 7],
[7, 7]]
]
class Tetrimino:
def __init__(self, x, y):
self.type = random.randint(0, len(tetriminoes) -1)
self.color = self.type + 1
self.rotation = 0
self.x = x
self.y = y
def image(self):
return tetriminoes[self.type][self.rotation % len(tetriminoes[self.type])]
def rotate(self):
self.rotation +=1
def check_collision(grid, shape, offset):
off_x, off_y = offset
for cy, row in enumerate(shape):
for cx, cell in enumerate(row):
try:
if cell and grid[cy + off_y][cx + off_x]:
return True
except IndexError:
return True
return False
def remove_row(grid, row):
del grid[row]
grid.insert(0, [0 for _ in range(COLUMNS)])
def join_matrices(mat1, mat2, mat2_off):
off_x, off_y = mat2_off
for y, row in enumerate(mat2):
for x, val in enumerate(row):
mat1[y + off_y -1][x + off_x] += val
return mat1
def new_board():
board = [[0 for _ in range(COLUMNS)] for _ in range(ROWS)]
return board
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("シンプルなテトリス")
clock = pygame.time.Clock()
grid = new_board()
tetrimino = Tetrimino(COLUMNS // 2, 0)
fall_time = 0
fall_speed = 0.5
while True:
grid_temp = [row[:] for row in grid]
fall_time += clock.get_rawtime()
clock.tick()
if fall_time / 1000 > fall_speed:
fall_time = 0
tetrimino.y +=1
if check_collision(grid, tetrimino.image(), (tetrimino.x, tetrimino.y)):
tetrimino.y -=1
grid = join_matrices(grid, tetrimino.image(), (tetrimino.x, tetrimino.y))
tetrimino = Tetrimino(COLUMNS // 2, 0)
if check_collision(grid, tetrimino.image(), (tetrimino.x, tetrimino.y)):
print("ゲームオーバー")
pygame.quit()
sys.exit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
tetrimino.x -=1
if check_collision(grid, tetrimino.image(), (tetrimino.x, tetrimino.y)):
tetrimino.x +=1
elif event.key == pygame.K_RIGHT:
tetrimino.x +=1
if check_collision(grid, tetrimino.image(), (tetrimino.x, tetrimino.y)):
tetrimino.x -=1
elif event.key == pygame.K_DOWN:
tetrimino.y +=1
if check_collision(grid, tetrimino.image(), (tetrimino.x, tetrimino.y)):
tetrimino.y -=1
elif event.key == pygame.K_UP:
tetrimino.rotate()
if check_collision(grid, tetrimino.image(), (tetrimino.x, tetrimino.y)):
tetrimino.rotate()
tetrimino.rotate()
tetrimino.rotate()
screen.fill(colors[0])
# グリッドの描画
for y in range(ROWS):
for x in range(COLUMNS):
if grid[y][x]:
pygame.draw.rect(screen, colors[grid[y][x]],
(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
# テトリミノの描画
for y, row in enumerate(tetrimino.image()):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, colors[tetrimino.color],
((tetrimino.x + x) * BLOCK_SIZE, (tetrimino.y + y) * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE), 0)
# ライン消去のチェック
for i, row in enumerate(grid):
if 0 not in row:
remove_row(grid, i)
pygame.display.update()
if __name__ == '__main__':
main()
注意事項:
必要なライブラリのインストール: このコードを実行するには、PythonとPygameがインストールされている必要があります。Pygameは以下のコマンドでインストールできます。
pip install pygame
基本的な実装: 提供したコードはテトリスの基本的な機能を実現した簡易的なものです。スコアの計算やレベルの概念、ゲームオーバー時の処理など、完全なゲームとするためには追加の実装が必要です。
学習目的: このコードを参考にして、機能を拡張したり、自分なりにカスタマイズしてみてください。ゲーム開発の良い練習になります。
何か質問や不明な点があれば、お気軽にお知らせください。
Not yet included in a published release.