|
| 1 | +import pygame,os |
| 2 | +pygame.init() |
| 3 | +path = os.path.dirname(os.path.realpath(__file__)) |
| 4 | +ts = 80 |
| 5 | +mx = -1 |
| 6 | +my = -1 |
| 7 | +oy = ts/2 |
| 8 | +def floor(num): |
| 9 | + return round(num//1) |
| 10 | +screen = pygame.display.set_mode((floor(ts*8),floor(ts*8.5))) |
| 11 | +pygame.display.set_caption("Checkers") |
| 12 | +font = pygame.font.Font((str(path)+'/assets/fonts/font.ttf'),floor(oy)) |
| 13 | +text = font.render("T u r n",True,(255,255,255)) |
| 14 | +t_rect = text.get_rect(center=(ts*4,floor(oy/2))) |
| 15 | +wins = font.render("W I N N E R",True,(255,255,255)) |
| 16 | +w_rect = wins.get_rect(center=(ts*4,floor(oy/2))) |
| 17 | +def draw_checkers(): |
| 18 | + for i in range(8): |
| 19 | + for j in range(8): |
| 20 | + if (i+j)%2 == 1: |
| 21 | + pygame.draw.rect(screen,(0,0,0),((j*ts),(i*ts)+oy,ts,ts)) |
| 22 | + else: |
| 23 | + pygame.draw.rect(screen,(255,255,255),((j*ts),(i*ts)+oy,ts,ts)) |
| 24 | +def checkib(x,y): |
| 25 | + if x > 7 or x < 0 or y > 7 or y < 0: |
| 26 | + return False |
| 27 | + return True |
| 28 | +set_board = [[0,1,0,1,0,1,0,1], |
| 29 | + [1,0,1,0,1,0,1,0], |
| 30 | + [0,1,0,1,0,1,0,1], |
| 31 | + [0,0,0,0,0,0,0,0], |
| 32 | + [0,0,0,0,0,0,0,0], |
| 33 | + [2,0,2,0,2,0,2,0], |
| 34 | + [0,2,0,2,0,2,0,2], |
| 35 | + [2,0,2,0,2,0,2,0],] |
| 36 | +blank_board = [[0,0,0,0,0,0,0,0], |
| 37 | + [0,0,0,0,0,0,0,0], |
| 38 | + [0,0,0,0,0,0,0,0], |
| 39 | + [0,0,0,0,0,0,0,0], |
| 40 | + [0,0,0,0,0,0,0,0], |
| 41 | + [0,0,0,0,0,0,0,0], |
| 42 | + [0,0,0,0,0,0,0,0], |
| 43 | + [0,0,0,0,0,0,0,0],] |
| 44 | +board = set_board.copy() |
| 45 | +#board = blank_board.copy() |
| 46 | +all_moves = [] |
| 47 | +turn = 1 |
| 48 | +ded = 0 |
| 49 | +checker_a = (80,80,80) |
| 50 | +checker_b = (200,0,0) |
| 51 | +checker_ka = (40,40,40) |
| 52 | +checker_kb = (150,0,0) |
| 53 | +beven = (161,97,23) |
| 54 | +bodd = (212,160,87) |
| 55 | +def gen_high(r,g,b,a): |
| 56 | + highlight = pygame.Surface((ts,ts),pygame.SRCALPHA) |
| 57 | + highlight.fill((r,g,b,a)) |
| 58 | + return highlight |
| 59 | +r_high = gen_high(255,0,0,128) |
| 60 | +y_high = gen_high(255,255,0,128) |
| 61 | +g_high = gen_high(0,255,0,128) |
| 62 | +class AnyNum: |
| 63 | + def __eq__(self,other): |
| 64 | + return True |
| 65 | +Any = AnyNum() |
| 66 | +def get_moves(x,y,op,curr_out=[],fk=False,lmj=False,addr=False): |
| 67 | + def check_move(x,y,dx,dy,extra=''): |
| 68 | + try: |
| 69 | + if board[y+dy][x+dx]%2 == side and board[y+dy][x+dx] != 0: |
| 70 | + return False |
| 71 | + if x+dx < 0 or x+dx > 7 or y+dy < 0 or y+dy > 7: |
| 72 | + return False |
| 73 | + if board[y+dy][x+dx] == 0: |
| 74 | + if extra=='cj': |
| 75 | + return False |
| 76 | + elif extra=='pos': |
| 77 | + return [[x+dx,y+dy]] |
| 78 | + else: |
| 79 | + return True |
| 80 | + else: |
| 81 | + if board[y+dy+dy][x+dx+dx] == 0: |
| 82 | + if extra=='cj': |
| 83 | + return True |
| 84 | + elif extra=='pos': |
| 85 | + return [[x+dx+dx,y+dy+dy]] |
| 86 | + else: |
| 87 | + return True |
| 88 | + else: |
| 89 | + return False |
| 90 | + except: |
| 91 | + return False |
| 92 | + def listadd(a,b): |
| 93 | + for c in b: |
| 94 | + if not [c[0],c[1],Any,Any] in a: |
| 95 | + if c[0] >= 0 and c[0] <= 7 and c[1] >= 0 and c[1] <= 8: |
| 96 | + a.append(c) |
| 97 | + return(a) |
| 98 | + def chkkng(y,dy,op): |
| 99 | + try: |
| 100 | + if op == 1: |
| 101 | + if y+dy == 7: |
| 102 | + return True |
| 103 | + elif op == 2: |
| 104 | + if y+dy == 0: |
| 105 | + return True |
| 106 | + elif op>2: |
| 107 | + return True |
| 108 | + return False |
| 109 | + except: |
| 110 | + return False |
| 111 | + out = curr_out |
| 112 | + out = listadd(out,[[x,y,[],False]]) |
| 113 | + if fk and (not op>2): |
| 114 | + if op == 1 and y == 7: |
| 115 | + op = 3 |
| 116 | + elif op == 2 and y == 0: |
| 117 | + op = 4 |
| 118 | + character = op |
| 119 | + side = character%2 |
| 120 | + if character != 2: |
| 121 | + if check_move(x,y,-1,1,side): |
| 122 | + if not lmj and not check_move(x,y,-1,1,'cj'): |
| 123 | + out = listadd(out,[[x-1,y+1,[],chkkng(y,1,op)]]) |
| 124 | + if check_move(x,y,-1,1,'cj') and (not [x-2,y+2,Any,Any] in out): |
| 125 | + a = out.index([x,y,Any,Any]) |
| 126 | + a = out[a][2] |
| 127 | + out = listadd(out,[[x-2,y+2,a+[[x-1,y+1]],chkkng(y,2,op)]]) |
| 128 | + a = get_moves(x-2,y+2,op,out,True,True) |
| 129 | + out = listadd(out,a) |
| 130 | + if check_move(x,y,1,1): |
| 131 | + if not lmj and not check_move(x,y,1,1,'cj'): |
| 132 | + out = listadd(out,[[x+1,y+1,[],chkkng(y,1,op)]]) |
| 133 | + if check_move(x,y,1,1,'cj') and (not [x+2,y+2,Any,Any] in out): |
| 134 | + a = out.index([x,y,Any,Any]) |
| 135 | + a = out[a][2] |
| 136 | + out = listadd(out,[[x+2,y+2,a+[[x+1,y+1]],chkkng(y,2,op)]]) |
| 137 | + a = get_moves(x+2,y+2,op,out,True,True) |
| 138 | + out = listadd(out,a) |
| 139 | + if character != 1: |
| 140 | + if check_move(x,y,-1,-1): |
| 141 | + if not lmj and not check_move(x,y,-1,-1,'cj'): |
| 142 | + out = listadd(out,[[x-1,y-1,[],chkkng(y,-1,op)]]) |
| 143 | + if check_move(x,y,-1,-1,'cj') and (not [x-2,y-2,Any,Any] in out): |
| 144 | + a = out.index([x,y,Any,Any]) |
| 145 | + a = out[a][2] |
| 146 | + out = listadd(out,[[x-2,y-2,a+[[x-1,y-1]],chkkng(y,-2,op)]]) |
| 147 | + a = get_moves(x-2,y-2,op,out,True,True) |
| 148 | + out = listadd(out,a) |
| 149 | + if check_move(x,y,1,-1): |
| 150 | + if not lmj and not check_move(x,y,1,-1,'cj'): |
| 151 | + out = listadd(out,[[x+1,y-1,[],chkkng(y,-1,op)]]) |
| 152 | + if check_move(x,y,1,-1,'cj') and (not [x+2,y-2,Any,Any] in out): |
| 153 | + a = out.index([x,y,Any,Any]) |
| 154 | + a = out[a][2] |
| 155 | + out = listadd(out,[[x+2,y-2,a+[[x+1,y-1]],chkkng(y,-2,op)]]) |
| 156 | + a = get_moves(x+2,y-2,op,out,True,True) |
| 157 | + out = listadd(out,a) |
| 158 | + return out |
| 159 | +def draw_checker(id,x,y,size=ts): |
| 160 | + pos = floor(x*ts+(ts/2)),floor((y*ts+(ts/2))+oy) |
| 161 | + if id%2 == 1: |
| 162 | + pygame.draw.circle(screen,checker_a,pos,floor(size/2.1)) |
| 163 | + elif id%2 == 0 and (not id == 0): |
| 164 | + pygame.draw.circle(screen,checker_b,pos,floor(size/2.1)) |
| 165 | + if id == 3: |
| 166 | + pygame.draw.circle(screen,checker_ka,pos,floor(size/3)) |
| 167 | + elif id == 4: |
| 168 | + pygame.draw.circle(screen,checker_kb,pos,floor(size/3)) |
| 169 | +def boardcount(num): |
| 170 | + cnt = 0 |
| 171 | + for xrow in board: |
| 172 | + cnt += xrow.count(num) |
| 173 | + return cnt |
| 174 | +def draw_screen(moves=True): |
| 175 | + global all_moves |
| 176 | + if ded > 0: |
| 177 | + if ded % 2 == 1: |
| 178 | + screen.fill(checker_a) |
| 179 | + else: |
| 180 | + screen.fill(checker_b) |
| 181 | + screen.blit(wins,w_rect) |
| 182 | + draw_checkers() |
| 183 | + draw_checker(ded,3.5,3.5,ts*8) |
| 184 | + pygame.display.update() |
| 185 | + return |
| 186 | + if turn == 1: |
| 187 | + screen.fill(checker_a) |
| 188 | + else: |
| 189 | + screen.fill(checker_b) |
| 190 | + screen.blit(text,t_rect) |
| 191 | + draw_checkers() |
| 192 | + for i in range(8): |
| 193 | + for j in range(8): |
| 194 | + k = board[i][j] |
| 195 | + draw_checker(k,j,i) |
| 196 | + if board[my][mx] != 0 and moves and (turn == (board[my][mx]%2)) and checkib(mx,my): |
| 197 | + screen.blit(y_high,(mx*ts,my*ts+oy)) |
| 198 | + all_moves = get_moves(mx,my,board[my][mx],[]) |
| 199 | + for move in all_moves[1:]: |
| 200 | + if len(move[2]) == 0: |
| 201 | + screen.blit(g_high,(move[0]*ts,move[1]*ts+oy)) |
| 202 | + else: |
| 203 | + screen.blit(r_high,(move[0]*ts,move[1]*ts+oy)) |
| 204 | + pygame.display.update() |
| 205 | +draw_screen(True) |
| 206 | +while True: |
| 207 | + for event in pygame.event.get(): |
| 208 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 209 | + mpos = pygame.mouse.get_pos() |
| 210 | + mx,my = floor(mpos[0]/ts), floor((mpos[1]-oy)/ts) |
| 211 | + if [mx,my,Any,Any] in all_moves[1:]: |
| 212 | + a = all_moves.index([mx,my,Any,Any]) |
| 213 | + b = all_moves[a][3] |
| 214 | + if b==True and board[all_moves[0][1]][all_moves[0][0]]<3: |
| 215 | + board[my][mx] = board[all_moves[0][1]][all_moves[0][0]]+2 |
| 216 | + else: |
| 217 | + board[my][mx] = board[all_moves[0][1]][all_moves[0][0]] |
| 218 | + board[all_moves[0][1]][all_moves[0][0]] = 0 |
| 219 | + b = all_moves[a][2] |
| 220 | + for i in b: |
| 221 | + board[i[1]][i[0]] = 0 |
| 222 | + all_moves = [] |
| 223 | + turn = 1-turn |
| 224 | + if boardcount(1)+boardcount(3) == 0: |
| 225 | + ded = 4 |
| 226 | + elif boardcount(2)+boardcount(4) == 0: |
| 227 | + ded = 3 |
| 228 | + draw_screen(False) |
| 229 | + else: |
| 230 | + draw_screen() |
| 231 | + if event.type == pygame.QUIT: |
| 232 | + exit() |
0 commit comments