tetris oyunu yap
- User
tetris oyunu yap
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
tetris oyunu yap
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
İşte basit bir Tetris oyunu Python kodu (Pygame kütüphanesi ile):
import pygame
import random
# Renkler
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
COLORS = [
(0, 255, 255), # Cyan - I
(0, 0, 255), # Blue - J
(255, 165, 0), # Orange - L
(255, 255, 0), # Yellow - O
(0, 255, 0), # Green - S
(128, 0, 128), # Purple - T
(255, 0, 0) # Red - Z
]
# Tetromino şekilleri
SHAPES = [
[[1, 1, 1, 1]], # I
[[1, 0, 0], [1, 1, 1]], # J
[[0, 0, 1], [1, 1, 1]], # L
[[1, 1], [1, 1]], # O
[[0, 1, 1], [1, 1, 0]], # S
[[0, 1, 0], [1, 1, 1]], # T
[[1, 1, 0], [0, 1, 1]] # Z
]
# Oyun ayarları
CELL_SIZE = 30
GRID_WIDTH = 10
GRID_HEIGHT = 20
SCREEN_WIDTH = CELL_SIZE * (GRID_WIDTH + 8)
SCREEN_HEIGHT = CELL_SIZE * GRID_HEIGHT
class Tetromino:
def __init__(self, x, y):
self.x = x
self.y = y
self.shape_idx = random.randint(0, len(SHAPES) - 1)
self.color = COLORS[self.shape_idx]
self.shape = SHAPES[self.shape_idx]
self.rotation = 0
def rotate(self):
# Matris döndürme
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
class TetrisGame:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Tetris")
self.clock = pygame.time.Clock()
self.font = pygame.font.SysFont(None, 36)
self.reset_game()
def reset_game(self):
self.grid = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
self.current_piece = self.new_piece()
self.next_piece = self.new_piece()
self.game_over = False
self.score = 0
self.level = 1
self.fall_speed = 0.5 # Saniyede kare sayısı
self.fall_time = 0
def new_piece(self):
return Tetromino(GRID_WIDTH // 2 - 1, 0)
def valid_position(self, shape, x, y):
for r, row in enumerate(shape):
for c, cell in enumerate(row):
if cell:
if (y + r >= GRID_HEIGHT or
x + c < 0 or
x + c >= GRID_WIDTH or
self.grid[y + r][x + c]):
return False
return True
def merge_piece(self):
for r, row in enumerate(self.current_piece.shape):
for c, cell in enumerate(row):
if cell:
self.grid[self.current_piece.y + r][self.current_piece.x + c] = self.current_piece.color
def clear_lines(self):
lines_cleared = 0
for r in range(GRID_HEIGHT):
if all(self.grid[r]):
lines_cleared += 1
# Satırı sil ve yukarı kaydır
for y in range(r, 0, -1):
self.grid[y] = self.grid[y-1][:]
self.grid[0] = [0] * GRID_WIDTH
# Puanlama
if lines_cleared == 1:
self.score += 100
elif lines_cleared == 2:
self.score += 300
elif lines_cleared == 3:
self.score += 500
elif lines_cleared == 4:
self.score += 800
def draw_grid(self):
for y, row in enumerate(self.grid):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(
self.screen,
cell,
(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
)
pygame.draw.rect(
self.screen,
WHITE,
(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE),
1
)
def draw_piece(self, piece, offset_x=0, offset_y=0):
for r, row in enumerate(piece.shape):
for c, cell in enumerate(row):
if cell:
pygame.draw.rect(
self.screen,
piece.color,
((piece.x + c + offset_x) * CELL_SIZE,
(piece.y + r + offset_y) * CELL_SIZE,
CELL_SIZE, CELL_SIZE)
)
pygame.draw.rect(
self.screen,
WHITE,
((piece.x + c + offset_x) * CELL_SIZE,
(piece.y + r + offset_y) * CELL_SIZE,
CELL_SIZE, CELL_SIZE),
1
)
def draw_next_piece(self):
next_x = GRID_WIDTH + 2
next_y = 5
text = self.font.render("Sıradaki:", True, WHITE)
self.screen.blit(text, (next_x * CELL_SIZE, (next_y - 2) * CELL_SIZE))
for r, row in enumerate(self.next_piece.shape):
for c, cell in enumerate(row):
if cell:
pygame.draw.rect(
self.screen,
self.next_piece.color,
((next_x + c) * CELL_SIZE,
(next_y + r) * CELL_SIZE,
CELL_SIZE, CELL_SIZE)
)
pygame.draw.rect(
self.screen,
WHITE,
((next_x + c) * CELL_SIZE,
(next_y + r) * CELL_SIZE,
CELL_SIZE, CELL_SIZE),
1
)
def draw_score(self):
score_text = self.font.render(f"Puan: {self.score}", True, WHITE)
level_text = self.font.render(f"Seviye: {self.level}", True, WHITE)
self.screen.blit(score_text, ((GRID_WIDTH + 2) * CELL_SIZE, CELL_SIZE * 10))
self.screen.blit(level_text, ((GRID_WIDTH + 2) * CELL_SIZE, CELL_SIZE * 12))
def draw_game_over(self):
text = self.font.render("OYUN BİTTİ!", True, (255, 0, 0))
restart = self.font.render("Yeniden başlamak için R", True, WHITE)
self.screen.blit(text, (CELL_SIZE * 2, CELL_SIZE * 8))
self.screen.blit(restart, (CELL_SIZE * 1, CELL_SIZE * 10))
def run(self):
running = True
last_time = pygame.time.get_ticks()
while running:
current_time = pygame.time.get_ticks()
delta_time = (current_time - last_time) / 1000.0
last_time = current_time
# Otomatik düşme
if not self.game_over:
self.fall_time += delta_time
if self.fall_time >= self.fall_speed:
self.fall_time = 0
self.current_piece.y += 1
if not self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y):
self.current_piece.y -= 1
self.merge_piece()
self.clear_lines()
self.current_piece = self.next_piece
self.next_piece = self.new_piece()
if not self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y):
self.game_over = True
# Olay işleme
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if self.game_over and event.key == pygame.K_r:
self.reset_game()
if not self.game_over:
if event.key == pygame.K_LEFT:
self.current_piece.x -= 1
if not self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y):
self.current_piece.x += 1
elif event.key == pygame.K_RIGHT:
self.current_piece.x += 1
if not self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y):
self.current_piece.x -= 1
elif event.key == pygame.K_DOWN:
self.current_piece.y += 1
if not self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y):
self.current_piece.y -= 1
elif event.key == pygame.K_UP:
rotated = self.current_piece.rotate()
if self.valid_position(rotated, self.current_piece.x, self.current_piece.y):
self.current_piece.shape = rotated
elif event.key == pygame.K_SPACE:
# Hızlı düşürme
while self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y + 1):
self.current_piece.y += 1
self.merge_piece()
self.clear_lines()
self.current_piece = self.next_piece
self.next_piece = self.new_piece()
if not self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y):
self.game_over = True
# Çizim
self.screen.fill(BLACK)
# Izgara çizgileri
for x in range(GRID_WIDTH):
for y in range(GRID_HEIGHT):
pygame.draw.rect(
self.screen,
GRAY,
(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE),
1
)
self.draw_grid()
if not self.game_over:
self.draw_piece(self.current_piece)
self.draw_next_piece()
self.draw_score()
if self.game_over:
self.draw_game_over()
pygame.display.flip()
self.clock.tick(60)
pygame.quit()
if __name__ == "__main__":
game = TetrisGame()
game.run()
pip install pygame
tetris.py gibi bir dosyaya kaydedinpython tetris.py
Bu kod temel bir Tetris oyunu sağlar. Daha fazla özellik eklemek için (seviye artışı, hızlanma, animasyonlar vb.) kodu genişletebilirsiniz.