def render_selected_piece_moves(self): if self.selected_sprite is not None: # Clear the list of possible move markers self.selected_piece_moves = [] # Get all the possible moves of the piece legal_moves = self.selected_piece.get_legal_moves(self.board) for (rank, file) in legal_moves: square = self.board.look_at(rank, file) # Center the xy-coords in the center of the tile if square is None: circ = Circle( x=self.board_x + (file * self.tile_width) + (self.tile_width * 0.5), y=self.board_y + (rank * self.tile_height) + (self.tile_height * 0.5), radius=self.tile_width * .25, color=(100,100,100), batch=self.selected_piece_batch ) self.selected_piece_moves.append(circ) # If there is an enemy piece on the square we can move to, draw a red circle elif square is not None: if square.color is not self.board.player_turn: circ = Circle( x=self.board_x + (file * self.tile_width) + (self.tile_width * 0.5), y=self.board_y + (rank * self.tile_height) + (self.tile_height * 0.5), radius=self.tile_width * .4, color=(255, 0, 0), batch=self.selected_piece_batch ) self.selected_piece_moves.append(circ) else: self.selected_piece_moves = [] self.selected_piece_batch = pyglet.graphics.Batch()
def __init__(self, x: int, y: int, frame: Frame, grid: Grid, renderbox: RenderBox, name: str = 'Player1', batch: Batch = None, debug: bool = False): # Player Sprite Select Related self.player_res_list = [ PlayerImages.p1, PlayerImages.p2, PlayerImages.p3 ] self.player_select = 0 self.player_res = self.player_res_list[self.player_select] # Initialize Base Class super().__init__(x=x, y=y, res=self.player_res.jump_right, frame=frame, grid=grid, renderbox=renderbox, name=name, batch=batch, usage='dynamic', is_anchor_x_centered=True) # Movement Related self.BASE_WALKING_SPEED = 200 self.BASE_JUMPING_SPEED = 300 self.vx = 0.0 self.vy = 0.0 self.facing = 'right' self.status = 'jumping' self.arrow_key_buffer = ArrowKeyBuffer() # Grid Related self.up_contact_obj_list = cast(List[GridObject], []) self.down_contact_obj_list = cast(List[GridObject], []) self.left_contact_obj_list = cast(List[GridObject], []) self.right_contact_obj_list = cast(List[GridObject], []) # Debug self.debug = debug self.ref_point = Circle(x=self.camera_x, y=self.camera_y, radius=5, color=(255, 0, 0)) self.ref_rect = Rectangle(x=self.camera_x, y=self.camera_y, width=self.width, height=self.height, color=(0, 0, 255)) self.ref_rect.anchor_x = self.ref_rect.width // 2
def _build_circle_render(self) -> Circle: """ Creates a circle located at the screen coordinates (if they exist). Uses the color specified in our globals w/ a size of 10px radius. Assigns the object to our batch & group """ if self.screen_coords: return Circle(self.screen_coords[0], self.screen_coords[1], CIRCLE_SIZE, color=self.color, batch=main_batch, group=self.group) return Circle(0, 0, 10, color=self.color, batch=main_batch, group=self.group)
def render_checked_king(self): # Find the white king and black king and see if either is in check for king in self.board.pieces: if king.id == src.constants.KING and self.board.is_in_check(king.color): circ = Circle( x=self.board_x + (king.file * self.tile_width) + (self.tile_width * 0.5), y=self.board_y + (king.rank * self.tile_height) + (self.tile_height * 0.5), radius=self.tile_width * .4, color=(255, 0, 0), batch=self.selected_piece_batch ) self.selected_piece_moves.append(circ)
def __init__(self, n): super(App, self).__init__(height=SCREEN_SIZE, width=SCREEN_SIZE) self.state_of_phase = StateOfPhases(n) self.objects = list(map( lambda x: Circle( x=x[0] * SCREEN_SIZE, y=x[1] * SCREEN_SIZE, radius=x[2] * MASS_RADIUS_RATIO, color=rd_color() ), zip( self.state_of_phase[0, 0], self.state_of_phase[0, 1], self.state_of_phase[2, 0] ) ))
def draw(self): c = Circle(self.pos[X], self.pos[Y], self.radius) c.draw()
def draw(self): c = Circle(self.pos[0], self.pos[1], self.size) c.opacity = 100 c.draw()
controller.open() window = pyglet.window.Window(720, 480) batch = pyglet.graphics.Batch() text = f"Detected: {controller.name}\nController GUID: {controller.guid}" controller_label = pyglet.text.Label(text=text, x=10, y=window.height-20, multiline=True, width=720, batch=batch) left_trigger = Rectangle(70, 360 + (controller.lefttrigger * 50), 40, 10, batch=batch) right_trigger = Rectangle(610, 360 + (controller.lefttrigger * 50), 40, 10, batch=batch) d_pad = Rectangle(280, 190, 10, 10, batch=batch) left_stick = Arc(180, 240, 20, batch=batch) right_stick = Arc(540, 240, 20, batch=batch) buttons = {'a': Circle(440, 170, 9, color=(50, 255, 50), batch=batch), 'b': Circle(460, 190, 9, color=(255, 50, 50), batch=batch), 'x': Circle(420, 190, 9, color=(50, 50, 255), batch=batch), 'y': Circle(440, 210, 9, color=(255, 255, 50), batch=batch), 'leftshoulder': Rectangle(70, 290, 40, 10, batch=batch), 'rightshoulder': Rectangle(610, 290, 40, 10, batch=batch), 'start': Circle(390, 240, 9, batch=batch), 'guide': Circle(360, 240, 9, color=(255, 255, 100), batch=batch), 'back': Circle(330, 240, 9, batch=batch), 'leftstick': Circle(180, 240, 9, batch=batch), 'rightstick': Circle(540, 240, 9, batch=batch)} for button in buttons.values(): button.visible = False
class Player(GameObject): def __init__(self, x: int, y: int, frame: Frame, grid: Grid, renderbox: RenderBox, name: str = 'Player1', batch: Batch = None, debug: bool = False): # Player Sprite Select Related self.player_res_list = [ PlayerImages.p1, PlayerImages.p2, PlayerImages.p3 ] self.player_select = 0 self.player_res = self.player_res_list[self.player_select] # Initialize Base Class super().__init__(x=x, y=y, res=self.player_res.jump_right, frame=frame, grid=grid, renderbox=renderbox, name=name, batch=batch, usage='dynamic', is_anchor_x_centered=True) # Movement Related self.BASE_WALKING_SPEED = 200 self.BASE_JUMPING_SPEED = 300 self.vx = 0.0 self.vy = 0.0 self.facing = 'right' self.status = 'jumping' self.arrow_key_buffer = ArrowKeyBuffer() # Grid Related self.up_contact_obj_list = cast(List[GridObject], []) self.down_contact_obj_list = cast(List[GridObject], []) self.left_contact_obj_list = cast(List[GridObject], []) self.right_contact_obj_list = cast(List[GridObject], []) # Debug self.debug = debug self.ref_point = Circle(x=self.camera_x, y=self.camera_y, radius=5, color=(255, 0, 0)) self.ref_rect = Rectangle(x=self.camera_x, y=self.camera_y, width=self.width, height=self.height, color=(0, 0, 255)) self.ref_rect.anchor_x = self.ref_rect.width // 2 @property def x(self) -> int: return super().x @x.setter def x(self, x: int): super().x = x if self.debug: self.ref_point.x = self.camera_x self.ref_rect.x = self.camera_x @property def y(self) -> int: return super().y @y.setter def y(self, y: int): super().y = y if self.debug: self.ref_point.y = self.camera_y self.ref_rect.y = self.camera_y def change_player(self, idx: int): self.player_res = self.player_res_list[idx] self.player_select = idx self.update_sprite() def toggle_player(self): self.change_player( (self.player_select + 1) % len(self.player_res_list)) def toggle_debug(self): self.debug = not self.debug self.grid.show_contacts = not self.grid.show_contacts def change_sprite(self, image): self.sprite.image = image if self.debug: self.ref_rect.width = self.sprite.width self.ref_rect.height = self.sprite.height self.ref_rect.anchor_x = self.ref_rect.width // 2 def update_sprite(self): if self.status == 'idle': if self.facing == 'right': self.change_sprite(self.player_res.idle_right.img) elif self.facing == 'left': self.change_sprite(self.player_res.idle_left.img) else: raise Exception elif self.status == 'jumping': if self.facing == 'right': self.change_sprite(self.player_res.jump_right.img) elif self.facing == 'left': self.change_sprite(self.player_res.jump_left.img) else: raise Exception elif self.status == 'walking': if self.facing == 'right': self.change_sprite(self.player_res.walk_right_anim.animation) elif self.facing == 'left': self.change_sprite(self.player_res.walk_left_anim.animation) else: raise Exception else: raise Exception @property def is_idle(self) -> bool: return self.status == 'idle' @property def is_walking(self) -> bool: return self.status == 'walking' @property def is_jumping(self) -> bool: return self.status == 'jumping' def face(self, direction: str): if direction == 'right': self.facing = 'right' self.update_sprite() elif direction == 'left': self.facing = 'left' self.update_sprite() else: raise Exception def start_walking(self, direction: str): if direction == 'right': self.vx = self.BASE_WALKING_SPEED self.facing = 'right' self.status = 'walking' self.update_sprite() elif direction == 'left': self.vx = -self.BASE_WALKING_SPEED self.facing = 'left' self.status = 'walking' self.update_sprite() else: raise Exception def stop_walking(self): self.status = 'idle' self.vx = 0 self.update_sprite() def start_jumping(self): self.status = 'jumping' self.vy = self.BASE_JUMPING_SPEED self.update_sprite() def start_falling(self): self.status = 'jumping' self.update_sprite() def stop_jumping(self): self.status = 'idle' self.vy = 0 self.update_sprite() def draw(self): if self.debug: self.ref_point.draw() self.ref_rect.draw() super().draw() def move(self, dx: int, dy: int): player_grid_obj = self.grid.contained_obj_list.get_obj_from_name( self.name) other_renderable_objects = self.renderbox.get_all_renderable_objects( exclude_names=[self.name]) other_renderable_names = [ other_renderable_object.name for other_renderable_object in other_renderable_objects ] other_grid_objects = self.grid.contained_obj_list.get_objects( include_names=other_renderable_names) all_grid_objects = self.grid.contained_obj_list.get_objects() self.up_contact_obj_list = [] self.down_contact_obj_list = [] self.left_contact_obj_list = [] self.right_contact_obj_list = [] self.grid.reset_contacts() # Move X proposed_player_occupied_spaces = player_grid_obj.get_occupied_spaces( dx=dx) collision = False for proposed_player_occupied_space in proposed_player_occupied_spaces: for other_grid_object in other_grid_objects: if proposed_player_occupied_space in other_grid_object.occupied_spaces: other_grid_object.is_in_contact = True if dx > 0: self.right_contact_obj_list.append(other_grid_object) elif dx < 0: self.left_contact_obj_list.append(other_grid_object) else: raise Error( f'Player got stuck in object in x direction.') collision = True if not collision: self.set_x(x=self.x + dx, fix_camera=True) self.grid.move(dx=-dx) else: if len(self.left_contact_obj_list) > 0: dx_adjustment = self.left_contact_obj_list[ 0].x_right + 1 - self.x_left self.set_x(x=self.x + dx_adjustment, fix_camera=True) self.grid.move(dx=-dx_adjustment) elif len(self.right_contact_obj_list) > 0: dx_adjustment = self.right_contact_obj_list[ 0].x_left - self.x_right self.set_x(x=self.x + dx_adjustment, fix_camera=True) self.grid.move(dx=-dx_adjustment) else: raise Exception # Move Y proposed_player_occupied_spaces = player_grid_obj.get_occupied_spaces( dy=dy) collision = False for proposed_player_occupied_space in proposed_player_occupied_spaces: for other_grid_object in other_grid_objects: if proposed_player_occupied_space in other_grid_object.occupied_spaces: other_grid_object.is_in_contact = True if dy > 0: self.up_contact_obj_list.append(other_grid_object) elif dy < 0: self.down_contact_obj_list.append(other_grid_object) else: raise Error( f'Player got stuck in object in y direction.') collision = True if not collision: self.set_y(y=self.y + dy, fix_camera=True) self.grid.move(dy=-dy) self.start_falling() else: self.vy = 0 if len(self.down_contact_obj_list) > 0: dy_adjustment = self.down_contact_obj_list[ 0].y_top - self.y_bottom self.set_y(y=self.y + dy_adjustment, fix_camera=True) self.grid.move(dy=-dy_adjustment) if self.is_jumping: self.stop_jumping() if self.arrow_key_buffer.is_pressed: self.start_walking( direction=self.arrow_key_buffer.buffer[-1]) else: self.vx = 0 elif len(self.up_contact_obj_list) > 0: dy_adjustment = self.up_contact_obj_list[ 0].y_bottom - self.y_top self.set_y(y=self.y + dy_adjustment, fix_camera=True) self.grid.move(dy=-dy_adjustment) else: raise Exception
def draw(self): c = Circle(self.pos[X], self.pos[Y], (self.radius * 2)) c.opacity = 100 c.draw()