def replace_draw(self, screen, camera_offset=(0, 0)): if self.image is None: return if self.ignore_offset: x = self._draw_position[0] y = self._draw_position[1] else: x = self._draw_position[0] - camera_offset[0] y = self._draw_position[1] - camera_offset[1] camera_offset = camera_offset[0], camera_offset[1] rect = self.image.get_rect(topleft=self._draw_position) rect.move_ip(0, 0) screen_rect = screen.get_rect(topleft=camera_offset) overlap = screen_rect.clip(rect) overlap.move_ip(-camera_offset[0], -camera_offset[1]) #0 is to left #> is to right corner = overlap.topleft area = Rect(0, 0, 0, 0) area.size = overlap.size if corner[0] == 0: area.x = -x else: area.x = 0 if corner[1] == 0: area.y = -y else: area.y = 0 screen.blit(self.image, overlap, area)
def render_hidden_hand(board_render, pov, seat): player = board_render.match.players[pov] rect = Rect(board_render.surface.get_rect()) tile_offset = 5 full_hand_width = (len(player.hand) * (SMALL_TILE_SIZE[0] + tile_offset) - tile_offset) TILE_POS = [ ((rect.width - full_hand_width) / 2, (SMALL_TILE_SIZE[1] + 15)), ((SMALL_TILE_SIZE[1] + 15), (rect.height - full_hand_width) / 2), ((rect.width - full_hand_width) / 2, 10), (10, (rect.height - full_hand_width) / 2), ] xpos, ypos = TILE_POS[seat] if seat == 0: rect.bottom = ypos elif seat == 1: rect.x = rect.width - xpos elif seat == 2: rect.y = ypos elif seat == 3: rect.x = xpos hand = [Piece(PieceType.ERROR)] * len(player.hand) if seat % 2 == 0: return _render_hand( board_render, SMALL_TILE_SIZE, player, hand, player.melded_hand, rect, xpos, tile_offset=tile_offset, should_interact=False, small_tile=True, ) return _render_vertical_hand( board_render, SMALL_TILE_SIZE, player, hand, player.melded_hand, rect, ypos, tile_offset=tile_offset, should_interact=False, rotation=1, small_tile=True, )
def get_world_collisions(self, collider): tw, th = self.tile_map.tile_width, self.tile_map.tile_height tmw, tmh = self.tile_map.width, self.tile_map.height # determine which grid square(s) the collider is in left, right = int(collider.rect.left / tw), int(collider.rect.right / tw) top, bottom = int(collider.rect.top / th), int(collider.rect.bottom / th) r = Rect(left * tw, top * th, tw, th) collisions = [] # each of these tiles is potentially intersecting the collider for x in range(left, right + 1): if x < 0 or x >= tmw: continue for y in range(top, bottom + 1): if y < 0 or y >= tmh: continue if not self.tile_map.get_passable((x, y)): # a non-passable tile might be within range: now use a pixel-perfect collision test r.x = x * tw r.y = y * th if collider.rect.colliderect(r): collisions.append( Collision(moved_collider=collider, hit_thing=(x, y), moved_collider_position=copy_vector( collider.position))) return collisions
def delta(self, rect: pg.Rect) -> pg.Rect: rect = rect.copy() rect.x = ceil(rect.x * self.dz + self.dx) rect.y = ceil(rect.y * self.dz + self.dy) rect.w = ceil(rect.w * self.dz) rect.h = ceil(rect.h * self.dz) return rect
def bounce_off_ip(bounce_obj_rect: Rect, bounce_obj_speed, \ hit_obj_rect: Rect, hit_obj_speed): """ Calculate the speed and position of the `bounce_obj` after it bounces off the `hit_obj`. The position of `bounce_obj_rect` and the value of `bounce_obj_speed` will be updated. This function should be called only when two objects are colliding. @param bounce_obj_rect The Rect of the bouncing object @param bounce_obj_speed The 2D speed vector of the bouncing object. @param hit_obj_rect The Rect of the hit object @param hit_obj_speed The 2D speed vector of the hit object """ # Treat the hit object as an unmovable object speed_diff_x = bounce_obj_speed[0] - hit_obj_speed[0] speed_diff_y = bounce_obj_speed[1] - hit_obj_speed[1] # The relative position between top and bottom, and left and right # of two objects at the last frame rect_diff_bT_hB = hit_obj_rect.bottom - bounce_obj_rect.top + speed_diff_y rect_diff_bB_hT = hit_obj_rect.top - bounce_obj_rect.bottom + speed_diff_y rect_diff_bL_hR = hit_obj_rect.right - bounce_obj_rect.left + speed_diff_x rect_diff_bR_hL = hit_obj_rect.left - bounce_obj_rect.right + speed_diff_x # Get the surface distance from the bouncing object to the hit object # and the new position for the bouncing object if it really hit the object # according to their relative position ## The bouncing object is at the bottom if rect_diff_bT_hB < 0 and rect_diff_bB_hT < 0: surface_diff_y = rect_diff_bT_hB extract_pos_y = hit_obj_rect.bottom ## The bouncing object is at the top elif rect_diff_bT_hB > 0 and rect_diff_bB_hT > 0: surface_diff_y = rect_diff_bB_hT extract_pos_y = hit_obj_rect.top - bounce_obj_rect.height else: surface_diff_y = -1 if speed_diff_y > 0 else 1 ## The bouncing object is at the right if rect_diff_bL_hR < 0 and rect_diff_bR_hL < 0: surface_diff_x = rect_diff_bL_hR extract_pos_x = hit_obj_rect.right ## The bouncing object is at the left elif rect_diff_bL_hR > 0 and rect_diff_bR_hL > 0: surface_diff_x = rect_diff_bR_hL extract_pos_x = hit_obj_rect.left - bounce_obj_rect.width else: surface_diff_x = -1 if speed_diff_x > 0 else 1 # Calculate the duration to hit the surface for x and y coordination. time_hit_y = surface_diff_y / speed_diff_y time_hit_x = surface_diff_x / speed_diff_x if time_hit_y >= 0 and time_hit_y >= time_hit_x: bounce_obj_speed[1] *= -1 bounce_obj_rect.y = extract_pos_y if time_hit_x >= 0 and time_hit_y <= time_hit_x: bounce_obj_speed[0] *= -1 bounce_obj_rect.x = extract_pos_x
def crop_center(image, size): rect = Rect((0, 0), size) res_surf = Surface(size, SRCALPHA, 32) blit_pos = [0, 0] if image.get_width() > rect.width: blit_pos[0] = (-image.get_width()+rect.width) // 2 else: rect.x = (image.get_width()-rect.width) // 2 if image.get_height() > rect.height: blit_pos[1] = (-image.get_height()+rect.height) // 2 else: rect.y = (image.get_height()-rect.height) // 2 print(rect) print(blit_pos) res_surf.blit(crop(image, rect), blit_pos) return res_surf
def makeRooms(self): numRooms = random.randint(NUMROOMSMIN, NUMROOMSMAX) for roomID in range(numRooms): attempts = 0 room = Rect(0,0,0,0) room.width = random.randint(ROOMMIN, ROOMMAX) room.height = random.randint(ROOMMIN, ROOMMAX) posFound = False while not posFound: if attempts == MAXROOMALLOCATTEMPTS: #print("Could not resolve room placement for room %i, bailing" % (roomID+1)) break room.x = random.randint(2, WORLDSIZE[0] - 1) room.y = random.randint(5, WORLDSIZE[1] - 1) if (room.x + room.width) >= (WORLDSIZE[0] - 1) or (room.y + room.height) >= (WORLDSIZE[1] - 4): attempts += 1 continue posFound = True for r,w in self.roomInfo: if r.inflate(2*ROOMSPACING, 2*ROOMSPACING).colliderect(room): posFound = False attempts += 1 break if not posFound: continue #Place waypoint wpX = random.randint(room.x + 1 + int(CORTHICKNESS / 2), room.x + room.width - 1 - int(CORTHICKNESS / 2)) wpY = random.randint(room.y + 1 + int(CORTHICKNESS / 2), room.y + room.height - 1 - int(CORTHICKNESS / 2) ) self.roomInfo.append( (room, (wpX, wpY)) ) for x in range(room.x, room.x + room.width): for y in range(room.y, room.y + room.height): self.mapGrid[x][y] = "#" #Sort rooms in order of Y coordinates self.roomInfo.sort(key=lambda r: r[0].y)
def render_dora_pile(board_render): group = Group() match = board_render.match board = match.current_board dora_revealed = board.get_revealed_dora() dora_revealed += [Piece(PieceType.ERROR)] * (5 - board.dora_revealed) tile_offset = 10 full_width = (SMALL_TILE_SIZE[0] + tile_offset) * 5 - tile_offset xpos = 0 rect = Rect(board_render.surface.get_rect()) rect.y = 10 rect.x = rect.width - full_width - 10 for tile in dora_revealed: tile_pos = (rect.x + xpos, rect.y) tile_sprite = TileRender(board_render.small_dictionary, tile, tile_pos, small_tile=True) group.add(tile_sprite) xpos += tile_offset + SMALL_TILE_SIZE[0] return group
def scale_rect(rectangle: pygame.Rect, scale=2): rectangle.x = rectangle.x * scale rectangle.y = rectangle.y * scale rectangle.w = rectangle.w * scale rectangle.h = rectangle.h * scale return rectangle
elif ball.y+10 >= height: sideSound.play() ball.y = height-10 ball_speed_y = -ball_speed_y if ball.x < 0 or ball.x > width: timePassed = 0.0 failSound.play() if ball.x < 0: aiScore += 1 else: playerScore += 1 ball_speed_x = speeds_x[random.randint(0, 1)] ball_speed_y = speeds_y[random.randint(0, 5)] ball.x = width/2 ball.y = height/2 # draw screen.fill(black) pygame.draw.rect(screen, white, playerPaddle) pygame.draw.rect(screen, white, aiPaddle) pygame.draw.rect(screen, white, ball) playerF = font.render(str(playerScore), True, white) aiF = font.render(str(aiScore), True, white) screen.blit(playerF, (width/4, 25)) screen.blit(aiF, (3*width/4, 25)) for i in range(20): # centerline pygame.draw.line(screen, white, (width/2, i*40), (width/2, i*40+20), 5)
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Generate Random Map", self.gui, self.ui_elements['panel']) # map size label_rect = Rect(0, 0, 150, get_param('element_height')) label_rect.y += scene_label_rect.bottom + get_param('element_padding') dd_rect = Rect(0, 0, 250, get_param('element_height')) dd_rect.y = label_rect.y label_rect.centerx = 125 dd_rect.centerx = 325 self.ui_elements['d_size_label'] = UILabel(label_rect, "Map Size", self.gui, self.ui_elements['panel']) self.ui_elements['dd_map_size'] = UIDropDownMenu( ['64', '128', '256'], '64', dd_rect, self.gui, self.ui_elements['panel']) # Seed label_rect.y += get_param('element_height') + get_param( 'element_padding') ip_rect = Rect(0, 0, 180, get_param('element_height')) ip_rect.centerx = (panel_rect.w // 2) + 30 ip_rect.y = label_rect.y s_btn_rect = Rect(0, 0, 60, get_param('element_height')) s_btn_rect.x = ip_rect.right + get_param('element_padding') s_btn_rect.y = ip_rect.y self.ui_elements['seed_label'] = UILabel(label_rect, 'Map Seed', self.gui, self.ui_elements['panel']) self.ui_elements['input_seed'] = UITextEntryLine( ip_rect, self.gui, self.ui_elements['panel']) self.ui_elements['btn_rnd_seed'] = UIButton(s_btn_rect, 'Random', self.gui, self.ui_elements['panel']) # I want to add two sliders, 1 for mountains and 1 for water, these would be used to control the upper # and lower limits of the height mapping. h_sl_ops = (0, 100) h_sl_sel = 50 label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect = Rect(0, 0, 200, get_param('element_height')) h_sl_rect.centerx = (panel_rect.w // 2) + get_param('element_padding') + 30 h_sl_rect.y = label_rect.y self.ui_elements['hsl_grass'] = UILabel(label_rect, 'Grass', self.gui, self.ui_elements['panel']) self.ui_elements['hs_grass'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect.y = label_rect.y self.ui_elements['hsl_water'] = UILabel(label_rect, 'Water', self.gui, self.ui_elements['panel']) self.ui_elements['hs_water'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect.y = label_rect.y self.ui_elements['hsl_mountain'] = UILabel(label_rect, 'Mountain', self.gui, self.ui_elements['panel']) self.ui_elements['hs_mountain'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) # buttons button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.centerx = panel_rect.w // 2 button_rect.y = label_rect.bottom + get_param('element_padding') self.ui_elements['btn_preview'] = UIButton(button_rect, "Generate Preview", self.gui, self.ui_elements['panel']) button_rect.w = 200 button_rect.y += get_param('element_height') + get_param( 'element_padding') button_rect.centerx = (panel_rect.w // 2) - 100 self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel']) # the apply button always starts off disabled button_rect.centerx = (panel_rect.w // 2) + 100 self.ui_elements['btn_next'] = UIButton(button_rect, "Next", self.gui, self.ui_elements['panel']) pv_rect = Rect(0, 0, 300, 300) pv_rect.centerx = panel_rect.w // 2 pv_rect.y = button_rect.bottom + get_param('element_padding') self.ui_elements['pv_image'] = UIImage(pv_rect, self.pv_img, self.gui, self.ui_elements['panel'])