Untitled


SUBMITTED BY: Kiliandeca

DATE: Aug. 1, 2016, 3:41 p.m.

FORMAT: Python 3

SIZE: 2.2 kB

HITS: 540

  1. from tkinter import *
  2. carte = [
  3. [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
  4. [1, 0, 1, 0, 1, 1, 0, 1, 0, 1],
  5. [0, 0, 0, 0, 1, 2, 0, 1, 0, 1],
  6. [0, 1, 0, 1, 1, 1, 1, 1, 0, 2],
  7. [0, 1, 0, 0, 0, 0, 0, 1, 1, 1],
  8. [0, 1, 1, 1, 0, 1, 0, 0, 0, 0],
  9. [0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
  10. [1, 1, 0, 1, 0, 0, 0, 0, 1, 0],
  11. [2, 1, 0, 1, 0, 1, 1, 0, 1, 1],
  12. [0, 0, 0, 1, 0, 2, 1, 0, 0, "f"]]
  13. xjoueur = 0
  14. yjoueur = 0
  15. tx = 0
  16. ty = 0
  17. score = 0
  18. fen = Tk()
  19. canva = Canvas(fen, width=200, height=200, bg="ivory")
  20. label = Label(fen, text="Score: " + str(score))
  21. label.pack(side = TOP)
  22. canva.pack()
  23. def affichercarte():
  24. canva.delete("all")
  25. global xjoueur, yjoueur, rect
  26. rect = canva.create_rectangle(xjoueur*20, yjoueur*20, xjoueur*20+20, yjoueur*20+20,fill="red")
  27. y = 0
  28. for ligne in carte:
  29. x = 0
  30. for colone in ligne:
  31. if carte[y][x] == 1:
  32. m = canva.create_rectangle(x*20, y*20, x*20+20, y*20+20, fill="black")
  33. canva.pack()
  34. if carte[y][x] == 2:
  35. m = canva.create_rectangle(x*20+5, y*20+5, x*20+20-5, y*20+20-5, fill="yellow")
  36. if carte[y][x] == "f":
  37. canva.create_rectangle(x*20, y*20, x*20+20, y*20+20, fill="green")
  38. x+= 1
  39. y += 1
  40. def mouvement(direction):
  41. global xjoueur,yjoueur,tx,ty
  42. tx = xjoueur
  43. ty = yjoueur
  44. if direction == "z" and yjoueur>0:
  45. ty -= 1
  46. if direction == "s" and yjoueur<9:
  47. ty += 1
  48. if direction == "d" and xjoueur<9:
  49. tx += 1
  50. if direction == "q" and xjoueur>0:
  51. tx -= 1
  52. if carte[ty][tx] != 1:
  53. return True
  54. def clavier(touche):
  55. global yjoueur, xjoueur, ty, tx, score
  56. if mouvement(touche.keysym):
  57. xjoueur = tx
  58. yjoueur = ty
  59. if carte[yjoueur][xjoueur] == 2:
  60. score += 1
  61. carte[yjoueur][xjoueur] = 0
  62. affichercarte()
  63. label.config(text="Score: " + str(score))
  64. canva.coords(rect, xjoueur*20, yjoueur*20, xjoueur*20+20, yjoueur*20+20)
  65. affichercarte()
  66. canva.focus_set()
  67. canva.bind("<Key>", clavier)
  68. canva.pack()
  69. fen.mainloop()

comments powered by Disqus