Beispiel #1
0
    def reset(self):  #resetting the game to the start
        self.is_playing = False
        self.is_finished = False
        self.current_level = 0
        self.death_count = [0]
        self.pos_mouse = (0, 0)

        self.menus[1] = screens.Scores()

        self.levels = [
            levels.Level1(self.char_id),
            levels.Level2(self.char_id),
            levels.Level3(self.char_id),
            levels.Level4(self.char_id, self.enemy_id),
            levels.Level5(self.char_id, self.enemy_id),
            levels.Level6(self.char_id, self.enemy_id),
            levels.Level7(self.char_id, self.enemy_id),
            levels.Level8(self.char_id),
            levels.Level9(self.char_id),
            levels.Level10(self.char_id)
        ]
        self.credits = screens.Credits()
        self.time_init = 0
        self.time_game = 0
        self.score = 0
Beispiel #2
0
    def __init__(self, dim_x, dim_y):
        self.window = pygame.display.set_mode((dim_x, dim_y))
        self.music = pygame.mixer.music.load('Files/SoundTrack.ogg')

        self.is_open = True
        self.is_playing = False
        self.is_in_menu = False
        self.is_in_scores = False
        self.is_finished = False
        self.current_level = 0
        self.death_count = [0]
        self.pos_mouse = (0, 0)

        self.char_id = 1
        self.enemy_id = 2

        self.menus = [screens.MainMenu(), screens.Scores(), screens.Settings()]
        self.levels = [
            levels.Level1(self.char_id),
            levels.Level2(self.char_id),
            levels.Level3(self.char_id),
            levels.Level4(self.char_id, self.enemy_id),
            levels.Level5(self.char_id, self.enemy_id),
            levels.Level6(self.char_id, self.enemy_id),
            levels.Level7(self.char_id, self.enemy_id),
            levels.Level8(self.char_id),
            levels.Level9(self.char_id),
            levels.Level10(self.char_id)
        ]
        self.credits = screens.Credits()
        self.time_init = 0
        self.time_game = 0
        self.score = 0

        self.is_paused = False

        pygame.display.set_caption('I Keep Having This Dream')
Beispiel #3
0
class Game:
    LEVELS = [
        levels.Level7(),
        levels.Level6(),
        levels.Level5(),
        levels.Level1(),
        levels.Level3(),
        levels.Level8(),
        levels.Level4(),
        levels.Level9(),
        levels.Level2()
    ]

    MENU_WIDTH = 200

    def __init__(self, width, height, fpsLock):
        self.width = width
        self.height = height
        self.fpsLock = fpsLock

        self.level = 0
        self.life = 3
        self.fps = 0
        self.timeTot = 0
        self.time = 0
        self.state = STATE.INITIAL

        self.gamefield = pygame.Surface((width - self.MENU_WIDTH, height))
        self.gamefield = self.gamefield.convert()

        self.menufield = pygame.Surface((self.MENU_WIDTH, height))
        self.menufield = self.menufield.convert()

        self.addButtons()
        
    def addButtons(self):
        self.fpsMessage  = Message((self.MENU_WIDTH-25, 15), str(self.fps))
        self.lifeMessage = Message((self.MENU_WIDTH/2, 60),
                                   "life: {}".format(self.life))
        self.levlMessage = Message((self.MENU_WIDTH/2, 90),
                                   "level: {}".format(self.level))
        self.timeMessage = Message((self.MENU_WIDTH/2, 120),
                                   "time: {0}/{1}".format(
                                       self.time / self.fpsLock, self.timeTot / self.fpsLock))
        self.endMessage = Message(((self.width - self.MENU_WIDTH)/2, self.height/2),
                                  "GAME OVER", size=60, color=(250,0,0))
        self.winMessage = Message(((self.width - self.MENU_WIDTH)/2, self.height/2),
                                  "WIN!",
                                  size=60, color=(247, 167, 111))
        
        pos, w, h = (self.MENU_WIDTH/2, 350), self.MENU_WIDTH*0.75, 40
        self.restartButton = Button(pos, w, h, "restart")
        
        pos, w, h = (self.MENU_WIDTH/2, 400), self.MENU_WIDTH*0.75, 40
        self.addNewMButton = Button(pos, w, h, "new missile")
        
        pos, w, h = (self.MENU_WIDTH/2, 200), self.MENU_WIDTH*0.75, 40
        self.nextLvlButton = Button(pos, w, h, "next level")
        
        pos, w, h = (self.MENU_WIDTH/2, 250), self.MENU_WIDTH*0.75, 40
        self.prevLvlButton = Button(pos, w, h, "prev. level")
        
        pos, w, h = (self.MENU_WIDTH/2, 300), self.MENU_WIDTH*0.75, 40
        self.pauseButton = Button(pos, w, h, "pause")
        
    def pause(self):
        if self.state == STATE.WAIT_UNTILL_STOP:
            self.state = STATE.PAUSE
            self.pauseButton.message.update("continue")
        elif self.state == STATE.PAUSE:
            self.state = STATE.WAIT_UNTILL_STOP
            self.pauseButton.message.update("pause")

    def lose(self):
        self.initLevel()
        self.life -= 1
        self.time = 0
        if self.life == 0:
            self.state = STATE.LOSS
        else:
            self.state = STATE.INITIAL

    def win(self):
        self.state = STATE.WIN
            
    def restart(self):
        self.initLevel()
        self.time = 0
        self.life = 3
        self.state = STATE.INITIAL

    def goToNextLevel(self):
        self.level = min(len(self.LEVELS) - 1, self.level + 1)
        self.restart()

    def goToPreviousLevel(self):
        self.level = max(0, self.level - 1)
        self.restart()
        
    def checkMenuSelection(self, pos):
        return pos[0] > self.width-self.MENU_WIDTH

    def checkButtonsSelection(self, pos):
        x, y = pos[0] - self.width + self.MENU_WIDTH, pos[1]
        self.restartButton.checkSelected((x,y))
        self.addNewMButton.checkSelected((x,y))
        self.nextLvlButton.checkSelected((x,y))
        self.prevLvlButton.checkSelected((x,y))
        self.pauseButton.checkSelected((x,y))
        
    def checkButtonsActivation(self, pos):
        x, y = pos[0] - self.width + self.MENU_WIDTH, pos[1]
        if self.restartButton.checkActivated((x,y)):
            self.restart()
        if self.addNewMButton.checkActivated((x,y)) and self.state not in (STATE.LOSS, STATE.WIN):
            self.state = STATE.INITIAL
        if self.nextLvlButton.checkActivated((x,y)):
            self.goToNextLevel()
        if self.prevLvlButton.checkActivated((x,y)):
            self.goToPreviousLevel()
        if self.pauseButton.checkActivated((x,y)):
            self.pause()
        
    def display(self, screen):
        self.gamefield.fill((116, 208, 242))
        self.menufield.fill((0, 109, 193))
        
        self.fpsMessage.update(str(self.fps))
        self.fpsMessage.display(self.menufield)
        self.lifeMessage.update("life: {}".format(self.life))
        self.lifeMessage.display(self.menufield)
        self.timeMessage.update("time: {0:.1f}/{1}".format(
            self.time / float(self.fpsLock), self.timeTot / self.fpsLock))
        self.timeMessage.display(self.menufield)
        self.levlMessage.update("level: {}".format(self.level + 1))
        self.levlMessage.display(self.menufield)
        
        self.restartButton.display(self.menufield)
        self.addNewMButton.display(self.menufield)
        self.nextLvlButton.display(self.menufield)
        self.prevLvlButton.display(self.menufield)
        self.pauseButton.display(self.menufield)
        
        self.world.display(self.gamefield)
        if self.state == STATE.LOSS:
            self.endMessage.display(self.gamefield)        
        if self.state == STATE.WIN:
            self.winMessage.display(self.gamefield)
            
        screen.blit(self.menufield, (self.width-self.MENU_WIDTH, 0))
        screen.blit(self.gamefield, (0, 0))
        
    def initLevel(self):
        width, height = self.width - self.MENU_WIDTH, self.height
        world = World(width, height)
        level = self.LEVELS[self.level]
        level.initialize(world)
        self.timeTot = level.MAX_TIME
        self.world = world
    
    def run(self, screen, fpsClock):
        self.initLevel()

        while True:
            if self.time == self.timeTot:
                self.lose()

            pos = pygame.mouse.get_pos()
            self.checkButtonsSelection(pos)

            if self.state == STATE.INITIAL:
                self.world.canon.setDirection(pos)
            elif self.state == STATE.CHOOSE_POWER:
                self.world.canon.setSpeed(pos)
            elif self.state == STATE.WAIT_UNTILL_STOP:
                self.world.canon.setDirection(pos)
                self.world.move(self.win, self.lose)
                self.time += 1

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if self.checkMenuSelection(pos):
                        self.checkButtonsActivation(pos)
                    else:
                        if self.state == STATE.INITIAL:
                            self.state = STATE.CHOOSE_POWER
                            self.world.canon.state = 1
                        elif self.state == STATE.CHOOSE_POWER:
                            self.state = STATE.WAIT_UNTILL_STOP
                            self.world.canon.state = 2
                            self.world.addMissile()
                        elif self.state == STATE.LOSS:
                            self.restart()
                            continue
                        elif self.state == STATE.WIN:
                            self.goToNextLevel()
                            continue

            self.display(screen)
            pygame.display.update()
            self.fps = fpsClock.tick(self.fpsLock)
Beispiel #4
0
    def run(self):
        pygame.mixer.music.play(-1)
        while self.is_open:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.is_open = False
                    break

                if event.type == pygame.MOUSEBUTTONDOWN:
                    self.pos_mouse = pygame.mouse.get_pos()
                    break

                if self.is_playing:
                    try:
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                                self.levels[
                                    self.current_level].player.velocity_x = 7

                            if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                                self.levels[
                                    self.current_level].player.velocity_x = -7

                            if event.key == pygame.K_UP or event.key == pygame.K_w:
                                if not self.levels[
                                        self.current_level].player.is_jumping:
                                    self.levels[
                                        self.
                                        current_level].player.velocity_y = -15
                                    self.levels[
                                        self.
                                        current_level].player.is_jumping = True

                            if event.key == pygame.K_r:
                                self.levels[
                                    self.current_level].player.was_hurt = True
                                self.death_count[0] += 1

                        if event.type == pygame.KEYUP:
                            if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or \
                               event.key == pygame.K_d or event.key == pygame.K_a:
                                self.levels[
                                    self.current_level].player.velocity_x = 0

                            if event.key == pygame.K_UP or event.key == pygame.K_w:
                                self.levels[
                                    self.current_level].player.velocity_y = 0

                    except IndexError:
                        pass

            if not self.is_playing and not self.is_in_menu and not self.is_in_scores:
                self.menus[0].draw(self.window)
                self.menus[0].update(self.window)

                if self.menus[0].elements[1].is_pressed(
                        self.menus[0].element_locations[1], self.pos_mouse):
                    self.is_playing = True
                    self.time_init = time.time()

                self.is_in_menu = self.menus[0].elements[2].is_pressed(
                    self.menus[0].element_locations[2], self.pos_mouse)
                self.is_in_scores = self.menus[0].elements[3].is_pressed(
                    self.menus[0].element_locations[3], self.pos_mouse)
                self.is_open = not self.menus[0].elements[4].is_pressed(
                    self.menus[0].element_locations[4], self.pos_mouse)

            elif self.is_finished:
                self.credits.draw(self.window)
                self.credits.update()
                if self.credits.have_rolled():
                    self.is_playing = False
                    self.reset()

            elif self.is_in_menu:
                self.menus[2].draw(self.window)
                if self.menus[2].elements[0].is_pressed(
                        self.menus[2].element_locations[0], self.pos_mouse):
                    self.is_in_menu = False
                    self.pos_mouse = (0, 0)

                if self.menus[2].elements[1].is_pressed(
                        self.menus[2].element_locations[1], self.pos_mouse):
                    if not self.is_paused:
                        self.is_paused = True
                        pygame.mixer.music.pause()
                    else:
                        self.is_paused = False
                        pygame.mixer.music.unpause()
                    self.pos_mouse = (0, 0)

                if self.menus[2].elements[2].is_pressed(
                        self.menus[2].element_locations[2], self.pos_mouse):
                    open('Files/scoreboard.txt', 'w').close()
                    self.pos_mouse = (0, 0)
                    self.menus[1] = screens.Scores()

                if self.menus[2].elements[3].is_pressed(
                        self.menus[2].element_locations[3], self.pos_mouse):
                    if self.char_id == 1:
                        self.char_id = 2
                    else:
                        self.char_id = 1

                    self.levels = [
                        levels.Level1(self.char_id),
                        levels.Level2(self.char_id),
                        levels.Level3(self.char_id),
                        levels.Level4(self.char_id, self.enemy_id),
                        levels.Level5(self.char_id, self.enemy_id),
                        levels.Level6(self.char_id, self.enemy_id),
                        levels.Level7(self.char_id, self.enemy_id),
                        levels.Level8(self.char_id),
                        levels.Level9(self.char_id),
                        levels.Level10(self.char_id)
                    ]

                    self.pos_mouse = (0, 0)

                if self.menus[2].elements[4].is_pressed(
                        self.menus[2].element_locations[4], self.pos_mouse):
                    if self.enemy_id == 1:
                        self.enemy_id = 2
                    else:
                        self.enemy_id = 1

                    self.levels = [
                        levels.Level1(self.char_id),
                        levels.Level2(self.char_id),
                        levels.Level3(self.char_id),
                        levels.Level4(self.char_id, self.enemy_id),
                        levels.Level5(self.char_id, self.enemy_id),
                        levels.Level6(self.char_id, self.enemy_id),
                        levels.Level7(self.char_id, self.enemy_id),
                        levels.Level8(self.char_id),
                        levels.Level9(self.char_id),
                        levels.Level10(self.char_id)
                    ]

                    self.pos_mouse = (0, 0)

            elif self.is_in_scores:
                self.menus[1].draw(self.window)
                if self.menus[1].elements[0].is_pressed(
                        self.menus[1].element_locations[0], self.pos_mouse):
                    self.is_in_scores = False
                    self.pos_mouse = (0, 0)

            else:
                try:
                    self.levels[self.current_level].draw(self.window)

                    deaths = objects.Text(str(self.death_count[0]), 24)
                    deaths.draw(self.window, [10, 450])

                    self.levels[self.current_level].update(self.death_count)

                    if self.levels[self.current_level].is_finished():
                        self.current_level += 1
                        pygame.time.wait(1000)

                except IndexError:
                    self.time_game = time.time() - self.time_init
                    self.score = 10000 - self.time_game - (
                        self.death_count[0] * 50)
                    self.is_finished = True
                    scoreboard = open('Files/scoreboard.txt', 'a')
                    scoreboard.write(
                        time.strftime("%d/%m/%Y") + ',' +
                        str(int(self.death_count[0])) + ',' +
                        str(int(self.time_game)) + ',' + str(int(self.score)) +
                        '\n')
                    scoreboard.close()

            pygame.display.update()