From 6703438e1449f59427f7f1223c7c19148a3b428c Mon Sep 17 00:00:00 2001 From: dgdzd Date: Tue, 8 Oct 2024 12:52:12 +0200 Subject: [PATCH 1/7] Refactored some code --- gui/__pycache__/game.cpython-39.pyc | Bin 0 -> 877 bytes gui/__pycache__/tchess.cpython-39.pyc | Bin 0 -> 1512 bytes gui/game.py | 32 +++++++++++++++++--------- gui/tchess.py | 26 ++++++++------------- {ressources => resources}/black_1.png | Bin {ressources => resources}/black_2.png | Bin {ressources => resources}/black_3.png | Bin {ressources => resources}/black_4.png | Bin {ressources => resources}/black_5.png | Bin {ressources => resources}/black_6.png | Bin {ressources => resources}/idle | 0 {ressources => resources}/white_1.png | Bin {ressources => resources}/white_2.png | Bin {ressources => resources}/white_3.png | Bin {ressources => resources}/white_4.png | Bin {ressources => resources}/white_5.png | Bin {ressources => resources}/white_6.png | Bin 17 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 gui/__pycache__/game.cpython-39.pyc create mode 100644 gui/__pycache__/tchess.cpython-39.pyc rename {ressources => resources}/black_1.png (100%) rename {ressources => resources}/black_2.png (100%) rename {ressources => resources}/black_3.png (100%) rename {ressources => resources}/black_4.png (100%) rename {ressources => resources}/black_5.png (100%) rename {ressources => resources}/black_6.png (100%) rename {ressources => resources}/idle (100%) rename {ressources => resources}/white_1.png (100%) rename {ressources => resources}/white_2.png (100%) rename {ressources => resources}/white_3.png (100%) rename {ressources => resources}/white_4.png (100%) rename {ressources => resources}/white_5.png (100%) rename {ressources => resources}/white_6.png (100%) diff --git a/gui/__pycache__/game.cpython-39.pyc b/gui/__pycache__/game.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ee2e831a77ca0fe74002b04c891b6cca88c88de1 GIT binary patch literal 877 zcmZuv!EVz)5Zzs`9mj3jB1#Y3ddww)D~h5vrAnh9w~(SrtDu$1F0LadcGr%eQ8~3g zz!v~Verd0qa^}VbW*h~m#9H$_vmVczH@i`z5fE6N4LANw3Hgq}zsrH(0IcW&D57Xe z1g+joFvWzUh~UZ*uHwQ|uBa(b_^KxA$`?V8sJaU7hzf3**idvxnj1@~L&D__;1USQ zk|7|?k|VeX9>5sv-onHKuwoZLS8YHGRIxh(Z8%VzTi%PNdW?0~3w<+ce#ZkLqar1r&E-dCDnPF)e;sf7uFZtn45cc@~B zJg6>)N^ks+Fsx7Pio;*jU@zZDlUde!VrgP<^cDQvzkD{zO*YYGF*?^~njBr~q8P>V zWE6vbYNy#aN{bFS>)ho5zZg5YvEQq*1b6CpC4O(c6bq5KIn%%%ktQ| z$ux?!GgQQIGD@EobM~qH#|{2&V|? zGteca4WyiAYM!FMDdp8XN>^XZW0du0fGZEVOdVhgY4AjRTE)#x>*d!tNwWvLmSu%` z2##>OFAk3e=27Lpe0_Wz3a{HAo}8WuZxHr-C+C8{Iz1g2ymM8l|8ZKsz3Q@qCtTf6 MpZbhbj<$Z-Z$$&VvH$=8 literal 0 HcmV?d00001 diff --git a/gui/__pycache__/tchess.cpython-39.pyc b/gui/__pycache__/tchess.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..331fa9831c1287b27b1d77738dec660eef93a796 GIT binary patch literal 1512 zcmZux%WfMt6eWk8hn}_-*J+RfRnelzY`n;#D2k#;YsZg`k;ZBZ3=|BeLs}VmG#Zhk z)H0-%@(KNb(X!q2Q@ZJDRt37!qCe18@1+vQE;<7q-pj`&c@8h5R;y0n`?F>be&>Yz zh0g7df%624{uDqEK~oY?Q(GezFiMVzumpQe1Y24G7xbL?d<|pCS&Vr5v%q-*MSlVy zO`O0IR9LS`zyuQbB6(H)>pV^3s7S_HOkvvF4{hAr0Tlf`fS{brNl5w@ zAcbNTY{*M{PKIQKz5~8ndMg}ND_B`y5ej<;uOS#wE9` z-G4j!4$&x@3&jxqGZ;u{Nd;X|N(yVpO1>h8*2U4Lkp>3%aVy03Ki8E7L_7b)#_yK^XOoaXN-JY>Kxk~CFFS7pmr zG6QV}vHaC^DqD@GnNT$x<6?-5TnBJT;!D+lHs4ZPP^n!6)NTViU{%gE7f~UxU%wAP zK+-l86tzt~%VVBZr(Jrzb4%XMoIB#;{_s3T+zT)ST|hoAAS;GsNCkUH7Oa5$UQ&?w zaKUfv&ldJgemQe%&gb@hBKRJ8Kp?kV@Yybz+XmU3t@sMwipitN2XC^f=UYBo{|!s7 z|3LT+02+Qpdx6kL@DT6@`krb-71VwlM@pP0*AnQ9vVk;O!KS%)qEAz_ zn_)PaM(L(Ubs&;@h=9>=t!VT_DHWa$9$unR3y*qruVdPLJ8<$DJ^-5|dXU=t#zi8` WkpGKUd0%e!@)cfdjgdXtwEhLKid9?y literal 0 HcmV?d00001 diff --git a/gui/game.py b/gui/game.py index 87ba136..d4b8986 100644 --- a/gui/game.py +++ b/gui/game.py @@ -1,6 +1,5 @@ import pygame as pg import os -import numpy as np WHITE = 16 BLACK = 0 @@ -14,16 +13,27 @@ class Game(): def __init__(self): - self.board = np.ndarray(shape=(8, 8), dtype= int) - self.board[0][0] = WHITE | PAWN + self.boardSize = 512 + self.board = [0 for i in range(64)] + self.board[0] = WHITE | PAWN + + self.pieces_tex = { + 0: None, + + WHITE | KING: pg.image.load("resources\\white_1.png"), + WHITE | QUEEN: pg.image.load("resources\\white_2.png"), + WHITE | BISHOP: pg.image.load("resources\\white_3.png"), + WHITE | KNIGHT: pg.image.load("resources\\white_4.png"), + WHITE | ROOK: pg.image.load("resources\\white_5.png"), + WHITE | PAWN: pg.image.load("resources\\white_6.png"), + + BLACK | KING: pg.image.load("resources\\black_1.png"), + BLACK | QUEEN: pg.image.load("resources\\black_2.png"), + BLACK | BISHOP: pg.image.load("resources\\black_3.png"), + BLACK | KNIGHT: pg.image.load("resources\\black_4.png"), + BLACK | ROOK: pg.image.load("resources\\black_5.png"), + BLACK | PAWN: pg.image.load("resources\\black_6.png"), + } -class Pieces(): - def __init__(self): - self.board = Game() - self.pieces_img, index = {}, 0b0000 - for i in ['white_1', 'black_1', 'white_2', 'black_2', 'white_3', 'black_3', 'white_4', 'black_4', 'white_5', 'black_5', 'white_6', 'black_6' ]: - index = bin(int(index)-16) if 'white' in i else bin(int(index)-16) - self.pieces_img[index] = pg.image.load('\\ressources\\' + i + '.png') - self.pieces = [self.pieces.append([i, j, self.board.board[i][j]]) for i in len(8) for j in range(8) if not self.board.board[i][j] == 0] diff --git a/gui/tchess.py b/gui/tchess.py index feb9786..55b326a 100644 --- a/gui/tchess.py +++ b/gui/tchess.py @@ -1,5 +1,4 @@ import pygame as pg -import numpy as np import gui.game as game class Aplication(): @@ -11,7 +10,7 @@ def __init__(self): self.FPS = 120 self.running = True self.clicking = False - self.game = game.Pieces() + self.game = game.Game() def update(self): self.screen.fill((0, 0, 0)) @@ -25,22 +24,17 @@ def update(self): self.clicking = False # do stuff - self.drawing(50) + self.render() pg.display.flip() pg.display.set_caption('Chess Bot vs Player Game | ' + str(round(self.clock.get_fps(), 1))) self.clock.tick(self.FPS) - def drawing(self, a): - for i in range(0, self.WIDTH // (a), 2): - for j in range(0, self.HEIGHT // (a), 1): - if j // 2 == j / 2: - pg.draw.rect(self.screen, (0, 0, 0), pg.Rect(i * a, j * a, a, a)) - pg.draw.rect(self.screen, (255, 255, 255), pg.Rect(i * a + a, j * a, a, a)) - else: - pg.draw.rect(self.screen, (255, 255, 255), pg.Rect(i * a, j * a, a, a)) - pg.draw.rect(self.screen, (0, 0, 0), pg.Rect(i * a, j * a + a, a, a)) - - for i in self.game.pieces: - idle = i[2].get_rect(center=(i[0], i[1])) - self.screen.blit(i[2], idle) + def render(self): + col = ((237, 212, 175), (170, 125, 92)) + sqrSize = self.game.boardSize/8 + for x in range(8): + for y in range(8): + pg.draw.rect(self.screen, col[(x+y)%2], pg.Rect(x*sqrSize, y*sqrSize, sqrSize, sqrSize)) + + diff --git a/ressources/black_1.png b/resources/black_1.png similarity index 100% rename from ressources/black_1.png rename to resources/black_1.png diff --git a/ressources/black_2.png b/resources/black_2.png similarity index 100% rename from ressources/black_2.png rename to resources/black_2.png diff --git a/ressources/black_3.png b/resources/black_3.png similarity index 100% rename from ressources/black_3.png rename to resources/black_3.png diff --git a/ressources/black_4.png b/resources/black_4.png similarity index 100% rename from ressources/black_4.png rename to resources/black_4.png diff --git a/ressources/black_5.png b/resources/black_5.png similarity index 100% rename from ressources/black_5.png rename to resources/black_5.png diff --git a/ressources/black_6.png b/resources/black_6.png similarity index 100% rename from ressources/black_6.png rename to resources/black_6.png diff --git a/ressources/idle b/resources/idle similarity index 100% rename from ressources/idle rename to resources/idle diff --git a/ressources/white_1.png b/resources/white_1.png similarity index 100% rename from ressources/white_1.png rename to resources/white_1.png diff --git a/ressources/white_2.png b/resources/white_2.png similarity index 100% rename from ressources/white_2.png rename to resources/white_2.png diff --git a/ressources/white_3.png b/resources/white_3.png similarity index 100% rename from ressources/white_3.png rename to resources/white_3.png diff --git a/ressources/white_4.png b/resources/white_4.png similarity index 100% rename from ressources/white_4.png rename to resources/white_4.png diff --git a/ressources/white_5.png b/resources/white_5.png similarity index 100% rename from ressources/white_5.png rename to resources/white_5.png diff --git a/ressources/white_6.png b/resources/white_6.png similarity index 100% rename from ressources/white_6.png rename to resources/white_6.png From 51eb53ed296f1186d9181e403d9a7144b7304da0 Mon Sep 17 00:00:00 2001 From: dgdzd <113797245+dgdzd@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:45:41 +0200 Subject: [PATCH 2/7] Added support for all platforms (paths slashes) --- .idea/workspace.xml | 93 +++++++++++++++++++++++++ gui/__pycache__/game.cpython-310.pyc | Bin 0 -> 1472 bytes gui/__pycache__/tchess.cpython-310.pyc | Bin 0 -> 1681 bytes gui/game.py | 33 +++++---- gui/tchess.py | 7 +- main.py | 2 +- 6 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 gui/__pycache__/game.cpython-310.pyc create mode 100644 gui/__pycache__/tchess.cpython-310.pyc diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..bd3a460 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1728328548744 + + + 1728329364900 + + + + + + + + + \ No newline at end of file diff --git a/gui/__pycache__/game.cpython-310.pyc b/gui/__pycache__/game.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1ee6038d8bac44ae83fc575f9de07247c00727c5 GIT binary patch literal 1472 zcmZuxO>fgM7`ELcP1ANCFvgeo5&~(LmhaOvZG&l>hB{zFlOj+Eb=GAhO|UaK|@u>irhb9*R zIN>xPHcfkGGtTS^uh<&bY@O@2!4130tG3BayT)s_wM@9h>nDWQkC|QPbc4(^h7gBz zh7*886M-Ty1Uze~A%GJyGyuBLN)IlahbE^0ywnT$pq!l$^a3YnN?UeAuZwX<5AM1K z?I|?*4rVw#A`an?YQc1K$azu6+*JnwhCq~W;VAWIejPU9QCT?@H zPkHSV7|-p;v)5tg2gnpGW(0UO=)YZon>XlftzecB2emvK;kdTBn_G>GY#;4h!rKS;;PFC7Z5HYTtc{xa0B5c!Yzbp e1PuAVvy?fTxtqfplepF-Ce@ghH3~hv=h$xoB4(ce literal 0 HcmV?d00001 diff --git a/gui/__pycache__/tchess.cpython-310.pyc b/gui/__pycache__/tchess.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..caa5df116c8dba15b9c0d856e38f2698b6abb8a3 GIT binary patch literal 1681 zcmZ{k&u=3&6vzE*Ce!?Co0gUW51Kf`IRoO0kFKuGYOL%S`4iTw4m{cOMZ^XyTpRVVN@zjykDL&$H~ zxc_lzJb{vb3?hl72??mJ*^mdEk^>@{eUCS2Y>uS<;&ZUam|y97$EVd8Cy}D`DcH z3Y^_!6kSwKgjFEoG&U9AKRK;xdXlDb+ONC_{_zD`qThoV;sDgAmB>|c{u9y55W^p* zJ5RH_lgW27JsK!;SBKE<>)vo^5AJp}%%S%JZH6B;GOB+|H(z7ls&5 z7(ynHdZNi-f;<*3&x1(tnf0QEEja9@0Rbfz0PDAZ+a0%tgyCC;-vfTW5pG~QvK={8h) z4Fz*#AxN=uCz^`CFJNBpe>jAk%HXZve-slukHw>$Kh_BHkLAx1-<4G@BjObNZRKJIU``p{xCK2Y3 z#uE`xay%c8K+4pc9oy?WftyY7omw1*2QSLDZQ{vUm;YL^@;+Nc^B8Yaj(O!lR(9Cm D&nt!e literal 0 HcmV?d00001 diff --git a/gui/game.py b/gui/game.py index d4b8986..6a76a79 100644 --- a/gui/game.py +++ b/gui/game.py @@ -1,8 +1,8 @@ import pygame as pg -import os +import platform WHITE = 16 -BLACK = 0 +BLACK = 8 KING = 1 QUEEN = 2 @@ -16,23 +16,26 @@ def __init__(self): self.boardSize = 512 self.board = [0 for i in range(64)] self.board[0] = WHITE | PAWN + self.board[5] = BLACK | KING + + prefix = "resources\\" if platform.system() == "Windows" else "resources/" self.pieces_tex = { 0: None, - WHITE | KING: pg.image.load("resources\\white_1.png"), - WHITE | QUEEN: pg.image.load("resources\\white_2.png"), - WHITE | BISHOP: pg.image.load("resources\\white_3.png"), - WHITE | KNIGHT: pg.image.load("resources\\white_4.png"), - WHITE | ROOK: pg.image.load("resources\\white_5.png"), - WHITE | PAWN: pg.image.load("resources\\white_6.png"), - - BLACK | KING: pg.image.load("resources\\black_1.png"), - BLACK | QUEEN: pg.image.load("resources\\black_2.png"), - BLACK | BISHOP: pg.image.load("resources\\black_3.png"), - BLACK | KNIGHT: pg.image.load("resources\\black_4.png"), - BLACK | ROOK: pg.image.load("resources\\black_5.png"), - BLACK | PAWN: pg.image.load("resources\\black_6.png"), + WHITE | KING: pg.image.load(f"{prefix}white_1.png"), + WHITE | QUEEN: pg.image.load(f"{prefix}white_2.png"), + WHITE | BISHOP: pg.image.load(f"{prefix}white_3.png"), + WHITE | KNIGHT: pg.image.load(f"{prefix}white_4.png"), + WHITE | ROOK: pg.image.load(f"{prefix}white_5.png"), + WHITE | PAWN: pg.image.load(f"{prefix}white_6.png"), + + BLACK | KING: pg.image.load(f"{prefix}black_1.png"), + BLACK | QUEEN: pg.image.load(f"{prefix}black_2.png"), + BLACK | BISHOP: pg.image.load(f"{prefix}black_3.png"), + BLACK | KNIGHT: pg.image.load(f"{prefix}black_4.png"), + BLACK | ROOK: pg.image.load(f"{prefix}black_5.png"), + BLACK | PAWN: pg.image.load(f"{prefix}black_6.png"), } diff --git a/gui/tchess.py b/gui/tchess.py index 55b326a..052085a 100644 --- a/gui/tchess.py +++ b/gui/tchess.py @@ -33,8 +33,11 @@ def update(self): def render(self): col = ((237, 212, 175), (170, 125, 92)) sqrSize = self.game.boardSize/8 - for x in range(8): - for y in range(8): + for y in range(8): + for x in range(8): + current_piece = self.game.board[8*y+x] pg.draw.rect(self.screen, col[(x+y)%2], pg.Rect(x*sqrSize, y*sqrSize, sqrSize, sqrSize)) + if current_piece != 0: + self.screen.blit(self.game.pieces_tex[current_piece], pg.Rect(x*sqrSize, y*sqrSize, sqrSize, sqrSize)) diff --git a/main.py b/main.py index 678c4f0..d6f5096 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ ''' -Change the variable MODE to test either the GUI mode or the Terminal mode +Change the variable MODE to test either the GUI mode or the Terminal mode. Note that Terminal mode is currently not working well. ''' MODE = "GUI" From 7aa9e97ad845f7529138e41c5aae37cd07a536f8 Mon Sep 17 00:00:00 2001 From: dgdzd <113797245+dgdzd@users.noreply.github.com> Date: Tue, 8 Oct 2024 22:36:47 +0200 Subject: [PATCH 3/7] minor change --- gui/__pycache__/game.cpython-310.pyc | Bin 1472 -> 1582 bytes gui/game.py | 4 ++++ 2 files changed, 4 insertions(+) diff --git a/gui/__pycache__/game.cpython-310.pyc b/gui/__pycache__/game.cpython-310.pyc index 1ee6038d8bac44ae83fc575f9de07247c00727c5..b2230c82fdddf6f9a962c70ddb2716aa3e0b0408 100644 GIT binary patch delta 238 zcmX@Wy^e=BpO=@50SIRFvZi03$a{;A8OV_ZVrL*OUbOLRJflYqLzch-!G#Pi46)L+ zOf^g?tl7*(=Sn!Ugcb;cP$Puk1F~*$#K&jmWtPOp^MjmfFnKeJIuF}NJ|PZ9Ek>pWmVcA)u$VJyPHtco JpRCO~6#&}_Ks^8e delta 103 zcmZ3-bAX#SpO=@50SKhJS<@va^4{WO0&--5*cph66E=R0XJkuZ2xia}+dPM9B_orj z;A9_`dI3$QTP($?Icc}p3W`$GGAkzkV3A`~n=H<%&cyX`vKOm4qw3^$tm2brvQ7m6 D)czYF diff --git a/gui/game.py b/gui/game.py index 6a76a79..4af3288 100644 --- a/gui/game.py +++ b/gui/game.py @@ -37,6 +37,10 @@ def __init__(self): BLACK | ROOK: pg.image.load(f"{prefix}black_5.png"), BLACK | PAWN: pg.image.load(f"{prefix}black_6.png"), } + + for key in self.pieces_tex.keys(): + if key != 0: + self.pieces_tex[key] = pg.transform.scale(self.pieces_tex[key], (self.boardSize/8, self.boardSize/8)) From ff3e0fc237bb6c7363066b0dac82676d9d3557ea Mon Sep 17 00:00:00 2001 From: Noah CAMPAGNE Date: Wed, 9 Oct 2024 10:56:30 +0200 Subject: [PATCH 4/7] Added events --- gui/__pycache__/game.cpython-310.pyc | Bin 1582 -> 1538 bytes gui/__pycache__/tchess.cpython-310.pyc | Bin 1681 -> 2228 bytes gui/events/__pycache__/event.cpython-310.pyc | Bin 0 -> 655 bytes gui/events/event.py | 10 +++++++ gui/events/mouse_events.py | 21 +++++++++++++++ gui/game.py | 19 ++++++++++++++ gui/tchess.py | 26 ++++++++++++++++--- 7 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 gui/events/__pycache__/event.cpython-310.pyc create mode 100644 gui/events/event.py create mode 100644 gui/events/mouse_events.py diff --git a/gui/__pycache__/game.cpython-310.pyc b/gui/__pycache__/game.cpython-310.pyc index b2230c82fdddf6f9a962c70ddb2716aa3e0b0408..29ff7b8d5510c2a051a4c7d35b47cf8b20814e2e 100644 GIT binary patch delta 143 zcmZ3-)5OD<&&$ij00gT{*wR7@`=#G*c84C&Na&^-TJT8CEd` zMfq8&CB-ozsYSV&&Kar2#WCrnnK9{!xv6>umAANTax#lclJj#5>{K@UFdt=PRGKWr h+QX7@`=#G*cAQMu+uG@qzk|1qC^o z$%!SI`FX|q1x5K;sU>ha9>`BGO3cj#OT%R23o1)8^7HgdQj2mklQU9_i}lk>GxgIG zb5r#SDsOSwcyV?N5r$UpfBOAjO8S(sQLm=Q<= Hp%4=Q0KqlJ diff --git a/gui/__pycache__/tchess.cpython-310.pyc b/gui/__pycache__/tchess.cpython-310.pyc index caa5df116c8dba15b9c0d856e38f2698b6abb8a3..20f58c3301e714ff4644d6848956841ca9297328 100644 GIT binary patch literal 2228 zcmZ`)&2Jk;6rb6hU9TM{P23PSEv4o|mE1!QMdE-^RY;SjNJOOx0!BeAcW2_Rv;N5J zx+IY!A*KHTR}MBO;KDz{X|J4oK;njkkl^=b^Wj!vSM&D0`FQhY-tYIeKR;h#cy6ux z?e7D|ekWu0abY|_OGzlEc)}#NU#IIzmlu7a>p6_Gt}lJt<0PdW_J}E0iI+@?Q%44h zZ?mR1g%`Hf(TSH3?gyMpYSjcuANRH-YZbyv05$muC**@&j zIGE6^a^{XJ6C6>|>^bE^R+WI9&lbj4?3{%*Q{IoXa#8sgZ|Mhk43`u`bir2BnKp2x zhR!-YgxJ|=x*HoChQe!lIX}$#k@NH4yIC8?&Qv(otIRc4wBZ946q{X_La{4G%{NEa*@SdExom*AMNK}L?0>QUYzCb#*^*5 zWQM(7+-u7q!o}@F%SF3|72*+OUg`FSsou_7nU-Z`T5YQ$gCf<*-p|a`5c%KlKL2oc zVEP@MrMo-YbmO(>I!$-m!+1A~NWD8a%7ZYZZNspHCFwFmjW6;l54dmN3a{`Leth*Z zgLltY7)qU?brB)*{2`s%8d~}u#E2_?lb!GpfP*iR)nOe#Xyhzotj<;`4er+T0p?DD zch+>w6?}5UZFa&|Mm;G77T^fW{J1_7`0Tt_a7dXi zquG%EghC;X5%z?0mN^}FEGFzB`{q*|f_FUh`1_7owYV!~!6`6^zA~Zl#L0lfQ;ym9 zPJqZxb>yzHkwbFdHJ=rl+05q03xwV5%7~2@?dqioXt`Q=T2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event.cpython-310.pyc b/gui/events/__pycache__/event.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..670b7f24a18320c6f02f3156ac08a21d2c75bde7 GIT binary patch literal 655 zcmZuty-ve05I*OxZU9j_An^!Un3xbCBtR^vOJRwEtf)4nC{2o;B7w@(J_w9`B_k%@ zf(7mp3a!LRclLefJAd~T8yg{@y&nesH#|QmSvD7wV|3HTa1IhAo1ssg7^KsM$n7D% zV!s`G2rG!0gxI-)KE$r_l)nQikn8}vr8|STArZTAq%VV+7jxObncVtk4=OKhGaHyv zaR`C{H2w1?1Vbi1F7 zbaAIjlUyo29Gu)LW0L-OkSJnc>SK5GXuV#VWz=K>3?bvp`+5p1p1V}C-px4g_2|2W z(Vf(&c((9DJYSp%yfTJLc0p4$a{oBmARUcVE+eKJSgJ_Y!N=6DrBYt}NWu{?v{#Eg zZPhfEs-385xe$3eR6^KLh+!ee8Qs@~xF4rkU8#hz9?4=k@h=BOZa0NU^Smfi%8U@2 rHr7oFB*0Q2Ruy|%)exl+`EaN0RcHtIsPzA*&GLhq(^1t+o3!B@&2NSW literal 0 HcmV?d00001 diff --git a/gui/events/event.py b/gui/events/event.py new file mode 100644 index 0000000..f43a977 --- /dev/null +++ b/gui/events/event.py @@ -0,0 +1,10 @@ +class Event: + listeners: list[function] = [] + + def call(): + for listener in Event.listeners: + listener() + + def addListener(func: function): + Event.listeners.append(func) + diff --git a/gui/events/mouse_events.py b/gui/events/mouse_events.py new file mode 100644 index 0000000..4e018d2 --- /dev/null +++ b/gui/events/mouse_events.py @@ -0,0 +1,21 @@ +from gui.events.event import * + +class MouseEvent(Event): + def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: + super().__init__() + self.mouseX = mouseX + self.mouseY = mouseY + +class ClickEvent(MouseEvent): + def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: + super().__init__(mouseX, mouseY, mouseButton) + self.button = mouseButton + +class ReleaseEvent(MouseEvent): + def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: + super().__init__(mouseX, mouseY, mouseButton) + +class DragEvent(MouseEvent): + def __init__(self, piece: int, mouseX: int, mouseY: int, mouseButton: int) -> None: + super().__init__(mouseX, mouseY, mouseButton) + self.piece = piece \ No newline at end of file diff --git a/gui/game.py b/gui/game.py index 4af3288..e82dcd4 100644 --- a/gui/game.py +++ b/gui/game.py @@ -1,6 +1,25 @@ import pygame as pg import platform + +''' +The system used is a 5-bit encoding, with the first 2 bits representing the team of the piece. + WHITE = 0b10000 + BLACK = 0b01000 + +The last 3 bits represent the type of piece. + KING = 0b00001 + QUEEN = 0b00010 + BISHOP = 0b00011 + KNIGHT = 0b00100 + ROOK = 0b00101 + PAWN = 0b00111 + +If we want a white rook for example, we do a bitwise OR operation. +Example: WHITE | ROOK = 16 | 5 + = 0b10101 + +''' WHITE = 16 BLACK = 8 diff --git a/gui/tchess.py b/gui/tchess.py index 052085a..5bc4cae 100644 --- a/gui/tchess.py +++ b/gui/tchess.py @@ -1,5 +1,7 @@ import pygame as pg import gui.game as game +import gui.events.event as event +from gui.events.mouse_events import * class Aplication(): def __init__(self): @@ -11,6 +13,14 @@ def __init__(self): self.running = True self.clicking = False self.game = game.Game() + self.mouseState = [False, False, False] + self.dragState = {"x": 0, "y": 0, "piece": 0, "offsetX": 0, "offsetY": 0} + + def get_piece_at(self, x: int, y: int): + x /= 64 + y /= 64 + return self.game.board[8*y+x] + def update(self): self.screen.fill((0, 0, 0)) @@ -18,10 +28,18 @@ def update(self): if event.type == pg.QUIT: self.running = False exit() - if pg.mouse.get_pressed()[0]: - self.clicking = True - else: - self.clicking = False + + c = pg.mouse.get_pressed() + for i in range(3): + if self.mouseState[i] != c[i]: # If the button state is not the same as the one registered last frame, call an event. + if c[i]: + ClickEvent.call() + print(self.get_piece_at(pg.mouse.get_pos()[0], pg.mouse.get_pos([1]))) + else: + ReleaseEvent.call() + else: + DragEvent.call() + self.mouseState = c # do stuff self.render() From 6edd8052f7853d60a9aff0297b662e70fcce0b96 Mon Sep 17 00:00:00 2001 From: dgdzd <113797245+dgdzd@users.noreply.github.com> Date: Thu, 10 Oct 2024 07:45:05 +0200 Subject: [PATCH 5/7] Changed event system --- gui/__pycache__/game.cpython-310.pyc | Bin 1538 -> 1586 bytes gui/__pycache__/tchess.cpython-310.pyc | Bin 2228 -> 2305 bytes gui/events/__pycache__/event.cpython-310.pyc | Bin 655 -> 678 bytes .../__pycache__/mouse_events.cpython-310.pyc | Bin 0 -> 1539 bytes gui/events/event.py | 6 +++--- gui/events/mouse_events.py | 13 +++++++++++-- gui/game.py | 1 - gui/tchess.py | 5 ++--- main.py | 2 -- 9 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 gui/events/__pycache__/mouse_events.cpython-310.pyc diff --git a/gui/__pycache__/game.cpython-310.pyc b/gui/__pycache__/game.cpython-310.pyc index 29ff7b8d5510c2a051a4c7d35b47cf8b20814e2e..8d6abfda4077527b69fe9996c74194861c57e946 100644 GIT binary patch delta 153 zcmZqT*~G(}&&$ij00ddL+0s{R_?EoLG{XpI5A3P?VpQS^}rzf&BEM z#N1r4G)y+Wpt2+*KTp3TwJ0|;IU}{WSU^ij$wP^e`$+E@W*G6l7s!VFF?(6k-AZLiI78 delta 105 zcmdnQ)5OD@&&$ij00gT{*wR;RNl*QEb5+9Ew@@$rR`bhP;Y--x(B2KE+k1G zx*$3I<+MX;P@^vG&>6X55R;ThmKDdl@@C8>WZ5h88UZqvXEBIjf#tMto|oDB#2%Z6 zA`V$@ajVOuS**iMX&>Sx6GuNx9O64m&5nqxnq)-#03?`lBVoNh!D(d^qF7MYfMZ)p zM@|*Tc5Hl5?!m;2DqsPJ>I!7=MKvauK?99)%7K1x{pn53KqMt!5p$S``f6Iz4ID(z c;=%~0W(cbxq_M{hoZto|af$Cp(+ht51stYc6aWAK delta 325 zcmXAjJxc>Y5Qg{Ny}N9}T{LOThZ>?W2ogcXMnRMyh+-oa7AH1$BzIyG_Rj87h+rwg z{DIsr5YbBTSJ;T9g|(fn8#BYqJTUKk+!S95PTz5IOj~W=>d=$3&8v&uEq9>$p&v)? zsjsercGr(0w{so1L_DZ8v^s%@IW|PX|0_>e$U@M(m<6X@(H0Vt?nEn>)#A2}=!f1! z1&8z@T1V?cp$F_llv%>Z`BJFk38{nF8Rb(37EdK?G#&FWJ1V5iGsd*;0GDS}?CO5@geld996Tt$Tjaz`I{lqY{@L`S3m diff --git a/gui/events/__pycache__/event.cpython-310.pyc b/gui/events/__pycache__/event.cpython-310.pyc index 670b7f24a18320c6f02f3156ac08a21d2c75bde7..8f2802a5a38188805714cc6f726dc75a2171d569 100644 GIT binary patch delta 195 zcmeBYUB=3r&&$ij00e63?CE=?qcKsVu3iDU3D@DNHHM zEeug?DJ&_hK$<;;Eto-*ed4stph$hkf`XjPR delta 173 zcmZ3++Rw_H&&$ij00eh!+0t)MY%MHN94YL<44NDhdoq162B` R+D!JW0*o9?LX-2E%mHP(EqDL` diff --git a/gui/events/__pycache__/mouse_events.cpython-310.pyc b/gui/events/__pycache__/mouse_events.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3a02b7f3d00ed1937cddfe8ea2d6a5dce29a0abf GIT binary patch literal 1539 zcmb7EOK;Oa5Z?7Gj*~!Wi^TJalpYd^GeSJY1#+MqAQD=Pcas_%+w9s%t#WIn{tbG} zUs{fwIQPbhnROCJZ7Rx2o}F)BGv7R%bvglo@$siO@oYkVAy^*{fG6;&*C06IG$HTE zE5a>q9}{j1dSs6*zz%l-yTSr&1NL|WaKm5+u+N)-n+Cf)*dx8xZ+MaPsHVFSYU{&a zfU`a*f{Z99Ba2gEa|=EjKCn0TPso@WOYMjM1IgyLdlHI2(xAuNSl*AGN;3yqg_?z zLTaC}IE@R&qzjj%2V(AMM~P%yugC^2Q{7}NNH8qmY- zI!gVe>vIojq~TOBrUS;N87~vm+l+lH!(_2z$7x~S)SU&7NF)g`@51wyxQ5yd6lfP? zd4`Hl9#FG#Y04#@#Uwt5hE5>g_!#IzD!;{AOt_fT1*D7ps3$Jb>e;YQ?puB|w(Laf|--<+pi~9Xf z>N{YdD%YBNP1>qSUrP2myr8iBcj)2Gbx*E9_AXpA@iSx*T_9_IHfZ7@h|P@+&Fyk; z9GK>AaQ}F{xtqHALWYy7x7|Dzk=Ps}tcK-4eN%NnZOj}~9j34AK&@4WybqUquI#{M z^z-I66wEfB#Xra4*2>u{gzp~Qz(P`0qwG?NE5n|ryU^PGMK_yCyH#W0NmEr4qixQw bmEo$4wg0H<$0YcqYjwS@yW{)?)Ak~q literal 0 HcmV?d00001 diff --git a/gui/events/event.py b/gui/events/event.py index f43a977..85c4c3a 100644 --- a/gui/events/event.py +++ b/gui/events/event.py @@ -1,10 +1,10 @@ class Event: - listeners: list[function] = [] + listeners: list = [] - def call(): + def call(self): for listener in Event.listeners: listener() - def addListener(func: function): + def addListener(func): Event.listeners.append(func) diff --git a/gui/events/mouse_events.py b/gui/events/mouse_events.py index 4e018d2..6f0fa95 100644 --- a/gui/events/mouse_events.py +++ b/gui/events/mouse_events.py @@ -5,11 +5,16 @@ def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: super().__init__() self.mouseX = mouseX self.mouseY = mouseY + self.mouseButton = mouseButton + + def call(self): + for listener in MouseEvent.listeners: + listener(self.mouseX, self.mouseY, self.mouseButton) + class ClickEvent(MouseEvent): def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: super().__init__(mouseX, mouseY, mouseButton) - self.button = mouseButton class ReleaseEvent(MouseEvent): def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: @@ -18,4 +23,8 @@ def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: class DragEvent(MouseEvent): def __init__(self, piece: int, mouseX: int, mouseY: int, mouseButton: int) -> None: super().__init__(mouseX, mouseY, mouseButton) - self.piece = piece \ No newline at end of file + self.piece = piece + + def call(self): + for listener in DragEvent.listeners: + listener(self.mouseX, self.mouseY, self.mouseButton, self.piece) \ No newline at end of file diff --git a/gui/game.py b/gui/game.py index e82dcd4..68cf315 100644 --- a/gui/game.py +++ b/gui/game.py @@ -18,7 +18,6 @@ If we want a white rook for example, we do a bitwise OR operation. Example: WHITE | ROOK = 16 | 5 = 0b10101 - ''' WHITE = 16 BLACK = 8 diff --git a/gui/tchess.py b/gui/tchess.py index 5bc4cae..4ddbe79 100644 --- a/gui/tchess.py +++ b/gui/tchess.py @@ -17,8 +17,8 @@ def __init__(self): self.dragState = {"x": 0, "y": 0, "piece": 0, "offsetX": 0, "offsetY": 0} def get_piece_at(self, x: int, y: int): - x /= 64 - y /= 64 + x = int(x/64) + y = int(y/64) return self.game.board[8*y+x] @@ -34,7 +34,6 @@ def update(self): if self.mouseState[i] != c[i]: # If the button state is not the same as the one registered last frame, call an event. if c[i]: ClickEvent.call() - print(self.get_piece_at(pg.mouse.get_pos()[0], pg.mouse.get_pos([1]))) else: ReleaseEvent.call() else: diff --git a/main.py b/main.py index d6f5096..0214e29 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,3 @@ - - ''' Change the variable MODE to test either the GUI mode or the Terminal mode. Note that Terminal mode is currently not working well. From f9141f36e2f3ed6f8449ec56bd846f0b3c956217 Mon Sep 17 00:00:00 2001 From: dgdzd <113797245+dgdzd@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:39:49 +0200 Subject: [PATCH 6/7] changed event (not working right now) --- gui/__pycache__/tchess.cpython-310.pyc | Bin 2305 -> 2766 bytes gui/events/__pycache__/event.cpython-310.pyc | Bin 678 -> 779 bytes .../__pycache__/mouse_events.cpython-310.pyc | Bin 1539 -> 2072 bytes gui/events/event.py | 9 ++++-- gui/events/mouse_events.py | 10 +++++-- gui/tchess.py | 28 +++++++++++------- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/gui/__pycache__/tchess.cpython-310.pyc b/gui/__pycache__/tchess.cpython-310.pyc index b9c7069ccac436b271e689d5c169b4b97910f877..e7026e860b6227e4640003836dd695096526e563 100644 GIT binary patch literal 2766 zcmaJ@TW=dh6rS0gU9T@m6E{sr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ache__/event.cpython-310.pyc index 8f2802a5a38188805714cc6f726dc75a2171d569..e5983d9b6386c5bdd38d1dac0e49cf40c0bd68e3 100644 GIT binary patch literal 779 zcmZuuO>fgc5S>{&iR-3CM7{LF-BQ(DI93P*hzrUEaljHCPd;;-y(kWz(H@yg4M>Qvqi;<1{jks9wXo+zJY1?}kVS6#XG zsK~9;l{PjdOnVdb^69*$W1o|AaEou?^Vv6aVuM?B-WOrcHmL9%$cuB>BEr(68t|=v z5icI76&`qU#|w5ewq{o^Rv8?5T`i9K=^;| z!cLV-E-zAsI@lvYSZ*Q>B*B!0`7So_gXlBqA>LpnhTJ_aBJY6yIB6PPDW-nCs?vB= zvH~6J-W|~o+T%p2_y4wYObN>vF7WPYkMW>txJ)|@q^y!sOWB4}mbF?H{GLj=TqQ+c z>GU^ap8rJ3q^fGy>no*UEzBu9*8jjUoDM#3&~h<^DG7Eu>9UImdvgD2YlEvE$cwI* HDa_z6|J$j{ literal 678 zcmZutOHRZv40ZaMDP<6!1xG+C3wi;BK!Dhw*^F3F2}M;CN>Q7Zq=N*4)o>74atJPx zEw^9;cFVvZVaax4$Ig4NURenNZ7UzEk>;XqvLf(zd9T zi5p#C%l0R8yyrTts;)in^5(9+s#V-dT@~4tG$zicMJx%i>C;YgXT2;fOZvnNFoY~% z-sgST@!FwQM!@DqXHu(Uw1x*_^z1yq3uCDC1T=Lc_m_7Owh?Dk0Zx>>@%0bd-hk`4d> diff --git a/gui/events/__pycache__/mouse_events.cpython-310.pyc b/gui/events/__pycache__/mouse_events.cpython-310.pyc index 3a02b7f3d00ed1937cddfe8ea2d6a5dce29a0abf..b834de7b885265702d6a2c7eb3b4513b2ca759ce 100644 GIT binary patch literal 2072 zcmb7EL30~56qa_i>-EMVZks||N(gBwm_vNyz%UGuGF%`B+8MwzjPR_Kbkp_PmDbE8 zV@}RY{{}t!FYT36&lxTZ2i~W(y>8;nfOhnxCut?U?|u4)o0~DC^4Csu{A0k_pKw-- zAm>Y}_B%3)DW0*X>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P1>qSUrP2myr8iBcj)2Gbx*E9_AXpA@iSx*T_9_IHfZ7@h|P@+&Fyk; z9GK>AaQ}F{xtqHALWYy7x7|Dzk=Ps}tcK-4eN%NnZOj}~9j34AK&@4WybqUquI#{M z^z-I66wEfB#Xra4*2>u{gzp~Qz(P`0qwG?NE5n|ryU^PGMK_yCyH#W0NmEr4qixQw bmEo$4wg0H<$0YcqYjwS@yW{)?)Ak~q diff --git a/gui/events/event.py b/gui/events/event.py index 85c4c3a..a43b82d 100644 --- a/gui/events/event.py +++ b/gui/events/event.py @@ -3,8 +3,11 @@ class Event: def call(self): for listener in Event.listeners: - listener() + if type(listener) == list: + listener[0](listener[1]) + else: + listener() - def addListener(func): - Event.listeners.append(func) + def addListener(func, mself = None): + Event.listeners.append(func) if mself == None else Event.listeners.append([func, mself]) diff --git a/gui/events/mouse_events.py b/gui/events/mouse_events.py index 6f0fa95..a3098a6 100644 --- a/gui/events/mouse_events.py +++ b/gui/events/mouse_events.py @@ -9,7 +9,10 @@ def __init__(self, mouseX: int, mouseY: int, mouseButton: int) -> None: def call(self): for listener in MouseEvent.listeners: - listener(self.mouseX, self.mouseY, self.mouseButton) + if type(listener) == list: + listener[0](listener[1], self.mouseX, self.mouseY, self.mouseButton) + else: + listener(self.mouseX, self.mouseY, self.mouseButton) class ClickEvent(MouseEvent): @@ -27,4 +30,7 @@ def __init__(self, piece: int, mouseX: int, mouseY: int, mouseButton: int) -> No def call(self): for listener in DragEvent.listeners: - listener(self.mouseX, self.mouseY, self.mouseButton, self.piece) \ No newline at end of file + if type(listener) == list: + listener[0](listener[1], self.mouseX, self.mouseY, self.mouseButton, self.piece) + else: + listener(self.mouseX, self.mouseY, self.mouseButton, self.piece) \ No newline at end of file diff --git a/gui/tchess.py b/gui/tchess.py index 4ddbe79..12f6fe2 100644 --- a/gui/tchess.py +++ b/gui/tchess.py @@ -13,14 +13,21 @@ def __init__(self): self.running = True self.clicking = False self.game = game.Game() - self.mouseState = [False, False, False] + self.mouseState = [False, False, False, (0, 0)] self.dragState = {"x": 0, "y": 0, "piece": 0, "offsetX": 0, "offsetY": 0} + ClickEvent.addListener(self.onStartDrag, self) - def get_piece_at(self, x: int, y: int): - x = int(x/64) - y = int(y/64) - return self.game.board[8*y+x] + def get_piece_at(self, pos: tuple[int, int]): + x = int(pos[0]/64) + y = int(pos[1]/64) + i = 8*y+x + return self.game.board[i] if i >= 0 and i <= 63 else None + def onStartDrag(self, mouseX: int, mouseY: int, mouseButton: int, piece: int): + self.dragState["x"] = mouseX + self.dragState["y"] = mouseY + self.dragState["piece"] = piece + print("cacadrag") def update(self): self.screen.fill((0, 0, 0)) @@ -30,15 +37,16 @@ def update(self): exit() c = pg.mouse.get_pressed() + piece = self.get_piece_at(pg.mouse.get_pos()) for i in range(3): if self.mouseState[i] != c[i]: # If the button state is not the same as the one registered last frame, call an event. + self.mouseState[i] = c[i] if c[i]: - ClickEvent.call() + ClickEvent(self.mouseState[3][0], self.mouseState[3][1], i).call() else: - ReleaseEvent.call() - else: - DragEvent.call() - self.mouseState = c + ReleaseEvent(self.mouseState[3][0], self.mouseState[3][1], i).call() + elif c[i]: + DragEvent(piece, self.mouseState[3][0], self.mouseState[3][1], i).call() # do stuff self.render() From ebfc0e2bcc781e33daf845b485704d736617bc99 Mon Sep 17 00:00:00 2001 From: Noah CAMPAGNE Date: Fri, 11 Oct 2024 15:21:41 +0200 Subject: [PATCH 7/7] Changed event system --- gui/game.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gui/game.py b/gui/game.py index 68cf315..8316b85 100644 --- a/gui/game.py +++ b/gui/game.py @@ -58,7 +58,4 @@ def __init__(self): for key in self.pieces_tex.keys(): if key != 0: - self.pieces_tex[key] = pg.transform.scale(self.pieces_tex[key], (self.boardSize/8, self.boardSize/8)) - - - + self.pieces_tex[key] = pg.transform.scale(self.pieces_tex[key], (self.boardSize/8, self.boardSize/8)) \ No newline at end of file