def draw_border(canvas, pos, size, line_width, color, shift=4): """ Draw a rounded rectangle. :param canvas: simplegui.Canvas :param pos: (int or float, int or float) or [int or float, int or float] :param size: (int or float, int or float) or [int or float, int or float] :param line_width: int >= 0 :param color: str :param shift: int """ assert_position(pos) assert_position(size) assert isinstance(line_width, int) or isinstance(line_width, float), \ type(line_width) assert line_width >= 0, line_width assert isinstance(color, str), type(str) assert isinstance(shift, int), type(shift) x0 = pos[0] y0 = pos[1] width = size[0] - 1 height = size[1] - 1 canvas.draw_polygon( ((x0, y0 + shift), (x0 + shift, y0), (x0 + width - shift, y0), (x0 + width, y0 + shift), (x0 + width, y0 + height - shift), (x0 + width - shift, y0 + height), (x0 + shift, y0 + height), (x0, y0 + height - shift)), line_width, color)
def __init__(self, position, velocity, angle_velocity, num, little=False): """ Set an asteroid sprite. :param position: (int or float, int or float) or [int or float, int or float] :param velocity: (int or float, int or float) or [int or float, int or float] :param angle_velocity: int or float :param num: 1, 2 or 3 :param litle: bool """ assert_position(position) assert_position(velocity) assert (isinstance(angle_velocity, int) or isinstance(angle_velocity, float)), type(angle_velocity) assert num in (1, 2, 3) assert isinstance(little, bool), type(little) Sprite.__init__(self, position, velocity, 0, angle_velocity, ('little-asteroid-' if little else 'asteroid-') + str(num)) self.num = num self.little = little
def __init__(self, center, size, radius=None, lifespan=None, animated=False): """ Set informations. If radius is None then use maximum of size components. :param center: (int or float, int or float) or [int or float, int or float] :param size: ((int or float) > 0, (int or float) > 0) or [(int or float) > 0, (int or float) > 0] :param radius: None or ((int or float) > 0) :param lifespan: None or ((int or float) > 0) :param animated: bool """ assert_position(center) assert_position(size, True, True) assert ((radius is None) or ((isinstance(radius, int) or isinstance(radius, float)) and (radius > 0))), radius assert ((lifespan is None) or ((isinstance(lifespan, int) or isinstance(lifespan, float)) and (lifespan > 0))), lifespan assert isinstance(animated, bool), type(animated) self._center = list(center) self._size = list(size) self._radius = (max(size) if radius is None else radius) self._lifespan = (lifespan if lifespan else float('inf')) self._animated = animated
def draw_border(canvas, pos, size, line_width, color, shift=4): """ Draw a rounded rectangle. :param canvas: simplegui.Canvas :param pos: (int or float, int or float) or [int or float, int or float] :param size: (int or float, int or float) or [int or float, int or float] :param line_width: int >= 0 :param color: str :param shift: int """ assert_position(pos) assert_position(size) assert isinstance(line_width, int) or isinstance(line_width, float), \ type(line_width) assert line_width >= 0, line_width assert isinstance(color, str), type(str) assert isinstance(shift, int), type(shift) x0 = pos[0] y0 = pos[1] width = size[0] - 1 height = size[1] - 1 canvas.draw_polygon(((x0, y0 + shift), (x0 + shift, y0), (x0 + width - shift, y0), (x0 + width, y0 + shift), (x0 + width, y0 + height - shift), (x0 + width - shift, y0 + height), (x0 + shift, y0 + height), (x0, y0 + height - shift)), line_width, color)
def vector_to_angle(vector): """ Return the angle (in radians) corresponding to the direction of vector. :param vector: (int or float, int or float) or [int or float, int or float] :return: -math.pi <= float <= math.pi """ assert_position(vector) return math.atan2(vector[1], vector[0])
def in_pos(self, pos): """ If pos is in this card then return True, else return False. :param pos: (int or float, int or float) or [int or float, int or float] :return: bool """ assert_position(pos) return (self.pos_x <= pos[0] < self.pos_x + Card.WIDTH) and (self.pos_y <= pos[1] < self.pos_y + Card.HEIGHT)
def in_pos(self, pos): """ If pos is in this card then return True, else return False. :param pos: (int or float, int or float) or [int or float, int or float] :return: bool """ assert_position(pos) return ((self.pos_x <= pos[0] < self.pos_x + Card.WIDTH) and (self.pos_y <= pos[1] < self.pos_y + Card.HEIGHT))
def expose(self, pos): """ Expose the card pointed by position pos, and check if good cards are exposed. :param pos: (int or float, int or float) or [int or float, int or float] """ assert_position(pos) for card in self.deck: if card.in_pos(pos): # this is the pointed card if not card.exposed: if len(self.selected_cards) == self.nb_repeat_cards: # Good number of exposed cards # Reinit exposed cards for c in self.selected_cards: c.selected = False if not self.new_founded: # but not good cards c.exposed = False self.selected_cards = [] # Expose the pointed card card.exposed = True card.selected = True self.selected_cards.append(card) if len(self.selected_cards) == self.nb_repeat_cards: # Good number of exposed cards # Update the moves counter self.nb_moves += 1 label_moves.set_text('Nb moves: ' + str(self.nb_moves)) # If all exposed cards and pointed card are the same self.new_founded = all([ c.num == self.selected_cards[0].num for c in self.selected_cards[1:] ]) if self.new_founded: # good cards are exposed self.nb_founded += 1 if self.nb_founded == self.nb_different_cards: # Completed game for c in self.selected_cards: c.selected = False break
def __init__(self, position, velocity, angle, media_name): """ Set ship sprite. :param position: (int or float, int or float) or [int or float, int or float] :param velocity: (int or float, int or float) or [int or float, int or float] :param angle: int or float :param media_name: str """ assert_position(position) assert_position(velocity) assert isinstance(angle, int) or isinstance(angle, float), type(angle) assert isinstance(media_name, str), type(media_name) Sprite.__init__(self, position, velocity, angle, 0, media_name) self.thrust = False
def __init__(self, position, velocity, angle, image, image_info): """ Set ship sprite. :param position: (int or float, int or float) or [int or float, int or float] :param velocity: (int or float, int or float) or [int or float, int or float] :param angle: int or float :param image: simplegui.Image :param image_info: ImageInfo """ assert_position(position) assert_position(velocity) assert isinstance(angle, int) or isinstance(angle, float), type(angle) assert isinstance(image_info, ImageInfo), type(image_info) Sprite.__init__(self, position, velocity, angle, 0, image, image_info) self.thrust = False
def __init__(self, position, velocity, angle, angle_velocity, media_name): """ Set sprite. :param position: (int or float, int or float) or [int or float, int or float] :param velocity: (int or float, int or float) or [int or float, int or float] :param angle: int or float :param angle_velocity: int or float :param media_name: str """ assert_position(position) assert_position(velocity) assert isinstance(angle, int) or isinstance(angle, float), type(angle) assert (isinstance(angle_velocity, int) or isinstance(angle_velocity, float)), type(angle_velocity) assert isinstance(media_name, str), type(media_name) if (ricerocks.sounds_active and (media_name in ricerocks.medias._sounds)): sound = ricerocks.medias.get_sound(media_name) sound.rewind() sound.play() self.position = list(position) self.velocity = list(velocity) self.angle = angle self.angle_velocity = angle_velocity self.image = ricerocks.medias.get_image(media_name) img_info = ricerocks.img_infos[media_name] self.animated = img_info.get_animated() self.image_center = img_info.get_center() self.image_draw_size = img_info.get_draw_size() self.image_size = img_info.get_size() self.lifespan = img_info.get_lifespan() self.radius = img_info.get_radius()
def __init__(self, position, velocity, angle, angle_velocity, image, image_info, sound=None): """ Set sprite. :param position: (int or float, int or float) or [int or float, int or float] :param velocity: (int or float, int or float) or [int or float, int or float] :param angle: int or float :param image: simplegui.Image :param image_info: ImageInfo :param sound: simplegui.Sound """ assert_position(position) assert_position(velocity) assert isinstance(angle, int) or isinstance(angle, float), type(angle) assert (isinstance(angle_velocity, int) or isinstance(angle_velocity, float)), type(angle_velocity) assert isinstance(image_info, ImageInfo), type(image_info) self.position = list(position) self.velocity = list(velocity) self.angle = angle self.angle_velocity = angle_velocity self.image = image self.image_center = image_info.get_center() self.image_size = image_info.get_size() self.radius = image_info.get_radius() if sound: sound.rewind() sound.play()