Skip to content

Commit

Permalink
Now clicked tiles will show a green square, but only work with one at…
Browse files Browse the repository at this point in the history
… a time
  • Loading branch information
RickMMA committed Apr 10, 2024
1 parent 3753cf3 commit 1fb0c0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
6 changes: 3 additions & 3 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def __init__(self, piece):
self.rect = pygame.Rect((self.pos[0], self.pos[1]),
(self._image.get_size()[0], self._image.get_size()[1]))
else:
self._image = pygame.image.load(
os.path.join(os.getcwd(), "assets", "chess_pieces", f"empty.png")).convert_alpha()
# Insert rect when there is no piece
self.rect = pygame.Rect((self.pos[0], self.pos[1]),
(self._default_image_size[0], self._default_image_size[1]))
Expand All @@ -107,7 +109,7 @@ def __init__(self, piece):

def render(self, surface):
if self.pieceName != "--":
surface.blit(self.image, (self.pos[0], self.pos[1]))
surface.blit(self._image, (self.pos[0], self.pos[1]))
else:
pass

Expand All @@ -123,5 +125,3 @@ def clicked(self):
t = (True, (self.pieceName, self.pos))
return t



37 changes: 25 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# For draw.rect has alpha value
SELECT_RED = (200, 10, 20, 50)
SELECT_GREEN = (0, 200, 20, 10)

# Screen information
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
Expand All @@ -35,15 +39,14 @@
surf = pygame.Surface((rec.width, rec.height))
surf.fill((255, 0, 0))





select_rect = pygame.Rect((0, 0), (64, 64))
show_rect = False



def on_event(event):
global gameState, mouse_pos, pressed_keys, pressed_mouse
global show_rect, select_rect

pressed_keys = pygame.key.get_pressed()
pressed_mouse = pygame.mouse.get_pressed()
Expand All @@ -54,9 +57,17 @@ def on_event(event):
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for k, c in board.current_board.items():
click = c.clicked()
if type(click) == tuple:
print(k, click[1][0], click[1][1])
click = c.clicked()
if type(click) == tuple:
select_rect.x = click[1][1][0]
select_rect.y = click[1][1][1]
print(k, click[1][0], click[1][1], board.current_board[k])
if show_rect == False:
show_rect = True
else:
show_rect = False


elif event.type == pygame.MOUSEMOTION:
pass
elif event.type == pygame.KEYDOWN:
Expand All @@ -73,6 +84,8 @@ def on_event(event):
pygame.event.pump()






if __name__ == "__main__":
Expand All @@ -97,10 +110,6 @@ def on_event(event):

while True:

for event in pygame.event.get():
on_event(event)


if gameState == False: # When the game is paused, i.e.: in the start menu
DISPLAYSURFACE.blit(img, (0, 0))
btn1.render()
Expand All @@ -112,11 +121,15 @@ def on_event(event):
elif gameState == True:
DISPLAYSURFACE.fill((200, 200, 200))
board.render(DISPLAYSURFACE)
if show_rect:
pygame.draw.rect(DISPLAYSURFACE, SELECT_GREEN, select_rect)
for keys, content in board.starting_board.items():
board.current_board[keys].render(DISPLAYSURFACE)
_rec = board.current_board['A1'].rect



for event in pygame.event.get():
on_event(event)


rec.x = mouse_pos[0] - 10
Expand Down

0 comments on commit 1fb0c0b

Please sign in to comment.