Esempio n. 1
0
def run_game():
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode(
        (settings.screen_width, settings.screen_height))
    bg = Background(screen)
    totoro = Totoro(screen, settings)
    acorns = Group()
    print(totoro.rect)
    while True:
        check_events(settings)

        create_acorn(acorns, screen, settings)

        screen.fill((0, 0, 0))
        bg.blitme()
        totoro.blitme()
        acorns.draw(screen)
        pygame.display.flip()

        totoro.update(acorns)
        acorns.update(acorns)
Esempio n. 2
0
def run_game():
    #Initialize game and make a screen object
    pygame.init()
    sounds.playsound('theme1')

    infrompy_settings = Settings()
    screen = pygame.display.set_mode(
        (infrompy_settings.screen_width, infrompy_settings.screen_height))
    screenRect = screen.get_rect()
    pygame.display.set_caption("Invaders")
    background = Background(infrompy_settings, screen)
    red_screen = Red_Screen(infrompy_settings, screen)

    # Make play button and replay button
    play_button = Button(infrompy_settings, screen, "Play")
    play_again_button = Button(infrompy_settings, screen,
                               "Your ship ded lol. Play Again ?")
    # Make an instance to store game stats
    stats = GameStats(infrompy_settings)
    # Make a ship, group of bullets and group of aliens
    ship = Ship(infrompy_settings, screen)
    ship_explosions = Group()
    bullets = Group()
    bullet_explosions = Group()
    aliens = Group()
    alien_explosions = Group()
    alien_bullets = Group()
    alien_bullet_explosions = Group()
    background = Background(infrompy_settings, screen)

    # Level board
    info_board = Info_Board(infrompy_settings, screen)
    # Bullets group
    gf.create_fleet(infrompy_settings, screen, ship, aliens)

    while True:
        background.blitme()
        gf.check_events(infrompy_settings, screen, ship, bullets,
                        bullet_explosions, play_button)
        if infrompy_settings.game_active:

            gf.update_aliens(infrompy_settings, aliens, screen, ship,
                             alien_explosions, alien_bullet_explosions,
                             red_screen)
            gf.update_alien_explosions(aliens, screen, alien_explosions)

            gf.update_ship(infrompy_settings, screen, ship, ship_explosions)
            gf.update_ship_explosions(ship, screen, ship_explosions)

            gf.update_bullet_explosions(aliens, screen, bullets,
                                        bullet_explosions)
            gf.update_bullets(infrompy_settings, aliens, screen, ship, bullets,
                              bullet_explosions, alien_explosions,
                              alien_bullets)

            gf.update_alien_bullets(infrompy_settings, aliens, screen, ship,
                                    alien_bullets, alien_bullet_explosions,
                                    red_screen)
            gf.update_alien_bullet_explosions(aliens, screen,
                                              alien_bullet_explosions)
            gf.update_red_screen(infrompy_settings, red_screen)

        gf.update_screen(infrompy_settings, screen, ship, ship_explosions,
                         aliens, bullets, bullet_explosions, alien_explosions,
                         alien_bullets, alien_bullet_explosions, info_board,
                         play_button, play_again_button, red_screen)
def run_game():
    #初始化游戏
    pygame.init()
    #创建一个屏幕对象
    ai_setting = Setting()
    screen = pygame.display.set_mode(
        (ai_setting.screen_width, ai_setting.screen_height))
    # jack=Jack(screen)
    background = Background(screen)
    #设置窗口名称
    pygame.display.set_caption("唤兽世界")

    gf = Game_function(screen)

    jifen = JiFen()

    kai_shi = Kaishi(screen)
    kai_shi.kaishi()

    begingame = False
    pygame.mixer.music.load("music/begin_music.wav")
    pygame.mixer.music.play(-1, 0)

    spirits = Group()

    xl = 6
    for i in range(5):
        x = random.randint(0, 1201)
        y = random.randint(0, 801)
        spirit = Spirit(screen, x, y)
        spirits.add(spirit)

    attack = Attack(screen)
    gems = Group()
    gem = Gem(screen)

    end = True
    win = True

    nests = Group()
    nest = Monster_nest(screen)
    nests.add(nest)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                #系统退出
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    begingame = True
                    background.blitme()

                    jack = Jack(screen, gf)

                    jifen.blit_me(screen)

                    pygame.mixer.music.load("music/music.wav")
                    pygame.mixer.music.play(-1, 0)

                    ai_xin = Ai_xin(screen)

                    jack.blitme()

                    frame_spirit = pygame.time.get_ticks()
                    frame_treehole = pygame.time.get_ticks()
                    frame_treehole_spirit = pygame.time.get_ticks()
                    frame_koxie = pygame.time.get_ticks()
                    frame_gem = pygame.time.get_ticks()
                    frame_hyjq = pygame.time.get_ticks()
                    frame_fq = pygame.time.get_ticks()
        if begingame == True and xl > 0 and jifen.jifen < ai_setting.win:
            jack.jack_move(background, spirits, gems, nests)
            jack.jack_attack(attack)

            jifen.blit_me(screen)

            ai_xin.js_xl(xl)

            frame_spirit = re.spirit_refresh(screen, spirits, frame_spirit)
            frame_gem = re.gem_refresh(screen, gems, frame_gem)
            nests.update()
            frame_treehole_spirit = re.treehole_spirit_refresh(
                screen, spirits, frame_treehole_spirit, nest.spirit_treehole_x,
                nest.spirit_treehole_y)

            for the_spirit in spirits:
                if pygame.sprite.spritecollideany(jack.game_jack,
                                                  the_spirit.spirit_group):
                    if not jack.wd:
                        if pygame.time.get_ticks() > frame_koxie:
                            frame_koxie = pygame.time.get_ticks()
                            xl_2 = xl
                            xl -= 1
                            if xl_2 > xl:
                                jack_ss = pygame.mixer.Sound(
                                    "music/jack_ss.wav")
                                jack_ss.set_volume(1)  #调节音量
                                jack_ss.play(0)
                            ai_xin.js_xl(
                                xl
                            )  #pygame.sprite.groupcollide(jack.jack_group,spirit.spirit_group,True,True) //碰撞消失
                            frame_koxie += 3000

                if pygame.sprite.groupcollide(attack.hyjq_group,
                                              the_spirit.spirit_group, True,
                                              True):
                    jifen.plus()
                if pygame.time.get_ticks() > frame_hyjq:
                    attack.hyjq_group.add(attack.game_hyjq)
                    frame_hyjq += 10000

                if pygame.sprite.groupcollide(attack.fq_group,
                                              the_spirit.spirit_group, False,
                                              True):
                    jifen.plus()
                if pygame.time.get_ticks() > frame_fq:
                    attack.fq_group.add(attack.game_fq)
                    frame_fq += 10000
            for gem in gems:
                f = True if xl <= 5 else False
                if pygame.sprite.groupcollide(jack.jack_group,
                                              gem.gem_group_xlbs1, False,
                                              f) and xl <= 5:
                    xl += 1
                elif pygame.sprite.groupcollide(jack.jack_group,
                                                gem.gem_group_xlbs2, False,
                                                f) and xl <= 5:
                    xl = xl + 1 if xl == 5 else xl + 2
        # gf.check_event()

        # gf.update_screen(screen,jack)

        elif xl == 0:
            if end == True:
                pygame.mixer.music.load("music/end_music.wav")
                pygame.mixer.music.play(-1, 0)
            end = False

            # pygame.mixer.music.stop()
            kai_shi.jieshu()
        elif jifen.jifen >= ai_setting.win:
            if win == True:
                pygame.mixer.music.load("music/win_music.wav")
                pygame.mixer.music.play(-1, 0)
            win = False

            kai_shi.shengli()

        pygame.display.flip()
Esempio n. 4
0
class FloppyBird:
    """main game class"""
    def __init__(self):
        """constructor"""
        pygame.init()

        self.screen_width = 1300
        self.screen_height = 700
        pygame.display.set_caption('FloppyBird')
        self.screen = pygame.display.set_mode(
            (self.screen_width, self.screen_height))
        self.delta_time = 0
        self.fps_limit = 60
        self.game_speed = 8

        self.run = True
        self.jump = False
        self.debug = False
        self.anglebool = True
        # intro BS
        self.intro = True
        self.button_height, self.button_width = 50, 250
        self.floppyfont = pygame.font.Font('freesansbold.ttf', 128)
        self.title = self.floppyfont.render('Floppy Bird', True, (255, 255, 0))
        self.title_rect = self.title.get_rect()
        self.title_rect.center = (self.screen_width // 2, 150)

        self.pipegroup = pygame.sprite.Group()

        self.clock = pygame.time.Clock()
        self.bird = Bird(self.screen)
        self.background = Background(self.screen, self.screen_width,
                                     self.game_speed)
        self.point_font = pygame.font.Font('freesansbold.ttf', 32)
        self.points = 0
        self.score = self.point_font.render(str(self.points), True, (0, 0, 0))
        self.score_rect = self.score.get_rect()
        self.score_rect.center = (self.screen_width // 2, 50)

        self.bird_grav = 4
        self.bird_max_grav = 8
        self.bird_jump_height = 15
        # pygame.display.set_icon(pygame.image.load())
        self.flopdabird()

    def flopdabird(self):
        """Function which is responsible for running the game"""
        self.clock.tick(self.fps_limit)
        while self.intro:
            self.screen.fill((0, 0, 0))
            self.draw_introBS()
            pygame.display.flip()
            self.eventmanager()
        while self.run:
            self.update()
            self.draw()
            self.pipe_controls()
            self.eventmanager()
            self.movement()
            self.pointcalc()
            if not self.debug:
                self.collision()
            pygame.display.flip()
            self.delta_time = self.clock.tick(self.fps_limit) / 1000.0
            # print(self.delta_time)
        pygame.quit()

    def draw_introBS(self):
        pygame.draw.rect(
            self.screen, (255, 0, 0),
            ((self.screen_width // 2) - (self.button_width // 2),
             (self.screen_height // 2) -
             (self.button_height // 2), self.button_width, self.button_height))
        self.screen.blit(self.title, self.title_rect)

    def eventmanager(self):
        """handles events"""
        space_pressed = False
        if not self.intro:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.run = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        space_pressed = True

            if space_pressed:
                self.bird_grav = -self.bird_jump_height
        elif self.intro:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.run = False
                    self.intro = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        self.run = False
                        self.intro = False
                # if event.type == pygame.MOUSEBUTTONUP:
                #     if event.button == 1:
                #         if
        if space_pressed:
            self.bird_grav = -self.bird_jump_height

    def update(self):
        self.background.update(self.delta_time)

    def pointcalc(self):
        if self.pipegroup.sprites()[0].rect.left == self.bird.rect.left:
            self.points += 1
            self.score = self.point_font.render(str(self.points), True,
                                                (0, 0, 0))

    def collision(self):
        if pygame.sprite.spritecollide(self.bird, self.pipegroup, 0):
            self.run = False

    def movement(self):
        """movement logic"""
        if self.bird_grav < self.bird_max_grav:
            self.bird_grav += 1
        elif self.bird_grav > self.bird_max_grav:
            self.bird_grav = self.bird_max_grav

        self.bird.rect.bottom += self.bird_grav

        # end game if bird hits ground
        if self.bird.rect.bottom >= self.screen_height:
            self.bird.rect.bottom = self.screen_height
            self.run = False

        # don't allow bird to go past top of screen
        if self.bird.rect.top <= 0:
            self.bird.rect.top = 0

    def draw(self):
        """handles drawing"""

        self.background.blitme()

        if self.bird_grav < 0:
            self.screen.blit(self.bird.imageup, self.bird.rect)
        elif self.bird_grav == 0:
            self.screen.blit(self.bird.image, self.bird.rect)
        elif self.bird_grav > 0:
            self.screen.blit(self.bird.imagedown, self.bird.rect)

        for pipe in self.pipegroup.sprites():
            pipe.draw()
        self.screen.blit(self.score, self.score_rect)

    def getrandomcolor(self):
        """pick a color at random from the colors list"""

        colors = [(255, 0, 0), (255, 128, 0), (255, 255, 0), (128, 255, 0),
                  (0, 255, 0), (0, 255, 128), (0, 255, 255), (0, 128, 255),
                  (0, 0, 255), (128, 0, 255), (255, 0, 255), (255, 0, 128)]
        return colors[random.randint(0, len(colors) - 1)]

    def pipe_controls(self):
        """create, remove, and move the pipes"""

        pipe_width = 85

        for pipe in self.pipegroup.sprites():
            pipe.rect.left -= self.game_speed

        for pipe in self.pipegroup.sprites():
            if pipe.rect.left <= -pipe_width:
                self.pipegroup.remove(pipe)

        if len(self.pipegroup.sprites()) == 2:
            if self.pipegroup.sprites()[0].rect.left <= self.screen_width // 2:
                # for adjusting pipes
                space = random.randint(200, 300)
                top_height = random.randint(50, self.screen_height - space)

                # no need to modify below
                color = self.getrandomcolor()
                x = self.screen_width
                bottom_y = top_height + space
                bottom_height = self.screen_height - top_height - space
                top_rect = pygame.Rect(x, 0, pipe_width, top_height)
                bottom_rect = pygame.Rect(x, bottom_y, pipe_width,
                                          bottom_height)
                self.pipegroup.add(Pipes(self.screen, top_rect, color))
                self.pipegroup.add(Pipes(self.screen, bottom_rect, color))
        elif not self.pipegroup.sprites():
            # for adjusting pipes
            space = random.randint(200, 300)
            top_height = random.randint(50, self.screen_height - space)

            # no need to modify below
            color = self.getrandomcolor()
            x = self.screen_width
            bottom_y = top_height + space
            bottom_height = self.screen_height - top_height - space
            top_rect = pygame.Rect(x, 0, pipe_width, top_height)
            bottom_rect = pygame.Rect(x, bottom_y, pipe_width, bottom_height)
            self.pipegroup.add(Pipes(self.screen, top_rect, color))
            self.pipegroup.add(Pipes(self.screen, bottom_rect, color))
def run_game():
    # Initialize pygame, settings, and create a screen object
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Space Potaters")

    # Make buttons
    play_button = Button(ai_settings, screen, 'play')
    high_score_button = Button(ai_settings, screen, 'hs')
    back_button = Button(ai_settings, screen, 'back')

    # Create background
    background = Background(ai_settings, screen)

    # Create instance to store game states and create scoreboard
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    #  Make a ship.
    ship = Ship(ai_settings, screen)

    # Make group to store bullets
    bullets = Group()
    # Make alien group
    aliens = Group()
    # Made group to store alien bullets
    a_bullets = Group()

    # Create alien fleet
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # Create timer
    timer = Timer(ai_settings)

    # create ufo
    ufo = Ufo(ai_settings, screen)

    # Create explosion group
    kabooms = Group()

    # Make a bunker
    bunker = Group()

    background.blitme()

    # start main game loop
    while True:
        gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
                        aliens, bullets, a_bullets, background, bunker,
                        high_score_button, back_button)

        if stats.game_active:
            timer.update()
            ship.update()
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
                              bullets, a_bullets, bunker, kabooms, ufo)
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
                             bullets, a_bullets, bunker, timer)
            gf.update_a_bullets(ai_settings, screen, stats, sb, ship, aliens,
                                bullets, a_bullets, bunker, timer)

        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
                         a_bullets, play_button, timer, bunker, background,
                         high_score_button, back_button, kabooms, ufo)
Esempio n. 6
0
class FloppyBird:
    """main game class"""
    def __init__(self, genomes, networks, ge):
        """constructor"""
        pygame.init()

        self.screen_width = 1300
        self.screen_height = 700
        pygame.display.set_caption('FloppyBird')
        self.screen = pygame.display.set_mode(
            (self.screen_width, self.screen_height))
        self.delta_time = 0
        self.fps_limit = 60
        self.game_speed = 8

        self.run = True
        self.jump = False
        self.debug = False
        self.anglebool = True
        # intro BS
        self.intro = False
        self.button_height, self.button_width = 50, 250
        self.floppyfont = pygame.font.Font('freesansbold.ttf', 128)
        self.title = self.floppyfont.render('Floppy Bird', True, (255, 255, 0))
        self.title_rect = self.title.get_rect()
        self.title_rect.center = (self.screen_width // 2, 150)

        self.pipegroup = []

        self.clock = pygame.time.Clock()
        self.birds = []
        self.background = Background(self.screen, self.screen_width,
                                     self.game_speed)
        self.point_font = pygame.font.Font('freesansbold.ttf', 32)
        self.points = 0
        self.score = self.point_font.render(str(self.points), True, (0, 0, 0))
        self.score_rect = self.score.get_rect()
        self.score_rect.center = (self.screen_width // 2, 50)
        '''for AI'''
        self.nets = networks
        self.genomes = genomes
        self.ge = ge

        for _, g in self.genomes:
            self.birds.append(Bird(self.screen))

        # pygame.display.set_icon(pygame.image.load())
        self.flopdabird()

    def flopdabird(self):
        """Function which is responsible for running the game"""
        self.clock.tick(self.fps_limit)
        while self.intro:
            self.screen.fill((0, 0, 0))
            self.draw_introBS()
            pygame.display.flip()
            self.eventmanager()
        while self.run:
            self.update()
            self.draw()
            self.pipe_controls()
            self.eventmanager()
            self.movement()
            self.pointcalc()
            if not self.debug:
                self.collision()
            pygame.display.flip()
            self.delta_time = self.clock.tick(self.fps_limit) / 1000.0
            # print(self.delta_time)
        # pygame.quit()

    def draw_introBS(self):
        pygame.draw.rect(
            self.screen, (255, 0, 0),
            ((self.screen_width // 2) - (self.button_width // 2),
             (self.screen_height // 2) -
             (self.button_height // 2), self.button_width, self.button_height))
        self.screen.blit(self.title, self.title_rect)

    def eventmanager(self):
        """handles events"""
        space_pressed = False
        if not self.intro:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.run = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        space_pressed = True

            if space_pressed:
                for bird in self.birds:
                    bird.jump()
        elif self.intro:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.run = False
                    self.intro = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        self.run = False
                        self.intro = False
                # if event.type == pygame.MOUSEBUTTONUP:
                #     if event.button == 1:
                #         if
        if space_pressed:
            for bird in self.birds:
                bird.jump()

    def update(self):
        self.background.update(self.delta_time)

    def pointcalc(self):

        for i, bird in enumerate(self.birds):
            if self.pipegroup[0].x == bird.rect.left:
                self.ge[i].fitness += 5
                bird.score += 1
        score_list = [bird.score for bird in self.birds]
        self.score = self.point_font.render(
            str(score_list and max(score_list)), True, (0, 0, 0))

    def collision(self):
        for pipe in self.pipegroup:
            for i, bird in enumerate(self.birds):
                if pipe.collide(bird, bird.gravity):
                    self.ge[i].fitness -= 1
                    self.birds.pop(i)
                    self.nets.pop(i)

                    print('collision triggered')
                    #FloppyBird()

    def movement(self):
        """movement logic"""
        pipe_ind = 0
        if len(self.birds) > 0:
            if len(self.pipegroup
                   ) > 0 and self.birds[0].rect.left > self.pipegroup[
                       0].x + self.pipegroup[0].PIPE_TOP.get_width():
                pipe_ind = 1
        else:
            self.run = False
        for i, bird in enumerate(self.birds):
            bird.move()
            self.ge[i].fitness += 0.1
            bird_x, bird_y = bird.get_xy()
            output = self.nets[i].activate(
                (np.log(bird_y),
                 np.log(abs(bird_y - self.pipegroup[pipe_ind].height)),
                 np.log(abs(bird_y - self.pipegroup[pipe_ind].bottom))))
            if output[0] > 0.5:
                bird.jump()
            # end game if bird hits ground
            if bird.rect.bottom >= self.screen_height:
                self.birds.pop(i)
                self.ge.pop(i)
                self.nets.pop(i)
            if bird.rect.top <= 0:
                self.birds.pop(i)
                self.ge.pop(i)
                self.nets.pop(i)

    def draw(self):
        """handles drawing"""

        self.background.blitme()
        for bird in self.birds:
            bird.draw(self.screen)

        if self.pipegroup:
            for pipe in self.pipegroup:
                pipe.draw(self.screen)
        self.screen.blit(self.score, self.score_rect)

    def getrandomcolor(self):
        """pick a color at random from the colors list"""

        colors = [(255, 0, 0), (255, 128, 0), (255, 255, 0), (128, 255, 0),
                  (0, 255, 0), (0, 255, 128), (0, 255, 255), (0, 128, 255),
                  (0, 0, 255), (128, 0, 255), (255, 0, 255), (255, 0, 128)]
        return colors[random.randint(0, len(colors) - 1)]

    def pipe_controls(self):
        """create, remove, and move the pipes"""

        pipe_width = 105

        for pipe in self.pipegroup:
            pipe.move(self.game_speed)

        for pipe in self.pipegroup:
            if pipe.x <= -pipe_width:
                self.pipegroup.remove(pipe)

        if len(self.pipegroup) == 2:
            if self.pipegroup[1].x <= self.screen_width // 2:
                self.pipegroup.append(Pipes(self.screen))

        elif not self.pipegroup:
            # for adjusting pipes
            space = random.randint(200, 300)
            top_height = random.randint(50, self.screen_height - space)

            # no need to modify below
            color = self.getrandomcolor()
            x = self.screen_width
            bottom_y = top_height + space
            bottom_height = self.screen_height - top_height - space
            for i in range(0, 2):
                self.pipegroup.append(Pipes(self.screen))
                self.pipegroup[i].x = self.screen_width + i * (
                    self.screen_width // 2)