| 1 | importˆàrandomˆàasˆàrnd |
|---|
| 2 | importˆàTkinterˆàasˆàtk |
|---|
| 3 | importˆàsnake |
|---|
| 4 | |
|---|
| 5 | directions =ˆà[(0,1),ˆà(1,0),ˆà(0,-1),ˆà(-1,0)] |
|---|
| 6 | |
|---|
| 7 | classˆàDict(dict): |
|---|
| 8 | ˆà ˆà """Create a dictionary.""" |
|---|
| 9 | ˆà ˆà pass |
|---|
| 10 | |
|---|
| 11 | classˆàCell(object): |
|---|
| 12 | ˆà ˆà """Cells. |
|---|
| 13 | |
|---|
| 14 | ˆà ˆà Atributes: |
|---|
| 15 | ˆà ˆà - 'x' - absciss of the cell in field |
|---|
| 16 | ˆà ˆà - 'y' - ordinate of the cell in field |
|---|
| 17 | ˆà ˆà - 'canvas' - Widget the cell belongs to |
|---|
| 18 | ˆà ˆà - 'snake' - snake the cell belongs to, possible values: |
|---|
| 19 | ˆà ˆà ˆà ˆà snake |
|---|
| 20 | ˆà ˆà ˆà ˆà None |
|---|
| 21 | ˆà ˆà ˆà ˆà 'my'ˆà ˆà ˆà ˆà \ |
|---|
| 22 | ˆà ˆà ˆà ˆà 'enemy'ˆà ˆà ˆà/ˆà for patterns only |
|---|
| 23 | |
|---|
| 24 | ˆà ˆà - 'type' - type of the cell, possible values: |
|---|
| 25 | ˆà ˆà ˆà ˆà 'empty' |
|---|
| 26 | ˆà ˆà ˆà ˆà 'wall' |
|---|
| 27 | ˆà ˆà ˆà ˆà 'body' |
|---|
| 28 | ˆà ˆà ˆà ˆà 'head' |
|---|
| 29 | ˆà ˆà ˆà ˆà 'tail' |
|---|
| 30 | ˆà ˆà ˆà ˆà 'any'ˆà } for patterns only |
|---|
| 31 | ˆà ˆà ˆà ˆà 'void' } for cells out of the field |
|---|
| 32 | ˆà ˆà """ |
|---|
| 33 | ˆà ˆà defˆà__init__(self,ˆàx,ˆày,ˆàcanvas =ˆàNone): |
|---|
| 34 | ˆà ˆà ˆà ˆà """Initialyze the cell with default parameters: |
|---|
| 35 | ˆà ˆà ˆà ˆà ˆà ˆà type = 'empty' |
|---|
| 36 | ˆà ˆà ˆà ˆà ˆà ˆà snake = None""" |
|---|
| 37 | ˆà ˆà ˆà ˆà self.x =ˆàx |
|---|
| 38 | ˆà ˆà ˆà ˆà self.y =ˆày |
|---|
| 39 | ˆà ˆà ˆà ˆà self.canvas =ˆàcanvas |
|---|
| 40 | ˆà ˆà ˆà ˆà self.snake =ˆàNone |
|---|
| 41 | ˆà ˆà ˆà ˆà self.type =ˆà'empty' |
|---|
| 42 | ˆà ˆà ˆà ˆà return |
|---|
| 43 | ˆà ˆà |
|---|
| 44 | ˆà ˆà defˆàredraw(self,ˆàoffset,ˆàc_size): |
|---|
| 45 | ˆà ˆà ˆà ˆà """Draw a cell based on it content""" |
|---|
| 46 | ˆà ˆà ˆà ˆà x0=offset[0]ˆà+ˆàself.x*c_size |
|---|
| 47 | ˆà ˆà ˆà ˆà y0=offset[1]ˆà+ˆàself.y*c_size |
|---|
| 48 | ˆà ˆà ˆà ˆà x1=offset[0]ˆà+ˆà(self.x+1)*c_size |
|---|
| 49 | ˆà ˆà ˆà ˆà y1=offset[1]ˆà+ˆà(self.y+1)*c_size |
|---|
| 50 | ˆà ˆà ˆà ˆà x2=offset[0]ˆà+ˆà(self.x+1/2.0)*c_size |
|---|
| 51 | ˆà ˆà ˆà ˆà ifˆàself.type ==ˆà'wall': |
|---|
| 52 | ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill="grey") |
|---|
| 53 | ˆà ˆà ˆà ˆà ˆà ˆà pass |
|---|
| 54 | ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'empty': |
|---|
| 55 | ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill="black") |
|---|
| 56 | ˆà ˆà ˆà ˆà ˆà ˆà pass |
|---|
| 57 | ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'body': |
|---|
| 58 | ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill=self.snake.color) |
|---|
| 59 | ˆà ˆà ˆà ˆà ˆà ˆà pass |
|---|
| 60 | ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'head': |
|---|
| 61 | ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_oval(x0,ˆày0,ˆàx1,ˆày1,ˆàfill=self.snake.color) |
|---|
| 62 | ˆà ˆà ˆà ˆà ˆà ˆà pass |
|---|
| 63 | ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'tail': |
|---|
| 64 | ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_polygon(x0,ˆày0,ˆàx1,ˆày0,ˆàx2,ˆày1,ˆàfill=self.snake.color) |
|---|
| 65 | ˆà ˆà ˆà ˆà ˆà ˆà pass |
|---|
| 66 | ˆà ˆà ˆà ˆà return |
|---|
| 67 | ˆà ˆà |
|---|
| 68 | ˆà ˆà defˆà__eq__(self,ˆàpattern): |
|---|
| 69 | ˆà ˆà ˆà ˆà """Check the equaliation of the cell to the pattern cell.""" |
|---|
| 70 | ˆà ˆà ˆà ˆà ifˆàpattern.type ==ˆà'any': |
|---|
| 71 | ˆà ˆà ˆà ˆà ˆà ˆà returnˆàTrue |
|---|
| 72 | ˆà ˆà ˆà ˆà ifˆàpattern.type !=ˆàself.type: |
|---|
| 73 | ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse |
|---|
| 74 | ˆà ˆà ˆà ˆà ifˆàpattern.snake_type ==ˆà'my'ˆàandˆàpattern.snake !=ˆàself.snake: |
|---|
| 75 | ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse |
|---|
| 76 | ˆà ˆà ˆà ˆà elifˆàpattern.snake_type ==ˆà'enemy'ˆàandˆàpattern.snake ==ˆàself.snake: |
|---|
| 77 | ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse |
|---|
| 78 | ˆà ˆà ˆà ˆà returnˆàTrue |
|---|
| 79 | ˆà ˆà |
|---|
| 80 | ˆà ˆà defˆà__ne__(self,ˆàpattern): |
|---|
| 81 | ˆà ˆà ˆà ˆà """Check the discrepancy of the cell to the pattern cell.""" |
|---|
| 82 | ˆà ˆà ˆà ˆà returnˆànotˆàselfˆà==ˆàpattern |
|---|
| 83 | ˆà ˆà |
|---|
| 84 | ˆà ˆà defˆàclear(self): |
|---|
| 85 | ˆà ˆà ˆà ˆà """Change the cell parameters back to default.""" |
|---|
| 86 | ˆà ˆà ˆà ˆà self.snake =ˆàNone |
|---|
| 87 | ˆà ˆà ˆà ˆà self.type =ˆà'empty' |
|---|
| 88 | ˆà ˆà ˆà ˆà return |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | classˆàEngine(object): |
|---|
| 92 | ˆà ˆà """Engine |
|---|
| 93 | |
|---|
| 94 | ˆà ˆà Atributes: |
|---|
| 95 | |
|---|
| 96 | ˆà ˆà - 'field' - game field: |
|---|
| 97 | ˆà ˆà ˆà ˆà 'field.w' - width of the field count in cells |
|---|
| 98 | ˆà ˆà ˆà ˆà 'field.h' - hight of the field count in cells |
|---|
| 99 | ˆà ˆà - 'canvas' - Widget game field is showing on |
|---|
| 100 | ˆà ˆà - 'snakes' - list of snakes loaded |
|---|
| 101 | ˆà ˆà - 'psnakes' - list of snakes loaded in previous match, if other snakes are not loaded |
|---|
| 102 | ˆà ˆà - 'start_snake_length' - starting length of the snake""" |
|---|
| 103 | ˆà ˆà |
|---|
| 104 | ˆà ˆà defˆà__init__(self,ˆàcanvas): |
|---|
| 105 | ˆà ˆà ˆà ˆà """Initialyze the engine: |
|---|
| 106 | ˆà ˆà ˆà ˆà start_snake_length = 10""" |
|---|
| 107 | ˆà ˆà ˆà ˆà self.canvas =ˆàcanvas |
|---|
| 108 | ˆà ˆà ˆà ˆà self.snakes =ˆà[None,ˆàNone,ˆàNone,ˆàNone] |
|---|
| 109 | ˆà ˆà ˆà ˆà self.psnakes =ˆà[None,ˆàNone,ˆàNone,ˆàNone] |
|---|
| 110 | ˆà ˆà ˆà ˆà self.init_field() |
|---|
| 111 | ˆà ˆà ˆà ˆà self.start_snake_length =ˆà10 |
|---|
| 112 | ˆà ˆà ˆà ˆà return |
|---|
| 113 | ˆà ˆà |
|---|
| 114 | ˆà ˆà defˆàinit_fieldˆà(self): |
|---|
| 115 | ˆà ˆà ˆà ˆà """Initialyze the field: |
|---|
| 116 | ˆà ˆà ˆà ˆà width = 31 |
|---|
| 117 | ˆà ˆà ˆà ˆà hieght = 31 |
|---|
| 118 | ˆà ˆà ˆà ˆà perimeter is made by walls""" |
|---|
| 119 | ˆà ˆà ˆà ˆà self.field =ˆàDict() |
|---|
| 120 | ˆà ˆà ˆà ˆà self.field.w =ˆà31 |
|---|
| 121 | ˆà ˆà ˆà ˆà self.field.h =ˆà31 |
|---|
| 122 | ˆà ˆà ˆà ˆà f_w =ˆàself.field.w |
|---|
| 123 | ˆà ˆà ˆà ˆà f_h =ˆàself.field.h |
|---|
| 124 | ˆà ˆà ˆà ˆà forˆàx inˆàrange(f_w): |
|---|
| 125 | ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(f_h): |
|---|
| 126 | ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆày]ˆà=ˆàCell(x,ˆày,ˆàself.canvas) |
|---|
| 127 | ˆà ˆà ˆà ˆà forˆày inˆàrange(f_h): |
|---|
| 128 | ˆà ˆà ˆà ˆà ˆà ˆà self.field[0,ˆày].type =ˆà'wall' |
|---|
| 129 | ˆà ˆà ˆà ˆà ˆà ˆà self.field[f_w-1,ˆày].type =ˆà'wall' |
|---|
| 130 | ˆà ˆà ˆà ˆà forˆàx inˆàrange(1,f_w-1): |
|---|
| 131 | ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆà0].type =ˆà'wall' |
|---|
| 132 | ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆàf_h-1].type =ˆà'wall' |
|---|
| 133 | ˆà ˆà ˆà ˆà self.refill() |
|---|
| 134 | ˆà ˆà ˆà ˆà self.redraw() |
|---|
| 135 | ˆà ˆà ˆà ˆà return |
|---|
| 136 | ˆà ˆà |
|---|
| 137 | ˆà ˆà defˆàstep(self): |
|---|
| 138 | ˆà ˆà ˆà ˆà """Do the step of the game.""" |
|---|
| 139 |
|---|