面白いことを考えた。
ゲーマー殺しのテトリミノを作ってみたwww
いきなりテトリミノXが出現し、こんなのどうやって消せるんじゃい?!
という嫌がらせをしてみた。
改造箇所は[[1, 0 ,1], [0, 1, 0],[1, 0, 1]], #Xと、それに対応する色WHITEを追加しただけ。
ど真ん中に悪魔のテトリミノXがデーン!と鎮座www
ギョエエエ〜とゲーマー発狂www

import pygame
import random
import sys
# 初期化
pygame.init()
# 色の定義
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
ORANGE = (255, 165, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
PURPLE = (128, 0, 128)
RED = (255, 0, 0)
GRAY = (100, 100, 100)
# ゲーム設定
BLOCK_SIZE = 30
GRID_WIDTH = 10
GRID_HEIGHT = 20
BUTTON_AREA_HEIGHT = 100
SCREEN_WIDTH = BLOCK_SIZE * GRID_WIDTH
SCREEN_HEIGHT = BLOCK_SIZE * GRID_HEIGHT + BUTTON_AREA_HEIGHT
GAME_AREA_LEFT = 0
# テトリミノの形状
SHAPES = [
[[1, 1, 1, 1]], # I
[[1, 1], [1, 1]], # O
[[1, 1, 1], [0, 1, 0]], # T
[[1, 1, 1], [1, 0, 0]], # L
[[1, 1, 1], [0, 0, 1]], # J
[[0, 1, 1], [1, 1, 0]], # S
[[1, 1, 0], [0, 1, 1]], # Z
[[1, 0 ,1], [0, 1, 0],[1, 0, 1]], #X
]
# テトリミノの色
COLORS = [CYAN, YELLOW, PURPLE, ORANGE, BLUE, GREEN, RED, WHITE]
# ボタンの定義
class Button:
def __init__(self, x, y, width, height, text, color):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.color = color
self.text_color = WHITE
self.font = pygame.font.SysFont('comicsans', 20)
def draw(self, surface):
pygame.draw.rect(surface, self.color, self.rect)
pygame.draw.rect(surface, WHITE, self.rect, 2)
text_surface = self.font.render(self.text, True, self.text_color)
text_rect = text_surface.get_rect(center=self.rect.center)
surface.blit(text_surface, text_rect)
def is_clicked(self, pos):
return self.rect.collidepoint(pos)
# ゲーム画面の作成
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("テトリス")
clock = pygame.time.Clock()
class Tetrimino:
def __init__(self):
self.shape_idx = random.randint(0, len(SHAPES) - 1)
self.shape = SHAPES[self.shape_idx]
self.color = COLORS[self.shape_idx]
self.x = GRID_WIDTH // 2 - len(self.shape[0]) // 2
self.y = 0
def rotate(self):
rows = len(self.shape)
cols = len(self.shape[0])
rotated = [[0 for _ in range(rows)] for _ in range(cols)]
for r in range(rows):
for c in range(cols):
rotated[c][rows - 1 - r] = self.shape[r][c]
return rotated
def create_grid():
return [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
def draw_grid(grid):
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
rect = pygame.Rect(
GAME_AREA_LEFT + x * BLOCK_SIZE,
y * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE
)
if grid[y][x]:
pygame.draw.rect(screen, COLORS[grid[y][x] - 1], rect)
pygame.draw.rect(screen, WHITE, rect, 1)
def draw_tetrimino(tetrimino):
for y, row in enumerate(tetrimino.shape):
for x, cell in enumerate(row):
if cell:
rect = pygame.Rect(
GAME_AREA_LEFT + (tetrimino.x + x) * BLOCK_SIZE,
(tetrimino.y + y) * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE
)
pygame.draw.rect(screen, tetrimino.color, rect)
pygame.draw.rect(screen, WHITE, rect, 1)
def valid_space(tetrimino, grid):
for y, row in enumerate(tetrimino.shape):
for x, cell in enumerate(row):
if cell:
if (tetrimino.x + x < 0 or tetrimino.x + x >= GRID_WIDTH or
tetrimino.y + y >= GRID_HEIGHT or
(tetrimino.y + y >= 0 and grid[tetrimino.y + y][tetrimino.x + x])):
return False
return True
def check_lost(grid):
return any(cell for cell in grid[0])
def clear_rows(grid, score):
cleared_rows = 0
for y in range(GRID_HEIGHT):
if all(grid[y]):
cleared_rows += 1
for y2 in range(y, 0, -1):
grid[y2] = grid[y2-1][:]
grid[0] = [0 for _ in range(GRID_WIDTH)]
if cleared_rows == 1:
score += 100
elif cleared_rows == 2:
score += 300
elif cleared_rows == 3:
score += 500
elif cleared_rows == 4:
score += 800
return score
def draw_score(score):
font = pygame.font.SysFont('comicsans', 20)
label = font.render(f'Score: {score}', 1, WHITE)
screen.blit(label, (10, GRID_HEIGHT * BLOCK_SIZE + 10))
def main():
grid = create_grid()
current_tetrimino = Tetrimino()
next_tetrimino = Tetrimino()
change_tetrimino = False
run = True
score = 0
fall_time = 0
fall_speed = 0.5
# 操作ボタンの作成
button_width = 60
button_height = 50
button_y = GRID_HEIGHT * BLOCK_SIZE + 20
left_button = Button(20, button_y, button_width, button_height, "←", BLUE)
right_button = Button(100, button_y, button_width, button_height, "→", BLUE)
rotate_button = Button(180, button_y, button_width, button_height, "回転", GREEN)
down_button = Button(260, button_y, button_width, button_height, "↓", RED)
buttons = [left_button, right_button, rotate_button, down_button]
while run:
fall_time += clock.get_rawtime()
clock.tick()
if fall_time/1000 > fall_speed:
fall_time = 0
current_tetrimino.y += 1
if not valid_space(current_tetrimino, grid) and current_tetrimino.y > 0:
current_tetrimino.y -= 1
change_tetrimino = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if left_button.is_clicked(pos):
current_tetrimino.x -= 1
if not valid_space(current_tetrimino, grid):
current_tetrimino.x += 1
elif right_button.is_clicked(pos):
current_tetrimino.x += 1
if not valid_space(current_tetrimino, grid):
current_tetrimino.x -= 1
elif rotate_button.is_clicked(pos):
rotated_shape = current_tetrimino.rotate()
old_shape = current_tetrimino.shape
current_tetrimino.shape = rotated_shape
if not valid_space(current_tetrimino, grid):
current_tetrimino.shape = old_shape
elif down_button.is_clicked(pos):
current_tetrimino.y += 1
if not valid_space(current_tetrimino, grid):
current_tetrimino.y -= 1
if change_tetrimino:
for y, row in enumerate(current_tetrimino.shape):
for x, cell in enumerate(row):
if cell and current_tetrimino.y + y >= 0:
grid[current_tetrimino.y + y][current_tetrimino.x + x] = current_tetrimino.shape_idx + 1
current_tetrimino = next_tetrimino
next_tetrimino = Tetrimino()
change_tetrimino = False
score = clear_rows(grid, score)
if check_lost(grid):
font = pygame.font.SysFont('comicsans', 40)
label = font.render('Game Over!', 1, WHITE)
screen.blit(label, (SCREEN_WIDTH//2 - label.get_width()//2, SCREEN_HEIGHT//2 - label.get_height()//2))
pygame.display.update()
pygame.time.delay(2000)
run = False
screen.fill(BLACK)
draw_grid(grid)
draw_tetrimino(current_tetrimino)
draw_score(score)
# ボタンを描画
for button in buttons:
button.draw(screen)
pygame.display.update()
# ゲームオーバー後の処理
font = pygame.font.SysFont('comicsans', 20)
restart_button = Button(SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2 + 30, 200, 50, "再プレイ", GREEN)
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting = False
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if restart_button.is_clicked(pos):
waiting = False
main()
screen.fill(BLACK)
font_large = pygame.font.SysFont('comicsans', 40)
game_over_text = font_large.render("Game Over!", True, WHITE)
screen.blit(game_over_text, (SCREEN_WIDTH//2 - game_over_text.get_width()//2, SCREEN_HEIGHT//2 - 50))
score_text = font.render(f"最終スコア: {score}", True, WHITE)
screen.blit(score_text, (SCREEN_WIDTH//2 - score_text.get_width()//2, SCREEN_HEIGHT//2))
restart_button.draw(screen)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
追伸
[[0, 1 ,0], [1, 0, 1],[0, 1, 0]], #◆ 色BLACK
なんかかましたら修羅場になるだろうwww