def setup_smoke(self): rect = Rect(0, 0, 200, 200) file_name = RESOURCE_DIR + 'smokes.zip' images = get_images_from_zip_file(file_name, 'smoke puff up', 'smoke_puff') self.smoke_normal = AnimatedSprite(rect, self, images, 8) rect = Rect(0, 0, 200, 200) file_name = RESOURCE_DIR + 'Exp_type_C.png' images = slice_image_into_tiles(Image.open(file_name), 48) self.smoke_special = AnimatedSprite(rect, self, images, 2)
def setup(self): self.climb = 0 self.cloud_height = 200 self.game_state = GAME_WAITING self.high_scores = HighScores() self.high_score_msg = None ground_level = self.create_ground(12) self.generate_clouds() rect = Rect(0, 0, IMAGE_WIDTH, IMAGE_WIDTH) rect.center(self.bounds.center()) rect.y = ground_level self.player = Player(rect, self) self.player_apex_frame = False self.player_max_y = self.bounds.h * 0.6 self.setup_smoke()
def create_ground(self, max_blocks=12): block_size_w = self.bounds.w / max_blocks block_size_h = block_size_w * 171 / 101 # image is 101 x 171 pixels for i in range(max_blocks): rect = Rect(i * block_size_w, 0, block_size_w, block_size_h) GrassBlock(rect, self) return block_size_h * 0.7 # the new ground level
def generate_clouds(self): while self.cloud_height < self.bounds.h * 2: q = min(self.climb, DIFFICULTY_Q) min_dist = int(MAX_CLOUD_DIST * q / DIFFICULTY_Q) max_dist = int(MAX_CLOUD_DIST / 2 + min_dist / 2) self.cloud_height += random.randint(min_dist, max_dist) rect = Rect(random.random() * (self.bounds.w - 150), self.cloud_height, 0, 0) cloud = Cloud(rect, self) if random.random() < ENEMY_DENSITY: # generate new enemy rect = Rect(0, 0, 64, 64) rect.center(cloud.frame.center()) rect.y = cloud.frame.top() - 15 enemy = Enemy(rect, self) enemy.velocity = cloud.velocity
def __init__(self, **kwargs): w, h = ui.get_screen_size() va = Rect(-2 * w, -h, 4 * w, 2 * h) super().__init__(viewable_area=va, **kwargs) b = BoxNode((va[2], va[3]), parent=self, fill_color='blue', dynamic=False) b.node.physicsBody = None c1 = CircleNode(20, parent=self, fill_color='red', position=(va.x, va.y)) c2 = CircleNode(20, parent=self, fill_color='red', position=(va.x + va.width, va.y)) c3 = CircleNode(20, parent=self, fill_color='red', position=(va.x + va.width, va.y + va.height)) c4 = CircleNode(20, parent=self, fill_color='red', position=(va.x, va.y + va.height))
def viewable_area(self, *args): if args: value = args[0] if type(value) is not Rect: if value is not None and len(value) == 4: value = Rect(*value) self._viewable_area = value else: return self._viewable_area
def __init__(self, rect=Rect(), parent=None): cloud_image = self.cloud_maker() new_rect = pil_rect_to_scene_rect(cloud_image.getbbox()) rect.w, rect.h = new_rect.w, new_rect.h super(self.__class__, self).__init__(rect, parent, load_pil_image(cloud_image)) self.velocity.x = random.randint( -1, 4) # give clouds a 2-in-6 chance to be moving if self.velocity.x > 1: self.velocity.x = 0 self.velocity.x *= 50
def bounds(self, *args): if args: value = args[0] raise NotImplementedError('Setting bounds on a scene not supported') else: x,y,w,h = self.view.frame c = self.convert_from_view corner = c((x, y+h)) other = c((x+w, y)) return Rect(*corner, other.x-corner.x, other.y-corner.y)
def cg_to_py(value): if type(value) == ObjCInstanceMethodProxy: value = value() if type(value) == CGPoint: return ui.Point(value.x, value.y) elif type(value) == CGVector: return ui.Point(value.dx, value.dy) elif type(value) == CGRect: return Rect(value.origin.x, value.origin.y, value.size.width, value.size.height) elif type(value) == CGSize: return Size(value.width, value.height)
def __init__(self, rect=Rect(), parent=None): super(self.__class__, self).__init__(rect, parent, 'Alien_Monster') self.tint = Color(1, 0, 1, 0)
def __init__(self, rect=Rect(), parent=None): super(self.__class__, self).__init__(rect, parent, 'PC_Grass_Block')
def __init__(self, rect=Rect(), parent=None): super(self.__class__, self).__init__(rect, parent, game_character)
from dataclasses import dataclass from typing import ClassVar, List import time from functools import partial from scene import SpriteNode, ShapeNode, LabelNode, Action, Scene, Node, Rect, Point, Texture from urllib.request import urlopen FPS: float = 60 DEBUG = False #url = urlopen('https://pre00.deviantart.net/ba81/th/pre/f/2009/242/f/4/space_invaders_sprite_sheet_by_gooperblooper22.png') with open('space_invaders.png', 'rb') as f: img = ui.Image.from_data(f.read()) whole_sprite = Texture(img) taito_sprite = whole_sprite.subtexture(Rect(0.0, 0.837, 1, 0.163)) invader1 = whole_sprite.subtexture(Rect(0.0, 0.88, 0.05, 0.012)) invader2 = whole_sprite.subtexture(Rect(0.054, 0.88, 0.05, 0.012)) invader3 = whole_sprite.subtexture(Rect(0.112, 0.88, 0.05, 0.012)) invader4 = whole_sprite.subtexture(Rect(0.167, 0.88, 0.05, 0.012)) invader5 = whole_sprite.subtexture(Rect(0.232, 0.88, 0.05, 0.012)) invader6 = whole_sprite.subtexture(Rect(0.285, 0.88, 0.05, 0.012)) ufo = whole_sprite.subtexture(Rect(0.342, 0.88, 0.09, 0.012)) ship = whole_sprite.subtexture(Rect(0.445, 0.88, 0.05, 0.01)) shield1 = whole_sprite.subtexture(Rect(0.505, 0.88, 0.085, 0.019)) shield2 = whole_sprite.subtexture(Rect(0.598, 0.88, 0.085, 0.019)) shield3 = whole_sprite.subtexture(Rect(0.683, 0.88, 0.085, 0.019)) shield4 = whole_sprite.subtexture(Rect(0.77, 0.88, 0.085, 0.019)) shield5 = whole_sprite.subtexture(Rect(0.77, 0.853, 0.085, 0.019)) shield6 = whole_sprite.subtexture(Rect(0.575, 0.856, 0.085, 0.019)) beam = whole_sprite.subtexture(Rect(0.66, 0.853, 0.03, 0.019))
def body(self) -> Rect: r = self.frame return Rect(r.x, r.y, r.w - 10, r.h - 50)
def __init__(self, x, y, w, h): Rect.__init__(self, x, y, w, h) self.reset()
def get_hit_boxes(self): x, y = self.position return [ Rect(x, y + self.gap, Pipe.blocksize, self.ptoph), Rect(x, 0, Pipe.blocksize, self.pbottomh) ]
def pil_rect_to_scene_rect(pil_rect=(1, 2, 3, 4)): if pil_rect: l, t, r, b = pil_rect return Rect(l, t, r - l, b - t) else: return Rect()
def get_hitbox(self): w, h = (42, 36) x, y = self.position return Rect(x - w / 2, y - h / 2, w, h)
def __init__(self, rect=Rect(), parent=None, image_name='Boy'): super(Sprite, self).__init__(rect) if parent: parent.add_layer(self) self.image = image_name self.velocity = Point(0, 0)
False >>> ui.Point(1,1) in ui.Rect(0,0,10,10) True >>> ui.Point(-1,1) in ui.Rect(0,0,10,10) False #============================== assert(ui.Rect is scene.Rect) #============================== >>> v=ui.View(0,0,100,100) >>> b=ui.Button(frame=(v.width,0,-30,30)) >>> b.frame Rect(70.00, 0.00, 30.00, 30.00) #============================== touch in sprite_node # throws a TypeError -- I like this syntax! touch.location in sprite_node # throws a TypeError touch in sprite_node.frame # always returns False touch.location in sprite_node.frame # a real hit test ... import scene class MyScene(scene.Scene): def setup(self): self.snake = scene.SpriteNode(parent=self, position=self.bounds.center(),