Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.fbb.msu.ru/trac/snake/changeset/13%3A5755534e5a8b
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 09:25:31 2016
Êîäèðîâêà: IBM-866
Changeset 13:5755534e5a8b òÀÓ Python Battle

Changeset 13:5755534e5a8b


Ignore:
Timestamp:
12/13/10 00:11:13 (5 years ago)
Author:
Alex Martynov <martiran@òÀæ>
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.
Message:

Automated merge with òÀËssh://kodomo.fbb.msu.ru/snake

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • engine.py

    r11 r13 ˆà
    ˆà1import random as rndˆà
    ˆà2ˆà
    13directions = [(0,1), (1,0), (0,-1), (-1,0)]ˆà
    24rtm = [[0, -1], [1, 0]]ˆà
    òÀæ òÀæ ˆà
    2931        self.w = min(canvas.height, canvas.width)ˆà
    3032        self.h = min(canvas.height, canvas.width)ˆà
    31ˆà        self.snakes = []ˆà
    ˆà33        self.snakes = [None, None, None, None]ˆà
    3234        self.init_field()ˆà
    3335        returnˆà
    òÀæ òÀæ ˆà
    4648    def step(self):ˆà
    4749        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ˆà
    5158        self.redraw()    ˆà
    5259        returnˆà
    5360    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ˆà
    5499        passˆà
    55100    def create_snake(self, snake_number):ˆà
    56101        passˆà
    57102    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ˆà
    58117        passˆà
    59118    def redraw(self):ˆà
    òÀæ òÀæ ˆà
    62121        snake.legal_dir = []ˆà
    63122        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)):ˆà
    66125                snake.legal_dir.append(direction)ˆà
    67126                returnˆà
  • engine.py

    r12 r13 ˆà
    3939            for y in range(21):ˆà
    4040                self.field[x, y] = Cell(x, y, self.canvas)ˆà
    41ˆà                passˆà
    42ˆà            passˆà
    4341        for y in range(21):ˆà
    4442            self.field[0, y].type = 'wall'ˆà
    4543            self.field[20, y].type = 'wall'ˆà
    46ˆà            passˆà
    4744        for x in range(1,20):ˆà
    4845            self.field[x, 0].type = 'wall'ˆà
    4946            self.field[x, 20].type = 'wall'ˆà
    50ˆà            passˆà
    5147        returnˆà
    5248    def step(self):ˆà
Note: See TracChangeset for help on using the changeset viewer.