Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import*
- from tkinter import*
- from tkinter.messagebox import*
- from PIL import Image, ImageTk
- import tkinter as Tk
- import tkinter.messagebox as tkmessagebox
- moves = ['move_left','move_right','move_up','move_down','move_downleft','move_upleft','move_downright','move_upright']
- move_errors = [1,'pion_j1','pion_j2',2]
- list_grille = []
- game_loop = True
- continu=False
- pos_pion_j1 = [1,4]
- pos_pion_j2 = [7,4]
- for i in range(0,9):
- list_grille.append([])
- for j in range(0,9):
- list_grille[i].append(0)
- for y in range(0,9):
- list_grille[0][y]=2
- list_grille[8][y]=2
- for x in range(0,9):
- list_grille[x][0]=2
- list_grille[x][8]=2
- list_grille[1][4]='pion_j1'
- list_grille[7][4]='pion_j2'
- def mur(x,y):
- list_grille[x][y]=1
- def mur_ai():
- global randmuraix
- global randmuraiy
- list_grille[randmuraix][randmuraiy]=1
- def check_move(x,y):
- if list_grille[x][y] in move_errors:
- return False
- elif x<0:
- return False
- elif y<0:
- return False
- else :
- return True
- def check_defeat(x,y):
- test=0
- for i in range(-1,2):
- for j in range(-1,2):
- if check_move(x-j,y-i)==False:
- test +=1
- if test==9:
- return True
- else:
- return False
- def pos_converter(x):
- for i in range(100,800,100):
- if x<=i:
- return int(i/100)
- def callback_move(event):
- global grid
- global pos_pion_j1
- global pos_pion_j2
- global pionref
- global cercle1
- global cercle2
- xm,ym=pos_converter(event.x),pos_converter(event.y)
- x0=(xm-1)*100
- y0=(ym-1)*100
- X=xm*100
- Y=ym*100
- if pionref=='pion_j1':
- if xm in range(pos_pion_j1[0]-1,pos_pion_j1[0]+2):
- if ym in range(pos_pion_j1[1]-1,pos_pion_j1[1]+2):
- if check_move(xm,ym)==True:
- refx=pos_pion_j1[0]
- refy=pos_pion_j1[1]
- list_grille[refx][refy]=0
- grid.delete(cercle1)
- list_grille[pos_pion_j1[0]][pos_pion_j1[1]]=0
- pos_pion_j1[0]=xm
- pos_pion_j1[1]=ym
- list_grille[xm][ym]='pion_j1'
- cercle1=grid.create_oval(x0,y0,X,Y,outline='black',fill='red')
- grid.bind("<Button-1>", callback_mur)
- else:
- if xm in range(pos_pion_j2[0]-1,pos_pion_j2[0]+2):
- if ym in range(pos_pion_j2[1]-1,pos_pion_j2[1]+2):
- if check_move(xm,ym)==True:
- refx=pos_pion_j2[0]
- refy=pos_pion_j2[1]
- list_grille[refx][refy]=0
- grid.delete(cercle2)
- list_grille[pos_pion_j2[0]][pos_pion_j2[1]]=0
- pos_pion_j2[0]=xm
- pos_pion_j2[1]=ym
- list_grille[xm][ym]='pion_j2'
- cercle2=grid.create_oval(x0,y0,X,Y,outline='black',fill='blue')
- grid.bind("<Button-1>", callback_mur)
- def callback_mur(event):
- global grid
- global gamemode
- global pionref
- x,y=pos_converter(event.x),pos_converter(event.y)
- x0=(x-1)*100
- y0=(y-1)*100
- X=x*100
- Y=y*100
- if check_move(x,y)==True:
- mur(x,y)
- grid.create_rectangle(x0,y0,X,Y,outline='black',fill='black')
- if pionref=='pion_j1':
- if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
- game_loop = False
- grid.bind('<Button-1>',callback_move)
- if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
- main_window.destroy()
- showwarning('player 2 win')
- if check_defeat(pos_pion_j2[0],pos_pion_j2[1])==True:
- main_window.destroy()
- showwarning('player 1 win')
- if gamemode=='solo':
- if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
- main_window.destroy()
- showwarning('AI win')
- ai_turn()
- else:
- if pionref=='pion_j1':
- pionref=''
- user2_turn()
- else:
- user1_turn()
- def user1_turn():
- global pionref
- global game_loop
- pionref='pion_j1'
- grid.focus_set()
- if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
- game_loop = False
- else :
- grid.bind('<Button-1>',callback_move)
- grid.mainloop()
- def user2_turn():
- global pionref
- global game_loop
- grid.focus_set()
- if check_defeat(pos_pion_j2[0],pos_pion_j2[1])==True:
- game_loop = False
- else :
- grid.bind('<Button-1>',callback_move)
- grid.mainloop()
- def ai_turn():
- global pos_pion_j2
- global cercle2
- global game_loop
- global randmuraix
- global randmuraiy
- if check_defeat(pos_pion_j2[0],pos_pion_j2[1])==True:
- game_loop = False
- else :
- refx=pos_pion_j2[0]
- refy=pos_pion_j2[1]
- randxmove=randint(pos_pion_j2[0]-1,pos_pion_j2[0]+1)
- randymove=randint(pos_pion_j2[1]-1,pos_pion_j2[1]+1)
- while check_move(randxmove,randymove)==False:
- randxmove=randint(pos_pion_j2[0]-1,pos_pion_j2[0]+1)
- randymove=randint(pos_pion_j2[1]-1,pos_pion_j2[1]+1)
- pos_pion_j2[0]=randxmove
- pos_pion_j2[1]=randymove
- list_grille[randxmove][randymove]='pion_j2'
- list_grille[refx][refy]=0
- grid.delete(cercle2)
- x,y=pos_pion_j2[0],pos_pion_j2[1]
- x0=(x-1)*100
- y0=(y-1)*100
- X=x*100
- Y=y*100
- cercle2=grid.create_oval(x0,y0,X,Y,outline='black',fill='blue')
- randmuraix=randint(0,6)
- randmuraiy=randint(0,6)
- while check_move(randmuraix,randmuraiy)== False:
- randmuraix=randint(0,6)
- randmuraiy=randint(0,6)
- mur_ai()
- x,y=randmuraix,randmuraiy
- x0=(x-1)*100
- y0=(y-1)*100
- X=x*100
- Y=y*100
- grid.create_rectangle(x0,y0,X,Y,outline='black',fill='black')
- grid.bind('<Button-1>',callback_move)
- main_window = Tk.Tk()
- can_start = Tk.Canvas(main_window, width=900, height=900)
- can_start.pack()
- tk_img = ImageTk.PhotoImage(file ='logo pysola.gif')
- can_start.create_image(450, 450, image=tk_img)
- def menu_start():
- solo_btn = Button(main_window,text='\n \n \n Solo \n \n \n',command=game_solo,cursor='star',bg='grey')
- button_solo = can_start.create_window(450, 300,anchor='n', window=solo_btn)
- multi_btn = Button(main_window,text='\n \n \n Multi \n \n \n',command=game_multi,bg='grey')
- button_multi= can_start.create_window(450,500,window=multi_btn,anchor='n')
- def game_solo():
- global label
- global can_start
- global grid
- global cercle1
- global cercle2
- global gamemode
- global main_window
- gamemode='solo'
- main_window.destroy()
- main_window=Tk.Tk()
- grid = Canvas(main_window,width = 700, height =700,bg='white')
- grid.pack(pady=5)
- grid.create_line(1,1,1,700)
- for i in range(0,700):
- for h in range (0,700):
- if i/100 in range(0,7):
- grid.create_line(i,0,i,700)
- if h/100 in range(0,7):
- grid.create_line(0,h,700,h)
- cercle1=grid.create_oval(0,300,100,400,outline='black',fill='red')
- cercle2=grid.create_oval(600,300,700,400,outline='black',fill='blue')
- tkmessagebox.showinfo('Tour joueur 1','Deplacez vous en cliquant/pavnum')
- while game_loop== True:
- user1_turn()
- def game_multi():
- global can_start
- global grid
- global cercle1
- global cercle2
- global gamemode
- gamemode=''
- can_start.destroy()
- grid = Canvas(main_window,width = 700, height =700,bg='white')
- grid.pack(pady=5)
- grid.create_line(1,1,1,700)
- for i in range(0,700):
- for h in range (0,700):
- if i/100 in range(0,7):
- grid.create_line(i,0,i,700)
- if h/100 in range(0,7):
- grid.create_line(0,h,700,h)
- cercle1=grid.create_oval(0,300,100,400,outline='black',fill='red')
- cercle2=grid.create_oval(600,300,700,400,outline='black',fill='blue')
- tkmessagebox.showinfo('Tour joueur 1','Deplacez vous en cliquant/pavnum')
- while game_loop== True:
- user1_turn()
- menu_start()
- main_window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement