Kode Python Membuat Petak Catur 8x8
KODE PYTHON MEMBUAT PETAK CATUR 8x8
1. Papan 9x9 Petak
import pygame
import sys
import time
# Inisialisasi pygame
pygame.init()
# Ukuran papan catur dan layar
BOARD_SIZE = 720 # Ukuran papan catur 9x9
TILE_SIZE = BOARD_SIZE // 8
SIDE_PANEL_WIDTH = 200 # Lebar area untuk timer
SCREEN_WIDTH = BOARD_SIZE + SIDE_PANEL_WIDTH
SCREEN_HEIGHT = BOARD_SIZE
# Buat layar lebih lebar agar timer muat
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Chess Nine Game")
# Warna
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
LIGHT_BROWN = (240, 217, 181)
DARK_BROWN = (181, 136, 99)
TEXT_COLOR = (255, 255, 0) # Warna kuning agar jelas di latar hitam
# Timer (5 menit per pemain)
START_TIME = 300
timers = {"white": START_TIME, "black": START_TIME}
# Font untuk timer
font = pygame.font.Font(None, 70) # Ukuran font diperbesar
# Fungsi menggambar papan catur
def draw_board():
for y in range(8):
for x in range(8):
color = LIGHT_BROWN if (x + y) % 2 == 0 else DARK_BROWN
pygame.draw.rect(SCREEN, color, (x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
# Fungsi menggambar timer
def draw_timer(turn):
# Panel hitam di sebelah kanan
pygame.draw.rect(SCREEN, BLACK, (BOARD_SIZE, 0, SIDE_PANEL_WIDTH, SCREEN_HEIGHT))
# Tampilkan teks timer dengan warna kontras
white_time = font.render(f"{timers['white']}s", True, TEXT_COLOR)
black_time = font.render(f"{timers['black']}s", True, TEXT_COLOR)
# Label pemain
white_label = font.render("WHITE", True, TEXT_COLOR)
black_label = font.render("BLACK", True, TEXT_COLOR)
# Gambar teks timer dan label pemain
SCREEN.blit(white_label, (BOARD_SIZE + 40, 150))
SCREEN.blit(white_time, (BOARD_SIZE + 50, 200))
SCREEN.blit(black_label, (BOARD_SIZE + 40, 350))
SCREEN.blit(black_time, (BOARD_SIZE + 50, 400))
# Garis pemisah
pygame.draw.line(SCREEN, TEXT_COLOR, (BOARD_SIZE, SCREEN_HEIGHT // 2), (SCREEN_WIDTH, SCREEN_HEIGHT // 2), 5)
# Fungsi utama permainan
def main():
clock = pygame.time.Clock()
turn = "white"
last_update = time.time()
running = True
while running:
SCREEN.fill(WHITE) # Bersihkan layar
draw_board() # Gambar papan catur
draw_timer(turn) # Gambar timer
# Update timer setiap detik
current_time = time.time()
if current_time - last_update >= 1:
timers[turn] -= 1
last_update = current_time
if timers[turn] <= 0:
print(f"{turn.capitalize()} kehabisan waktu! Permainan selesai.")
running = False
# Event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
clock.tick(60)
# Jalankan permainan
if __name__ == "__main__":
main()
2. Posisi Bidak Pion dan Garuda
import pygame
# Konstanta permainan
WIDTH, HEIGHT = 720, 720
SQUARE_SIZE = WIDTH // 9 # Ukuran kotak papan 9x9
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
# Inisialisasi pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Catur Nine")
# Kelas untuk Bidak Catur
class Piece:
def __init__(self, color, row, col):
self.color = color
self.row = row
self.col = col
self.selected = False
def draw(self, screen):
pass # Akan diimplementasikan di subclass
class Pawn(Piece):
def draw(self, screen):
color = BLACK if self.color == "black" else WHITE
pygame.draw.circle(screen, color, (self.col * SQUARE_SIZE + SQUARE_SIZE//2,
self.row * SQUARE_SIZE + SQUARE_SIZE//2), 20)
def get_valid_moves(self, board):
moves = []
direction = -1 if self.color == "white" else 1
# Gerakan normal pion
if board[self.row + direction][self.col] is None:
moves.append((self.row + direction, self.col))
# Menangkap secara diagonal
if self.col > 0 and board[self.row + direction][self.col - 1] is not None:
moves.append((self.row + direction, self.col - 1))
if self.col < 8 and board[self.row + direction][self.col + 1] is not None:
moves.append((self.row + direction, self.col + 1))
# Aturan F2P: Pion di kolom e tidak bisa melewati e5
if self.col == 4 and self.row == 4:
moves = []
return moves
class Garuda(Piece):
def draw(self, screen):
color = (255, 0, 0) if self.color == "black" else (0, 255, 0)
pygame.draw.rect(screen, color,
(self.col * SQUARE_SIZE + 10, self.row * SQUARE_SIZE + 10,
SQUARE_SIZE - 20, SQUARE_SIZE - 20))
def get_valid_moves(self, board):
moves = []
for dr, dc in [(2, 0), (-2, 0), (0, 2), (0, -2)]:
new_row, new_col = self.row + dr, self.col + dc
if 0 <= new_row < 9 and 0 <= new_col < 9:
moves.append((new_row, new_col))
return moves
# Membuat papan permainan
def create_board():
board = [[None for _ in range(9)] for _ in range(9)]
# Menempatkan pion
for i in range(9):
board[1][i] = Pawn("black", 1, i)
board[7][i] = Pawn("white", 7, i)
# Menempatkan Garuda
board[0][6] = Garuda("black", 0, 6)
board[8][6] = Garuda("white", 8, 6)
return board
def draw_board(screen, board):
for row in range(9):
for col in range(9):
color = GRAY if (row + col) % 2 == 0 else WHITE
pygame.draw.rect(screen, color,
(col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
piece = board[row][col]
if piece:
piece.draw(screen)
# Game Loop
def main():
board = create_board()
running = True
while running:
screen.fill((0, 0, 0))
draw_board(screen, board)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
Comments
Post a Comment