Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.62 KB | None | 0 0
  1. from random import*
  2. from tkinter import*
  3. from tkinter.messagebox import*
  4. from PIL import Image, ImageTk
  5. import tkinter as Tk
  6. import tkinter.messagebox as tkmessagebox
  7.  
  8.  
  9. moves = ['move_left','move_right','move_up','move_down','move_downleft','move_upleft','move_downright','move_upright']
  10. move_errors = [1,'pion_j1','pion_j2',2]
  11. list_grille = []
  12. game_loop = True
  13. continu=False
  14. pos_pion_j1 = [1,4]
  15. pos_pion_j2 = [7,4]
  16.  
  17. for i in range(0,9):
  18.     list_grille.append([])
  19.     for j in range(0,9):
  20.         list_grille[i].append(0)
  21. for y in range(0,9):
  22.     list_grille[0][y]=2
  23.     list_grille[8][y]=2
  24.  
  25. for x in range(0,9):
  26.     list_grille[x][0]=2
  27.     list_grille[x][8]=2
  28.  
  29. list_grille[1][4]='pion_j1'
  30. list_grille[7][4]='pion_j2'
  31.  
  32. def mur(x,y):
  33.     list_grille[x][y]=1
  34.  
  35. def mur_ai():
  36.     global randmuraix
  37.     global randmuraiy
  38.  
  39.  
  40.  
  41.     list_grille[randmuraix][randmuraiy]=1
  42.  
  43.  
  44.  
  45. def check_move(x,y):
  46.     if list_grille[x][y] in move_errors:
  47.         return False
  48.     elif x<0:
  49.         return False
  50.     elif y<0:
  51.         return False
  52.     else :
  53.         return True
  54. def check_defeat(x,y):
  55.     test=0
  56.     for i in range(-1,2):
  57.         for j in range(-1,2):
  58.             if check_move(x-j,y-i)==False:
  59.                 test +=1
  60.     if test==9:
  61.         return True
  62.     else:
  63.         return False
  64.  
  65. def pos_converter(x):
  66.     for i in range(100,800,100):
  67.         if x<=i:
  68.             return int(i/100)
  69. def callback_move(event):
  70.     global grid
  71.     global pos_pion_j1
  72.     global pos_pion_j2
  73.     global pionref
  74.     global cercle1
  75.     global cercle2
  76.     xm,ym=pos_converter(event.x),pos_converter(event.y)
  77.     x0=(xm-1)*100
  78.     y0=(ym-1)*100
  79.     X=xm*100
  80.     Y=ym*100
  81.     if pionref=='pion_j1':
  82.         if xm in range(pos_pion_j1[0]-1,pos_pion_j1[0]+2):
  83.             if ym in range(pos_pion_j1[1]-1,pos_pion_j1[1]+2):
  84.                 if check_move(xm,ym)==True:
  85.                     refx=pos_pion_j1[0]
  86.                     refy=pos_pion_j1[1]
  87.                     list_grille[refx][refy]=0
  88.                     grid.delete(cercle1)
  89.                     list_grille[pos_pion_j1[0]][pos_pion_j1[1]]=0
  90.                     pos_pion_j1[0]=xm
  91.                     pos_pion_j1[1]=ym
  92.                     list_grille[xm][ym]='pion_j1'
  93.                     cercle1=grid.create_oval(x0,y0,X,Y,outline='black',fill='red')
  94.                     grid.bind("<Button-1>", callback_mur)
  95.  
  96.  
  97.  
  98.     else:
  99.         if xm in range(pos_pion_j2[0]-1,pos_pion_j2[0]+2):
  100.             if ym in range(pos_pion_j2[1]-1,pos_pion_j2[1]+2):
  101.                 if check_move(xm,ym)==True:
  102.                     refx=pos_pion_j2[0]
  103.                     refy=pos_pion_j2[1]
  104.                     list_grille[refx][refy]=0
  105.                     grid.delete(cercle2)
  106.                     list_grille[pos_pion_j2[0]][pos_pion_j2[1]]=0
  107.                     pos_pion_j2[0]=xm
  108.                     pos_pion_j2[1]=ym
  109.                     list_grille[xm][ym]='pion_j2'
  110.                     cercle2=grid.create_oval(x0,y0,X,Y,outline='black',fill='blue')
  111.                     grid.bind("<Button-1>", callback_mur)
  112.  
  113. def callback_mur(event):
  114.     global grid
  115.     global gamemode
  116.     global pionref
  117.     x,y=pos_converter(event.x),pos_converter(event.y)
  118.     x0=(x-1)*100
  119.     y0=(y-1)*100
  120.     X=x*100
  121.     Y=y*100
  122.     if check_move(x,y)==True:
  123.         mur(x,y)
  124.         grid.create_rectangle(x0,y0,X,Y,outline='black',fill='black')
  125.         if pionref=='pion_j1':
  126.             if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
  127.                 game_loop = False
  128.  
  129.         grid.bind('<Button-1>',callback_move)
  130.         if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
  131.                     main_window.destroy()
  132.                     showwarning('player 2 win')
  133.         if check_defeat(pos_pion_j2[0],pos_pion_j2[1])==True:
  134.                     main_window.destroy()
  135.                     showwarning('player 1 win')
  136.  
  137.         if gamemode=='solo':
  138.  
  139.             if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
  140.  
  141.                     main_window.destroy()
  142.                     showwarning('AI win')
  143.             ai_turn()
  144.         else:
  145.             if pionref=='pion_j1':
  146.                 pionref=''
  147.  
  148.                 user2_turn()
  149.             else:
  150.  
  151.                 user1_turn()
  152.  
  153. def user1_turn():
  154.     global pionref
  155.     global game_loop
  156.     pionref='pion_j1'
  157.  
  158.     grid.focus_set()
  159.     if check_defeat(pos_pion_j1[0],pos_pion_j1[1])==True:
  160.         game_loop = False
  161.     else :
  162.         grid.bind('<Button-1>',callback_move)
  163.         grid.mainloop()
  164.  
  165. def user2_turn():
  166.     global pionref
  167.     global game_loop
  168.     grid.focus_set()
  169.     if check_defeat(pos_pion_j2[0],pos_pion_j2[1])==True:
  170.             game_loop = False
  171.     else :
  172.         grid.bind('<Button-1>',callback_move)
  173.         grid.mainloop()
  174.  
  175. def ai_turn():
  176.     global pos_pion_j2
  177.     global cercle2
  178.     global game_loop
  179.     global randmuraix
  180.     global randmuraiy
  181.     if check_defeat(pos_pion_j2[0],pos_pion_j2[1])==True:
  182.         game_loop = False
  183.     else :
  184.         refx=pos_pion_j2[0]
  185.         refy=pos_pion_j2[1]
  186.         randxmove=randint(pos_pion_j2[0]-1,pos_pion_j2[0]+1)
  187.         randymove=randint(pos_pion_j2[1]-1,pos_pion_j2[1]+1)
  188.         while check_move(randxmove,randymove)==False:
  189.             randxmove=randint(pos_pion_j2[0]-1,pos_pion_j2[0]+1)
  190.             randymove=randint(pos_pion_j2[1]-1,pos_pion_j2[1]+1)
  191.         pos_pion_j2[0]=randxmove
  192.         pos_pion_j2[1]=randymove
  193.         list_grille[randxmove][randymove]='pion_j2'
  194.         list_grille[refx][refy]=0
  195.         grid.delete(cercle2)
  196.         x,y=pos_pion_j2[0],pos_pion_j2[1]
  197.         x0=(x-1)*100
  198.         y0=(y-1)*100
  199.         X=x*100
  200.         Y=y*100
  201.         cercle2=grid.create_oval(x0,y0,X,Y,outline='black',fill='blue')
  202.         randmuraix=randint(0,6)
  203.         randmuraiy=randint(0,6)
  204.         while check_move(randmuraix,randmuraiy)== False:
  205.             randmuraix=randint(0,6)
  206.             randmuraiy=randint(0,6)
  207.  
  208.         mur_ai()
  209.         x,y=randmuraix,randmuraiy
  210.         x0=(x-1)*100
  211.         y0=(y-1)*100
  212.         X=x*100
  213.         Y=y*100
  214.         grid.create_rectangle(x0,y0,X,Y,outline='black',fill='black')
  215.         grid.bind('<Button-1>',callback_move)
  216.  
  217.  
  218. main_window = Tk.Tk()
  219. can_start = Tk.Canvas(main_window, width=900, height=900)
  220. can_start.pack()
  221. tk_img = ImageTk.PhotoImage(file ='logo pysola.gif')
  222. can_start.create_image(450, 450, image=tk_img)
  223.  
  224. def menu_start():
  225.  
  226.     solo_btn = Button(main_window,text='\n \n \n                   Solo                  \n \n \n',command=game_solo,cursor='star',bg='grey')
  227.     button_solo = can_start.create_window(450, 300,anchor='n', window=solo_btn)
  228.     multi_btn = Button(main_window,text='\n \n \n                  Multi                 \n \n \n',command=game_multi,bg='grey')
  229.     button_multi= can_start.create_window(450,500,window=multi_btn,anchor='n')
  230.  
  231.  
  232. def game_solo():
  233.         global label
  234.         global can_start
  235.         global grid
  236.         global cercle1
  237.         global cercle2
  238.         global gamemode
  239.         global main_window
  240.         gamemode='solo'
  241.         main_window.destroy()
  242.         main_window=Tk.Tk()
  243.         grid = Canvas(main_window,width = 700, height =700,bg='white')
  244.         grid.pack(pady=5)
  245.         grid.create_line(1,1,1,700)
  246.         for i in range(0,700):
  247.             for h in range (0,700):
  248.                 if i/100 in range(0,7):
  249.                     grid.create_line(i,0,i,700)
  250.                     if h/100 in range(0,7):
  251.                         grid.create_line(0,h,700,h)
  252.         cercle1=grid.create_oval(0,300,100,400,outline='black',fill='red')
  253.         cercle2=grid.create_oval(600,300,700,400,outline='black',fill='blue')
  254.         tkmessagebox.showinfo('Tour joueur 1','Deplacez vous en cliquant/pavnum')
  255.         while game_loop== True:
  256.             user1_turn()
  257.  
  258. def game_multi():
  259.         global can_start
  260.         global grid
  261.         global cercle1
  262.         global cercle2
  263.         global gamemode
  264.         gamemode=''
  265.         can_start.destroy()
  266.  
  267.         grid = Canvas(main_window,width = 700, height =700,bg='white')
  268.         grid.pack(pady=5)
  269.         grid.create_line(1,1,1,700)
  270.         for i in range(0,700):
  271.             for h in range (0,700):
  272.                 if i/100 in range(0,7):
  273.                     grid.create_line(i,0,i,700)
  274.                     if h/100 in range(0,7):
  275.                         grid.create_line(0,h,700,h)
  276.         cercle1=grid.create_oval(0,300,100,400,outline='black',fill='red')
  277.         cercle2=grid.create_oval(600,300,700,400,outline='black',fill='blue')
  278.         tkmessagebox.showinfo('Tour joueur 1','Deplacez vous en cliquant/pavnum')
  279.         while game_loop== True:
  280.             user1_turn()
  281.  
  282. menu_start()
  283. main_window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement