Пример #1
0
 def initObj(self):
     # 设置大小,用于背景图片的缩放
     self.gameNet = GameNet(self)
     self.gameNet.connectToServer()
     self.menu = Menu((self.width, self.height), self)
     self.rules = Rules(self.width, self.height)
     self.setLevel = SetLevel(self.width, self.height)
     self.matching = Matching((self.width, self.height), self)
     self.game = Game(self)
     # finish 界面的大小和游戏界面一样
     self.finish = Finish((self.game.width, self.game.height))
Пример #2
0
    def __init__(self):
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        # Make the Obstacles & Finish
        self.obstacles = pygame.sprite.Group()
        self._create_set_of_obstacles()
        self.finish = Finish(self)
        self.ship = Ship(self)
Пример #3
0
def display_page(pathname):
    if pathname == '/app.py':
        return Game()
    elif pathname == '/finish.py':
        return Finish()
    else:
        return Home()
Пример #4
0
def load_level(level):
    walls = []
    players = []
    monsters = []
    finishes = []

    #Parse the level string above. W = wall, F = exit, P = player, M = monster
    x = y = 0
    for row in levels[level]:
        for col in row:
            if col == 'W':
                walls.append(Wall((x, y)))
            if col == 'P':
                players.append(Player((x, y)))
            if col == 'M':
                monsters.append(Monster((x, y)))
            if col == 'F':
                finishes.append(Finish((x, y)))
            x += 32
        y += 32
        x = 0
    return walls, players, monsters, finishes
Пример #5
0
def main():
    ms = 'k'
    while True:
        pygame.init()
        maze = maze_generation()
        screen = pygame.display.set_mode(DISPLAY)
        pygame.display.set_caption('Maze game')

        bg = Surface(DISPLAY)
        bg.fill(Color(BACKGROUND_COLOR))
        screen.blit(bg, (0, 0))

        hero = NPC(maze[1][0] * 10, maze[1][1] * 10)
        finish = Finish(maze[2][0] * 10, maze[2][1] * 10)

        entites = pygame.sprite.Group()
        platforms = []
        entites.add(hero)
        entites.add(finish)
        level = maze[0]

        clock = pygame.time.Clock()

        x = y = 0
        for row in level:
            for col in row:
                if col == 0:
                    pf = Platform(x, y)
                    entites.add(pf)
                    platforms.append(pf)
                x += PLATFORM_WIDTH
            y += PLATFORM_HEIGHT
            x = 0

        stepc = 0

        while True:
            for i in pygame.event.get():
                if i.type == KEYDOWN and i.key == K_LEFT:
                    stepc += 1
                    hero.rect.x -= 10
                    for p in platforms:
                        if sprite.collide_rect(hero, p):
                            hero.rect.left = p.rect.right
                if i.type == KEYDOWN and i.key == K_RIGHT:
                    stepc += 1
                    hero.rect.x += 10
                    for p in platforms:
                        if sprite.collide_rect(hero, p):
                            hero.rect.right = p.rect.left
                if i.type == KEYDOWN and i.key == K_UP:
                    stepc += 1
                    hero.rect.y -= 10
                    for p in platforms:
                        if sprite.collide_rect(hero, p):
                            hero.rect.top = p.rect.bottom
                if i.type == KEYDOWN and i.key == K_DOWN:
                    hero.rect.y += 10
                    stepc += 1
                    for p in platforms:
                        if sprite.collide_rect(hero, p):
                            hero.rect.bottom = p.rect.top
                if i.type == QUIT:
                    pygame.quit()
                    exit()
            pygame.display.update()
            screen.blit(bg, (0, 0))
            entites.draw(screen)
            clock.tick(60)
            ms = finish.collaid(hero, stepc)
            if ms == 'kek':
                break
            pygame.display.update()
Пример #6
0
class AlienInvasion(object):
    def __init__(self):
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        # Make the Obstacles & Finish
        self.obstacles = pygame.sprite.Group()
        self._create_set_of_obstacles()
        self.finish = Finish(self)
        self.ship = Ship(self)

    def runGame(self):

        while True:
            self._check_events()
            self.ship.update()

            self._update_screen()

    def _check_events(self):
        """Responds to keyboard and mouse events"""
        # funkcja get() zwraca listę zdarzeń, które miały miejsce od czasu jej poprzedniego wywołania
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                # self._check_play_button(mouse_pos) <===================

    def _check_keydown_events(self, event):
        """Respond to keypresses"""
        if event.key == pygame.K_UP:
            self.ship.moving_up = True
        elif event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            # self._fire_bullet() <===================
            sys.exit()

    def _check_keyup_events(self, event):
        """Respond to key releases"""
        if event.key == pygame.K_UP:
            self.ship.moving_up = False
        elif event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False

    def _update_screen(self):
        """Update images on the screen and flip to the new screen"""
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()
        self.finish.blitme()
        """
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.aliens.draw(self.screen)
        """

        # Draw the score information.
        # self.sb.show_score()

        # Draw the play button if the game is inactive.
        # if not self.stats.game_active:
        #    self.play_button.draw_button()
        self.obstacles.draw(self.screen)

        # Make the most recently drawn screen visible.
        pygame.display.flip()

    def _create_set_of_obstacles(self):
        """Create the set of obstacles"""
        obstacle = Obstacle(self)
        obstacle_width, obstacle_height = obstacle.rect.size
        available_space_x = self.settings.screen_width - (2 * obstacle_width)
        number_obstacles_x = available_space_x // (2 * obstacle_width)

        # Create the full set of obstacles
        for row_number in range(1, 4):
            # Create the full row of obstacles
            for obstacle_number in range(1, 4):
                self._create_obstacle(obstacle_number, row_number)

    def _create_obstacle(self, obstacle_number, row_number):
        """Create an obstacle and place it in the row"""
        obstacle = Obstacle(self)
        obstacle_width, obstacle_height = obstacle.rect.size

        # There are supposed to be a set 3x3 of obstacles
        # ... and we just hope that the screen is big enough ;)
        obstacle.rect.centerx = self.settings.screen_width / 4 * obstacle_number
        obstacle.rect.centery = self.settings.screen_height / 4 * row_number
        self.obstacles.add(obstacle)
Пример #7
0
def game():
    group = pygame.sprite.Group()
    player_anim = Anime(group, (228, 288))
    board = Board.load_map(None, name)
    board.set_view(30, 30, 30)
    lst = []
    if sum(board.hero[0]):
        player = Player(screen, player_anim, board, board.hero[0])
        player_anim.resize(30)
    if sum(board.hero[1]):
        tverdolobiy = Tverdolobiy(group, board.hero[1], board, player)
        tverdolobiy.resize(30)
        lst.append(tverdolobiy)
    if sum(board.hero[2]):
        merzopakostniy = Merzopakostniy(group, board.hero[2], board, player)
        merzopakostniy.resize(30)
    if sum(board.hero[3]):
        legushka = Legushka(group, board.hero[3], board, player)
        legushka.resize(30)
        lst.append(legushka)
    running = True
    c = 0
    while running:
        c += 1
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                board.get_click(event.pos)
            if event.type == pygame.KEYDOWN:
                if event.key in up:
                    player.set_dir('up')
                if event.key in down:
                    player.set_dir('down')
                if event.key in left:
                    player.set_dir('left')
                if event.key in right:
                    player.set_dir('right')
                if event.key == pygame.K_LEFT:
                    player.set_dir('left')
                if event.key == pygame.K_RIGHT:
                    player.set_dir('right')
                if event.key == pygame.K_UP:
                    player.set_dir('up')
                if event.key == pygame.K_DOWN:
                    player.set_dir('down')
        screen.fill((0, 0, 0))
        board.render()
        if not len(board.check_points):
            board.check_points.add(None)
        if not board.are_left():
            board.reset()
        player.move([])
        player_anim.rect.x, player_anim.rect.y = player.x, player.y
        if sum(board.hero[2]):
            merzopakostniy.move()
        if not c % 2:
            if sum(board.hero[3]):
                legushka.move()
            if sum(board.hero[1]):
                tverdolobiy.move()
        group.draw(screen)
        if c % 20 == 0:
            player_anim.update()
        if sum(board.hero[2]):
            check = Check(player, merzopakostniy.num_bomb() + lst)
        elif sum(board.hero[0]):
            check = Check(player, lst)
        if check.checkaed():
            running = False
        draw(str(len(board.check_points) - 1 + board.count))
        pygame.display.flip()
    Finish(group, str(len(board.check_points) - 1 + board.count))
Пример #8
0
class Main(object):
    """main loop of the game"""
    def __init__(self):
        pygame.init()
        # 包括读取设置文件,设置游戏基本属性
        self.initAttr()
        # 传递所需的参数,实例化所包含的各个类
        self.initObj()
        self.initContext()

    def initAttr(self):
        self.width, self.height = WINDOW_WIDTH, WINDOW_HEIGHT
        self.state = STATE.menu
        self.running = True
        self.level = SETLEVEL["DEFAULT_LEVEL"]
        self.channelID = None

    def initObj(self):
        # 设置大小,用于背景图片的缩放
        self.gameNet = GameNet(self)
        self.gameNet.connectToServer()
        self.menu = Menu((self.width, self.height), self)
        self.rules = Rules(self.width, self.height)
        self.setLevel = SetLevel(self.width, self.height)
        self.matching = Matching((self.width, self.height), self)
        self.game = Game(self)
        # finish 界面的大小和游戏界面一样
        self.finish = Finish((self.game.width, self.game.height))

    # 绘制游戏主窗体
    def initContext(self):
        self.screen = pygame.display.set_mode((self.width, self.height), DOUBLEBUF)
        pygame.display.set_caption("Boxes")

        self.clock = pygame.time.Clock();

    def linkSuccess(self):
        print "link success"
        print self.channelID

    def enterMatching(self):
        self.matching.getRooms()

    def enemyComming(self, turn, gameID):
        self.gameID = gameID
        self.game.enemyComming(turn)
        self.game.setHome()

    def joinGame(self, level, gameID, turn):
        self.game = Game(self)
        self.game.gameID = gameID
        self.state = STATE.game
        self.game.setLevel(level)
        self.game.enemyComming(turn)
        self.game.setAway()

    def enterMenu(self):
        self.state = STATE.menu

    # 进入游戏界面事件
    def enterGame(self, level):
        self.game.setLevel(level)
        self.game.initContext()
        self.state = STATE.game

    def startedNewGame(self, level):
        print "open room", level
        self.gameNet.openRoom(level)

    def leaveServer(self, gameID):
        self.gameNet.leaveServer(self.gameID)
        self.state = STATE.menu

    def backToMenu(self):
        self.initContext()
        self.state = STATE.menu

    def addScore(self):
        self.game.addScore()

    def enemyAddScore(self):
        self.game.enemyAddScore()

    def getRooms(self, matching, page, num):
        self.gameNet.getRooms(matching, page, num)

    def update(self):
        self.clock.tick(60)
        self.screen.fill(0)
        self.gameNet.pump()

        if STATE.exit == self.state:
            exit()

        elif STATE.setLevel == self.state:
            var = self.setLevel.draw(self.screen)
            if (var in STATE):
                self.state = var
            else:
                self.startedNewGame(var)
                self.enterGame(var)

        elif STATE.game == self.state:
            self.state = self.game.draw()
            if (self.state != STATE.game):
                self.initContext()

        elif STATE.matching == self.state:
            self.state = self.matching.draw(self.screen)

        elif STATE.menu == self.state:
            self.menu.draw(self.screen)
            self.state = self.menu.clickListener()

        elif STATE.rules == self.state:
            self.rules.draw(self.screen)
            self.state = self.rules.clickListener()

        elif STATE.finish == self.state:
            var = self.finish.draw(self.screen)
            if (var != STATE.finish):
                self.backToMenu()

        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                if STATE.game == self.state:
                    self.gameNet.exitFlag = True
                    self.gameNet.leaveServer(self.game.gameID)
                    sleep(0.5)
                    pygame.quit()
                    exit()
                else:
                    pygame.quit()
                    exit()
                # exit()
            # if (event.type == pygame.KEYDOWN):
            #     if (event.key == pygame.K_ESCAPE):
            #         exit() 
            #         pygame.quit()
        pygame.display.flip()

    def winning(self):
        self.finish.setWin(self.game.gameID)
        self.enterFinish()

    def lost(self):
        self.finish.setLost(self.game.gameID)
        self.enterFinish()

    def drawGame(self):
        self.finish.setDraw(self.game.gameID)
        self.enterFinish()

    def enterFinish(self):
        self.state = STATE.finish

    def run(self):
        while self.running:
            self.update()

    def exit(self):
        pygame.quit()
        exit()