def __init__(self, start_pos, vel=[UNIT, 0], unit_size=UNIT, color='Gold', images=None): '''Constructor''' if images is None: self.images = dict([ (key, None) for key in ['head', 'neck', 'tail', 'straight', 'left', 'right'] ]) else: self.images = images self.head = sprite.Sprite('head', start_pos, vel, 0, (unit_size, unit_size), color, draw_method=draw_head, image=self.images['head'], update_method=sprite.update_toroid) self.neck = sprite.Sprite('neck', start_pos, vel, self.head.rot, (unit_size, unit_size), color, image=self.images['neck']) self.body = [] self.tail = None self.size = (unit_size, unit_size) self.color = color self.controls = dict([('left', 1), ('right', -1)])
def new_food(): '''Put new food down''' food_pos = rand_pos() while snake.is_on(sprite.Sprite(pos=food_pos, size=[UNIT, UNIT])): food_pos = rand_pos() return sprite.Sprite('food', food_pos, [0, 0], 0, [UNIT, UNIT], FOOD_COLOR, line_color=FOOD_COLOR, draw_method=sprite.draw_circle, image=images['food'])
def update(self): '''Update the snakes position, try to eat food, and grow''' new_seg = sprite.Sprite('seg', self.head.pos, [0, 0], self.head.rot, self.size, self.color, image=self.images['straight']) self.head.update((WIDTH, HEIGHT)) if self.neck.rot != self.head.rot: if (self.head.rot - self.neck.rot) % 4 == 1: new_seg.image = self.images['left'] else: new_seg.image = self.images['right'] self.neck.rot = self.head.rot self.neck.pos = self.head.pos if self.eat_food(): self.body.append(new_seg) else: self.body.append(new_seg) old_seg = self.body.pop(0) if self.tail: self.tail.pos = old_seg.pos self.tail.rot = old_seg.rot if self.tail is None and len(self.body) > 0: self.tail = self.body.pop(0) self.tail.name = 'tail' self.tail.draw_method = draw_tail #self.tail.image = None self.tail.image = self.images['tail']
def make_brick(grid_pos, color): '''Makes a brick''' return sprite.Sprite(name=color, pos=grid_to_continuous(grid_pos), size=BRICK_SIZE, color=simplegui.COLOR_PALETTE[color], line_width=2, image=images[color])
def new_paddle(paddle_pos, paddle_size=PADDLE_SIZE): '''Makes a paddle''' return sprite.Sprite(name='Paddle', pos=paddle_pos, size=paddle_size, color=simplegui.COLOR_PALETTE['White'], image=None, update_method=sprite.update_stay_in_world)
def add_point_sprite(points, brick): '''Adds a point sprite to the world''' point_sprites.append( sprite.Sprite(name=str(points), pos=brick.pos, vel=[0, 2], size=brick.size, color=brick.color, life=30, draw_method=sprite.draw_name))
def make_spare_ball(ball_number): '''Adds a spare ball''' return sprite.Sprite( name='SpareBall', pos=(SPARE_BALL_POS[0] + (ball_number + 1) * BALL_SIZE[0], SPARE_BALL_POS[1]), size=BALL_SIZE, color=simplegui.COLOR_PALETTE['White'], image=None, draw_method=sprite.draw_circle)
def new_ball(): '''Returns a new ball''' return sprite.Sprite(name='Ball', pos=grid_to_continuous([ GRID_WIDTH / 2, GRID_HEIGHT - (GRID_HEIGHT - NUM_ROWS - TOP_GAP) / 2 ]), vel=random_vel(ball_speed, [-45, -135]), size=BALL_SIZE, color=simplegui.COLOR_PALETTE['White'], image=None, draw_method=sprite.draw_circle, update_method=sprite.update_bounce)
def new_game(): '''Resets the board''' global brick_rows, paddle, ball, spare_balls, gutter, game_over, point_sprites, score, level game_over = False score = 0 level = 1 brick_rows = new_bricks() ball = new_ball() spare_balls = [make_spare_ball(i) for i in range(SPARE_BALLS)] paddle = new_paddle(grid_to_continuous([GRID_WIDTH / 2, GRID_HEIGHT - 2])) gutter = sprite.Sprite(pos=(WIDTH / 2, HEIGHT), size=(WIDTH, 2)) point_sprites = []
def new_cursor(pos): '''Makes a new cursor''' return sprite.Sprite('block', pos, size=(BLOCK_H, BLOCK_H), image=images[cursor_color])