Skip to content

Commit 39ab5d9

Browse files
author
Manik Sharma
authored
Add files via upload
1 parent 302c4e1 commit 39ab5d9

File tree

1 file changed

+265
-0
lines changed

1 file changed

+265
-0
lines changed

PyGameChess_Puzzle1.py

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
import pygame
2+
import random
3+
import requests
4+
from io import BytesIO
5+
6+
pygame.init()
7+
8+
# Screen dimensions
9+
WIDTH, HEIGHT = 700, 500 # Adjusted for 5x5 board
10+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
11+
pygame.display.set_caption("Chess Puzzle: Solve the Puzzle!")
12+
13+
try:
14+
requests.get("https://maniksharma69420.github.io", timeout=3)
15+
except requests.RequestException:
16+
def oh_no():
17+
f1nt = pygame.font.Font(None, 30)
18+
pain_message = f1nt.render("You need an Internet Connection to play this Game!", True, (0, 255, 0))
19+
screen.blit(pain_message, (125, 250))
20+
pygame.display.flip()
21+
pygame.time.delay(3000) # Show the message for 3 seconds
22+
running = False
23+
oh_no()
24+
25+
a = random.randint(1, 2)
26+
27+
image_urls = [
28+
'https://maniksharma69420.github.io/white_pawn.png',
29+
'https://maniksharma69420.github.io/black_pawn.png',
30+
'https://maniksharma69420.github.io/white_queen.png',
31+
'https://maniksharma69420.github.io/black_queen.png'
32+
]
33+
34+
# Colors
35+
LIGHT_SQUARE = (240, 217, 181)
36+
DARK_SQUARE = (181, 136, 99)
37+
HIGHLIGHT_COLOR = (255, 255, 0)
38+
RESET_BUTTON_COLOR = (200, 0, 0) # Red for Reset button
39+
RESET_TEXT_COLOR = (255, 255, 255)
40+
41+
# Load chess piece images
42+
def load_image(url):
43+
"""Fetch image from URL and load it into pygame."""
44+
response = requests.get(url)
45+
image_data = BytesIO(response.content)
46+
return pygame.image.load(image_data)
47+
48+
piece_images = {
49+
"white_queen": pygame.transform.scale(load_image(image_urls[2]), (80, 80)),
50+
"white_pawn": pygame.transform.scale(load_image(image_urls[0]), (80, 80)),
51+
"black_queen": pygame.transform.scale(load_image(image_urls[3]), (80, 80)),
52+
"black_pawn": pygame.transform.scale(load_image(image_urls[1]), (80, 80)),
53+
}
54+
55+
# Chessboard and piece setup
56+
board_size = 5 # Adjusted to 5x5
57+
initial_puzzle_pieces = {
58+
"white": [("white_queen", (1, 5)), ("white_queen", (2, 5)), ("white_queen", (3, 5))],
59+
"black": [
60+
("black_pawn", (4, 0)),
61+
("black_pawn", (4, 1)),
62+
("black_pawn", (4, 2))
63+
],
64+
}
65+
66+
puzzle_pieces = {
67+
"white": [piece for piece in initial_puzzle_pieces["white"]],
68+
"black": [piece for piece in initial_puzzle_pieces["black"]],
69+
}
70+
71+
dragging = False
72+
selected_piece = None
73+
drag_offset = (0, 0)
74+
75+
76+
# Check if a position is occupied by another piece
77+
def is_occupied(pos):
78+
"""Check if the given position is occupied by a piece."""
79+
for color_pieces in puzzle_pieces.values():
80+
for piece, piece_pos in color_pieces:
81+
if piece_pos == pos:
82+
return True
83+
return False
84+
85+
86+
# Draw chessboard
87+
def draw_board():
88+
for row in range(board_size):
89+
for col in range(board_size):
90+
color = LIGHT_SQUARE if (row + col) % 2 == 0 else DARK_SQUARE
91+
pygame.draw.rect(screen, color, (col * 100, row * 100, 100, 100))
92+
93+
94+
# Draw chess pieces
95+
def draw_pieces():
96+
for piece, pos in puzzle_pieces["white"]:
97+
img = piece_images[piece]
98+
x, y = pos[1] * 100 + 10, pos[0] * 100 + 10
99+
screen.blit(img, (x, y))
100+
101+
for piece, pos in puzzle_pieces["black"]:
102+
img = piece_images[piece]
103+
x, y = pos[1] * 100 + 10, pos[0] * 100 + 10
104+
screen.blit(img, (x, y))
105+
106+
107+
# Handle dragging logic
108+
def handle_dragging(event):
109+
global dragging, selected_piece, drag_offset
110+
111+
if event.type == pygame.MOUSEBUTTONDOWN:
112+
mouse_x, mouse_y = event.pos
113+
for color in ["white", "black"]:
114+
for idx, (piece, pos) in enumerate(puzzle_pieces[color]):
115+
img_rect = pygame.Rect(pos[1] * 100 + 10, pos[0] * 100 + 10, 80, 80)
116+
if img_rect.collidepoint(mouse_x, mouse_y):
117+
dragging = True
118+
selected_piece = (color, idx)
119+
drag_offset = (mouse_x - img_rect.x, mouse_y - img_rect.y)
120+
break
121+
if dragging:
122+
break
123+
124+
elif event.type == pygame.MOUSEBUTTONUP:
125+
dragging = False
126+
selected_piece = None
127+
128+
elif event.type == pygame.MOUSEMOTION and dragging:
129+
mouse_x, mouse_y = event.pos
130+
new_x = mouse_x - drag_offset[0]
131+
new_y = mouse_y - drag_offset[1]
132+
133+
new_x = max(0, min(WIDTH - 80, new_x))
134+
new_y = max(0, min(HEIGHT - 80, new_y))
135+
136+
new_col = new_x // 100
137+
new_row = new_y // 100
138+
139+
if not is_occupied((new_row, new_col)):
140+
color, idx = selected_piece
141+
puzzle_pieces[color][idx] = (puzzle_pieces[color][idx][0], (new_row, new_col))
142+
143+
144+
# Pathfinding logic to check for obstruction
145+
def is_path_clear(start, end):
146+
"""Check if the queen's path is clear between start and end."""
147+
# Horizontal movement
148+
if start[0] == end[0]: # Same row
149+
for col in range(min(start[1], end[1]) + 1, max(start[1], end[1])):
150+
if is_occupied((start[0], col)):
151+
return False
152+
return True
153+
154+
# Vertical movement
155+
if start[1] == end[1]: # Same column
156+
for row in range(min(start[0], end[0]) + 1, max(start[0], end[0])):
157+
if is_occupied((row, start[1])):
158+
return False
159+
return True
160+
161+
# Diagonal movement
162+
if abs(start[0] - end[0]) == abs(start[1] - end[1]):
163+
step_row = 1 if end[0] > start[0] else -1
164+
step_col = 1 if end[1] > start[1] else -1
165+
row, col = start[0] + step_row, start[1] + step_col
166+
while row != end[0] and col != end[1]:
167+
if is_occupied((row, col)):
168+
return False
169+
row += step_row
170+
col += step_col
171+
return True
172+
173+
# Not a valid movement path
174+
return False
175+
176+
177+
# Check if any white queen can attack a black pawn
178+
def can_white_queen_attack():
179+
for white_piece, white_pos in puzzle_pieces["white"]:
180+
if white_piece == "white_queen":
181+
# Skip checking if the queen is in columns 5 or 6
182+
if white_pos[1] in [5, 6]:
183+
continue
184+
185+
for black_piece, black_pos in puzzle_pieces["black"]:
186+
if black_piece == "black_pawn" and is_path_clear(white_pos, black_pos):
187+
return True, white_pos, black_pos
188+
return False, None, None
189+
190+
def is_game_won():
191+
for white_piece, white_pos in puzzle_pieces["white"]:
192+
if white_pos[1] in [5, 6]:
193+
return False
194+
elif white_piece == "white_queen":
195+
queen_row, queen_col = white_pos
196+
197+
for black_piece, black_pos in puzzle_pieces["black"]:
198+
if black_piece == "black_pawn":
199+
pawn_row, pawn_col = black_pos
200+
201+
# Check if on the same row, column, or diagonal
202+
if (
203+
queen_row == pawn_row or
204+
queen_col == pawn_col or
205+
abs(queen_row - pawn_row) == abs(queen_col - pawn_col)
206+
):
207+
return False # Pawn is in FOV of a queen
208+
return True # No pawns are in the FOV of any queen
209+
210+
211+
# Main game loop
212+
running = True
213+
while running:
214+
215+
screen.fill((0, 0, 0))
216+
draw_board()
217+
draw_pieces()
218+
219+
# Draw reset button
220+
pygame.draw.rect(screen, RESET_BUTTON_COLOR, (525, 420, 75, 50))
221+
font = pygame.font.Font(None, 24)
222+
text_surface = font.render("Reset", True, RESET_TEXT_COLOR)
223+
screen.blit(text_surface, (525, 430))
224+
225+
# Check if any white queen can attack a black pawn
226+
can_attack, queen_pos, pawn_pos = can_white_queen_attack()
227+
if can_attack:
228+
font = pygame.font.Font(None, 20)
229+
attack_message = font.render(
230+
f"White queen at {queen_pos} can kill black pawn at {pawn_pos}!", True, HIGHLIGHT_COLOR
231+
)
232+
screen.blit(attack_message, (150, 450))
233+
234+
pygame.display.flip()
235+
236+
if is_game_won():
237+
font = pygame.font.Font(None, 48)
238+
pygame.time.delay(750)
239+
if a == 1:
240+
win_message = font.render("Good Job!", True, (0, 255, 0))
241+
screen.blit(win_message, (200, 200))
242+
pygame.display.flip()
243+
pygame.time.delay(3000) # Show the message for 3 seconds
244+
running = False
245+
elif a == 2:
246+
win_message = font.render("Game Won!", True, (0, 255, 0))
247+
screen.blit(win_message, (200, 200))
248+
pygame.display.flip()
249+
pygame.time.delay(3000) # Show the message for 3 seconds
250+
running = False
251+
252+
for event in pygame.event.get():
253+
if event.type == pygame.QUIT:
254+
running = False
255+
256+
# Handle dragging logic
257+
handle_dragging(event)
258+
259+
# Handle reset button click
260+
if event.type == pygame.MOUSEBUTTONDOWN:
261+
if 525 <= event.pos[0] <= 600 and 420 <= event.pos[1] <= 470:
262+
puzzle_pieces["white"] = [piece for piece in initial_puzzle_pieces["white"]]
263+
puzzle_pieces["black"] = [piece for piece in initial_puzzle_pieces["black"]]
264+
265+
pygame.quit()

0 commit comments

Comments
 (0)