class GameConfig: player_attack_speed = 0.5 enemy_enter_speed = random.randint(1000, 3000) player_move_speed = 3 # (left,top) (width,height) screen = Rect((0, 0), (480, 700)) enemy_move_speed = random.randint(1, 3) enemy_attack_speed = 1 fps = 60
def __init__(self, owner, pos, direction): self.owner = owner self.game = self.owner.game self.pos = [pos[0], pos[1]] self.direction = [direction[0], direction[1]] self.color = (0,0,0) self.bounces_left = random.randint(0,3) self.rect = Rect(0, 0, 5, 5)
def intersect(obj, ball): edges = dict( left=Rect(obj.left, obj.top, 1, obj.height), #left right=Rect(obj.right - 1, obj.top, 1, obj.height), #right top=Rect(obj.left, obj.top, obj.width, 1), #top bottom=Rect(obj.left, obj.bottom - 1, obj.width, 1)) #bottom number = dict( left=1, #left right=2, #right top=3, #top bottom=6) #bottom collisions = set(edge for edge, rect in edges.items() if ball.bounds.colliderect(rect)) if not collisions: return 0 return sum([number[item] for item in collisions])
def __init__(self, x, y): self.x = x self.y = y self.height = FLOOR - 120 self.img = SAW_IMG self.passed = False self.rect = Rect(self.x, self.y, 120, 120) self.saw_height()
def hit(self,x,y,t,s): tiles = self.tiles tw,th = tiles[0].image.get_width(),tiles[0].image.get_height() t.tx = x t.ty = y t.rect = Rect(x*tw,y*th,tw,th) t._rect = t.rect if hasattr(t,'hit'): t.hit(self,t,s)
def __init__(self): self.rect = Rect(0, 400, 720, 80) self.obstacles = [] self.points = 0 self.time_elapsed_since_last_action = 0 self.delay_for_next = random.randrange(800, 2500) self.clock = pygame.time.Clock()
def head(surface, mod, pos): # adjust for body size radius = ((mod["bodySize"] + 1) * 4) + 10 color = skinColor(mod) draw.circle(surface, (0,0,0), pos, radius) draw.circle(surface, color, pos, int(radius * .90)) return Rect(pos[0] - radius, pos[1] - radius, radius * 2, radius * 2)
def camera_configure(camera, target_rect): l, t, _, _ = target_rect _, _, w, h = camera l, t = -l + WIN_WIDTH / 2, -t + WIN_HEIGHT / 2 l = max(-(camera.width - WIN_WIDTH), min(0, l)) t = min(0, max(-(camera.height - WIN_HEIGHT), t)) return Rect(l, t, w, h)
def __init__(self, owner, pos, direction): self.owner = owner self.game = self.owner.game self.pos = [pos[0], pos[1]] self.direction = [direction[0], direction[1]] self.color = (0, 0, 0) self.free = False self.rect = Rect(0, 0, 5, 5)
def _render_value(self, color_bg, value, w_rect, position: Tuple[int, int], color: Tuple[int, int, int]): text = str(value) x, y = position rect = Rect(x - 5, y - 2, w_rect, 15) x_text = x + w_rect // 2 - 3 - len(text) * 4 self.ui_render.rect_filled(color_bg, rect) self.ui_render.rect(COLOR_GRAY, rect, 1) self.ui_render.text(self.font_details, text, (x_text, y), color)
def _render_text(self, text: str, color: Tuple[int, int, int], background: bool): x = self._rect.right - 50 - len(text) * 3 y = self._rect.top + 17 if background: w_label_rect = 90 rect_label = Rect(self._rect.right - 95, y - 2, w_label_rect, 15) self._ui_render.rect_filled((40, 40, 40), rect_label) self._ui_render.text(self._font, text, (x, y), color)
def __init__(self, x, y, speed=(0, 0), fps=DEFAULT_FPS): self.fps = fps self.speed = speed self.bounds = Rect(x, y, 50, 50) self.bounds.center = (x, y) self.direction = TO_THE_RIGHT self.image = load_image('monster01.png', -1) self.image_dx = (self.bounds.w - self.image.get_width()) // 2 self.image_dy = (self.bounds.h // 2 - self.image.get_height())
def __add__(self, other): if isinstance(other, Vec2d): return Vec2d(self.x + other.x, self.y + other.y) elif isinstance(other, Rect): return Rect(other.x + self.x, other.y + self.y, other.w, other.h) elif hasattr(other, "__getitem__"): return Vec2d(self.x + other[0], self.y + other[1]) else: return Vec2d(self.x + other, self.y + other)
def __init__(self, image, tile_size, size, attributes, pos, *groups): self.image = image self.tile_size = Pos(tile_size) self.size = Pos(size) self.pos = Pos(pos) self.old_pos = Pos(self.pos) self.rect = self.get_surface_pos(pos) self.old_rect = Rect(self.rect) pygame.sprite.DirtySprite.__init__(self, *groups)
def __init__(self, surface, width, total_time): self.frames = [] height = surface.get_height() for x in range(0, surface.get_width() - 1, width): rect = Rect(x, 0, width, height) self.frames.append(surface.subsurface(rect)) self.total_time = total_time self.frame_time = float(total_time) / len(self.frames) self.t = 0
def draw(self, surface: Surface, dest_rect: Rect): self.display.fill(BACKGROUND_COLOR) for idx, digit in enumerate(self.value_list): digit_location = 16 * idx self.display.blit(self.digits, Rect(digit_location, 0, 16, 32), self.digit_rects[digit]) surface.blit(self.display, dest_rect)
def __init__(self, size, origin=(0, 0)): self.tile_width = 0 self.tile_height = 0 self.layers = Layers() self.fx, self.fy = 0, 0 # viewport focus point self.view_w, self.view_h = size # viewport size self.view_x, self.view_y = origin # viewport offset self.viewport = Rect(origin, size) self._old_focus = None
def main(): pygame.init() screen = pygame.display.set_mode(config['screen']['size']) elem = WaitForPlayersWindow(Rect(0, 0, *config['screen']['size']), Color('bisque')) m = Main(elem, screen) m.loop()
def player_spot(pos): clicking_areas = [[Rect((int(j * w + w / 2 - w / 4), int(i * h + h / 2 - w / 4), w / 2, h / 2)) for i in range(3)] for j in range(3)] for i, eaches in enumerate(clicking_areas): for j, each in enumerate(eaches): if Rect.collidepoint(each, pos[1], pos[0]): return i, j else: return None
def _fit_component_position(self, position: Rect) -> Rect: pos_x = self.position.x + self.position.width - position.width if ( position.x + position.width > self.position.width ) else position.x + self.position.x # TODO: finish this y coord if the x is working # TODO: Yes it is! pos_y = position.y + self.position.y return Rect(pos_x, pos_y, position.width, position.height)
def animate(self): s = pygame.Surface(self.size) height = self.size[1] self.offset += 0.1 for step in self.steps: src = Rect(step, 0, 2, height) dst = src.move(0, math.cos(self.offset + step * .02) * self.amount) s.blit(self.base, dst, src) return s
def on_update(self): self.rect = Rect(0, 0, 40, 40) self.rect.width = 40 self.rect.height = 40 self.rect.centerx = self.aimx self.rect.centery = self.aimy if self.smash_time > 0: self.smash_time -= 1
def __init__(self, position: Tuple, building_type: CityObjectData): self._WIDTH = 85 self._HEIGHT = 100 self._BORDER_SIZE = 2 description = 'Cost: {0}'.format(building_type.cost) super().__init__( Rect(position[0], position[1], self._WIDTH, self._HEIGHT), description) self._building_type = building_type
def __init__(self, ui_render: DrawableArea, font, bottomleft: Tuple[int, int]): self.ui_render = ui_render self.font = font self.bottomleft = bottomleft self.rect_padding = 5 self.w = 140 + self.rect_padding * 2 self.buffs = [] h = len(self.buffs) * 25 + self.rect_padding * 2 self.rect = Rect(self.bottomleft[0], self.bottomleft[1] - h, self.w, h)
def __init__( self, position: (int, int), index: (int, int), size: (int, int)) -> None: self.index = index w, h = size self.rect = Rect(position[0] + (w * index[0]), position[1] + (h * index[1]), w, h) self.flower = None self.neighbors = {}
def render(self, recently_clicked: bool): self._ui_render.rect_filled((40, 40, 50), self.rect) self._ui_render.image(self.image, self.rect.topleft) self._ui_render.rect(COLOR_ICON_OUTLINE, self.rect, 1) if recently_clicked: self._ui_render.rect(COLOR_ICON_HIGHLIGHTED, Rect(self.rect.x - 1, self.rect.y - 1, self.rect.w + 2, self.rect.h + 2), 3) elif self.hovered: self._ui_render.rect(COLOR_HOVERED, self.rect, 1) self._ui_render.text(self._font, self.label, (self.rect.x + 12, self.rect.y + self.rect.h + 4)) if self.cooldown_remaining_ratio > 0: cooldown_rect = Rect(self.rect.x + 1, self.rect.y + 1 + (self.rect.h - 2) * (1 - self.cooldown_remaining_ratio), self.rect.w - 2, (self.rect.h - 2) * self.cooldown_remaining_ratio + 1) self._ui_render.rect_filled((100, 30, 30), cooldown_rect) self._ui_render.rect((180, 30, 30), self.rect, 2)
def updateNPC(self, dt, level, player, enemies): deg = random() * 360 angle = math.radians(deg) radius = 0.5 randomCircleVec = Vector(radius * math.cos(angle), radius * math.sin(angle)) distanceToPlayerSqr = self.pos.distance_squared_to(player.pos) # Make the bug run away from the player if distanceToPlayerSqr < 6: fromPlayer = self.pos - player.pos if fromPlayer.length() != 0: fromPlayer.normalize_ip() fromPlayer.x /= 2 fromPlayer.y /= 2 else: fromPlayer = Vector(0, 0) collisions = pygame.sprite.spritecollide(self, enemies, False) pushAwayFrom = Vector(0, 0) if len(collisions) > 1: for collision in collisions: if collision.id != self.id: pushAwayFrom += self.pos - collision.pos pushAwayFrom.x = (pushAwayFrom.x / (len(collisions) - 1)) pushAwayFrom.y = (pushAwayFrom.y / (len(collisions) - 1)) self.velocity = self.velocity + randomCircleVec + fromPlayer + pushAwayFrom temp = Rect(self.rect) self.velocity.normalize_ip() self.velocity *= self.maxSpeed self.rect.x += self.velocity.x self.rect.y += self.velocity.y # Change the rect to get the collision to work collision = pygame.sprite.spritecollideany(self, level.solidObjectGroup) if collision: # Don't move self.rect = temp self.velocity = self.velocity * -1 else: self.pos.x = self.rect.x / self.tileSize.x self.pos.y = self.rect.y / self.tileSize.y if self.pos.x < 0: self.pos.x = 0 self.rect.x = 0 elif self.pos.x >= level.width - 1: self.pos.x = level.width - 1 self.rect.x = (level.width - 1) * self.tileSize.x if self.pos.y < 0: self.pos.y = 0 self.rect.y = 0 elif self.pos.y >= level.height - 1: self.pos.y = level.height - 1 self.rect.y = (level.height - 1) * self.tileSize.y # Move self.getNPCDirection() self.updateNPCAnimation(dt)
def update_world_area(self, world_area: Rect): self._world_area = world_area self._rect_inner = Rect(self.rect.x + 2, self.rect.y + 2, self.rect.w - 4, self.rect.h - 4) if world_area.w > world_area.h: self._rect_inner.h /= world_area.w / world_area.h self._rect_inner.y = self.rect.y + self.rect.h // 2 - self._rect_inner.h // 2 else: self._rect_inner.w /= world_area.h / world_area.w self._rect_inner.x = self.rect.x + self.rect.w // 2 - self._rect_inner.w // 2
def main(): pygame.init() screen = pygame.display.set_mode(config['screen']['size']) elem = MainMenu(Rect((0, 0), config['screen']['size']), Color('aquamarine3')) m = Main(elem, screen) m.loop()
def __init__(self): self._MARGIN = 10 self._WIDTH = 200 super().__init__( Rect(GameSettings.Screen.WIDTH - self._MARGIN - self._WIDTH, self._MARGIN, self._WIDTH, GameSettings.Screen.HEIGHT - 2 * self._MARGIN)) self._create_building_buttons()