Changeset 13:5755534e5a8b
- Timestamp:
- 12/13/10 00:11:13 (5 years ago)
- Branch:
- default
- Parents:
- 11:0ece930a4d1b (diff), 12:a1a766440639 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
engine.py
r11 r13 ˆà ˆà 1 import random as rndˆà ˆà 2 ˆà 1 3 directions = [(0,1), (1,0), (0,-1), (-1,0)]ˆà 2 4 rtm = [[0, -1], [1, 0]]ˆà òÀæ òÀæ ˆà 29 31 self.w = min(canvas.height, canvas.width)ˆà 30 32 self.h = min(canvas.height, canvas.width)ˆà 31 ˆà self.snakes = [ ]ˆàˆà 33 self.snakes = [None, None, None, None]ˆà 32 34 self.init_field()ˆà 33 35 returnˆà òÀæ òÀæ ˆà 46 48 def step(self):ˆà 47 49 for snake in self.snakes:ˆà 48 ˆà self.legal_moves()ˆà 49 ˆà self.move_snake(snake)ˆà 50 ˆà self.refill()ˆà ˆà 50 if snake == None:ˆà ˆà 51 passˆà ˆà 52 else:ˆà ˆà 53 self.legal_moves()ˆà ˆà 54 self.move_snake(snake)ˆà ˆà 55 self.refill()ˆà ˆà 56 passˆà ˆà 57 passˆà 51 58 self.redraw() ˆà 52 59 returnˆà 53 60 def move_snake(self, snake):ˆà ˆà 61 applied_dir = Noneˆà ˆà 62 for rule in snake.rules:ˆà ˆà 63 if applied_dir != None:ˆà ˆà 64 choose_dir = []ˆà ˆà 65 for direction in snake.legal_dir:ˆà ˆà 66 rule.direction = directionˆà ˆà 67 if rule.applies() == True:ˆà ˆà 68 choose_dir.append(direction)ˆà ˆà 69 passˆà ˆà 70 passˆà ˆà 71 if len(choose_move) != 0:ˆà ˆà 72 applied_dir = choose_dir[int(rnd.random()*len(choose_dir))]ˆà ˆà 73 passˆà ˆà 74 passˆà ˆà 75 else:ˆà ˆà 76 dir_sell = self.field[snake.sells[0].y + applied_dir[0], snake.sells[0].x + applied_dir[1]]ˆà ˆà 77 if dir_sell.type == 'empty':ˆà ˆà 78 snake.sells.insert(0,dir_sell)ˆà ˆà 79 del snake.sells[-1]ˆà ˆà 80 passˆà ˆà 81 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):ˆà ˆà 82 snake.sells.insert(0,dir_sell)ˆà ˆà 83 del dir_cell.snake.sells[-1]ˆà ˆà 84 passˆà ˆà 85 breakˆà ˆà 86 passˆà ˆà 87 if applied_dir == None:ˆà ˆà 88 applied_dir = legal_dir[int(rnd.random()*len(legal_dir))]ˆà ˆà 89 dir_sell = self.field[snake.sells[0].y + applied_dir[0], snake.sells[0].x + applied_dir[1]]ˆà ˆà 90 if dir_sell.type == 'empty':ˆà ˆà 91 snake.sells.insert(0,dir_sell)ˆà ˆà 92 del snake.sells[-1]ˆà ˆà 93 passˆà ˆà 94 elif (dir_cell.type == 'tail' and dir_cell.snake != snake):ˆà ˆà 95 snake.sells.insert(0,dir_sell)ˆà ˆà 96 del dir_cell.snake.sells[-1]ˆà ˆà 97 passˆà ˆà 98 passˆà 54 99 passˆà 55 100 def create_snake(self, snake_number):ˆà 56 101 passˆà 57 102 def refill(self):ˆà ˆà 103 for x in range(1,20):ˆà ˆà 104 for y in range(1,20):ˆà ˆà 105 self.field[x, y].type = 'empty'ˆà ˆà 106 passˆà ˆà 107 passˆà ˆà 108 for snake in self.snakes:ˆà ˆà 109 if snake == None:ˆà ˆà 110 passˆà ˆà 111 else:ˆà ˆà 112 snake.sells[0].type = 'head'ˆà ˆà 113 snake.sells[-1].type = 'tail'ˆà ˆà 114 snake.sells[1:-1].type = 'body'ˆà ˆà 115 passˆà ˆà 116 passˆà 58 117 passˆà 59 118 def redraw(self):ˆà òÀæ òÀæ ˆà 62 121 snake.legal_dir = []ˆà 63 122 for direction in directions:ˆà 64 ˆà dir_ cell = field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]]ˆà65 ˆà if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake == 'enemy')):ˆàˆà 123 dir_sell = self.field[snake.sells[0].y + direction[0], snake.sells[0].x + direction[1]]ˆà ˆà 124 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)):ˆà 66 125 snake.legal_dir.append(direction)ˆà 67 126 returnˆà -
engine.py
r12 r13 ˆà 39 39 for y in range(21):ˆà 40 40 self.field[x, y] = Cell(x, y, self.canvas)ˆà 41 ˆà passˆà42 ˆà passˆà43 41 for y in range(21):ˆà 44 42 self.field[0, y].type = 'wall'ˆà 45 43 self.field[20, y].type = 'wall'ˆà 46 ˆà passˆà47 44 for x in range(1,20):ˆà 48 45 self.field[x, 0].type = 'wall'ˆà 49 46 self.field[x, 20].type = 'wall'ˆà 50 ˆà passˆà51 47 returnˆà 52 48 def step(self):ˆà
Note: See TracChangeset
for help on using the changeset viewer.