Example #1
0
class Controls(ScoreScreen):
    def __init__(self):
        ScoreScreen.__init__(self)
        mixer.init()
        self.title = GameText("Controls", 94)
        self.title.centerx = SCREEN.width / 2
        self.title.y = 48
        self.title.color = COLOR.half_black
        self.title.create()

        self.bg = ASSET.controls
        self.state = STATE.controls

        self.select_sound = mixer.Sound(os.path.join("assets", "sfx", "sfx_twoTone.ogg"))
        self.select_sound.set_volume(0.5)

    def handle_events(self, event):
        self.state = STATE.controls
        if event.type == KEYDOWN:
            if event.key == K_e or event.key == K_RETURN:
                self.state = STATE.menu
                self.select_sound.play()

        elif event.type == JOYBUTTONDOWN:
            if self.controller.get_button(0):
                self.state = STATE.menu
                self.select_sound.play()

    def update(self):
        self.title.update()

    def draw(self, screen):
        screen.fill(COLOR.black)
        screen.blit(self.bg, (0, 0))
        self.title.draw_to(screen)
Example #2
0
    def __init__(self):
        ScoreScreen.__init__(self)
        mixer.init()
        self.title = GameText("Controls", 94)
        self.title.centerx = SCREEN.width / 2
        self.title.y = 48
        self.title.color = COLOR.half_black
        self.title.create()

        self.bg = ASSET.controls
        self.state = STATE.controls

        self.select_sound = mixer.Sound(os.path.join("assets", "sfx", "sfx_twoTone.ogg"))
        self.select_sound.set_volume(0.5)
Example #3
0
    def __init__(self):
        pygame.init()
        pygame.font.init()
        pygame.mixer.init()

        os.environ["SDL_VIDEO_CENTERED"] = "1"

        self.screen = SCREEN.display
        self.fpsClock = pygame.time.Clock()
        self.fps = GAME.fps
        self.ticks = 0
        self.firstrun = True

        self.music = Music("mainmenu.ogg", 0.5, -1)
        self.menu_music = False
        self.game_music = False

        self.game_time = GameText("0", 24, True)
        self.game_time.font_file = FONT.default
        self.game_time.centerx = SCREEN.width/2
        self.game_time.y = 18
        self.game_time.color = COLOR.white
        self.game_time.create()
        self.timer_red = False
        self.flash_timer = 0

        self.p1_score = GameText("0", 24, True)
        self.p1_score.font_file = FONT.default
        self.p1_score.left = 30
        self.p1_score.y = 18
        self.p1_score.color = COLOR.blue_sea
        self.p1_score.create()

        self.p2_score = GameText("0", 24, True)
        self.p2_score.font_file = FONT.default
        self.p2_score.right = SCREEN.width - 30
        self.p2_score.y = 18
        self.p2_score.color = COLOR.petal_green
        self.p2_score.create()

        self.bg_text = GameText("01", 72, True)
        self.bg_text.font_file = FONT.kenpixel_blocks
        self.bg_text.centerx = SCREEN.width/2
        self.bg_text.centery = SCREEN.height/2
        self.bg_text.color = COLOR.gray7
        self.bg_text.create()

        self.state = STATE.logo
        self.stage_number = 1
        self.stage = Stage(self.stage_number)
        self.hi_score = 0
        self.splashscreen = SplashScreen(0, 0, SPLASHSCREEN.width, SPLASHSCREEN.height)
        self.logoscreen = LogoScreen(0, 0, LOGO.width, LOGO.height)
        self.timer = GAME.time
        self.totalscore = 0

        self.p1_scores = {}
        self.p2_scores = {}

        self.camera = Camera(self.complex_camera, self.stage.level.width, self.stage.level.height)        

        self.bg = SCREEN.bg
        self.screen_color = (choice(COLOR.colors))
        self.menu = Menu(SCREEN.width, SCREEN.height, self.screen_color)
        self.score_screen = ScoreScreen()
        self.controls_screen = Controls()

        self.screen_number = 1
        self.capture_video = False

        self.countdownOverlay = CountDownOverlay()
        self.intro = True
        self.intro_countdown = self.fps * 4
        
        self.scanlines = ScanLines()

        self.controller_present = False
        try:
            self.player1_joy = pygame.joystick.Joystick(0)
            self.player1_joy.init()
            self.controller_present = True
        except:
            pass
Example #4
0
class GameEngine(object):
    def __init__(self):
        pygame.init()
        pygame.font.init()
        pygame.mixer.init()

        os.environ["SDL_VIDEO_CENTERED"] = "1"

        self.screen = SCREEN.display
        self.fpsClock = pygame.time.Clock()
        self.fps = GAME.fps
        self.ticks = 0
        self.firstrun = True

        self.music = Music("mainmenu.ogg", 0.5, -1)
        self.menu_music = False
        self.game_music = False

        self.game_time = GameText("0", 24, True)
        self.game_time.font_file = FONT.default
        self.game_time.centerx = SCREEN.width/2
        self.game_time.y = 18
        self.game_time.color = COLOR.white
        self.game_time.create()
        self.timer_red = False
        self.flash_timer = 0

        self.p1_score = GameText("0", 24, True)
        self.p1_score.font_file = FONT.default
        self.p1_score.left = 30
        self.p1_score.y = 18
        self.p1_score.color = COLOR.blue_sea
        self.p1_score.create()

        self.p2_score = GameText("0", 24, True)
        self.p2_score.font_file = FONT.default
        self.p2_score.right = SCREEN.width - 30
        self.p2_score.y = 18
        self.p2_score.color = COLOR.petal_green
        self.p2_score.create()

        self.bg_text = GameText("01", 72, True)
        self.bg_text.font_file = FONT.kenpixel_blocks
        self.bg_text.centerx = SCREEN.width/2
        self.bg_text.centery = SCREEN.height/2
        self.bg_text.color = COLOR.gray7
        self.bg_text.create()

        self.state = STATE.logo
        self.stage_number = 1
        self.stage = Stage(self.stage_number)
        self.hi_score = 0
        self.splashscreen = SplashScreen(0, 0, SPLASHSCREEN.width, SPLASHSCREEN.height)
        self.logoscreen = LogoScreen(0, 0, LOGO.width, LOGO.height)
        self.timer = GAME.time
        self.totalscore = 0

        self.p1_scores = {}
        self.p2_scores = {}

        self.camera = Camera(self.complex_camera, self.stage.level.width, self.stage.level.height)        

        self.bg = SCREEN.bg
        self.screen_color = (choice(COLOR.colors))
        self.menu = Menu(SCREEN.width, SCREEN.height, self.screen_color)
        self.score_screen = ScoreScreen()
        self.controls_screen = Controls()

        self.screen_number = 1
        self.capture_video = False

        self.countdownOverlay = CountDownOverlay()
        self.intro = True
        self.intro_countdown = self.fps * 4
        
        self.scanlines = ScanLines()

        self.controller_present = False
        try:
            self.player1_joy = pygame.joystick.Joystick(0)
            self.player1_joy.init()
            self.controller_present = True
        except:
            pass

    def reset(self):
        self.stage_number += 1
        if self.stage_number > self.stage.number_of_levels:
            self.stage_number = 1
            self.state = STATE.menu
            self.score_screen = ScoreScreen()
            self.controls_screen = Controls()
            self.firstrun = True
        else:
            self.firstrun = False
            self.state = STATE.game

        self.stage = Stage(self.stage_number)
        self.timer = GAME.time
        self.camera = Camera(self.complex_camera, self.stage.level.width, self.stage.level.height)
        self.screen_color = (choice(COLOR.colors))
        self.menu = Menu(SCREEN.width, SCREEN.height, self.screen_color)
        
        self.countdownOverlay = CountDownOverlay()
        self.intro = True
        self.intro_countdown = self.fps * 4        

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    self.state = STATE.menu
                if event.key == K_p:
                    if not self.capture_video:
                        self.capture_video = True # change to True and press p to capture screenshots
                    else:
                        self.capture_video = False
                if event.key == K_q:
                    #self.stage.level.spawn_data()
                    pass
            elif event.type == JOYBUTTONDOWN:
                if self.player1_joy.get_button(9) == 1:
                    self.state = STATE.menu
            if self.state == STATE.game:                
                if self.stage.level.timer == 0:
                    self.stage.level.timer = pygame.time.get_ticks()/1000.0
                self.stage.level.player1.handle_events(event)
                self.stage.level.player2.handle_events(event)
            elif self.state == STATE.menu:
                self.menu.handle_events(event)
                self.state = self.menu.state
            elif self.state == STATE.controls:
                self.controls_screen.handle_events(event)
                self.state = self.controls_screen.state
            elif self.state == STATE.scorescreen:
                self.score_screen.handle_events(event)
                self.state = self.score_screen.state
            elif self.state == STATE.exit:
                pygame.quit()
                sys.exit()

    def update(self):
        if self.state == STATE.nextlevel:
            self.reset()
            return
        if self.capture_video:
            self.makeVideo()

        if self.timer == 0:
            self.state = STATE.scorescreen

        if self.state == STATE.logo:
            self.logoscreen.update()
            self.state = self.logoscreen.state
        if self.state == STATE.splashscreen:
            self.splashscreen.update()
            self.state = self.splashscreen.state
        elif self.state == STATE.controls:
            self.controls_screen.update()
        elif self.state == STATE.menu:
            self.menu.update()
            if self.game_music:
                self.game_music = False
                mixer.music.fadeout(2000)
        elif self.state == STATE.scorescreen:
            self.p1_scores[self.stage_number-1] = self.stage.level.p1_data
            self.p2_scores[self.stage_number-1] = self.stage.level.p2_data

            self.score_screen.p1_scores[self.stage_number-1].text = self.stage.level.p1_data
            self.score_screen.p2_scores[self.stage_number-1].text = self.stage.level.p2_data

            self.score_screen.update(self.stage_number, 1, self.hi_score)
        elif self.state == STATE.game:
            if not self.game_music:
                mixer.music.load(os.path.join('assets', 'music', 'cyberspine.ogg'))
                mixer.music.play(-1)
                self.game_music = True
            self.camera.update(self.stage.level.player1)
            if self.intro_countdown <= GAME.fps:
                self.intro = False
                self.intro_countdown = 0
            if self.intro:
                self.countdownOverlay.update(self.intro_countdown)
                self.intro_countdown -= 1
            else:
                self.stage.level.update()
                if self.stage.level.intro:
                    self.timer = GAME.time
                if self.ticks > self.fps:
                    self.ticks = 0
                    self.timer -= 1
                else:
                    self.ticks += 1

            display_time = self.format_timer(self.timer)
            self.game_time.text = display_time
            if self.timer_red:
                self.animate_flash()
            else:
                self.game_time.color = COLOR.white
            self.game_time.update()

            self.p1_score.text = str(self.stage.level.p1_data)
            self.p1_score.update()

            self.p2_score.text = str(self.stage.level.p2_data)
            self.p2_score.update()
            self.bg_text.text = "0" + str(self.stage_number)
            self.bg_text.update()

    def draw(self):
        self.screen.fill(COLOR.black)

        if self.state == STATE.logo:
            self.logoscreen.draw(self.screen)
        elif self.state == STATE.splashscreen:
            self.splashscreen.draw(self.screen)
        elif self.state == STATE.scorescreen:
            self.score_screen.draw(self.screen)
        elif self.state == STATE.controls:
            self.controls_screen.draw(self.screen)
        elif self.state == STATE.game:
            self.screen.blit(SCREEN.bg, (0, 0))
            self.bg_text.draw_to(self.screen)
            self.screen.blit(ASSET.score_bg, (0, 0))
            self.stage.draw(self.screen)
            for particle in self.stage.level.particles:
                self.screen.blit(particle.image, self.camera.apply(particle))

            self.game_time.draw_to(self.screen)
            self.p1_score.draw_to(self.screen)
            self.p2_score.draw_to(self.screen)

            if self.intro:
                self.countdownOverlay.draw(self.screen)

        elif self.state == STATE.menu:
            self.menu.draw(self.screen)
            if not self.firstrun:
                pass
            
        self.scanlines.draw(self.screen)

    def run_game(self, fps=30):
        self.fps = fps
        while True:
            self.handle_events()
            self.update()
            self.draw()

            pygame.display.update()
            pygame.display.set_caption("CleanerBots v0.1 - " + str(int(self.fpsClock.get_fps())) + " fps")
            self.fpsClock.tick(self.fps)

    def complex_camera(self, camera_rect, target_rect):
        x, y, dummy, dummy = target_rect
        dummy, dummy, w, h = camera_rect
        x, y = int(SCREEN.width/2)-x, int(SCREEN.height/2) - y

        x = min(0, x)
        x = max(-(camera_rect.width-SCREEN.width), x)
        y = max(-(camera_rect.height-SCREEN.height), y)
        y = min(0, y)

        return pygame.Rect(x, y, w, h)

    def format_timer(self, timer):
        self.timer_red = False
        minutes = timer/60
        seconds = timer % 60
        if minutes == 0 and seconds < 10:
            self.timer_red = True
        if seconds < 10:
            seconds = "0" + str(seconds)
        return str(minutes)+":"+str(seconds)

    def makeVideo(self):
        pygame.image.save(self.screen, os.path.join("screenshots", "screenshot%d.jpg" %self.screen_number))
        self.screen_number += 1

    def animate_flash(self):
        self.flash_timer += 1
        if self.flash_timer < GAME.fps/4:
            self.game_time.color = COLOR.white
        elif self.flash_timer < GAME.fps/2:
            self.game_time.color = COLOR.red
        else:
            self.flash_timer = 0
Example #5
0
    def __init__(self):
        mixer.init()
        self.timer = 0

        self.title = GameText("Scoreboard", 94)
        self.title.centerx = SCREEN.width / 2
        self.title.y = 48
        self.title.color = COLOR.half_black

        self.hi_score = GameText("Hi-score: 0", 38)
        self.hi_score.centerx = SCREEN.width / 2
        self.hi_score.y = 172
        self.hi_score.color = COLOR.light_gray

        self.p1 = GameText("P1", 24, True)
        self.p1.x = 92
        self.p1.y = 280
        self.p1.color = COLOR.blue_sea

        self.p2 = GameText("P2", 24, True)
        self.p2.x = 92
        self.p2.y = 340
        self.p2.color = COLOR.petal_green

        self.p1_scores = []
        self.p2_scores = []
        for i in xrange(5):
            p1_score = GameText("0", 24, True)
            p1_score.centerx = 92 + 144 + 96 * i
            p1_score.y = 280
            p1_score.color = COLOR.gray7
            self.p1_scores.append(p1_score)

            p2_score = GameText("0", 24, True)
            p2_score.centerx = 92 + 144 + 96 * i
            p2_score.y = 340
            p2_score.color = COLOR.gray7
            self.p2_scores.append(p2_score)

        self.p1_total = GameText("0", 24, True)
        self.p1_total.right = SCREEN.width - SCREEN.width / 4
        self.p1_total.y = 280
        self.p1_total.color = COLOR.blue_sea

        self.p2_total = GameText("0", 24, True)
        self.p2_total.right = SCREEN.width - SCREEN.width / 4
        self.p2_total.y = 340
        self.p2_total.color = COLOR.petal_green

        self.p1_results = GameText("", 24, True)
        self.p1_results.x = SCREEN.width - SCREEN.width / 4 + 64
        self.p1_results.y = 280
        self.p1_results.color = COLOR.burnt_orange

        self.p2_results = GameText("", 24, True)
        self.p2_results.x = SCREEN.width - SCREEN.width / 4 + 64
        self.p2_results.y = 340
        self.p2_results.color = COLOR.burnt_orange

        self.bg = MENU.scoreScreen
        self.state = STATE.scorescreen
        self.stage = 1

        self.title.create()
        self.hi_score.create()
        self.p1.create()
        self.p2.create()

        for score in self.p1_scores:
            score.create()
        for score in self.p2_scores:
            score.create()

        self.p1_total.create()
        self.p2_total.create()
        self.p1_results.create()
        self.p2_results.create()

        self.controller = self.get_controller()

        self.select_sound = mixer.Sound(os.path.join("assets", "sfx", "sfx_twoTone.ogg"))
        self.select_sound.set_volume(0.5)
Example #6
0
class ScoreScreen(object):
    def __init__(self):
        mixer.init()
        self.timer = 0

        self.title = GameText("Scoreboard", 94)
        self.title.centerx = SCREEN.width / 2
        self.title.y = 48
        self.title.color = COLOR.half_black

        self.hi_score = GameText("Hi-score: 0", 38)
        self.hi_score.centerx = SCREEN.width / 2
        self.hi_score.y = 172
        self.hi_score.color = COLOR.light_gray

        self.p1 = GameText("P1", 24, True)
        self.p1.x = 92
        self.p1.y = 280
        self.p1.color = COLOR.blue_sea

        self.p2 = GameText("P2", 24, True)
        self.p2.x = 92
        self.p2.y = 340
        self.p2.color = COLOR.petal_green

        self.p1_scores = []
        self.p2_scores = []
        for i in xrange(5):
            p1_score = GameText("0", 24, True)
            p1_score.centerx = 92 + 144 + 96 * i
            p1_score.y = 280
            p1_score.color = COLOR.gray7
            self.p1_scores.append(p1_score)

            p2_score = GameText("0", 24, True)
            p2_score.centerx = 92 + 144 + 96 * i
            p2_score.y = 340
            p2_score.color = COLOR.gray7
            self.p2_scores.append(p2_score)

        self.p1_total = GameText("0", 24, True)
        self.p1_total.right = SCREEN.width - SCREEN.width / 4
        self.p1_total.y = 280
        self.p1_total.color = COLOR.blue_sea

        self.p2_total = GameText("0", 24, True)
        self.p2_total.right = SCREEN.width - SCREEN.width / 4
        self.p2_total.y = 340
        self.p2_total.color = COLOR.petal_green

        self.p1_results = GameText("", 24, True)
        self.p1_results.x = SCREEN.width - SCREEN.width / 4 + 64
        self.p1_results.y = 280
        self.p1_results.color = COLOR.burnt_orange

        self.p2_results = GameText("", 24, True)
        self.p2_results.x = SCREEN.width - SCREEN.width / 4 + 64
        self.p2_results.y = 340
        self.p2_results.color = COLOR.burnt_orange

        self.bg = MENU.scoreScreen
        self.state = STATE.scorescreen
        self.stage = 1

        self.title.create()
        self.hi_score.create()
        self.p1.create()
        self.p2.create()

        for score in self.p1_scores:
            score.create()
        for score in self.p2_scores:
            score.create()

        self.p1_total.create()
        self.p2_total.create()
        self.p1_results.create()
        self.p2_results.create()

        self.controller = self.get_controller()

        self.select_sound = mixer.Sound(os.path.join("assets", "sfx", "sfx_twoTone.ogg"))
        self.select_sound.set_volume(0.5)

    def get_controller(self):
        number_of_joysticks = pygame.joystick.get_count()
        if number_of_joysticks > 0:
            joystick = pygame.joystick.Joystick(0)
            joystick.init()
            return joystick
        return None

    def handle_events(self, event):
        self.state = STATE.scorescreen

        if event.type == KEYDOWN:
            if event.key == K_e or event.key == K_RETURN:
                self.state = STATE.nextlevel
                self.select_sound.play()

        elif event.type == JOYBUTTONDOWN:
            if self.controller.get_button(0):
                self.state = STATE.nextlevel
                self.select_sound.play()

    def update(self, stage, player, hi_score):

        score1 = 0
        score2 = 0
        for i in range(len(self.p1_scores)):
            score1 += int(self.p1_scores[i].text)
            score2 += int(self.p2_scores[i].text)
        self.p1_total.text = str(score1)
        self.p2_total.text = str(score2)

        self.title.update()
        self.p1.update()
        self.p2.update()
        for score in self.p1_scores:
            score.update()
        for score in self.p2_scores:
            score.update()
        self.p1_total.update()
        self.p2_total.update()

        self.p1_results.text = ""
        self.p2_results.text = ""
        if stage == 5:
            if int(self.p1_total.text) > int(self.p2_total.text):
                self.p1_results.text = RESULT.win
            elif int(self.p1_total.text) < int(self.p2_total.text):
                self.p2_results.text = RESULT.win
            else:
                self.p1_results.text = RESULT.draw
                self.p2_results.text = RESULT.draw
        self.p1_results.update()
        self.p2_results.update()
        self.stage = stage

        p1_total = int(self.p1_total.text)
        p2_total = int(self.p2_total.text)
        if p1_total > self.get_highscore():
            self.set_highscore(p1_total)
        elif p2_total > self.get_highscore():
            self.set_highscore(p2_total)

        self.hi_score.text = "Hi-score: " + str(self.get_highscore())
        self.hi_score.update()

        self.animate_flash()

    def draw(self, screen):
        screen.fill(COLOR.black)
        screen.blit(self.bg, (0, 0))
        self.title.draw_to(screen)
        self.hi_score.draw_to(screen)
        self.p1.draw_to(screen)
        self.p2.draw_to(screen)

        for i in range(self.stage):
            self.p1_scores[i].draw_to(screen)
            self.p2_scores[i].draw_to(screen)

        self.p1_total.draw_to(screen)
        self.p2_total.draw_to(screen)
        self.p1_results.draw_to(screen)
        self.p2_results.draw_to(screen)

    def get_highscore(self):
        # d = shelve.open(FILES.hiscore)
        # score = d['score']
        # return int(score)
        score = config["score"]
        return int(score)

    def set_highscore(self, score):
        # d = shelve.open(FILES.hiscore)
        # d['score'] = score
        # d.close()
        config["score"] = score

    def animate_flash(self):
        self.timer += 1
        if self.timer < GAME.fps / 8:
            self.p1_results.color = COLOR.gray
            self.p2_results.color = COLOR.gray
        elif self.timer < GAME.fps / 4:
            self.p1_results.color = COLOR.burnt_orange
            self.p2_results.color = COLOR.burnt_orange
        else:
            self.timer = 0