示例#1
0
    def __init__(self, screen):
        """Init the player ship and it's starting position"""
        self.screen = screen

        # load the ships image and get it's rect
        self.image = GFX().ship['c_0']
        self.mask = GFX().ship_mask['c_0']
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        # Start the ship at the bottom center of the screen
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Attributes
        self.pos_x = float(self.rect.centerx)
        self.shooting_timer = 0

        # Flags
        self.moving_left = False
        self.moving_right = False
        self.moving_center = False  # Used for resetting ship image
        self.shooting = False

        self.effects = Group()
    def __init__(self, dir, s_pos,t_pos,type='Arrow'):
        GFX.__init__(self)
        
        if ProjectileFX.rescache==None:
            ProjectileFX.rescache=Res('dc-item.png', TILESIZE)
        
        if type == 'Arrow':
            img = 73
        if dir == MOVE_UP:
            img = img
        if dir == MOVE_UP_RIGHT:
            img += 1
        if dir == MOVE_RIGHT:
            img += 2
        if dir == MOVE_DOWN_RIGHT:
            img += 3
        if dir == MOVE_DOWN:
            img += 4
        if dir == MOVE_DOWN_LEFT:
            img += 5
        if dir == MOVE_LEFT:
            img += 6
        if dir == MOVE_UP_LEFT:
            img += 7
            
        self.image=self.rescache.get(img)

        self.__pos_gen = self.__pos(s_pos, t_pos)
        self.redraw = True
示例#3
0
    def update(self, set='start'):
        """
        Goes trough the story list, keeps duration for showing the story and updates current image and text

        :param set: string start or end - which part of story to show
        :return: True if showing image, False if all images have been shown
        """
        # If it's time for next story
        if self.story_timer < time():
            self.story_image = None
            self.story_text = ''

            if set == 'start':
                story = self.start_story
            else:
                story = self.end_story

            if len(story):
                page = story.pop(0)
                # Set next story image, duration and text
                self.story_image = GFX().story[page['image']]
                self.story_timer = time() + page['time']
                if 'text' in page.keys():
                    self.story_text = page['text']
            else:
                return False
        else:
            # Allow skipping of story at the start of level
            if set == 'start':
                if Events().key_pressed:
                    self.story_timer = 0

        return True
示例#4
0
    def __init__(self, dir, s_pos,t_pos,item):
        GFX.__init__(self)
        
        self.image=item.get_dd_img()

        self.__pos_gen = self.__pos(s_pos, t_pos)
        self.redraw = True
    def __init__(self, dir, s_pos, t_pos, type='Arrow'):
        GFX.__init__(self)

        if ProjectileFX.rescache == None:
            ProjectileFX.rescache = Res('dc-item.png', TILESIZE)

        if type == 'Arrow':
            img = 73
        if dir == MOVE_UP:
            img = img
        if dir == MOVE_UP_RIGHT:
            img += 1
        if dir == MOVE_RIGHT:
            img += 2
        if dir == MOVE_DOWN_RIGHT:
            img += 3
        if dir == MOVE_DOWN:
            img += 4
        if dir == MOVE_DOWN_LEFT:
            img += 5
        if dir == MOVE_LEFT:
            img += 6
        if dir == MOVE_UP_LEFT:
            img += 7

        self.image = self.rescache.get(img)

        self.__pos_gen = self.__pos(s_pos, t_pos)
        self.redraw = True
class GameState:
    def __init__(self):
        self.gfx = GFX()

    def update(self):
        self.gfx.update()

    def render(self, display):
        self.gfx.render(display)
示例#7
0
文件: main.py 项目: minuJeong/gl_kata
class Client(object):
    def __init__(self, window):
        super().__init__()
        self.gfx = GFX()
        self.window = window

        self.gfx.init()

    def tick(self, elapsed_time, delta_time):
        self.gfx.tick(elapsed_time, delta_time)
示例#8
0
    def __init__(self, position):
        super(Hit, self).__init__()

        self.animation = GFX().fx_hits[randint(0, len(GFX().fx_hits) - 1)]
        self.animation_speed = 0.05
        self.frame = 0
        self.image = self.animation[0]
        self.rect = self.image.get_rect()
        self.rect.centerx = position[0]
        self.rect.centery = position[1]
        self.timer = 0
示例#9
0
    def __init__(self, position):
        super(Explosion, self).__init__(position)
        self.animation = GFX().fx_explosion[randint(
            0,
            len(GFX().fx_explosion) - 1)]
        self.animation_speed = 0.07

        self.image = self.animation[0]
        self.rect = self.image.get_rect()
        self.rect.centerx = position[0]
        self.rect.centery = position[1]
示例#10
0
    def __init__(self, position, speed, look=1, target=None):
        """ Create and aim bullet

        Target angle is -90 left, 0 straight down, 90 right
        :param position: Starting position: Sprite or [x, y]
        :param gfx: game graphics lib
        :param speed: speed of the bullet
        :param look: index of the bullet image
        :param target: Target to aim at None/int angle/[x,y]
        """
        super(EnemyBullet, self).__init__()

        # Set selected image, mask and rect
        self.image = GFX().bullets[look]
        self.mask = GFX().bullets_mask[look]
        self.rect = self.image.get_rect()

        # Set starting position
        if isinstance(position, Sprite):
            self.rect.centerx = position.rect.centerx
            self.rect.bottom = position.rect.bottom
        else:
            self.rect.centerx = position[0]
            self.rect.centery = position[1]

        # Attributes
        self.pos_x = float(self.rect.x)
        self.pos_y = float(self.rect.y)
        self.speedx = 0
        self.speedy = 0
        self.angle = 0

        if target is None:
            self.speedy = speed
        else:
            if isinstance(target, list):
                # Calculate angle to target
                self.angle = rt_angle(target[0] - self.rect.x,
                                      target[1] - self.rect.y)
            else:
                self.angle = target

            # Rotate bullet image
            if self.angle != 0:
                self.image = rotate(self.image, self.angle)
                self.mask = from_surface(self.image)
                self.rect = self.image.get_rect()

            # Perform vectoring based on angle
            self.angle += 90
            self.angle = radians(self.angle)
            self.speedx = cos(self.angle) * speed * -1
            self.speedy = sin(self.angle) * speed
示例#11
0
    def __init__(self, ship):
        super(Bullet, self).__init__()

        # Create bullet at the correct position
        self.image = GFX().bullets[0]
        self.mask = GFX().bullets_mask[0]
        self.rect = self.image.get_rect()
        self.rect.centerx = ship.rect.centerx
        self.rect.bottom = ship.rect.top

        # Attributes
        self.pos_y = float(self.rect.y)
        self.speed = CFG().bullet_speed
示例#12
0
 def draw_lives(self):
     x = self.lives_x
     lives = self.status.lives
     for a in range(CFG().max_lives):
         if lives != 0:
             self.screen.blit(
                 GFX().hud_lives[0],
                 (x, self.lives_y, self.lives_size, self.lives_size))
             lives -= 1
         else:
             self.screen.blit(
                 GFX().hud_lives[1],
                 (x, self.lives_y, self.lives_size, self.lives_size))
         x += 22
示例#13
0
class Bullet(Sprite):
    """A class to manage bullets from the ship"""
    def __init__(self, ship):
        super(Bullet, self).__init__()

        # Create bullet at the correct position
        self.image = GFX().bullets[0]
        self.mask = GFX().bullets_mask[0]
        self.rect = self.image.get_rect()
        self.rect.centerx = ship.rect.centerx
        self.rect.bottom = ship.rect.top

        # Attributes
        self.pos_y = float(self.rect.y)
        self.speed = CFG().bullet_speed

    def update(self, dt):
        """Move the bullet on screen"""
        self.pos_y -= self.speed * dt
        self.rect.y = self.pos_y

        if self.rect.bottom <= 0:
            self.kill()

    def blitme(self, screen):
        """Draw the bullet"""
        screen.blit(self.image, self.rect)
示例#14
0
    def __init__(self, int_screen, filename=''):
        if not filename:
            filename = str(CFG().start_level)

        with open(path.join(CFG().path_levels, filename), 'r') as file:
            level = loads(file.read())

        self.int_screen = int_screen
        self.timer = 0
        self.enemies = Group()
        self.enemy_hold = False
        self.show_name = False

        self.starting = True
        self.ending = False

        self.text = Text(int_screen, CFG().font_main, 16, (255, 255, 255))

        self.layout = level['layout']
        self.background = Background(self.int_screen, GFX().background[level['background']],
                                     type=level['background_type'],
                                     use_stars=level['stars'],
                                     lenght=len(self.layout))
        self.story = Story(level['prestory'], level['poststory'], self.int_screen)
        self.next_level = level['nextlevel']
        self.name = level['name']
        self.music = level['music']
示例#15
0
    def begin(self):
        _Inkplate.init(I2C(0, scl=Pin(22), sda=Pin(21)))

        self.ipg = InkplateGS2()
        self.ipm = InkplateMono()
        self.ipp = InkplatePartial(self.ipm)

        self.GFX = GFX(
            D_COLS,
            D_ROWS,
            self.writePixel,
            self.writeFastHLine,
            self.writeFastVLine,
            self.writeFillRect,
            None,
            None,
        )
示例#16
0
    def __init__(self, pos_x):
        """Init asteroid - square sprite with random image and rotation speed"""
        super(Asteroid, self).__init__()
        self.movement_speed = CFG().asteroid_small_speed

        # Stats
        self.health = 100
        self.reward = 100
        self.pickup = None

        # Select random asteroid image
        self.i = randint(0, len(GFX().asteroids)-1)
        self.image = GFX().asteroids[self.i][0]

        # Set starting position
        self.rect = self.image.get_rect()
        self.rect.centerx = pos_x
        self.rect.bottom = 0

        self.size = self.rect.height
        self.timer = 0

        self.num_frames = len(GFX().asteroids[self.i])    # Gen number of animation frames
        self.frame = randint(0, self.num_frames-1)   # Current frame number of animation - set random one
        self.rotation_speed = uniform(0.08, 0.2)     # time in seconds between animation frames
        self.direction = choice([1, -1])

        self.image = GFX().asteroids[self.i][self.frame]
        self.mask = GFX().asteroids_mask[self.i][self.frame]
示例#17
0
    def update(self, dt, enemy_bullets, ship):
        """Update movement and animation"""
        # If time between frames elapsed - set image of the sprite to the next frame
        if self.timer < time():
            self.timer = time() + self.rotation_speed

            self.frame += self.direction
            # loop animation
            if self.frame == self.num_frames:
                self.frame = 0
            if self.frame == -1:
                self.frame = self.num_frames-1

            self.image = GFX().asteroids[self.i][self.frame]
            self.mask = GFX().asteroids_mask[self.i][self.frame]

        # move the asteroid
        self.rect.bottom += self.movement_speed * dt
示例#18
0
 def draw_boss_lives(self):
     if self.status.boss_lives != 0:
         self.text.write('Boss', self.boss_x, self.boss_y, origin='center')
         lives = self.status.boss_lives
         x = self.boss_x - (self.boss_rect.width * 5 + 16)
         for a in range(10):
             if lives != 0:
                 self.screen.blit(
                     GFX().progressbar[0],
                     (x, self.boss_y + 25, self.boss_rect.width,
                      self.boss_rect.height))
                 lives -= 1
             else:
                 self.screen.blit(
                     GFX().progressbar[1],
                     (x, self.boss_y + 25, self.boss_rect.width,
                      self.boss_rect.height))
             x += 20
示例#19
0
    def __init__(self, status, screen, clock):
        self.status = status
        self.screen = screen
        self.rect = screen.get_rect()
        self.clock = clock
        self.text = Text(self.screen, CFG().font_main, 16, (255, 255, 255))

        bottom_border = CFG().screen_height - (
            (CFG().screen_height - CFG().int_scale_height) / 4)
        # Score position
        self.score_x = CFG().screen_width / 2
        self.score_y = bottom_border
        # Lives position
        self.lives_size = GFX().hud_lives[0].get_rect().width
        self.lives_x = 20
        self.lives_y = bottom_border - (self.lives_size / 2)
        # Boss lives position
        self.boss_x = CFG().screen_width / 2
        self.boss_y = ((CFG().screen_height - CFG().int_scale_height) / 6)
        self.boss_rect = GFX().progressbar[0].get_rect()
示例#20
0
    def __init__(self, ship):
        super(PlayerShield, self).__init__()

        self.ship = ship
        self.animation = GFX().fx_player_shield
        self.animation_speed = 0.05
        self.frame = 0
        self.image = self.animation[0]
        self.rect = self.image.get_rect()
        self.rect.centerx = self.ship.rect.centerx
        self.rect.centery = self.ship.rect.centery
        self.timer = 0
示例#21
0
class Asteroid(Sprite):
    def __init__(self, pos_x):
        """Init asteroid - square sprite with random image and rotation speed"""
        super(Asteroid, self).__init__()
        self.movement_speed = CFG().asteroid_small_speed

        # Stats
        self.health = 100
        self.reward = 100
        self.pickup = None

        # Select random asteroid image
        self.i = randint(0, len(GFX().asteroids)-1)
        self.image = GFX().asteroids[self.i][0]

        # Set starting position
        self.rect = self.image.get_rect()
        self.rect.centerx = pos_x
        self.rect.bottom = 0

        self.size = self.rect.height
        self.timer = 0

        self.num_frames = len(GFX().asteroids[self.i])    # Gen number of animation frames
        self.frame = randint(0, self.num_frames-1)   # Current frame number of animation - set random one
        self.rotation_speed = uniform(0.08, 0.2)     # time in seconds between animation frames
        self.direction = choice([1, -1])

        self.image = GFX().asteroids[self.i][self.frame]
        self.mask = GFX().asteroids_mask[self.i][self.frame]

    def update(self, dt, enemy_bullets, ship):
        """Update movement and animation"""
        # If time between frames elapsed - set image of the sprite to the next frame
        if self.timer < time():
            self.timer = time() + self.rotation_speed

            self.frame += self.direction
            # loop animation
            if self.frame == self.num_frames:
                self.frame = 0
            if self.frame == -1:
                self.frame = self.num_frames-1

            self.image = GFX().asteroids[self.i][self.frame]
            self.mask = GFX().asteroids_mask[self.i][self.frame]

        # move the asteroid
        self.rect.bottom += self.movement_speed * dt

    def hit(self):
        self.health -= 20
 def __init__(self, color1, color2, s_pos, t_pos, radius=1):
     GFX.__init__(self)
     self.f_image = []
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color1, (16, 16), 2, 2)
     self.f_image.append(surf)
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color2, (16, 16), 2, 2)
     self.f_image.append(surf)
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color1, (16, 16), 2, 2)
     self.f_image.append(surf)
     self.color1 = color1
     self.color2 = color2
     self.c = 0
     self.image = self.f_image[self.c]
     self.__pos_gen = self.__pos(s_pos, t_pos)
     self.redraw = False
     self.radius = radius
     self.cur_radius = 0
     self.explode = False
     self.finish = False
     self.lastpos = None
 def __init__(self, color1, color2, s_pos, t_pos):
     GFX.__init__(self)
     self.f_image = []
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color1, (15, 10), 2, 2)
     pygame.draw.circle(surf, color2, (10, 15), 2, 2)
     pygame.draw.circle(surf, color1, (20, 15), 2, 2)
     self.f_image.append(surf)
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color2, (15, 10), 2, 2)
     pygame.draw.circle(surf, color1, (10, 15), 2, 2)
     pygame.draw.circle(surf, color1, (20, 15), 2, 2)
     self.f_image.append(surf)
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color1, (15, 10), 2, 2)
     pygame.draw.circle(surf, color1, (10, 15), 2, 2)
     pygame.draw.circle(surf, color2, (20, 15), 2, 2)
     self.f_image.append(surf)
         
     self.c = 0
     self.image = self.f_image[self.c]
     self.__pos_gen = self.__pos(s_pos, t_pos)
     self.redraw = False
示例#24
0
    def __init__(self, color1, color2, s_pos, t_pos):
        GFX.__init__(self)
        self.f_image = []
        surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
        pygame.draw.circle(surf, color1, (15, 10), 2, 2)
        pygame.draw.circle(surf, color2, (10, 15), 2, 2)
        pygame.draw.circle(surf, color1, (20, 15), 2, 2)
        self.f_image.append(surf)
        surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
        pygame.draw.circle(surf, color2, (15, 10), 2, 2)
        pygame.draw.circle(surf, color1, (10, 15), 2, 2)
        pygame.draw.circle(surf, color1, (20, 15), 2, 2)
        self.f_image.append(surf)
        surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
        pygame.draw.circle(surf, color1, (15, 10), 2, 2)
        pygame.draw.circle(surf, color1, (10, 15), 2, 2)
        pygame.draw.circle(surf, color2, (20, 15), 2, 2)
        self.f_image.append(surf)

        self.c = 0
        self.image = self.f_image[self.c]
        self.__pos_gen = self.__pos(s_pos, t_pos)
        self.redraw = False
示例#25
0
 def __init__(self, color1, color2, s_pos, t_pos, radius=1):
     GFX.__init__(self)
     self.f_image = []
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color1, (16, 16), 2, 2)
     self.f_image.append(surf)
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color2, (16, 16), 2, 2)
     self.f_image.append(surf)
     surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
     pygame.draw.circle(surf, color1, (16, 16), 2, 2)
     self.f_image.append(surf)
     self.color1 = color1
     self.color2 = color2
     self.c = 0
     self.image = self.f_image[self.c]
     self.__pos_gen = self.__pos(s_pos, t_pos)
     self.redraw = False
     self.radius = radius
     self.cur_radius = 0
     self.explode = False
     self.finish = False
     self.lastpos = None
示例#26
0
    def __init__(self, status):
        """ Init enemy boss """
        super(Boss1, self).__init__(status)
        # Properties
        self.stage = 1
        self.next_stage = None
        self.max_health = 4000
        self.health = self.max_health
        self.status.boss_lives = 10
        self.reward = 2000
        self.animation_speed = 0.06
        # Shooting
        self.shoot_timer1 = 0
        self.canon = 1

        # Set images and rect
        self.img_down = GFX().boss1['down']
        self.img_right = GFX().boss1['right']
        self.img_up = GFX().boss1['up']

        self.mask_down = GFX().boss1_mask['down']
        self.mask_right = GFX().boss1_mask['right']
        self.mask_up = GFX().boss1_mask['up']

        self.image = self.img_down[0]
        self.mask = self.mask_down[0]
        self.rect = self.image.get_rect()

        # Movement animations - relative waypoints for movement and image for that movement
        self.animation1_start = [96, -64]
        self.animation1 = [[0, 128, self.img_down[0], self.mask_down[0]],
                           [5, 20, self.img_down[1], self.mask_down[1]],
                           [10, 20, self.img_down[2], self.mask_down[2]],
                           [20, 10, self.img_down[3], self.mask_down[3]],
                           [20, 5, self.img_right[0], self.mask_right[0]],
                           [338, 0, self.img_right[1], self.mask_right[1]],
                           [20, -5, self.img_right[0], self.mask_right[0]],
                           [20, -10, self.img_up[0], self.mask_up[0]],
                           [10, -20, self.img_up[1], self.mask_up[1]],
                           [5, -20, self.img_up[2], self.mask_up[2]],
                           [0, -128, self.img_up[3], self.mask_up[3]]]

        self.animation2_start = [192, -64]
        self.animation2 = [[0, 32, self.img_down[0], self.mask_down[0]],
                           [5, 20, self.img_down[1], self.mask_down[1]],
                           [10, 20, self.img_down[2], self.mask_down[2]],
                           [20, 10, self.img_down[3], self.mask_down[3]],
                           [20, 5, self.img_right[0], self.mask_right[0]],
                           [146, 0, self.img_right[1], self.mask_right[1]],
                           [20, -5, self.img_right[0], self.mask_right[0]],
                           [20, -10, self.img_up[0], self.mask_up[0]],
                           [10, -20, self.img_up[1], self.mask_up[1]],
                           [5, -20, self.img_up[2], self.mask_up[2]],
                           [0, -32, self.img_up[3], self.mask_up[3]]]

        self.init_animation(self.animation1_start)
        self.calculate_movement(self.animation1)
示例#27
0
    def __init__(self, position):
        """
        Adds lives or score to player when touched, moves down the level.

        :param position: list x,y where pickup should spawn
        """
        super(PickupShield, self).__init__()
        self.animation = GFX().pickups['shield']
        self.image = self.animation[0]
        self.rect = self.image.get_rect()
        self.rect.centerx = position[0]
        self.rect.centery = position[1]

        self.movement_speed = 0.1
        self.num_frames = len(self.animation)
        self.frame = 0
        self.timer = 0
        self.animation_speed = 0.1
示例#28
0
class Story():
    """ Shows story before and after level """
    def __init__(self, start_story, end_story, screen):
        """
        Init Story

        :param start_story: list of dict containing image name, time in seconds, and text
        :param end_story: list of dict containing image name, time in seconds, and text
        :param screen: surface to show the story on
        """
        self.start_story = start_story
        self.end_story = end_story
        self.screen = screen
        self.font = Text(self.screen, CFG().font_main, 11, (255, 255, 255), True)

        self.story_image = None
        self.story_timer = 0
        self.story_text = ''

        rect = self.screen.get_rect()
        self.text_x = rect.width / 2
        self.text_y = rect.height - (rect.height / 16)

    def update(self, set='start'):
        """
        Goes trough the story list, keeps duration for showing the story and updates current image and text

        :param set: string start or end - which part of story to show
        :return: True if showing image, False if all images have been shown
        """
        # If it's time for next story
        if self.story_timer < time():
            self.story_image = None
            self.story_text = ''

            if set == 'start':
                story = self.start_story
            else:
                story = self.end_story

            if len(story):
                page = story.pop(0)
                # Set next story image, duration and text
                self.story_image = GFX().story[page['image']]
                self.story_timer = time() + page['time']
                if 'text' in page.keys():
                    self.story_text = page['text']
            else:
                return False
        else:
            # Allow skipping of story at the start of level
            if set == 'start':
                if Events().key_pressed:
                    self.story_timer = 0

        return True

    def draw(self):
        """ Draw current story image and text"""
        self.screen.blit(self.story_image, self.story_image.get_rect())

        if self.story_text != '':
            text = self.story_text.split('\n')
            # Set initial padding from bottom based on number of lines
            y = self.text_y - (16 * len(text))

            for line in text:
                self.font.write(line, self.text_x, y, origin='center')
                y += 16
示例#29
0
class Inkplate:
    INKPLATE_1BIT = 0
    INKPLATE_2BIT = 1

    BLACK = 1
    WHITE = 0

    _width = D_COLS
    _height = D_ROWS

    rotation = 0
    displayMode = 0
    textSize = 1

    def __init__(self, mode):
        self.displayMode = mode
        try:
            os.mount(
                sdcard.SDCard(
                    machine.SPI(
                        1,
                        baudrate=80000000,
                        polarity=0,
                        phase=0,
                        bits=8,
                        firstbit=0,
                        sck=Pin(14),
                        mosi=Pin(13),
                        miso=Pin(12),
                    ),
                    machine.Pin(15),
                ),
                "/sd",
            )
        except:
            pass

    def begin(self):
        _Inkplate.init(I2C(0, scl=Pin(22), sda=Pin(21)))

        self.ipg = InkplateGS2()
        self.ipm = InkplateMono()
        self.ipp = InkplatePartial(self.ipm)

        self.GFX = GFX(
            D_COLS,
            D_ROWS,
            self.writePixel,
            self.writeFastHLine,
            self.writeFastVLine,
            self.writeFillRect,
            None,
            None,
        )

    def clearDisplay(self):
        self.ipg.clear()
        self.ipm.clear()

    def display(self):
        if self.displayMode == 0:
            self.ipm.display()
        elif self.displayMode == 1:
            self.ipg.display()

    def partialUpdate(self):
        if self.displayMode == 1:
            return
        self.ipp.display()

    def clean(self):
        self.einkOn()
        _Inkplate.clean(0, 1)
        _Inkplate.clean(1, 12)
        _Inkplate.clean(2, 1)
        _Inkplate.clean(0, 11)
        _Inkplate.clean(2, 1)
        _Inkplate.clean(1, 12)
        _Inkplate.clean(2, 1)
        _Inkplate.clean(0, 11)
        self.einkOff()

    def einkOn(self):
        _Inkplate.power_on()

    def einkOff(self):
        _Inkplate.power_off()

    def width(self):
        return self._width

    def height(self):
        return self._height

    # Arduino compatibility functions
    def setRotation(self, x):
        self.rotation = x % 4
        if self.rotation == 0 or self.rotation == 2:
            self._width = D_COLS
            self._height = D_ROWS
        elif self.rotation == 1 or self.rotation == 3:
            self._width = D_ROWS
            self._height = D_COLS

    def getRotation(self):
        return self.rotation

    def drawPixel(self, x, y, c):
        self.startWrite()
        self.writePixel(x, y, c)
        self.endWrite()

    def startWrite(self):
        pass

    def writePixel(self, x, y, c):
        if x > self.width() - 1 or y > self.height() - 1 or x < 0 or y < 0:
            return
        if self.rotation == 1:
            x, y = y, x
            x = self.height() - x - 1
        elif self.rotation == 2:
            x = self.width() - x - 1
            y = self.height() - y - 1
        elif self.rotation == 3:
            x, y = y, x
            y = self.width() - y - 1
        (self.ipm.pixel if self.displayMode == self.INKPLATE_1BIT else
         self.ipg.pixel)(x, y, c)

    def writeFillRect(self, x, y, w, h, c):
        for j in range(w):
            for i in range(h):
                self.writePixel(x + j, y + i, c)

    def writeFastVLine(self, x, y, h, c):
        for i in range(h):
            self.writePixel(x, y + i, c)

    def writeFastHLine(self, x, y, w, c):
        for i in range(w):
            self.writePixel(x + i, y, c)

    def writeLine(self, x0, y0, x1, y1, c):
        self.GFX.line(x0, y0, x1, y1, c)

    def endWrite(self):
        pass

    def drawFastVLine(self, x, y, h, c):
        self.startWrite()
        self.writeFastVLine(x, y, h, c)
        self.endWrite()

    def drawFastHLine(self, x, y, w, c):
        self.startWrite()
        self.writeFastHLine(x, y, w, c)
        self.endWrite()

    def fillRect(self, x, y, w, h, c):
        self.startWrite()
        self.writeFillRect(x, y, w, h, c)
        self.endWrite()

    def fillScreen(self, c):
        self.fillRect(0, 0, self.width(), self.height())

    def drawLine(self, x0, y0, x1, y1, c):
        self.startWrite()
        self.writeLine(x0, y0, x1, y1, c)
        self.endWrite()

    def drawRect(self, x, y, w, h, c):
        self.GFX.rect(x, y, w, h, c)

    def drawCircle(self, x, y, r, c):
        self.GFX.circle(x, y, r, c)

    def fillCircle(self, x, y, r, c):
        self.GFX.fill_circle(x, y, r, c)

    def drawTriangle(self, x0, y0, x1, y1, x2, y2, c):
        self.GFX.triangle(x0, y0, x1, y1, x2, y2, c)

    def fillTriangle(self, x0, y0, x1, y1, x2, y2, c):
        self.GFX.fill_triangle(x0, y0, x1, y1, x2, y2, c)

    def drawRoundRect(self, x, y, q, h, r, c):
        self.GFX.round_rect(x, y, q, h, r, c)

    def fillRoundRect(self, x, y, q, h, r, c):
        self.GFX.fill_round_rect(x, y, q, h, r, c)

    def setDisplayMode(self, mode):
        self.displayMode = mode

    def selectDisplayMode(self, mode):
        self.displayMode = mode

    def getDisplayMode(self):
        return self.displayMode

    def setTextSize(self, s):
        self.textSize = s

    def setFont(self, f):
        self.GFX.font = f

    def printText(self, x, y, s):
        self.GFX._very_slow_text(x, y, s, self.textSize, 1)

    def drawBitmap(self, x, y, data, w, h):
        byteWidth = (w + 7) // 8
        byte = 0
        self.startWrite()
        for j in range(h):
            for i in range(w):
                if i & 7:
                    byte <<= 1
                else:
                    byte = data[j * byteWidth + i // 8]
                if byte & 0x80:
                    self.writePixel(x + i, y + j, 1)
        self.endWrite()

    def drawImageFile(self, x, y, path, invert=False):
        with open(path, "rb") as f:
            header14 = f.read(14)
            if header14[0] != 0x42 or header14[1] != 0x4D:
                return 0
            header40 = f.read(40)

            w = int((header40[7] << 24) + (header40[6] << 16) +
                    (header40[5] << 8) + header40[4])
            h = int((header40[11] << 24) + (header40[10] << 16) +
                    (header40[9] << 8) + header40[8])
            dataStart = int((header14[11] << 8) + header14[10])

            depth = int((header40[15] << 8) + header40[14])
            totalColors = int((header40[33] << 8) + header40[32])

            rowSize = 4 * ((depth * w + 31) // 32)

            if totalColors == 0:
                totalColors = 1 << depth

            palette = None

            if depth <= 8:
                palette = [0 for i in range(totalColors)]
                p = f.read(totalColors * 4)
                for i in range(totalColors):
                    palette[i] = (54 * p[i * 4] + 183 * p[i * 4 + 1] +
                                  19 * p[i * 4 + 2]) >> 14
            # print(palette)
            f.seek(dataStart)
            for j in range(h):
                # print(100 * j // h, "% complete")
                buffer = f.read(rowSize)
                for i in range(w):
                    val = 0
                    if depth == 1:
                        px = int(invert ^ (palette[0] < palette[1])
                                 ^ bool(buffer[i >> 3] & (1 << (7 - i & 7))))
                        val = palette[px]
                    elif depth == 4:
                        px = (buffer[i >> 1] &
                              (0x0F if i & 1 == 1 else 0xF0)) >> (0 if i
                                                                  & 1 else 4)
                        val = palette[px]
                        if invert:
                            val = 3 - val
                    elif depth == 8:
                        px = buffer[i]
                        val = palette[px]
                        if invert:
                            val = 3 - val
                    elif depth == 16:
                        px = (buffer[(i << 1) | 1] << 8) | buffer[(i << 1)]

                        r = (px & 0x7C00) >> 7
                        g = (px & 0x3E0) >> 2
                        b = (px & 0x1F) << 3

                        val = (54 * r + 183 * g + 19 * b) >> 14

                        if invert:
                            val = 3 - val
                    elif depth == 24:
                        r = buffer[i * 3]
                        g = buffer[i * 3 + 1]
                        b = buffer[i * 3 + 2]

                        val = (54 * r + 183 * g + 19 * b) >> 14

                        if invert:
                            val = 3 - val
                    elif depth == 32:
                        r = buffer[i * 4]
                        g = buffer[i * 4 + 1]
                        b = buffer[i * 4 + 2]

                        val = (54 * r + 183 * g + 19 * b) >> 14

                        if invert:
                            val = 3 - val

                    if self.getDisplayMode() == self.INKPLATE_1BIT:
                        val >>= 1

                    self.drawPixel(x + i, y + h - j, val)
示例#30
0
文件: main.py 项目: minuJeong/gl_kata
    def __init__(self, window):
        super().__init__()
        self.gfx = GFX()
        self.window = window

        self.gfx.init()
示例#31
0
class Ship():
    def __init__(self, screen):
        """Init the player ship and it's starting position"""
        self.screen = screen

        # load the ships image and get it's rect
        self.image = GFX().ship['c_0']
        self.mask = GFX().ship_mask['c_0']
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        # Start the ship at the bottom center of the screen
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Attributes
        self.pos_x = float(self.rect.centerx)
        self.shooting_timer = 0

        # Flags
        self.moving_left = False
        self.moving_right = False
        self.moving_center = False  # Used for resetting ship image
        self.shooting = False

        self.effects = Group()

    def blitme(self):
        """Draw the ship"""
        self.screen.blit(self.image, self.rect)
        self.effects.draw(self.screen)

    def update(self, dt):
        """Updates ship movement and graphics"""
        # After shooting timer runs out remove shooting flag and reset ship image
        if self.shooting_timer < time():
            self.shooting = False
            self.moving_center = True

        # Calculating ship movement and setting correct image
        if self.moving_left and self.rect.left > 0:
            self.pos_x -= CFG().ship_speed * dt
            self.image = GFX().ship['l_0']
            self.mask = GFX().ship_mask['l_0']
        elif self.moving_right and self.rect.right < self.screen_rect.right:
            self.pos_x += CFG().ship_speed * dt
            self.image = GFX().ship['r_0']
            self.mask = GFX().ship_mask['r_0']
        elif self.moving_center:
            self.image = GFX().ship['c_0']
            self.mask = GFX().ship_mask['c_0']
            self.moving_center = False

        # if shooting use special ship image
        if self.shooting:
            if self.moving_left:
                self.image = GFX().ship['l_1']
            elif self.moving_right:
                self.image = GFX().ship['r_1']
            else:
                self.image = GFX().ship['c_1']

        # Apply ship movement
        self.rect.centerx = self.pos_x

        self.effects.update(dt)

    def hit(self):
        self.effects.add(shield.PlayerShield(self))
示例#32
0
    def update(self, dt):
        """Updates ship movement and graphics"""
        # After shooting timer runs out remove shooting flag and reset ship image
        if self.shooting_timer < time():
            self.shooting = False
            self.moving_center = True

        # Calculating ship movement and setting correct image
        if self.moving_left and self.rect.left > 0:
            self.pos_x -= CFG().ship_speed * dt
            self.image = GFX().ship['l_0']
            self.mask = GFX().ship_mask['l_0']
        elif self.moving_right and self.rect.right < self.screen_rect.right:
            self.pos_x += CFG().ship_speed * dt
            self.image = GFX().ship['r_0']
            self.mask = GFX().ship_mask['r_0']
        elif self.moving_center:
            self.image = GFX().ship['c_0']
            self.mask = GFX().ship_mask['c_0']
            self.moving_center = False

        # if shooting use special ship image
        if self.shooting:
            if self.moving_left:
                self.image = GFX().ship['l_1']
            elif self.moving_right:
                self.image = GFX().ship['r_1']
            else:
                self.image = GFX().ship['c_1']

        # Apply ship movement
        self.rect.centerx = self.pos_x

        self.effects.update(dt)
示例#33
0
        tft.pixel(xpos0 - x, ypos0 + y, col)
        tft.pixel(xpos0 - x, ypos0 - y, col)
        tft.pixel(xpos0 - y, ypos0 - x, col)
        tft.pixel(xpos0 + y, ypos0 - x, col)
        tft.pixel(xpos0 + x, ypos0 - y, col)
        if err <= 0:
            y += 1
            err += dy
            dy += 2
        if err > 0:
            x -= 1
            dx += 2
            err += dx - (rad << 1)


gfx = GFX(240, 320, tft.pixel, hline=fast_hline, vline=fast_vline)

k = 3.3 / (65535)

power.value(1)
led.value(1)
tft.erase()
# y ticks

tft.fill_rectangle(0, 0, tft.width, tft.height, BACKGROUND)
v = 0
for i in range(7):
    v += 0.5
    tft.set_pos(0, 198 - 32 * i)
    tft.print(str(v))