Example #1
0
def main():
    pygame.init()
    gutil.initializeDisplay(800, 600)

    glColor4f(1.0,1.0,1.0,1.0)

    done = False

    cowTex, w, h = gutil.loadImage('art/planetcute/Character Boy.png')
    cow = gutil.createTexDL(cowTex, w, h)
    alienTex, w, h = gutil.loadImage('art/alien.png')
    alien = gutil.createTexDL(alienTex, w, h)

    while not done:
        glLoadIdentity()
        glTranslatef(100, 100, 0)
        glCallList(cow)
        glTranslatef(400, 400, 0)
        glCallList(alien)

        pygame.display.flip()

        eventlist = pygame.event.get()
        for event in eventlist:
            if event.type == QUIT \
               or event.type == KEYDOWN and event.key == K_ESCAPE:
                done = True
Example #2
0
def main():
    pygame.init()
    gutil.initializeDisplay(800, 600)

    glColor4f(1.0, 1.0, 1.0, 1.0)

    done = False

    cowTex, w, h = gutil.loadImage('art/planetcute/Character Boy.png')
    cow = gutil.createTexDL(cowTex, w, h)
    alienTex, w, h = gutil.loadImage('art/alien.png')
    alien = gutil.createTexDL(alienTex, w, h)

    while not done:
        glLoadIdentity()
        glTranslatef(100, 100, 0)
        glCallList(cow)
        glTranslatef(400, 400, 0)
        glCallList(alien)

        pygame.display.flip()

        eventlist = pygame.event.get()
        for event in eventlist:
            if event.type == QUIT \
               or event.type == KEYDOWN and event.key == K_ESCAPE:
                done = True
Example #3
0
def main():
    pygame.init()
    gutil.initializeDisplay(swidth, sheight)

    from pygame import display
    print display.list_modes()

    live_dj = Dj()

    player = Player()  # On your own here, but it needs x and y fields.
    boss = Boss()
    muzzle_flash = MuzzleFlash()
    flame = Flame()
    enemy_bullets = BulletSet()

    player_bullets = BulletSet()

    space = [Texture("bg/space_%s" % n, z=-100) for n in range(1, 32)]
    start_screens = [
        Texture("start_screen_%s" % n, z=-100) for n in range(1, 3)
    ]

    class Background(object):
        def __init__(self):
            self.x = swidth // 2
            self.y = sheight // 2
            self.z = 0

        @property
        def texture(self):
            return space[(steps // 15) % len(space)]

    bg = Background()

    class StartScreen(object):
        def __init__(self):
            self.x = swidth // 2
            self.y = sheight // 2
            self.z = 0
            self.current_texture_index = 0

        @property
        def texture(self):
            return start_screens[(steps // 30) % len(start_screens)]

    start_screen = StartScreen()

    boss_weapons = [
        'easy_attack_1.xml',
        'spread_attack.xml',
        'laser_attack.xml',
        'gravity_attack.xml',
        'homing_laser.xml',
        'sweep_attack.xml',
        'flower_attack.xml',
    ]

    global steps
    start = time.time()

    joy = JoystickServer()
    joy.daemon = True
    joy.start()

    done = False

    gun = Debounce(15)
    laser = Debounce(60 * 5)

    last_time = time.time()
    game_tick = 1.0 / 60.0
    time_left = 0.0

    # Start twilio polling
    twilio = MessagePoll()
    twilio.start()
    last_twilio_msg = None
    last_twilio_msg_step = 0

    last_shot_step = None
    last_game_over = -GAME_OVER_FRAMES - 1

    last_boss_break_step = None

    def start_screen_visible():
        return last_game_over is not None and steps - last_game_over > GAME_OVER_FRAMES

    while not done:
        keys = pygame.key.get_pressed()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glColor3f(1, 1, 1)
        glPointSize(10)
        glLoadIdentity()

        rp = RenderPass()

        if not start_screen_visible() and (
                joy.state.buttons
                and joy.state.buttons[0]) or keys[pygame.K_z]:
            #if steps % 3:
            muzzle_flash.x = player.x + 85
            muzzle_flash.y = player.y + 6
            if steps % 2 == 0:
                muzzle_flash.current_texture_index += 1
                if muzzle_flash.current_texture_index >= len(
                        muzzle_flash.textures):
                    muzzle_flash.current_texture_index = 0
            muzzle_flash.texture = muzzle_flash.textures[
                muzzle_flash.current_texture_index]
            if not start_screen_visible() and player.health > 0:
                rp.mark_for_draw(muzzle_flash)
        else:
            last_shot_step = None

        # Flame.
        flame.x = player.x - 8
        flame.y = player.y
        if steps % 4 == 0:
            flame.current_texture_index += 1
            if flame.current_texture_index >= len(flame.textures):
                flame.current_texture_index = 0
        flame.texture = flame.textures[flame.current_texture_index]

        if start_screen_visible():
            rp.mark_for_draw(start_screen)
        else:
            if boss.health > 0:
                map(rp.mark_for_draw, enemy_bullets)
                rp.mark_for_draw(boss)

            if player.health > 0:
                map(rp.mark_for_draw, player_bullets)
                rp.mark_for_draw(player)
                rp.mark_for_draw(flame)
            rp.mark_for_draw(bg)

        glLoadIdentity()
        rp.render()

        if start_screen_visible():
            enemy_bullets.clear()
            player_bullets.clear()
        elif boss.health <= 0:
            draw_label('YOU KILLED THE DJ', swidth / 2 - 150, sheight / 2, 300,
                       40)
            if last_game_over is None:
                last_game_over = steps
        elif player.health <= 0:
            draw_label('GAME OVER', swidth / 2 - 100, sheight / 2, 200, 50)
            if last_game_over is None:
                last_game_over = steps
        else:
            # Health bars.
            draw_health_bar(player.health / 100., sheight - 30, (0., 0.8, 0.))
            draw_health_bar(boss.health / 100., 10, (0.8, 0., 0.))
            draw_label("PLAYER", 15, sheight - 29, 52, 16)
            draw_label("BOSS", 15, 11, 35, 16)

        # Twilio.
        if not start_screen_visible():
            TWILIO_WIDTH = (
                210,
                260,
            )
            draw_label("Tired of this DJ? Shoot him.",
                       swidth - TWILIO_WIDTH[0] - 10, 60, TWILIO_WIDTH[0], 16)
            draw_label("TEXT  (503) 8-CANVAS", swidth - TWILIO_WIDTH[1] - 10,
                       34, TWILIO_WIDTH[1], 23)

        if ((steps - last_twilio_msg_step) % (TWILIO_MSG_DURATION * 60)
                and last_twilio_msg is not None):
            #parts = msg[]
            offset = 0
            line_len = 40
            char_w = 12
            #for m in msg[offset:offset + line_len]:
            while True:
                if offset > len(msg):
                    break
                end = min(len(msg), offset + line_len)
                m = msg[offset:offset + line_len]
                w = len(m) * char_w
                h = 26
                rot = math.sin(steps / 6.) * (50 / len(m))
                x = swidth - ((line_len * char_w) +
                              (150 - ((steps * 1.07) - last_twilio_msg_step)))
                y = (sheight / 2) + 30
                y -= (h + 3) * (offset / line_len)
                draw_label(m, x, y, w, 23, rotation=rot)
                offset += line_len
        else:
            if last_twilio_msg is not None:
                boss.health -= TWILIO_ATTACK
            last_twilio_msg_step = 0
            last_twilio_msg = None
        if steps % 60 == 0 and twilio.messages:
            msg = twilio.messages.pop()
            last_twilio_msg_step = steps
            last_twilio_msg = msg

        # Boss movement.
        #print live_dj.activity_level()
        #print 75. / (live_dj.activity_level() + 1)
        activity_level = live_dj.activity_level()
        if activity_level > 3 or abs(boss.x - boss.starting_x) > 3:
            if last_boss_break_step is None:
                last_boss_break_step = steps
            boss.x = int(
                math.cos((steps - last_boss_break_step +
                          (2 * 3.1416 * 30)) / 30.) * 50 +
                (boss.starting_x - 50))
        else:
            last_boss_break_step = None

        boss.y = math.sin(steps / 75.) * 110 + (sheight / 2)

        pygame.display.flip()

        curtime = time.time()
        time_left += curtime - last_time
        last_time = curtime

        while time_left >= game_tick:
            time_left -= game_tick

            for hitbox in hitboxes['player_ship'].values():
                hitbox = HitBox(player, hitbox)
                if boss.health > 0 and enemy_bullets.collides(hitbox):
                    player.health -= BOSS_ATTACK
                    player.last_hit_step = steps

            for hitbox in hitboxes['boss_ship'].values():
                hitbox = HitBox(boss, hitbox)
                if player.health > 0 and player_bullets.collides(hitbox):
                    boss.health -= PLAYER_ATTACK
                    boss.last_hit_step = steps

            enemy_bullets.step(swidth, sheight, 100)
            player_bullets.step(swidth, sheight, 100)

            eventlist = pygame.event.get()
            for event in eventlist:
                if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
                    done = True

            if player.health > 0 and (
                    joy.state.buttons and joy.state.buttons[0]
                    or joy.state.buttons[8]
                    or joy.state.buttons[9]) or keys[pygame.K_z]:
                if gun.fire():
                    player_bullets.load("player_shot.xml",
                                        source=player,
                                        target=boss)
                    last_shot_step = steps

            if player.health > 0 and (
                    joy.state.buttons and joy.state.buttons[1]
                    or joy.state.buttons[2]
                    or joy.state.buttons[3]) or keys[pygame.K_x]:
                if laser.fire():
                    player_bullets.load("player_laser.xml",
                                        source=player,
                                        target=boss)

            player_bullets.update_roots(player.x + 30, player.y + 2)

            gun.step()
            laser.step()

            dx, dy = 0, 0
            if joy.state.hats:
                dx, dy = joy.state.hats[0]

            if keys[pygame.K_RIGHT]:
                dx = 1
            elif keys[pygame.K_LEFT]:
                dx = -1

            if keys[pygame.K_UP]:
                dy = 1
            elif keys[pygame.K_DOWN]:
                dy = -1

            player.move(dx, dy)

            if start_screen_visible() and (
                    keys[pygame.K_RETURN] or joy.state.buttons and
                (joy.state.buttons[7] or joy.state.buttons[6]
                 or joy.state.buttons[4] or joy.state.buttons[5]) and
                (player.health <= 0 or boss.health <= 0)):
                enemy_bullets.clear()
                player.health = 100.
                boss.health = 100.
                last_game_over = None

            if keys[pygame.K_q]:
                time.sleep(1)

            steps += 1

            if not start_screen_visible() and steps % 15 == 0:
                activity_level = live_dj.activity_level()
                _rand = random.random()
                if activity_level:
                    enemy_bullets.load(boss_weapons[activity_level],
                                       source=boss,
                                       target=player)
                elif _rand > 0.7:
                    enemy_bullets.load(boss_weapons[1],
                                       source=boss,
                                       target=player)

    print "FPS:", steps / (time.time() - start)
Example #4
0
def main():
    pygame.init()
    gutil.initializeDisplay(swidth, sheight)

    from pygame import display
    print display.list_modes()

    live_dj = Dj()

    player = Player()  # On your own here, but it needs x and y fields.
    boss = Boss()
    muzzle_flash = MuzzleFlash()
    flame = Flame()
    enemy_bullets = BulletSet()

    player_bullets = BulletSet()
    
    space = [Texture("bg/space_%s" % n, z=-100) for n in range(1, 32)]
    start_screens = [Texture("start_screen_%s" % n, z=-100) for n in range(1, 3)]

    class Background(object):
        def __init__(self):
            self.x = swidth // 2
            self.y = sheight // 2
            self.z = 0

        @property
        def texture(self):
            return space[(steps//15) % len(space)]
            
    bg = Background()

    class StartScreen(object):
        def __init__(self):
            self.x = swidth // 2
            self.y = sheight // 2
            self.z = 0
            self.current_texture_index = 0

        @property
        def texture(self):
            return start_screens[(steps//30) % len(start_screens)]

    start_screen = StartScreen()

    boss_weapons = [
        'easy_attack_1.xml',
        'spread_attack.xml',
        'laser_attack.xml',
        'gravity_attack.xml',
        'homing_laser.xml',
        'sweep_attack.xml',
        'flower_attack.xml',
    ]

    global steps
    start = time.time()

    joy = JoystickServer()
    joy.daemon = True
    joy.start()

    done = False

    gun = Debounce(15)
    laser = Debounce(60*5)
    
    last_time = time.time()
    game_tick = 1.0 / 60.0
    time_left = 0.0

    # Start twilio polling
    twilio = MessagePoll()
    twilio.start()
    last_twilio_msg = None
    last_twilio_msg_step = 0

    last_shot_step = None
    last_game_over = -GAME_OVER_FRAMES - 1

    last_boss_break_step = None

    def start_screen_visible():
        return last_game_over is not None and steps - last_game_over > GAME_OVER_FRAMES

    while not done:
        keys = pygame.key.get_pressed()
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glColor3f(1,1,1)
        glPointSize(10)
        glLoadIdentity()

        rp = RenderPass()

        if not start_screen_visible() and (joy.state.buttons and joy.state.buttons[0]) or keys[pygame.K_z]:
            #if steps % 3:
            muzzle_flash.x = player.x + 85
            muzzle_flash.y = player.y + 6
            if steps % 2 == 0:
                muzzle_flash.current_texture_index += 1
                if muzzle_flash.current_texture_index >= len(muzzle_flash.textures):
                    muzzle_flash.current_texture_index = 0
            muzzle_flash.texture = muzzle_flash.textures[muzzle_flash.current_texture_index]
            if not start_screen_visible() and player.health > 0:
                rp.mark_for_draw(muzzle_flash)
        else:
            last_shot_step = None

        # Flame.
        flame.x = player.x - 8
        flame.y = player.y
        if steps % 4 == 0:
            flame.current_texture_index += 1
            if flame.current_texture_index >= len(flame.textures):
                flame.current_texture_index = 0
        flame.texture = flame.textures[flame.current_texture_index]

        if start_screen_visible():
            rp.mark_for_draw(start_screen)
        else:
            if boss.health > 0:
                map(rp.mark_for_draw, enemy_bullets)
                rp.mark_for_draw(boss)

            if player.health > 0:
                map(rp.mark_for_draw, player_bullets)
                rp.mark_for_draw(player)
                rp.mark_for_draw(flame)
            rp.mark_for_draw(bg)

        glLoadIdentity()
        rp.render()

        if start_screen_visible():
            enemy_bullets.clear()
            player_bullets.clear()
        elif boss.health <= 0:
            draw_label('YOU KILLED THE DJ', swidth / 2 - 150, sheight / 2, 300, 40)
            if last_game_over is None:
                last_game_over = steps
        elif player.health <= 0:
            draw_label('GAME OVER', swidth / 2 - 100, sheight / 2, 200, 50)
            if last_game_over is None:
                last_game_over = steps
        else:
            # Health bars.
            draw_health_bar(player.health / 100., sheight - 30, (0., 0.8, 0.))
            draw_health_bar(boss.health / 100., 10, (0.8, 0., 0.))
            draw_label("PLAYER", 15, sheight - 29, 52, 16)
            draw_label("BOSS", 15, 11, 35, 16)

        # Twilio.
        if not start_screen_visible():
            TWILIO_WIDTH = (210, 260,)
            draw_label("Tired of this DJ? Shoot him.",
                    swidth - TWILIO_WIDTH[0] - 10, 60, TWILIO_WIDTH[0], 16)
            draw_label("TEXT  (503) 8-CANVAS", swidth - TWILIO_WIDTH[1] - 10, 34, TWILIO_WIDTH[1], 23)        

        if ((steps - last_twilio_msg_step) % (TWILIO_MSG_DURATION * 60)
                and last_twilio_msg is not None):
            #parts = msg[]
            offset = 0
            line_len = 40
            char_w = 12
            #for m in msg[offset:offset + line_len]:
            while True:
                if offset > len(msg):
                    break
                end = min(len(msg), offset + line_len)
                m = msg[offset:offset + line_len]
                w = len(m) * char_w
                h = 26
                rot = math.sin(steps / 6.) * (50 / len(m))
                x = swidth - ((line_len * char_w) + (150 - ((steps * 1.07) - last_twilio_msg_step)))
                y = (sheight / 2) + 30
                y -= (h + 3) * (offset / line_len)
                draw_label(m, x, y, w, 23, rotation=rot)
                offset += line_len
        else:
            if last_twilio_msg is not None:
                boss.health -= TWILIO_ATTACK
            last_twilio_msg_step = 0
            last_twilio_msg = None
        if steps % 60 == 0 and twilio.messages:
            msg = twilio.messages.pop()
            last_twilio_msg_step = steps
            last_twilio_msg = msg

        # Boss movement.
        #print live_dj.activity_level()
        #print 75. / (live_dj.activity_level() + 1)
        activity_level = live_dj.activity_level()
        if activity_level > 3 or abs(boss.x - boss.starting_x) > 3:
            if last_boss_break_step is None:
                last_boss_break_step = steps
            boss.x = int(math.cos((steps - last_boss_break_step + (2*3.1416*30)) / 30.) * 50 + (boss.starting_x - 50))
        else:
            last_boss_break_step = None

        boss.y = math.sin(steps / 75.) * 110 + (sheight / 2)

        pygame.display.flip()

        curtime = time.time()
        time_left += curtime - last_time
        last_time = curtime

        while time_left >= game_tick:
            time_left -= game_tick

            for hitbox in hitboxes['player_ship'].values():
                hitbox = HitBox(player, hitbox)
                if boss.health > 0 and enemy_bullets.collides(hitbox):
                    player.health -= BOSS_ATTACK
                    player.last_hit_step = steps

            for hitbox in hitboxes['boss_ship'].values():
                hitbox = HitBox(boss, hitbox)
                if player.health > 0 and player_bullets.collides(hitbox):
                    boss.health -= PLAYER_ATTACK
                    boss.last_hit_step = steps

            enemy_bullets.step(swidth, sheight, 100)
            player_bullets.step(swidth, sheight, 100)

            eventlist = pygame.event.get()
            for event in eventlist:
                if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
                    done = True
        
            if player.health > 0 and (joy.state.buttons and joy.state.buttons[0] or joy.state.buttons[8] or joy.state.buttons[9]) or keys[pygame.K_z]:
                if gun.fire():
                    player_bullets.load("player_shot.xml", source=player, target=boss)
                    last_shot_step = steps

            if player.health > 0 and (joy.state.buttons and joy.state.buttons[1] or joy.state.buttons[2] or joy.state.buttons[3]) or keys[pygame.K_x]:
                if laser.fire():
                    player_bullets.load("player_laser.xml", source=player, target=boss)

            player_bullets.update_roots(player.x + 30,
                                        player.y + 2)

            gun.step()
            laser.step()

            dx, dy = 0, 0
            if joy.state.hats:
                dx, dy = joy.state.hats[0]

            if keys[pygame.K_RIGHT]:
                dx = 1
            elif keys[pygame.K_LEFT]:
                dx = -1
                
            if keys[pygame.K_UP]:
                dy = 1
            elif keys[pygame.K_DOWN]:
                dy = -1
            
            player.move(dx, dy)

            if start_screen_visible() and (keys[pygame.K_RETURN] or joy.state.buttons and (joy.state.buttons[7] or joy.state.buttons[6] or joy.state.buttons[4] or joy.state.buttons[5]) and (player.health <= 0 or boss.health <= 0)):
                enemy_bullets.clear()
                player.health = 100.
                boss.health = 100.
                last_game_over = None

            if keys[pygame.K_q]:
                time.sleep(1)

            steps += 1
        
            if not start_screen_visible() and steps % 15 == 0:
                activity_level = live_dj.activity_level()
                _rand = random.random()
                if activity_level:
                    enemy_bullets.load(boss_weapons[activity_level], source=boss, target=player)
                elif _rand > 0.7:
                    enemy_bullets.load(boss_weapons[1], source=boss, target=player)
    
    print "FPS:", steps / (time.time() - start)