Exemple #1
0
class Controller():
    def __init__(self, dimensions):
        self.dimensions = dimensions  # dimensions of the grid
        self.paused = True  # the game starts paused

        self.grid = Grid(dimensions)

    def toggleCellStatus(self, cell):  # toggle between dead/alive
        if self.grid.statusGrid[cell[0]][cell[1]] == False:
            self.grid.statusGrid[cell[0]][cell[1]] = True
        else:
            self.grid.statusGrid[cell[0]][cell[1]] = False

    def togglePause(self):
        if self.paused == False: self.paused = True
        else: self.paused = False

    def clearGrid(self):
        self.grid = Grid(self.dimensions)

    def getLiveCellList(self):
        liveCells = []
        for y in range(self.dimensions[0]):
            for x in range(self.dimensions[1]):
                if self.grid.statusGrid[y][x]: liveCells.append((x, y))
        return liveCells

    def update(self):
        if not self.paused: self.grid.runGame()
Exemple #2
0
    def __init__(self, resolution=(800, 800)):

        self.resoltuion = resolution
        self.grid_size = 30
        self.field_size = resolution[0] / self.grid_size
        self.cur_time = 0
        self.powerup_time = None
        self.game_running = False
        self.window_running = False

        self.grid = Grid(size_x=self.field_size, size_y=self.field_size)
        self.load_map()

        self.player = Player(grid=self.grid, field=self.get_unblocked_field())

        self.powerup = PowerUp(grid=self.grid,
                               field=self.get_unblocked_field())

        self.enemies = []
        for i in xrange(ENEMY_COUNT):
            enemy = self.create_ghost()
            self.enemies.append(enemy)
 def setUp(self):
     self.game_frame = GameFrame
     self.grid = Grid(750, 500, 5, "Vanessa", "Saki", 0, 80, self.game_frame)
class GridTesting(unittest.TestCase):

    # Setting up GameFrame and Grid
    def setUp(self):
        self.game_frame = GameFrame
        self.grid = Grid(750, 500, 5, "Vanessa", "Saki", 0, 80, self.game_frame)

    # Player 1 or 2 collides with Player 1's tail
    def testCollisionTail1(self):
        self.grid.p1_obstacle_list.amount_of_obstacles = 12
        self.grid.player_one.x = 120
        self.grid.player_one.y = 140
        self.grid.player_two.x = 120
        self.grid.player_two.y = 140
        self.grid.p1_obstacle_list.x[4] = 120
        self.grid.p1_obstacle_list.y[4] = 140
        self.grid.check_collision()
        self.assertTrue(self.grid.player_one.is_alive == False, "player_one should be dead!")
        self.assertTrue(self.grid.player_two.is_alive == False, "player_two should be dead!")

    # Player 1 or 2 collides with Player 2's tail
    def testCollisionTail2(self):
        self.grid.p2_obstacle_list.amount_of_obstacles = 12
        self.grid.player_one.x = 120
        self.grid.player_one.y = 140
        self.grid.player_two.x = 120
        self.grid.player_two.y = 140
        self.grid.p2_obstacle_list.x[4] = 120
        self.grid.p2_obstacle_list.y[4] = 140
        self.grid.check_collision()
        self.assertTrue(self.grid.player_one.is_alive == False, "player_one should be dead!")
        self.assertTrue(self.grid.player_two.is_alive == False, "player_two should be dead!")

    # Player 1 or 2 collides with the obstacle on the map
    def testCollisionObstacleMap(self):
        self.grid.map_obstacle_list.amount_of_obstacles = 12
        self.grid.player_one.x = 120
        self.grid.player_one.y = 140
        self.grid.player_two.x = 120
        self.grid.player_two.y = 140
        self.grid.map_obstacle_list.x[4] = 120
        self.grid.map_obstacle_list.y[4] = 140
        self.grid.check_collision()
        self.assertTrue(self.grid.player_one.is_alive == False, "player_one should be dead!")
        self.assertTrue(self.grid.player_two.is_alive == False, "player_two should be dead!")

    # Player 1 or 2 collides with the wall (Right and Bottom walls)
    def testCollisionWall1(self):
        self.grid.map_obstacle_list.amount_of_obstacles = 12
        self.grid.player_one.x = 750
        self.grid.player_one.y = 500
        self.grid.check_collision()
        self.assertTrue(self.grid.player_one.is_alive == False, "player_one should be dead!")

    # Player 1 or 2 collides with the wall (Left and Top walls)
    def testCollisionWall2(self):
        self.grid.map_obstacle_list.amount_of_obstacles = 12
        self.grid.player_one.x = 0
        self.grid.player_one.y = 0
        self.grid.check_collision()
        self.assertTrue(self.grid.player_one.is_alive == False, "player_one should be dead!")

    # Check the status after first game play
    def testCheckGameCount1(self):
        self.grid.game_count = 0
        self.grid.player_one.is_alive = True
        self.grid.player_two.is_alive = True
        self.assertTrue(self.grid.on_timer() == True, "game_count is less than 3, it should return True!")

    # Check the status after second game play
    def testCheckGameCount2(self):
        self.grid.game_count = 1
        self.grid.player_one.is_alive = True
        self.grid.player_two.is_alive = True
        self.assertTrue(self.grid.on_timer() == True, "game_count is less than 3, it should return True!")

    # Check the status after third game play
    def testsCheckGameCount3(self):
        self.grid.game_count = 2
        self.grid.player_one.is_alive = True
        self.grid.player_two.is_alive = True
        self.assertTrue(self.grid.on_timer() == True, "game_count is less than 3, it should return True!")

    # Check the status after third game play
    def testsCheckGameCountOver(self):
        self.grid.game_count = 3
        self.grid.player_one.is_alive = True
        self.grid.player_two.is_alive = True
        self.assertTrue(self.grid.on_timer() == False, "game_count is equal to 3, it should return False!")
Exemple #5
0
    def __init__(self, dimensions):
        self.dimensions = dimensions  # dimensions of the grid
        self.paused = True  # the game starts paused

        self.grid = Grid(dimensions)
Exemple #6
0
 def clearGrid(self):
     self.grid = Grid(self.dimensions)
Exemple #7
0
class FieldHandler(object):
    def __init__(self, resolution=(800, 800)):

        self.resoltuion = resolution
        self.grid_size = 30
        self.field_size = resolution[0] / self.grid_size
        self.cur_time = 0
        self.powerup_time = None
        self.game_running = False
        self.window_running = False

        self.grid = Grid(size_x=self.field_size, size_y=self.field_size)
        self.load_map()

        self.player = Player(grid=self.grid, field=self.get_unblocked_field())

        self.powerup = PowerUp(grid=self.grid,
                               field=self.get_unblocked_field())

        self.enemies = []
        for i in xrange(ENEMY_COUNT):
            enemy = self.create_ghost()
            self.enemies.append(enemy)

    def get_unblocked_field(self):
        '''
        get a field which is not blocked
        '''
        field = random.choice(self.grid.fields)
        while field.blocked is True:
            print 'POWERUP FIELD'
            field = random.choice(self.grid.fields)
        return field

    def create_ghost(self):
        enemy = Enemy(grid=self.grid, field=self.get_unblocked_field())
        return enemy

    def load_map(self):
        with open('pac_map.json', 'r') as save_map:
            pac_map_raw = save_map.read()
            pac_map = json.loads(pac_map_raw)
        for raw_field in pac_map:
            if raw_field['blocked'] is True:
                field = self.grid.get_field_c(cx=raw_field['pos_x'],
                                              cy=raw_field['pos_y'])
                field.blocked = True

    def create_walls(self):
        for field in self.grid.fields:
            rvalue = random.random()
            if rvalue > 0.6:
                field.blocked = True

    def click(self, pos):
        x = pos[0] / self.field_size
        y = pos[1] / self.field_size
        for field in self.grid.fields:
            if not field.x == x:
                continue
            if not field.y == y:
                continue
            field.food += 10000

    def right_click(self, pos):
        x = pos[0] / self.field_size
        y = pos[1] / self.field_size
        for field in self.grid.fields:
            if not field.x == x:
                continue
            if not field.y == y:
                continue
            if not field.blocked:
                field.blocked = True
            else:
                field.blocked = False

    def check_collision(self):
        # time.sleep(0.5)
        #collision = self.player.field in enemy_fields
        dead_enemies = []
        for enemy in self.enemies:
            collision = self.player.field == enemy.field

            if collision and self.player.powerupped is False:
                print 'GAME OVER!'
                self.game_running = False
                game_over_label = PygameLabel('GAME OVER', (255, 0, 0),
                                              (300, 400), 100, 'Arial')
                # pygame_main.END_LABEL = game_over_label
                # sys.exit(0)
            elif collision and self.player.powerupped is True:
                self.player.kills += 1
                self.player.powerupped = False
                dead_enemies.append(enemy)

        for enemy in dead_enemies:
            self.enemies.remove(enemy)

        if self.player.kills == ENEMY_COUNT:
            with open('highscore.json', 'a') as highscore_file:
                highscore_file.write(str(self.cur_time) + '\n')
            print 'WIN!'
            sys.exit(0)

        collision_powerup = self.player.field == self.powerup.field
        if collision_powerup is True:
            print 'POWERUP COLLECTED!'
            self.player.powerupped = True
            self.powerup_time = time.time()
            self.powerup.field = self.get_unblocked_field()

    def draw_fields(self, display):
        enemy_fields = [enemy.field for enemy in self.enemies]

        for field in self.grid.fields:
            red = 0
            green = 0
            blue = 0

            field_surface = pygame.Surface((self.field_size, self.field_size))

            if field.blocked:
                red = 255
                blue = 255
                green = 255

            if field == self.player.field:
                if self.player.powerupped:
                    if self.ticks % 2 == 0:
                        red = 255
                    else:
                        blue = 255
                else:
                    blue = 255

            if field in enemy_fields:
                red = 255
                green = 255

            if field == self.powerup.field:
                green = 255

            pygame.draw.rect(field_surface, (red, green, blue),
                             (0, 0, self.field_size, self.field_size))
            field_surface = field_surface.convert()

            pos = (field.x * self.field_size, field.y * self.field_size)
            display.blit(field_surface, pos)

        return display