fromionimport*fromkandinskyimport*fromtimeimport*fromrandomimport*# You can put your name here
PLAYER_1="Joueur 1"PLAYER_2="Joueur 2"WINS_COUNTER=[0,0]WINNING_ROUNDS=3# Color
BLUE=color(0,0,255)RED=color(255,0,0)YELLOW=color(255,204,0)BLACK=color(0,0,0)WHITE=color(255,255,255)BACKGROUND_COLOR=WHITEPLAYER_1_COLOR=REDPLAYER_2_COLOR=YELLOW# Ball
BALL_RADIUS=15DEFAULT_BALL_COL=3# the middle one
# Do not touch !
BALL_COLUMNS_X=(25,70,115,160,205,250,295)BALL_COLUMNS_Y=(210,180,150,120,90,60)RECT_COLUMNS_X=(5,50,95,140,185,230,275)defget_blank_grid()->list[list]:return[[0,0,0,0,0,0,0],# first row
[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],# last and 6th row
]defgame_is_null(grid)->bool:last_row=grid[-1]foriinrange(7):# since there are 7 collumns
iflast_row[i]==0:returnFalsereturnTruedefget_row(col,grid,player):forindex_row,rowinenumerate(grid):ifrow[col]==0:returnindex_row# if there is no token yet in this row for this column
breakdefplace_token_on_screen(row,col,grid,player,color)->None:x=BALL_COLUMNS_X[col]y=BALL_COLUMNS_Y[row]draw_ball(x,y,BALL_RADIUS,color)defdraw_grid()->None:fill_rect(0,45,320,180,BLACK)forxinRECT_COLUMNS_X:fill_rect(x,45,40,180,BACKGROUND_COLOR)defdraw_ball(x_center,y_center,radius,color)->None:width=height=2*radius# diameter of the ball
r_sqared=radius**2foryinrange(width):forxinrange(height):dt_sqared=x**2+y**2ifdt_sqared<=r_sqared:fill_rect(x+x_center,y+y_center,1,1,color)fill_rect(x_center-x,y_center-y,1,1,color)fill_rect(x_center-x,y_center+y,1,1,color)fill_rect(x+x_center,y_center-y,1,1,color)defdraw_scores()->None:n_wins_1=str(WINS_COUNTER[0])n_wins_2=str(WINS_COUNTER[1])draw_string(n_wins_1,288,2,PLAYER_1_COLOR)draw_string("/",298,2,BLACK)draw_string(n_wins_2,308,2,PLAYER_2_COLOR)defdraw_turn(player,color)->None:ifplayer==1:txt=PLAYER_1+" to play"else:txt=PLAYER_2+" to play"draw_string(txt,2,2,color)defdisplay_round_winner(player)->None:fill_rect(0,0,320,45,BACKGROUND_COLOR)ifplayer==1:txt=PLAYER_1+" gagne la manche !"elifplayer==2:txt=PLAYER_2+" gagne la manche !"elifplayer==0:txt="A draw. Nobody wins."draw_string(txt,2,2)defdisplay_game_winner(player)->None:fill_rect(0,0,320,240,BACKGROUND_COLOR)ifplayer==1:txt=PLAYER_1+" remporte la partie !"elifplayer==2:txt=PLAYER_2+" remporte la partie !"draw_string(txt,20,110)defdraw_diagonal(x1,y1,x2,y2):dx=x2-x1dy=y2-y1steps=max(abs(dx),abs(dy))x_inc=dx/stepsy_inc=dy/stepsx=x1y=y1for_inrange(int(steps)+1):fill_rect(int(x)-1,int(y)-1,2,2,BLACK)x+=x_incy+=y_incdefdisplay_win(win:tuple[tuple])->None:(row1,col1),(row2,col2)=winx1=BALL_COLUMNS_X[col1]x2=BALL_COLUMNS_X[col2]y1=BALL_COLUMNS_Y[row1]y2=BALL_COLUMNS_Y[row2]ifx1==x2:h=y1-y2fill_rect(x2,y2,3,h,BLACK)elify1==y2:w=x2-x1fill_rect(x1,y1,w,3,BLACK)else:draw_diagonal(x1,y1,x2,y2)defchoose_col(player,color)->int:fill_rect(0,0,320,45,BACKGROUND_COLOR)draw_turn(player,color)draw_scores()col=DEFAULT_BALL_COLx=BALL_COLUMNS_X[col]draw_ball(x,20,BALL_RADIUS,color)sleep(1)update=Falsewhilenotkeydown(KEY_OK)andnotkeydown(KEY_EXE):ifkeydown(KEY_LEFT):col-=1update=Trueelifkeydown(KEY_RIGHT):col+=1update=Trueifupdate:update=Falseifcol>6:col=0ifcol<0:col=6x=BALL_COLUMNS_X[col]fill_rect(0,0,320,45,BACKGROUND_COLOR)draw_turn(player,color)draw_scores()draw_ball(x,20,BALL_RADIUS,color)sleep(0.15)returncoldefcheck_win(row,col,grid):player=grid[row][col]for[u,v]in[[1,0],[0,1],[1,1],[-1,1]]:total=1fordin[1,-1]:i_col,i_row=col+d*u,row+d*vwhile (0<=i_col<7)and(0<=i_row<6):ifgrid[i_row][i_col]==player:total+=1iftotal==4:return (row,col),(i_row,i_col)i_col+=d*ui_row+=d*velse:breakreturnFalsedefget_col(gamemode,player,color):ifplayer==1orgamemode==1:col=choose_col(player,color)else:col=ai()returncoldefai():returnrandint(0,6)defgame(grid,gamemode):player=randint(1,2)draw_grid()whileTrue:ifgame_is_null(grid):display_round_winner(0)whilenotkeydown(KEY_EXE):passgrid=get_blank_grid()draw_grid()ifplayer==1:color=PLAYER_1_COLORelse:color=PLAYER_2_COLORcol=get_col(gamemode,player,color)whilegrid[-1][col]!=0:# column is full
col=get_col(gamemode,player,)row=get_row(col,grid,player)grid[row][col]=player# update grid
place_token_on_screen(row,col,grid,player,color)win=check_win(row,col,grid)ifwin:WINS_COUNTER[player-1]+=1display_round_winner(player)display_win(win)ifWINS_COUNTER[player-1]==WINNING_ROUNDS:sleep(3)breakwhilenotkeydown(KEY_EXE):passgrid=get_blank_grid()draw_grid()player=2ifplayer==1else1display_game_winner(player)deflogo(x,y):# P
fill_rect(x,y,10,50,BLACK)fill_rect(x+30,y,10,20,BLACK)fill_rect(x,y,30,10,BLACK)fill_rect(x,y+20,30,10,BLACK)# 4
fill_rect(x+80,y,10,50,BLACK)fill_rect(x+50,y+20,40,10,BLACK)fill_rect(x+50,y+10,10,20,BLACK)draw_string("par Cosmow22",x+100,75)defmenu():logo(60,40)draw_string("Jouer avec un ami",60,120,RED)draw_string("Mode solo (ai)",60,150,BLACK)selected_mode=1# 1 vs 1
new_selected_mode=1whilenotkeydown(KEY_EXE)andnotkeydown(KEY_OK):ifkeydown(KEY_DOWN):new_selected_mode=2ifkeydown(KEY_UP):new_selected_mode=1ifselected_mode!=new_selected_mode:ifnew_selected_mode==1:draw_string("Jouer avec un ami",60,120,RED)draw_string("Mode solo (ai)",60,150,BLACK)selected_mode=1else:draw_string("Jouer avec un ami",60,120,BLACK)draw_string("Mode solo (ai)",60,150,RED)selected_mode=2returnselected_modedefmain():grid=get_blank_grid()gamemode=menu()try:game(grid,gamemode)exceptKeyboardInterrupt:returnmain()
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.