Best Python code snippet using ATX
entities.py
Source:entities.py
...27 self.__bounds_that_actually_draw_correctly.set_location(self.x, self.y)28 def update(self, delta_time, entities):29 raise NotImplementedError(30 "A class that inherits Entity did not implement the update(delta_time, entities) method")31 def _draw_bounds(self, surface, camera_type):32 self.__bounds_that_actually_draw_correctly.draw(surface, camera_type)33 def draw(self, surface):34 raise NotImplementedError(35 "A class that inherits Entity did not implement the draw(surface) method")36class Direction(IntEnum):37 NONE = 0,38 UP = 1,39 DOWN = 2,40 LEFT = 3,41 RIGHT = 442class Kinetic(Entity):43 def __init__(self, x, y, width, height, speed):44 super(Kinetic, self).__init__(x, y, width, height)45 self.velocity = Vector2()46 self.default_move_speed = speed47 self.move_speed = 048 self.facing = Direction.NONE49 self.collision_rectangles = []50 self.collision_width = 051 def _update_collision_rectangles(self):52 self.collision_width = self.move_speed + 153 self.collision_rectangles = [54 Rect(self.x + self.collision_width, self.y - self.collision_width,55 self.width - self.collision_width * 2, self.collision_width),56 Rect(self.x + self.collision_width, self.y + self.height, self.width -57 self.collision_width * 2, self.collision_width),58 Rect(self.x - self.collision_width, self.y + self.collision_width,59 self.collision_width, self.height - self.collision_width * 2),60 Rect(self.x + self.width, self.y + self.collision_width,61 self.collision_width, self.height - self.collision_width * 2)62 ]63 def _calculate_scaled_speed(self, delta_time):64 self.move_speed = self.default_move_speed * delta_time65 def _collision(self, entities):66 raise NotImplementedError(67 "A class that inherits Kinetic did not implement the collision(surface) method")68 def update(self, delta_time, entities):69 raise NotImplementedError(70 "A class that inherits Kinetic did not implement the update(delta_time, entities) method")71 def _draw_collision_rectangles(self, surface):72 for r in self.collision_rectangles:73 draw_rectangle(74 surface,75 r,76 CameraType.DYNAMIC,77 Color.RED,78 )79class Actor(Kinetic):80 def __init__(self, x, y, width, height, speed):81 super(Actor, self).__init__(x, y, width, height, speed)82 self.input = Input()83 def _update_input(self):84 raise NotImplementedError(85 "A class that inherits Actor did not implement the _update_input() method")86class Player(Actor):87 def __init__(self, x, y, width=10, height=10, speed=50):88 super(Player, self).__init__(x, y, width, height, speed)89 self.sprite = Sprite(self.x - 3, self.y - 22, SpriteType.PLAYER_F)90 self.arms = Sprite(self.x - 3, self.y - 22,91 SpriteType.PLAYER_ARM_SIDE_F)92 self.shadow = Sprite(self.x - 3, self.y - 21, SpriteType.PLAYER_SHADOW)93 self.set_color(Color.RED)94 self.item_carrying = None95 self.animation_walk = Animation(6, 6, 100)96 self.walking = False97 def set_location(self, x, y):98 super(Player, self).set_location(x, y)99 self.sprite.set_location(self.x - 3, self.y - 22)100 self.arms.set_location(self.x - 3, self.y - 22)101 self.shadow.set_location(self.x - 3, self.y - 21)102 if self.item_carrying != None:103 self.item_carrying.set_location(self.x - 3, self.sprite.y - 8)104 def _move(self, direction=Direction.NONE):105 self.facing = direction106 self.walking = True107 if self.facing == Direction.UP:108 self.sprite.set_sprite(SpriteType.PLAYER_B)109 self.arms.set_sprite(SpriteType.PLAYER_ARM_SIDE_B)110 self.set_location(self.x, self.y - self.move_speed)111 self.velocity.y = -1112 if self.facing == Direction.DOWN:113 self.sprite.set_sprite(SpriteType.PLAYER_F)114 self.arms.set_sprite(SpriteType.PLAYER_ARM_SIDE_F)115 self.set_location(self.x, self.y + self.move_speed)116 self.velocity.y = 1117 if self.facing == Direction.LEFT:118 self.sprite.set_sprite(SpriteType.PLAYER_L)119 self.arms.set_sprite(SpriteType.PLAYER_ARM_SIDE_L)120 self.set_location(self.x - self.move_speed, self.y)121 self.velocity.x = -1122 if self.facing == Direction.RIGHT:123 self.sprite.set_sprite(SpriteType.PLAYER_R)124 self.arms.set_sprite(SpriteType.PLAYER_ARM_SIDE_R)125 self.set_location(self.x + self.move_speed, self.y)126 self.velocity.x = 1127 def _update_input(self, delta_time):128 self.input.update(delta_time)129 self.walking = False130 if self.input.pressing(InputType.UP) and not self.input.pressing(InputType.DOWN):131 self._move(Direction.UP)132 if self.input.pressing(InputType.DOWN) and not self.input.pressing(InputType.UP):133 self._move(Direction.DOWN)134 if self.input.pressing(InputType.LEFT) and not self.input.pressing(InputType.RIGHT):135 self._move(Direction.LEFT)136 if self.input.pressing(InputType.RIGHT) and not self.input.pressing(InputType.LEFT):137 self._move(Direction.RIGHT)138 def _rectangle_collision_logic(self, entity):139 # Bottom140 if self.collision_rectangles[0].colliderect(entity.bounds) and self.velocity.y < 0:141 self.set_location(self.x, entity.bounds.bottom)142 # Top143 elif self.collision_rectangles[1].colliderect(entity.bounds) and self.velocity.y > 0:144 self.set_location(self.x, entity.bounds.top - self.bounds.height)145 # Right146 elif self.collision_rectangles[2].colliderect(entity.bounds) and self.velocity.x < 0:147 self.set_location(entity.bounds.right, self.y)148 # Left149 elif self.collision_rectangles[3].colliderect(entity.bounds) and self.velocity.x > 0:150 self.set_location(entity.bounds.left - self.bounds.width, self.y)151 def _collision(self, entities):152 for e in entities:153 if (154 not e.ignore and155 (156 isinstance(e, Building) or157 isinstance(e, Furniture) or158 isinstance(e, Wall) or159 isinstance(e, Tree)160 )161 # isinstance(e, NPC)162 ):163 self._rectangle_collision_logic(e)164 def _update_animation(self, delta_time):165 if self.walking:166 self.animation_walk.update(delta_time)167 self.sprite.set_frame(168 self.animation_walk.current_frame, self.animation_walk.columns)169 self.arms.set_frame(170 self.animation_walk.current_frame, self.animation_walk.columns)171 else:172 self.sprite.set_frame(0, self.animation_walk.columns)173 self.arms.set_frame(0, self.animation_walk.columns)174 def _update_item(self):175 if self.item_carrying != None:176 self.arms.increment_sprite_x(16 * 6)177 def update(self, delta_time, entities):178 self._calculate_scaled_speed(delta_time)179 self._update_input(delta_time)180 self._update_collision_rectangles()181 self._collision(entities)182 self._update_animation(delta_time)183 self._update_item()184 def draw(self, surface):185 if pygine.globals.debug:186 self._draw_bounds(surface, CameraType.DYNAMIC)187 self._draw_collision_rectangles(surface)188 else:189 self.shadow.draw(surface, CameraType.DYNAMIC)190 self.sprite.draw(surface, CameraType.DYNAMIC)191 self.arms.draw(surface, CameraType.DYNAMIC)192 if (self.item_carrying != None):193 self.item_carrying.draw(surface)194class SpeechBubble(Entity):195 def __init__(self, x, y, source, sprite_type=SpriteType.NONE):196 super(SpeechBubble, self).__init__(x, y, 1, 1)197 self.sprite = Sprite(x, y, SpriteType.SPEECH_BUBBLE)198 self.__content = Sprite(x + 8, y + 8, sprite_type)199 self.source = source200 def set_location(self, x, y):201 super(SpeechBubble, self).set_location(x, y)202 self.sprite.set_location(x, y)203 self.__content.set_location(x + 8, y + 8)204 def set_content(self, sprite_type):205 self.__content.set_sprite(sprite_type)206 self.__content.set_location(self.x + 8, self.y + 8)207 def update(self, delta_time, entities):208 pass209 def draw(self, surface):210 self.sprite.draw(surface, CameraType.DYNAMIC)211 self.__content.draw(surface, CameraType.DYNAMIC)212class NPCType(IntEnum):213 MALE = 0214 FEMALE = 1215class NPC(Kinetic):216 def __init__(self, x, y, type, can_move=True, horizontal=True, start_direction=1, walk_duration=5000):217 super(NPC, self).__init__(x, y, 10, 10, 25)218 self.type = type219 self.sprite = Sprite(self.x - 3, self.y - 22, SpriteType.NONE)220 self.shadow = Sprite(self.x - 3, self.y - 21, SpriteType.PLAYER_SHADOW)221 self.speech_bubble = SpeechBubble(self.x - 11, self.y - 32 - 11, self)222 self.radius = 32223 self.show_prompt = False224 self.set_color(Color.RED)225 self.walk_direction = 1 if start_direction >= 0 else -1226 self.horizontal = horizontal227 self.can_move = can_move228 self._walk_timer = Timer(walk_duration, True)229 self.animation_walk = Animation(6, 6, 100)230 self.walking = True231 self._set_walking_sprite()232 self._set_random_emotion()233 def set_location(self, x, y):234 super(NPC, self).set_location(x, y)235 self.sprite.set_location(self.x - 3, self.y - 22)236 self.shadow.set_location(self.x - 3, self.y - 21)237 self.speech_bubble.set_location(self.x - 11, self.y - 32 - 11)238 def _set_random_emotion(self):239 rand = randint(1, 4)240 if rand == 1:241 self.speech_bubble.set_content(SpriteType.FACE_HAPPY)242 elif rand == 2:243 self.speech_bubble.set_content(SpriteType.FACE_SAD)244 elif rand == 3:245 self.speech_bubble.set_content(SpriteType.FACE_MAD)246 elif rand == 4:247 self.speech_bubble.set_content(SpriteType.FACE_SURPRISED)248 def _stop_walking(self):249 if not self.horizontal:250 if self.type == NPCType.MALE:251 self.sprite.set_sprite(SpriteType.NPC_M_F)252 else:253 self.sprite.set_sprite(SpriteType.NPC_F_F)254 self._set_random_emotion()255 self.walking = not self.walking256 def _walk(self, delta_time):257 self._walk_timer.update(delta_time)258 if self._walk_timer.done:259 if random() < 0.25:260 self._stop_walking()261 if self.walking:262 self.walk_direction = -self.walk_direction263 self._set_walking_sprite()264 self._walk_timer.reset()265 self._walk_timer.start()266 if self.walking:267 if self.horizontal:268 self.set_location(self.x + self.move_speed *269 self.walk_direction, self.y)270 else:271 self.set_location(272 self.x, self.y + self.move_speed * self.walk_direction)273 def _set_walking_sprite(self):274 if self.can_move:275 if self.horizontal:276 if self.walk_direction > 0:277 if self.type == NPCType.MALE:278 self.sprite.set_sprite(SpriteType.NPC_M_R)279 else:280 self.sprite.set_sprite(SpriteType.NPC_F_R)281 else:282 if self.type == NPCType.MALE:283 self.sprite.set_sprite(SpriteType.NPC_M_L)284 else:285 self.sprite.set_sprite(SpriteType.NPC_F_L)286 else:287 if self.walk_direction > 0:288 if self.type == NPCType.MALE:289 self.sprite.set_sprite(SpriteType.NPC_M_F)290 else:291 self.sprite.set_sprite(SpriteType.NPC_F_F)292 else:293 if self.type == NPCType.MALE:294 self.sprite.set_sprite(SpriteType.NPC_M_B)295 else:296 self.sprite.set_sprite(SpriteType.NPC_F_B)297 else:298 if self.type == NPCType.MALE:299 self.sprite.set_sprite(SpriteType.NPC_M_F)300 else:301 self.sprite.set_sprite(SpriteType.NPC_F_F)302 def _within_radius(self, e):303 if distance_between(self.center, e.center) <= self.radius:304 self.show_prompt = True305 else:306 self.show_prompt = False307 def _update_conversation(self, entities):308 for e in entities:309 if isinstance(e, Player):310 last = self.show_prompt311 self._within_radius(e)312 if last != self.show_prompt:313 if self.show_prompt:314 entities.append(self.speech_bubble)315 else:316 entities.remove(self.speech_bubble)317 def _update_animation(self, delta_time):318 if self.walking:319 self.animation_walk.update(delta_time)320 self.sprite.set_frame(321 self.animation_walk.current_frame, self.animation_walk.columns)322 else:323 self.sprite.set_frame(0, self.animation_walk.columns)324 def _rectangle_collision_logic(self, entity):325 # Bottom326 if self.collision_rectangles[0].colliderect(entity.bounds) and self.velocity.y < 0:327 self.set_location(self.x, entity.bounds.bottom)328 # Top329 elif self.collision_rectangles[1].colliderect(entity.bounds) and self.velocity.y > 0:330 self.set_location(self.x, entity.bounds.top - self.bounds.height)331 # Right332 elif self.collision_rectangles[2].colliderect(entity.bounds) and self.velocity.x < 0:333 self.set_location(entity.bounds.right, self.y)334 # Left335 elif self.collision_rectangles[3].colliderect(entity.bounds) and self.velocity.x > 0:336 self.set_location(entity.bounds.left - self.bounds.width, self.y)337 def _collision(self, entities):338 for e in entities:339 if (340 isinstance(e, Building) or341 isinstance(e, Tree)342 ):343 self._rectangle_collision_logic(e)344 def update(self, delta_time, entities):345 self._update_conversation(entities)346 self._calculate_scaled_speed(delta_time)347 if self.can_move:348 self._walk(delta_time)349 # self._update_collision_rectangles()350 # self._collision(entities)351 if self.can_move:352 self._update_animation(delta_time)353 def draw(self, surface):354 if pygine.globals.debug:355 self._draw_bounds(surface, CameraType.DYNAMIC)356 else:357 self.shadow.draw(surface, CameraType.DYNAMIC)358 self.sprite.draw(surface, CameraType.DYNAMIC)359class Merchant(NPC):360 def __init__(self, x, y, type, speech_content):361 super(Merchant, self).__init__(x, y, type, can_move=False)362 self.speech_bubble.set_content(speech_content)363 def _update_conversation(self, entities):364 for e in entities:365 if isinstance(e, Player):366 last = self.show_prompt367 self._within_radius(e)368 if last != self.show_prompt:369 if self.show_prompt:370 entities.append(self.speech_bubble)371 else:372 entities.remove(self.speech_bubble)373class Building(Entity):374 def __init__(self, x, y, width, height):375 super(Building, self).__init__(x, y, width, height)376 self.sprite = None377 self.shadow = None378 def update(self, delta_time, entities):379 pass380 def draw(self, surface):381 if pygine.globals.debug:382 self._draw_bounds(surface, CameraType.DYNAMIC)383 else:384 self.shadow.draw(surface, CameraType.DYNAMIC)385 self.sprite.draw(surface, CameraType.DYNAMIC)386class SimpleHouse(Building):387 def __init__(self, x, y):388 super(SimpleHouse, self).__init__(x + 4, y + 24, 40, 40)389 self.sprite = Sprite(self.x - 4, self.y - 24, SpriteType.SIMPLE_HOUSE)390 self.shadow = Sprite(self.x - 4 - 16, self.y - 24,391 SpriteType.SIMPLE_HOUSE_SHADOW)392 self.set_color(Color.RED)393class SpecialHouse(Building):394 def __init__(self, x, y):395 super(SpecialHouse, self).__init__(x + 4, y + 24, 72, 40)396 self.sprite = Sprite(self.x - 4, self.y - 24, SpriteType.SPECIAL_HOUSE)397 self.shadow = Sprite(self.x - 4 - 16, self.y - 24,398 SpriteType.SPECIAL_HOUSE_SHADOW)399 self.set_color(Color.RED)400class Shop(Building):401 def __init__(self, x, y):402 super(Shop, self).__init__(x, y + 64, 80, 32)403 self.sprite = Sprite(self.x, self.y - 64, SpriteType.SHOP)404 self.shadow = Sprite(self.x - 16, self.y - 64,405 SpriteType.SHOP_SHADOW)406 self.set_color(Color.RED)407class Diner(Building):408 def __init__(self, x, y):409 super(Diner, self).__init__(x, y + 32, 128, 32)410 self.sprite = Sprite(self.x, self.y - 32, SpriteType.DINER)411 self.shadow = Sprite(self.x - 16, self.y - 32,412 SpriteType.DINER_SHADOW)413 self.set_color(Color.RED)414class Tree(Entity):415 def __init__(self, x, y):416 super(Tree, self).__init__(x, y, 20, 20)417 self.sprite = Sprite(self.x - 11 - 16, self.y -418 21, SpriteType.TREE_CLUSTER)419 # self.shadow = Sprite(self.x - 11 - 8, self.y - 21,420 # SpriteType.TREE_THING_SHADOW)421 def update(self, delta_time, entities):422 pass423 def draw(self, surface):424 if pygine.globals.debug:425 self._draw_bounds(surface, CameraType.DYNAMIC)426 else:427 # self.shadow.draw(surface, CameraType.DYNAMIC)428 self.sprite.draw(surface, CameraType.DYNAMIC)429class Furniture(Entity):430 def __init__(self, x, y, width, height):431 super(Furniture, self).__init__(x, y, width, height)432 self.sprite = None433 def update(self, delta_time, entities):434 pass435 def draw(self, surface):436 if pygine.globals.debug:437 self._draw_bounds(surface, CameraType.DYNAMIC)438 else:439 self.sprite.draw(surface, CameraType.DYNAMIC)440class FlowerPot(Furniture):441 def __init__(self, x, y):442 super(FlowerPot, self).__init__(x, y + 10, 16, 16)443 self.sprite = Sprite(self.x, self.y - 32, SpriteType.FLOWER_POT)444class Sofa(Furniture):445 def __init__(self, x, y):446 super(Sofa, self).__init__(x + 4, y + 10, 56, 16)447 self.sprite = Sprite(self.x - 4, self.y - 16, SpriteType.SOFA)448class Bed(Furniture):449 def __init__(self, x, y):450 super(Bed, self).__init__(x, y + 10, 32, 48)451 self.sprite = Sprite(self.x, self.y - 16, SpriteType.BED)452class Shelf(Furniture):453 def __init__(self, x, y, empty=True):454 super(Shelf, self).__init__(x, y + 10, 32, 16)455 if empty:456 self.sprite = Sprite(self.x, self.y - 48, SpriteType.SHELF_EMPTY)457 else:458 self.sprite = Sprite(self.x, self.y - 48, SpriteType.SHELF_FULL)459class CounterShop(Furniture):460 def __init__(self, x, y):461 super(CounterShop, self).__init__(x, y + 10, 112, 16)462 self.sprite = Sprite(self.x, self.y - 32, SpriteType.SHOP_COUNTER)463class CounterDiner(Furniture):464 def __init__(self, x, y):465 super(CounterDiner, self).__init__(x, y + 10 + 2, 256, 12)466 self.sprite = Sprite(self.x, self.y - 80 + 12,467 SpriteType.DINER_COUNTER)468class StoolTall(Furniture):469 def __init__(self, x, y):470 super(StoolTall, self).__init__(x, y + 10, 16, 6)471 self.sprite = Sprite(self.x, self.y - 16 - 6, SpriteType.STOOL_TALL)472class StoolShort(Furniture):473 def __init__(self, x, y):474 super(StoolShort, self).__init__(x, y + 10 + 4, 16, 6)475 self.sprite = Sprite(self.x, self.y - 16 - 4, SpriteType.STOOL_SHORT)476class Table(Furniture):477 def __init__(self, x, y):478 super(Table, self).__init__(x, y + 10, 32, 12)479 self.sprite = Sprite(self.x, self.y - 16, SpriteType.TABLE)480class Wall(Entity):481 def __init__(self, x, y, width, height):482 super(Wall, self).__init__(483 x * 16, y * 16 + 10, width * 16, height * 16)484 self.set_color(Color.BLUE)485 def apply_an_offset(self, x_offset, y_offset):486 self.set_location(self.x + x_offset, self.y + y_offset)487 def update(self, delta_time, entities):488 pass489 def draw(self, surface):490 if pygine.globals.debug:491 self._draw_bounds(surface, CameraType.DYNAMIC)492class ItemType(IntEnum):493 COFFEE = 0494 FISH = 1495 CROP = 2496 EGGS = 3497class Item(Entity):498 def __init__(self, x, y, value):499 super(Item, self).__init__(x, y, 16, 16)500 self._processed = False501 self._type = None502 self._sprite = None503 self.value = value504 def set_location(self, x, y):505 super(Item, self).set_location(x, y)506 self._sprite.set_location(self.x, self.y)507 def update(self, delta_time, entities):508 pass509 def draw(self, surface):510 self._sprite.draw(surface, CameraType.DYNAMIC)511class Coffee(Item):512 def __init__(self, x, y, value):513 super(Coffee, self).__init__(x, y, value)514 self._type = ItemType.COFFEE515 self._sprite = Sprite(x, y, SpriteType.COFFEE_RAW)516class Fish(Item):517 def __init__(self, x, y, value):518 super(Fish, self).__init__(x, y, value)519 self._type = ItemType.FISH520 self._sprite = Sprite(x, y, SpriteType.FISH_RAW)521class Crop(Item):522 def __init__(self, x, y, value):523 super(Crop, self).__init__(x, y, value)524 self._type = ItemType.CROP525 self._sprite = Sprite(x, y, SpriteType.CROP_RAW)526class Eggs(Item):527 def __init__(self, x, y, value):528 super(Eggs, self).__init__(x, y, value)529 self._type = ItemType.EGGS530 self._sprite = Sprite(x, y, SpriteType.EGGS_RAW)531class SellPad(Entity):532 def __init__(self, x, y, width, height, direction=Direction.UP):533 super(SellPad, self).__init__(x, y, width, height)534 self.direction = direction535 def __collision(self, entities):536 for e in entities:537 if e.bounds.colliderect(self.bounds):538 if isinstance(e, Player) and e.item_carrying is not None:539 if e.input.pressing(InputType.A) and int(e.facing) == int(self.direction):540 pygine.globals.money += e.item_carrying.value541 e.item_carrying = None542 def update(self, delta_time, entities):543 self.__collision(entities)544 def draw(self, surface):545 if pygine.globals.debug:546 self._draw_bounds(surface, CameraType.DYNAMIC)547###################################################################548#549# Coffee minigame stuff starts here!550#551###################################################################552class Boat(Actor):553 def __init__(self, x, y, beans=50):554 super(Boat, self).__init__(x, y, 83, 16, 50)555 self.beans = beans556 self.playbounds = Rectangle(557 0, 16 * 3, Camera.BOUNDS.width, Camera.BOUNDS.height - 16 * 3)558 self.sprite = Sprite(x - 16, y - 48, SpriteType.BOAT)559 self.shadow = Sprite(x - 16 - 16, y - 16, SpriteType.BOAT_SHADOW)560 self.blinks = 5561 self.invis_duration = 1600562 self.invis_timer = Timer(self.invis_duration)563 self.blink_timer = Timer(self.invis_duration / self.blinks / 2)564 self.damaged = False565 self.flashing = False566 self.dead = False567 def set_location(self, x, y):568 super(Boat, self).set_location(x, y)569 self.sprite.set_location(self.x - 16, self.y - 48)570 self.shadow.set_location(self.x - 16 - 16, self.y - 16)571 def _collision(self, entities):572 for e in entities:573 if not self.damaged:574 if isinstance(e, Bullet) or isinstance(e, Octopus):575 if self.bounds.colliderect(e.bounds):576 e.dead = True577 self.__decrease_health(5)578 elif isinstance(e, Rock):579 if self.bounds.colliderect(e.bounds):580 self.__decrease_health(10)581 self.__bounds_collision()582 def __decrease_health(self, amount):583 self.damaged = True584 self.beans -= amount585 self.invis_timer.start()586 self.blink_timer.start()587 self.sprite.set_sprite(SpriteType.BOAT_OWO)588 def _move(self, direction=Direction.NONE):589 self.facing = direction590 if self.facing == Direction.UP:591 self.set_location(self.x, self.y - self.move_speed)592 self.velocity.y = -1593 if self.facing == Direction.DOWN:594 self.set_location(self.x, self.y + self.move_speed)595 self.velocity.y = 1596 if self.facing == Direction.LEFT:597 self.set_location(self.x - self.move_speed, self.y)598 self.velocity.x = -1599 if self.facing == Direction.RIGHT:600 self.set_location(self.x + self.move_speed, self.y)601 self.velocity.x = 1602 def _update_input(self, delta_time):603 self.input.update(delta_time)604 if self.input.pressing(InputType.UP):605 self._move(Direction.UP)606 if self.input.pressing(InputType.DOWN):607 self._move(Direction.DOWN)608 if self.input.pressing(InputType.LEFT):609 self._move(Direction.LEFT)610 if self.input.pressing(InputType.RIGHT):611 self._move(Direction.RIGHT)612 def __update_health(self, delta_time):613 if self.damaged:614 self.invis_timer.update(delta_time)615 self.blink_timer.update(delta_time)616 if self.blink_timer.done:617 self.flashing = not self.flashing618 self.blink_timer.reset()619 self.blink_timer.start()620 if self.invis_timer.done:621 self.damaged = False622 self.flashing = False623 self.invis_timer.reset()624 self.sprite.set_sprite(SpriteType.BOAT)625 def __bounds_collision(self):626 if self.x < self.playbounds.x:627 self.x = self.playbounds.x628 elif self.x + self.width > self.playbounds.x + self.playbounds.width:629 self.x = self.playbounds.x + self.playbounds.width - self.width630 if self.y < self.playbounds.y:631 self.y = self.playbounds.y632 elif self.y + self.height > self.playbounds.y + self.playbounds.height:633 self.y = self.playbounds.y + self.playbounds.height - self.height634 def __check_death(self):635 if self.beans <= 0:636 # TODO: death logic here, maybe display transition and change scene?637 self.dead = True638 def update(self, delta_time, entities):639 self._calculate_scaled_speed(delta_time)640 if self.dead:641 self.flashing = False642 self.set_location(self.x - self.move_speed / 2 + randint(-2, 0), self.y + self.move_speed)643 return644 self._update_input(delta_time)645 self._collision(entities)646 self.__update_health(delta_time)647 self.__check_death()648 def draw(self, surface):649 if pygine.globals.debug:650 self._draw_bounds(surface, CameraType.DYNAMIC)651 else:652 if not self.flashing:653 self.shadow.draw(surface, CameraType.DYNAMIC)654 self.sprite.draw(surface, CameraType.DYNAMIC)655class Octopus(Kinetic):656 def __init__(self, x, y):657 super(Octopus, self).__init__(x, y, 16, 16, randint(10, 30))658 self.timer = Timer(randint(1, 5) * 1000, True)659 self.sprite = Sprite(x - 16, y - 16, SpriteType.OCTOPUS)660 self.shadow = Sprite(x - 16 - 8, y - 16 + 16,661 SpriteType.OCTOPUS_SHADOW)662 self.i = 0663 def set_location(self, x, y):664 super(Octopus, self).set_location(x, y)665 self.sprite.set_location(self.x - 16, self.y - 16)666 self.shadow.set_location(self.x - 16 - 8, self.y - 16 + 16)667 def __shoot(self, entities):668 entities.append(Bullet(self.x, self.y + self.height / 2, 200))669 def __move(self, delta_time):670 self.i += 1 * delta_time671 self.set_location(672 self.x - self.move_speed,673 self.y + math.sin(self.i)674 )675 def update(self, delta_time, entities): 676 self._calculate_scaled_speed(delta_time)677 self.__move(delta_time)678 self.timer.update(delta_time)679 if self.timer.done:680 if randint(1, 10) <= 7:681 self.__shoot(entities)682 self.timer.reset()683 self.timer.start()684 def draw(self, surface):685 if pygine.globals.debug:686 self._draw_bounds(surface, CameraType.DYNAMIC)687 else:688 self.shadow.draw(surface, CameraType.DYNAMIC)689 self.sprite.draw(surface, CameraType.DYNAMIC)690class Bullet(Kinetic):691 def __init__(self, x, y, speed=50):692 super(Bullet, self).__init__(x, y, 11, 12, speed)693 self.sprite = Sprite(x, y - 2, SpriteType.INK_BULLET)694 self.shadow = Sprite(x, y - 2 + 16, SpriteType.INK_BULLET_SHADOW)695 self.dead = False696 def set_location(self, x, y):697 super(Bullet, self).set_location(x, y)698 self.sprite.set_location(self.x, self.y - 2)699 self.shadow.set_location(self.x, self.y - 2 + 16)700 def update(self, delta_time, entities):701 self._calculate_scaled_speed(delta_time)702 self.set_location(self.x - self.move_speed, self.y)703 def draw(self, surface):704 if pygine.globals.debug:705 self._draw_bounds(surface, CameraType.DYNAMIC)706 else:707 self.shadow.draw(surface, CameraType.DYNAMIC)708 self.sprite.draw(surface, CameraType.DYNAMIC)709class Rock(Kinetic):710 def __init__(self, x, y, speed=75):711 super(Rock, self).__init__(x, y, 34, 14, speed)712 self.sprite = Sprite(x - 7, y - 16, SpriteType.ROCK)713 self.shadow = Sprite(self.x - 7 - 8, self.y -714 16, SpriteType.ROCK_SHADOW)715 self.dead = False716 def set_location(self, x, y):717 super(Rock, self).set_location(x, y)718 self.sprite.set_location(self.x - 7, self.y - 16)719 self.shadow.set_location(self.x - 7 - 8, self.y - 16)720 def update(self, delta_time, entities):721 self._calculate_scaled_speed(delta_time)722 self.set_location(self.x - self.move_speed, self.y)723 def draw(self, surface):724 if pygine.globals.debug:725 self._draw_bounds(surface, CameraType.DYNAMIC)726 else:727 self.shadow.draw(surface, CameraType.DYNAMIC)728 self.sprite.draw(surface, CameraType.DYNAMIC)729class SandWall(Entity):730 def __init__(self, x, y, layer, total_layers):731 super(SandWall, self).__init__(x, y - layer * 10, 64, 32)732 self.layer = layer733 self.sprite = Sprite(734 self.x, self.y, SpriteType.SAND_WALL)735 self.direction = 1736 self.parallax_layers = total_layers737 self.parallax_variance = 10738 self.parallax_speed = 0739 self.calculate_parallax_speed()740 def set_location(self, x, y):741 super(SandWall, self).set_location(x, y)742 self.sprite.set_location(self.x, self.y)743 def set_direction(self, direction):744 self.direction = direction745 self.calculate_parallax_speed()746 def calculate_parallax_speed(self):747 assert (self.parallax_variance > 0), \748 "Parallax Variance must be greater than zero!"749 self.parallax_speed = self.parallax_variance * \750 (self.parallax_layers - (self.layer + 1))**2 * self.direction * -1751 if self.parallax_speed == 0:752 self.parallax_speed = self.parallax_variance / 2 * self.direction * -1753 def update_parallax(self, delta_time): 754 self.set_location(self.x + self.parallax_speed * delta_time, self.y)755 def update(self, delta_time, entities):756 self.update_parallax(delta_time)757 def draw(self, surface):758 self.sprite.draw(surface, CameraType.DYNAMIC)759###################################################################760#761# Crop minigame stuff starts here!762#763###################################################################764class Mole(Entity):765 pass766class Mallet(Entity):767 pass768###################################################################769#770# Fish minigame stuff starts here!771#772###################################################################773class Hook(Actor):774 def __init__(self, ocean_depth):775 super(Hook, self).__init__(0, 0, 13, 13, 80)776 self.ocean_depth = ocean_depth777 self.sprite = Sprite(self.x, self.y - 7, SpriteType.HOOK)778 self.direction = 1779 self.total_hooked_fish = 0780 def set_location(self, x, y):781 super(Hook, self).set_location(x, y)782 self.sprite.set_location(self.x, self.y - 7)783 def __bounds_collision(self):784 if self.x < 0:785 self.x = 0786 elif self.x + self.width > Camera.BOUNDS.width:787 self.x = Camera.BOUNDS.width - self.width788 def _collision(self, entities):789 if self.y + Camera.BOUNDS.height / 2 > self.ocean_depth:790 self.direction = -1791 self.default_move_speed = 100792 self.__bounds_collision()793 for e in entities:794 if isinstance(e, OceanWall):795 if self.direction == -1:796 e.set_direction(-1)797 elif isinstance(e, Fishy) and not e.captured:798 if self.bounds.colliderect(e.bounds):799 e.hook_fish()800 self.total_hooked_fish += 1801 self.direction = -1802 def _move(self, direction=Direction.NONE):803 self.facing = direction804 if self.facing == Direction.LEFT:805 self.set_location(self.x - self.move_speed, self.y)806 self.velocity.x = -1807 if self.facing == Direction.RIGHT:808 self.set_location(self.x + self.move_speed, self.y)809 self.velocity.x = 1810 def _update_input(self, delta_time):811 self.input.update(delta_time)812 if self.input.pressing(InputType.LEFT):813 self._move(Direction.LEFT)814 if self.input.pressing(InputType.RIGHT):815 self._move(Direction.RIGHT)816 def update(self, delta_time, entities):817 self._calculate_scaled_speed(delta_time)818 self._update_input(delta_time)819 self._collision(entities)820 self.set_location(self.x, self.y + self.move_speed * self.direction)821 def draw(self, surface): 822 draw_line(823 surface,824 Camera.BOUNDS.width / 2 - 8, -8,825 self.x + 7, self.y - 6,826 CameraType.DYNAMIC,827 Color.BLACK,828 3829 )830 draw_line(831 surface,832 Camera.BOUNDS.width / 2 - 8, -8,833 self.x + 7, self.y - 6,834 CameraType.DYNAMIC,835 Color.WHITE,836 1837 )838 self.sprite.draw(surface, CameraType.DYNAMIC)839 if pygine.globals.debug:840 self._draw_bounds(surface, CameraType.DYNAMIC)841class OceanWall(Entity):842 def __init__(self, y, left, layer, total_layers):843 super(OceanWall, self).__init__(0 + layer *844 20 if left else Camera.BOUNDS.width - 32 - layer * 20, y, 32, 64)845 self.layer = layer846 self.sprite = Sprite(847 self.x, self.y, SpriteType.ROCK_WALL_L if left else SpriteType.ROCK_WALL_R)848 self.direction = 1849 self.parallax_layers = total_layers850 self.parallax_variance = 10851 self.parallax_speed = 0852 self.calculate_parallax_speed()853 def set_location(self, x, y):854 super(OceanWall, self).set_location(x, y)855 self.sprite.set_location(self.x, self.y)856 def set_direction(self, direction):857 self.direction = direction858 self.calculate_parallax_speed()859 def calculate_parallax_speed(self):860 assert (self.parallax_variance > 0), \861 "Parallax Variance must be greater than zero!"862 self.parallax_speed = self.parallax_variance * \863 (self.parallax_layers - (self.layer + 1))**2 * self.direction * -1864 def update_parallax(self, delta_time):865 self.set_location(self.x, self.y + self.parallax_speed * delta_time)866 def update(self, delta_time, entities):867 self.update_parallax(delta_time)868 def draw(self, surface):869 self.sprite.draw(surface, CameraType.DYNAMIC)870class Fishy(Kinetic):871 def __init__(self, y, left):872 super(Fishy, self).__init__(-32 if left else Camera.BOUNDS.width +873 16, y, 16, 16, randint(1, 10) * 10)874 self.type = randint(0, 1)875 self.velocity.x = -1 if left else 1876 if self.velocity.x > 0:877 if self.type == 0:878 self.sprite = Sprite(879 self.x, self.y, SpriteType.FISH_SMALL_L if left else SpriteType.FISH_SMALL_R)880 elif self.type == 1:881 self.sprite = Sprite(882 self.x, self.y, SpriteType.FISH_LARGE_L if left else SpriteType.FISH_LARGE_R)883 else:884 if self.type == 0:885 self.sprite = Sprite(886 self.x, self.y, SpriteType.FISH_SMALL_L if left else SpriteType.FISH_SMALL_L)887 elif self.type == 1:888 self.sprite = Sprite(889 self.x, self.y, SpriteType.FISH_LARGE_L if left else SpriteType.FISH_LARGE_L)890 self.captured = False891 def set_location(self, x, y):892 super(Fishy, self).set_location(x, y)893 self.sprite.set_location(self.x, self.y)894 def hook_fish(self):895 self.captured = True896 def _update_ai(self):897 self.set_location(self.x + self.move_speed * self.velocity.x, self.y)898 def flip_velocity(self):899 self.velocity.x *= -1900 if self.velocity.x > 0:901 if self.type == 0:902 self.sprite.set_sprite(SpriteType.FISH_SMALL_R)903 elif self.type == 1:904 self.sprite.set_sprite(SpriteType.FISH_LARGE_R)905 else:906 if self.type == 0:907 self.sprite.set_sprite(SpriteType.FISH_SMALL_L)908 elif self.type == 1:909 self.sprite.set_sprite(SpriteType.FISH_LARGE_L)910 def _collision(self, entities):911 if self.captured:912 for e in entities:913 if isinstance(e, Hook):914 if self.velocity.x > 0:915 self.set_location(916 e.x - 6 + randint(-3, 3), e.y + 4 + randint(-3, 3))917 else:918 self.set_location(919 e.x + 2 + randint(-3, 3), e.y + 4 + randint(-3, 3))920 if randint(1, 10) <= 1:921 self.flip_velocity()922 return923 if (self.bounds.left < -32 and self.velocity.x < 0) or (self.bounds.right > Camera.BOUNDS.width + 32 and self.velocity.x > 0):924 self.flip_velocity()925 def update(self, delta_time, entities):926 self._calculate_scaled_speed(delta_time)927 # self._update_collision_rectangles()928 self._update_ai()929 self._collision(entities)930 def draw(self, surface):931 if pygine.globals.debug:932 self._draw_bounds(surface, CameraType.DYNAMIC)933 self._draw_collision_rectangles(surface)934 else:...
dock_splitter.py
Source:dock_splitter.py
...114 self._max_bounds = self.parent.get_splitter_bounds( self )115 self._first_bounds = self.bounds116 self.index = self.parent.splitters.index( self )117 if not self._live_drag:118 self._draw_bounds( event, self.bounds )119 def mouse_up ( self, event ):120 """ Handles the left mouse button being released.121 """122 if self._click_pending:123 hx, hy, hdx, hdy = self._hot_spot124 if not self.is_in( event, hx, hy, hdx, hdy ):125 return126 is_horizontal = (self.style == 'horizontal')127 x, y, dx, dy = self.bounds128 if self._last_bounds is not None:129 if is_horizontal:130 y = self._last_bounds[1]131 else:132 x = self._last_bounds[0]133 state = self.state134 contents = self.parent.visible_contents135 ix1, iy1, idx1, idy1 = contents[ self.index ].bounds136 ix2, iy2, idx2, idy2 = contents[ self.index + 1 ].bounds137 if is_horizontal:138 if state != SPLIT_HMIDDLE:139 if ((y == self.bounds[1]) or140 (y < iy1) or141 ((y + dy) > (iy2 + idy2))):142 y = (iy1 + iy2 + idy2 - dy) / 2143 else:144 self._last_bounds = self.bounds145 if event.x < (hx + (hdx / 2)):146 y = iy1147 else:148 y = iy2 + idy2 - dy149 elif state != SPLIT_VMIDDLE:150 if ((x == self.bounds[0]) or151 (x < ix1) or152 ((x + dx) > (ix2 + idx2))):153 x = (ix1 + ix2 + idx2 - dx) / 2154 else:155 self._last_bounds = self.bounds156 if event.y < (hy + (hdy / 2)):157 x = ix2 + idx2 - dx158 else:159 x = ix1160 self.bounds = ( x, y, dx, dy )161 else:162 self._last_bounds, self._first_bounds = self._first_bounds, None163 if not self._live_drag:164 self._draw_bounds( event )165 self.parent.update_splitter( self, event.control )166 def mouse_move ( self, event ):167 """ Handles the mouse moving while the left mouse button is pressed.168 """169 if not self._click_pending:170 x, y, dx, dy = self._first_bounds171 mx, my, mdx, mdy = self._max_bounds172 if self.style == 'horizontal':173 y = y + event.y - self._xy[1]174 y = min( max( y, my ), my + mdy - dy )175 else:176 x = x + event.x - self._xy[0]177 x = min( max( x, mx ), mx + mdx - dx )178 bounds = ( x, y, dx, dy )179 if bounds != self.bounds:180 self.bounds = bounds181 if self._live_drag:182 self.parent.update_splitter( self, event.control )183 else:184 self._draw_bounds( event, bounds )185 def hover_enter ( self ):186 """ Handles the mouse hovering over the item.187 """188 pass189 def hover_exit ( self ):190 """ Handles the mouse exiting from hovering over the item.191 """192 pass193 def _draw_bounds ( self, event, bounds = None ):194 """ Draws the splitter bar in a new position while it is being dragged.195 """196 # Set up the drawing environment:197 window = event.control198 g, x0, y0 = window.screen_graphics...
polyline.py
Source:polyline.py
...64 def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):65 """ Draws a closed polygon. """66 gc.save_state()67 try:68# self._draw_bounds(gc)69 if len(self.points) >= 2:70 # Set the drawing parameters.71 gc.set_fill_color(self.pen.fill_color_)72 gc.set_stroke_color(self.pen.color_)73 gc.set_line_width(self.pen.line_width)74 # Draw the path.75 gc.begin_path()76 gc.lines(self.points)77 gc.stroke_path()78 finally:79 gc.restore_state()80 def _draw_bounds(self, gc):81 """ Draws the component bounds for testing purposes. """82 dx, dy = self.bounds83 x, y = self.position84 gc.rect(x, y, dx, dy)85 gc.stroke_path()86 def normal_left_down(self, event):87 """ Handles left mouse button clicks in 'normal' mode """88 print "Polyline selected at (%d, %d)" % (event.x, event.y)89 @on_trait_change("pen.+,points")90 def _update(self):91 if not self.points: return92 x_points = [x for x, y in self.points]93 y_points = [y for x, y in self.points]94 x = min(x_points)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!