1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| from tkinter import Tk, Canvas
from numpy import zeros
CASE_SIZE = 20
BALL_SIZE = 8 # radius
MOTIONS = {
'Up': (0, -1),
'Down': (0, 1),
'Left': (-1, 0),
'Right':(1, 0)
}
class Map():
def __init__(self):
self.map_game = zeros((10,10))
def get_map(self):
return self.map_game
class Ball():
def __init__(self,object_map,ball_pos,color,moving_status):
self.object_map = object_map
self.ball_pos = ball_pos
self.color = color
self.moving_status = moving_status
self.graphic = None
def check_map(self,vector_dir):
map_size = self.object_map.get_map()
row = self.ball_pos[0] + vector_dir[1]
col = self.ball_pos[1] + vector_dir[0]
if col >= 0 and col < map_size.shape[1] and \
row >= 0 and row < map_size.shape[0]:
return True
def set_pos(self,vector_dir,ball_number):
self.ball_pos = [self.ball_pos[0]+vector_dir[1],self.ball_pos[1]+vector_dir[0]]
self.graphic.update_graphic(vector_dir,ball_number)
def get_pos(self):
return self.ball_pos
def get_color(self):
return self.color
def get_moving_status(self):
return self.moving_status
class GUI():
def __init__(self,map_object, root,list_ball = None):
self.init_canvas(root, map_object)
self.init_balls(list_ball)
def init_canvas(self,root, map_object):
self.root = root
map_size = map_object.get_map()
lar = map_size.shape[0]*CASE_SIZE
hau = map_size.shape[1]*CASE_SIZE
self.can_game = Canvas(self.root,width=lar,height=hau,bg="black")
self.can_game.pack(padx=10,pady=10)
self.can_game.focus_set()
def init_balls(self,list_ball):
self.list_ball = list_ball
self.object_list = list_ball
self.object_graphic_list = []
for elt in self.object_list:
graphic_ball = self.ball_creation(elt.get_pos(),elt.get_color())
self.object_graphic_list.append(graphic_ball)
elt.graphic = self
if elt.get_moving_status() == 1:
self.ball = elt
for key in MOTIONS:
self.can_game.bind('<%s>' % key, self.keyboard_event)
def ball_creation(self,pos,coul):
row = pos[0]
col = pos[1]
return self.cercle(col*CASE_SIZE+CASE_SIZE/2,row*CASE_SIZE+CASE_SIZE/2, BALL_SIZE,coul)
def cercle(self,x, y, r, coul ='black'):
boule = self.can_game.create_oval(x-r, y-r, x+r, y+r, fill=coul)
return boule
def keyboard_event(self,event):
vector_dir = MOTIONS[event.keysym]
if self.ball.check_map(vector_dir):
self.ball.set_pos(vector_dir,0)
def init_auto_balls(self,ball_number,init_vector):
considered_ball = self.object_list[ball_number]
if considered_ball.check_map(init_vector):
considered_ball.set_pos(init_vector,ball_number)
self.can_game.after(1000,self.init_auto_balls(ball_number,init_vector))
def update_graphic(self,vector_dir,ball_number):
self.can_game.move(self.object_graphic_list[ball_number],vector_dir[0]*CASE_SIZE,vector_dir[1]*CASE_SIZE)
# --- Main ---
root = Tk()
object_map = Map()
ball1 = Ball(object_map,(0,0),'yellow',1)
ball2 = Ball(object_map,(8,8),'red',0)
GUI_object = GUI(object_map,root,[ball1,ball2])
GUI_object.init_auto_balls(1,(-1,0))
root.mainloop() |
Partager