Beispiel #1
0
##the transparent canvas for drawing the necessary geometric relationship.

##create some objects
clock = pygame.time.Clock()
car = player.Player()
cam = camera.Camera()
##create some objects

##create the spriteGroup contains objects
map_s = pygame.sprite.Group()
player_s = pygame.sprite.Group()
tracks_s = pygame.sprite.Group()
##create the spriteGroup contains objects

##some initalize tracks are points left  while driving
tracks.initialize()
cam.set_pos(car.x, car.y)
##some initalize tracks are points left  while driving

##add car
player_s.add(car)
##add car

##start angle from car
car.set_start_direction(90)
angle = 0  #the turning angle of the wheel
##start angle from car

#testcode for shell
#CENTER_X = 800
#CENTER_Y = 450
Beispiel #2
0

##create the spriteGroup contains objects
list_cone_yellow=[]
list_cone_blue=[]
list_path_point=[]
map_s= pygame.sprite.Group()
player_s= pygame.sprite.Group()
tracks_s= pygame.sprite.Group()
cone_s  = pygame.sprite.Group()
path_s  = pygame.sprite.Group()
##create the spriteGroup contains objects


##some initalize tracks are points left  while driving
tracks.initialize()
cam.set_pos(car.x, car.y)
##some initalize tracks are points left  while driving


##add car
player_s.add(car)
cone_s.add(coneb)
cone_s.add(coney)
path_s.add(startpoint)
list_cone_blue.append(coneb)
list_cone_yellow.append(coney)

list_path_point.append(startpoint)
##add car
Beispiel #3
0
def main():
    # initialize objects.
    clock = pygame.time.Clock()
    running = True
    font = pygame.font.Font(None, 24)
    car = player.Player()
    cam = camera.Camera()
    target = gamemode.Finish()
    bound_alert = boundary.Alert()
    time_alert = timeout.Alert()
    info = menu.Alert()
    pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2))
    # create sprite groups.
    map_s = pygame.sprite.Group()
    player_s = pygame.sprite.Group()
    traffic_s = pygame.sprite.Group()
    tracks_s = pygame.sprite.Group()
    target_s = pygame.sprite.Group()
    pointer_s = pygame.sprite.Group()
    timer_alert_s = pygame.sprite.Group()
    bound_alert_s = pygame.sprite.Group()
    menu_alert_s = pygame.sprite.Group()

    # generate tiles
    for tile_num in range(0, len(maps.map_tile)):
        maps.map_files.append(load_image(maps.map_tile[tile_num], False))
    for x in range(0, 10):
        for y in range(0, 10):
            map_s.add(maps.Map(maps.map_1[x][y], x * 1000, y * 1000, maps.map_1_rot[x][y]))

    # load tracks
    tracks.initialize()
    # load finish
    target_s.add(target)
    # load direction
    pointer_s.add(pointer)
    # load alerts
    timer_alert_s.add(time_alert)
    bound_alert_s.add(bound_alert)
    menu_alert_s.add(info)
    # load traffic
    traffic.initialize(CENTER_W, CENTER_H)
    for count in range(0, TRAFFIC_COUNT):
        traffic_s.add(traffic.Traffic())

    player_s.add(car)

    cam.set_pos(car.x, car.y)

    while running:
        # Check for key input. (KEYDOWN, trigger often)
        keys = pygame.key.get_pressed()
        # Render loop.

        # Check for menu/reset, (keyup event - trigger ONCE)
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if keys[K_m]:
                    if (info.visibility == True):
                        info.visibility = False

                if (keys[K_p]):
                    car.reset()
                    target.reset()
                if (keys[K_q]):
                    pygame.quit()
                    sys.exit(0)

            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False
                break


        if (target.timeleft > 0):
            if keys[K_c]:
                car.steerleft()
            if keys[K_b]:
                car.steerright()
            if keys[K_UP]:
                car.accelerate()
            else:
                car.soften()
            if keys[K_DOWN]:
                car.deaccelerate()
            if keys[K_RIGHT]:
                car.breakk()

        cam.set_pos(car.x, car.y)

        # Show text data.
        text_fps = font.render('FPS: ' + str(int(clock.get_fps())), 1, (224, 16, 16))
        textpos_fps = text_fps.get_rect(centery=25, centerx=60)

        text_score = font.render('Score: ' + str(target.score), 1, (224, 16, 16))
        textpos_score = text_fps.get_rect(centery=45, centerx=60)

        text_timer = font.render(
            'Timer: ' + str(int((target.timeleft /1000) / 1000)) + ":" + str(int((target.timeleft / 1500) % 300)), 6,
            (224, 16, 16))
        textpos_timer = text_fps.get_rect(centery=65, centerx=60)

        # Render Scene.
        screen.blit(background, (0, 0))

        # cam.set_pos(car.x, car.y)

        map_s.update(cam.x, cam.y)
        map_s.draw(screen)

        # Conditional renders/effects
        car.grass(screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g)
        if (car.tracks):
            tracks_s.add(tracks.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir))

        # Just render..
        tracks_s.update(cam.x, cam.y)
        tracks_s.draw(screen)

        player_s.update(cam.x, cam.y)
        player_s.draw(screen)

        traffic_s.update(cam.x, cam.y)
        traffic_s.draw(screen)

        target_s.update(cam.x, cam.y)
        target_s.draw(screen)

        pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x, target.y)
        pointer_s.draw(screen)

        # Conditional renders.
        if (boundary.breaking(car.x + CENTER_W, car.y + CENTER_H) == True):
            bound_alert_s.update()
            bound_alert_s.draw(screen)
        if (target.timeleft == 0):
            timer_alert_s.draw(screen)
            car.speed = 0
            text_score = font.render('Final Score: ' + str(target.score), 1, (224, 16, 16))
            textpos_score = text_fps.get_rect(centery=CENTER_H + 56, centerx=CENTER_W - 20)
        if (info.visibility == True):
            menu_alert_s.draw(screen)

        # Blit Blit..
        screen.blit(text_fps, textpos_fps)
        screen.blit(text_score, textpos_score)
        screen.blit(text_timer, textpos_timer)
        pygame.display.flip()

        # Check collision!!!
        if pygame.sprite.spritecollide(car, traffic_s, False):
            car.impact()
            target.car_crash()

        if pygame.sprite.spritecollide(car, target_s, True):
            target.claim_flag()
            target.generate_finish()
            target_s.add(target)

        clock.tick(64)
def main(id='001'):

    agentePontosGanho = AI_INSTANCES.Agente_Interativo(
        AI_CONFIG._L3_PARAMETRO_pontos, 'gte', gameControl.setVeiculosHardmode)
    agenteTempoGasto = AI_INSTANCES.Agente_Interativo(
        AI_CONFIG._L3_PARAMETRO_tempogasto, 'gte',
        gameControl.setExtraTimeHardmode)
    agenteChamadasAtendidas = AI_INSTANCES.Agente_Interativo(
        AI_CONFIG._L3_PARAMETRO_chamadas_atentidas, 'gte',
        gameControl.setPontosHardmode)
    agenteParadas = AI_INSTANCES.Agente_Interativo(
        AI_CONFIG._L3_PARAMETRO_paradas, 'gte', gameControl.setPontosHardmode)
    agenteEscrita = AI_INSTANCES.Agente_de_Escrita('./logs/' + id + '.txt', id,
                                                   3)

    agenteEscrita.escreveLog(['NOVOJOGO', str(0)])

    totalChamadasAtendidas = 0
    totalParadas = 0

    running = True
    # generate tiles
    for tile_num in range(0, len(maps.map_tile)):
        maps.map_files.append(load_image(maps.map_tile[tile_num], False))
    for x in range(0, 10):
        for y in range(0, 10):
            map_s.add(
                maps.Map(maps.map_1[x][y], x * 1000, y * 1000,
                         maps.map_1_rot[x][y]))

    # load tracks
    tracks.initialize()
    # load finish
    target_s.add(target)
    # load direction
    pointer_s.add(pointer)
    # load alerts
    timer_alert_s.add(time_alert)
    bound_alert_s.add(bound_alert)
    celular_s.add(celular_alert)
    stop_s.add(stop_alert)

    menu_alert_s.add(info)
    # load traffic
    traffic.initialize(CENTER_W, CENTER_H)

    for count in range(0, gameControl.quantidadeVeiculos):
        traffic_s.add(traffic.Traffic())

    player_s.add(car)

    cam.set_pos(car.x, car.y)

    end = False
    while running:
        # Render loop.
        # Check for menu/reset, (keyup event - trigger ONCE)
        keys = pygame.key.get_pressed()
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if keys[K_m]:
                    if (info.visibility == True):
                        info.visibility = False
                    else:
                        info.visibility = True
                if (keys[K_p]):
                    end = False
                    car.reset()
                    target.reset()
                if (keys[K_q]):
                    agenteEscrita.escreveLog(['SAIU', '-'])
                    pygame.quit()
                    sys.exit(0)
                if (keys[K_z]):  # atende ligacao
                    celular_alert.atender = True

            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                from source.menu import Menu as menu2
                aux = menu2()
                aux.main_menu()
                running = False
                agenteEscrita.escreveLog(['SAIU', '-'])
                break

        # Check for key input. (KEYDOWN, trigger often)
        if (target.timeleft > 0):
            if keys[K_LEFT]:
                car.steerleft()
            if keys[K_RIGHT]:
                car.steerright()
            if keys[K_UP]:
                car.accelerate()
            else:
                car.soften()
            if keys[K_DOWN]:
                car.deaccelerate()

            cam.set_pos(car.x, car.y)

        # Show text data.

        text_fps = font.render('', 1, (224, 16, 16))
        textpos_fps = text_fps.get_rect(centery=25, centerx=60)

        text_score = font.render('Pontuacao: ' + str(target.score), 1,
                                 (224, 16, 16))
        textpos_score = text_fps.get_rect(centery=45, centerx=60)

        text_timer = font.render(
            'Tempo: ' + str(int((target.timeleft / 60) / 60)) + ":" +
            str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16))
        textpos_timer = text_fps.get_rect(centery=65, centerx=60)

        # Render Scene.
        screen.blit(background, (0, 0))

        # cam.set_pos(car.x, car.y)

        map_s.update(cam.x, cam.y)
        map_s.draw(screen)

        # Conditional renders/effects
        car.grass(screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g)
        tracks_s = []
        if car.tracks:
            tracks_s.add(
                tracks.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir))

        # Just render..
        tracks_s.update(cam.x, cam.y)
        tracks_s.draw(screen)

        player_s.update(cam.x, cam.y)
        player_s.draw(screen)

        traffic_s.update(cam.x, cam.y)
        traffic_s.draw(screen)

        target_s.update(cam.x, cam.y)
        target_s.draw(screen)

        pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x,
                         target.y)
        pointer_s.draw(screen)

        # stop_alert.updateTimer(time.time())
        # Conditional renders.
        if (bounds.breaking(car.x + CENTER_W, car.y + CENTER_H) == True):
            bound_alert_s.update()
            bound_alert_s.draw(screen)

        if (target.timeleft == 0):
            timer_alert_s.draw(screen)
            car.speed = 0
            text_score = font.render('Pontuacao Final: ' + str(target.score),
                                     1, (224, 16, 16))
            textpos_score = text_fps.get_rect(centery=CENTER_H,
                                              centerx=CENTER_W - 100)
            if end is False:
                end = True
                gameControl.extraTimeValue = AI_CONFIG._L3_tempo_extra_facil
                gameControl.quantidadeVeiculos = AI_CONFIG._L3_quantidade_veiculos_facil
                gameControl.pontosChamadaAtendida = AI_CONFIG._L3_ponto_atender_facil
                gameControl.pontosPararCarro = AI_CONFIG._L3_ponto_parar_facil
                agenteEscrita.escreveLog(['TERMINOU', str(target.score)])

        celular_alert.grass(
            screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g,
            car.speed)
        if (target.totalTime > 300 and int(
            (target.totalTime / 60) % 60) % 20 == 0):
            celular_alert.visibility = True
            celular_alert.startTimer()
        if (celular_alert.tocando):
            celular_alert.chron.run()

        if (target.timeleft > 0 and celular_alert.visibility is True
                and celular_alert.tocando is True):
            celular_s.draw(screen)
            keys = pygame.key.get_pressed()
            if (keys[K_c] and car.speed > 0.4):
                celular_alert.visibility = False
                celular_alert.tocando = False
                target.score -= 70
                agenteEscrita.escreveLog(['ATENDEUERRADO', -70])
            if (celular_alert.visibility is True
                    and celular_alert.tocando is True and keys[K_c]
                    and car.speed <= 0.4):
                celular_alert.visibility = False
                celular_alert.tocando = False
                target.score += gameControl.pontosChamadaAtendida
                agenteEscrita.escreveLog(['ATENDEU', 200])
                agenteChamadasAtendidas.analizaEntrada(totalChamadasAtendidas)
            if (not celular_alert.cel_time()):
                celular_alert.visibility = False
                celular_alert.tocando = False
                target.score -= 100
                agenteEscrita.escreveLog(['NAOATENDEU', -100])

        stop_alert.stop_car(car.speed)
        if (target.totalTime != 0 and int(
            (target.totalTime / 60) % 60) % 15 == 0 and car.speed >= 0.1):
            stop_alert.visibility = True

        if (target.timeleft > 0 and stop_alert.visibility is True):
            stop_s.draw(screen)
            if (car.speed < 0.1):
                target.score += 200
                stop_alert.visibility = False
                agenteEscrita.escreveLog(['PAROU', 200])
                agenteParadas.analizaEntrada(totalParadas)
            else:
                target.score -= 1

        if (info.visibility == True):
            menu_alert_s.draw(screen)

        # Blit Blit..
        pygame.draw.rect(screen, (216, 216, 216), [40, 30, 200, 55])
        screen.blit(text_fps, textpos_fps)
        screen.blit(text_score, textpos_score)
        screen.blit(text_timer, textpos_timer)
        pygame.display.flip()

        # Check collision!!!
        if pygame.sprite.spritecollide(car, traffic_s, False):
            car.impact()
            target.car_crash()
            target.score -= 5
            agenteEscrita.escreveLog(['BATIDA', '-5'])

        if pygame.sprite.spritecollide(car, target_s, True):
            target.claim_flag(gameControl.extraTimeValue)
            target.score += 50
            target.generate_finish()
            target_s.add(target)
            agenteEscrita.escreveLog(
                ['TEMPOEXTRA', gameControl.extraTimeValue])

        agentePontosGanho.analizaEntrada(target.score)
        agenteTempoGasto.analizaEntrada(target.totalTime)
        clock.tick(64)


# cria semaforo

# Enter the mainloop.
# main()

# pygame.quit()
# sys.exit(0)
Beispiel #5
0
    def run(self):

        # generate tiles
        for tile_num in range(0, len(maps.map_tile)):
            maps.map_files.append(load_image(maps.map_tile[tile_num], False))
        for x in range(0, 10):
            for y in range(0, 10):
                self.map_s.add(
                    maps.Map(maps.map_1[x][y], x * 1000, y * 1000,
                             maps.map_1_rot[x][y]))

        # load tracks
        tracks.initialize()
        # load finish
        self.target_s.add(self.target)
        # load direction
        self.pointer_s.add(self.pointer)
        # load alerts
        self.timer_alert_s.add(self.time_alert)
        self.bound_alert_s.add(self.bound_alert)
        self.celular_s.add(self.celular_alert)
        self.stop_s.add(self.stop_alert)

        self.menu_alert_s.add(self.info)
        # load traffic
        traffic.initialize(CENTER_W, CENTER_H)
        for count in range(0, self.quantidadeVeiculos):
            self.traffic_s.add(traffic.Traffic())

        self.player_s.add(self.car)

        self.cam.set_pos(self.car.x, self.car.y)

        while self.running:
            # Render loop.
            # Check for menu/reset, (keyup event - trigger ONCE)
            for event in pygame.event.get():
                if event.type == pygame.KEYUP:
                    if keys[K_m]:
                        if (self.info.visibility == True):
                            self.info.visibility = False
                        else:
                            self.info.visibility = True
                    if (keys[K_p]):
                        self.car.reset()
                        self.target.reset()
                    if (keys[K_q]):
                        pygame.quit()
                        sys.exit(0)
                    if (keys[K_z]):  # atende ligacao
                        self.celular_alert.atender = True

                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    from source.menu import Menu as menu2
                    aux = menu2()
                    aux.main_menu()
                    running = False
                    break

            # Check for key input. (KEYDOWN, trigger often)
            keys = pygame.key.get_pressed()
            if (self.target.timeleft > 0):
                if keys[K_LEFT]:
                    self.car.steerleft()
                if keys[K_RIGHT]:
                    self.car.steerright()
                if keys[K_UP]:
                    self.car.accelerate()
                else:
                    self.car.soften()
                if keys[K_DOWN]:
                    self.car.deaccelerate()

                self.cam.set_pos(self.car.x, self.car.y)

            # Show text data.

            text_fps = self.font.render('', 1, (224, 16, 16))
            textpos_fps = text_fps.get_rect(centery=25, centerx=60)

            text_score = self.font.render(
                'Pontuacao: ' + str(self.target.score), 1, (224, 16, 16))
            textpos_score = text_fps.get_rect(centery=45, centerx=60)

            text_timer = self.font.render(
                'Tempo: ' + str(int((self.target.timeleft / 60) / 60)) + ":" +
                str(int((self.target.timeleft / 60) % 60)), 1, (224, 16, 16))
            textpos_timer = text_fps.get_rect(centery=65, centerx=60)

            # Render Scene.
            self.screen.blit(self.background, (0, 0))

            # cam.set_pos(car.x, car.y)

            self.map_s.update(self.cam.x, self.cam.y)
            self.map_s.draw(self.screen)

            # Conditional renders/effects
            self.car.grass(
                self.screen.get_at(
                    ((int(self.CENTER_W - 5), int(self.CENTER_H - 5)))).g)
            if self.car.tracks:
                self.tracks_s.add(
                    tracks.Track(self.cam.x + self.CENTER_W,
                                 self.cam.y + self.CENTER_H, self.car.dir))

            # Just render..
            self.tracks_s.update(self.cam.x, self.cam.y)
            self.tracks_s.draw(self.screen)

            self.player_s.update(self.cam.x, self.cam.y)
            self.player_s.draw(self.screen)

            self.traffic_s.update(self.cam.x, self.cam.y)
            self.traffic_s.draw(self.screen)

            self.target_s.update(self.cam.x, self.cam.y)
            self.target_s.draw(self.screen)

            self.pointer_s.update(self.car.x + self.CENTER_W,
                                  self.car.y + self.CENTER_H, self.target.x,
                                  self.target.y)
            self.pointer_s.draw(self.screen)

            # stop_alert.updateTimer(time.time())
            # Conditional renders.
            if (bounds.breaking(self.car.x + self.CENTER_W,
                                self.car.y + self.CENTER_H) == True):
                self.bound_alert_s.update()
                self.bound_alert_s.draw(self.screen)

            if (self.target.timeleft == 0):
                self.timer_alert_s.draw(self.screen)
                self.car.speed = 0
                text_score = self.font.render(
                    'Pontuacao Final: ' + str(self.target.score), 1,
                    (224, 16, 16))
                textpos_score = text_fps.get_rect(centery=self.CENTER_H + 56,
                                                  centerx=self.CENTER_W - 20)

                self.celular_alert.grass(
                    self.screen.get_at(
                        ((int(self.CENTER_W - 5), int(self.CENTER_H - 5)))).g,
                    self.car.speed)
            if (int((self.target.timeleft / 60) % 60) % 20 == 0):
                self.celular_alert.visibility = True
                self.celular_alert.startTimer()
            if (self.celular_alert.tocando):
                self.celular_alert.chron.run()

            if (self.target.timeleft > 0
                    and self.celular_alert.visibility is True):
                self.celular_s.draw(self.screen)
                keys = pygame.key.get_pressed()
                if (keys[K_c] and self.car.speed > 0.4):
                    self.target.score -= 50
                    self.celular_alert.tocando = False
                    self.celular_alert.visibility = False
                if (keys[K_c] and self.car.speed <= 0.4):
                    self.target.score += 200
                    self.celular_alert.tocando = False
                    self.celular_alert.visibility = False
                if (not self.celular_alert.cel_time()):
                    self.target.score -= 100
                    self.celular_alert.tocando = False
                    self.celular_alert.visibility = False

                self.stop_alert.stop_car(self.car.speed)
            if (int((self.target.timeleft / 60) % 60) % 15 == 0
                    and self.car.speed >= 0.1):
                self.stop_alert.visibility = True

            if (self.target.timeleft > 0
                    and self.stop_alert.visibility is True):
                self.stop_s.draw(self.screen)
                if (self.car.speed < 0.1):
                    self.target.score += 200
                    self.stop_alert.visibility = False
                else:
                    self.target.score -= 1

            if (self.info.visibility == True):
                self.menu_alert_s.draw(self.screen)

            # Blit Blit..
            pygame.draw.rect(self.screen, (216, 216, 216), [40, 30, 200, 55])
            self.screen.blit(text_fps, textpos_fps)
            self.screen.blit(text_score, textpos_score)
            self.screen.blit(text_timer, textpos_timer)
            pygame.display.flip()

            # Check collision!!!
            if pygame.sprite.spritecollide(self.car, self.traffic_s, False):
                self.car.impact()
                self.target.car_crash()
                self.target.score -= 5

            if pygame.sprite.spritecollide(self.car, self.target_s, True):
                self.target.claim_flag()
                self.target.score += 50
                self.target.generate_finish()
                self.target_s.add(self.target)

            self.clock.tick(64)

    # cria semaforo

    # initialization
        pygame.init()
        print pygame.display.Info().current_w
        print pygame.display.Info().current_h
        '''
        screen = pygame.display.set_mode((pygame.display.Info().current_w,
                                          pygame.display.Info().current_h),
                                          pygame.FULLSCREEN)
        '''
        self.screen = pygame.display.set_mode((1024, 768), pygame.FULLSCREEN)

        pygame.display.set_caption('Race of Math.')
        pygame.mouse.set_visible(True)
        self.font = pygame.font.Font(None, 24)
        # CENTER_W = int(1024 / 2)
        # CENTER_H = int(768 / 2)
        '''
        CENTER_W =  int(pygame.display.Info().current_w /2)
        CENTER_H =  int(pygame.display.Info().current_h /2)
        '''

        # new background surface
        self.background = pygame.Surface(self.screen.get_size())
        self.background = self.background.convert_alpha()
        self.background.fill((26, 26, 26))
Beispiel #6
0
def main():
#initialize objects.
    clock = pygame.time.Clock()
    running = True
    font = pygame.font.Font(None, 24)
    car = player.Player()
    cam = camera.Camera()
    target = gamemode.Finish()
    bound_alert = bounds.Alert()
    time_alert = timeout.Alert()
    info = menu.Alert()
    pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2))
#create sprite groups.
    map_s     = pygame.sprite.Group()
    player_s  = pygame.sprite.Group()
    traffic_s = pygame.sprite.Group()
    tracks_s  = pygame.sprite.Group()
    target_s  = pygame.sprite.Group()
    pointer_s = pygame.sprite.Group()
    timer_alert_s = pygame.sprite.Group()
    bound_alert_s = pygame.sprite.Group()
    menu_alert_s = pygame.sprite.Group()

#generate tiles
    for tile_num in range (0, len(maps.map_tile)):
        maps.map_files.append(load_image(maps.map_tile[tile_num], False))
    for x in range (0, 10):
        for y in range (0, 10):
            map_s.add(maps.Map(maps.map_1[x][y], x * 1000, y * 1000, maps.map_1_rot[x][y]))

#load tracks
    tracks.initialize()
#load finish
    target_s.add(target)
#load direction
    pointer_s.add(pointer)
#load alerts
    timer_alert_s.add(time_alert)
    bound_alert_s.add(bound_alert)
    menu_alert_s.add(info)
#load traffic
    traffic.initialize(CENTER_W, CENTER_H)
    for count in range(0, TRAFFIC_COUNT):
        traffic_s.add(traffic.Traffic())

    player_s.add(car)

    cam.set_pos(car.x, car.y)

    while running:
#Render loop.

#Check for menu/reset, (keyup event - trigger ONCE)
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if keys[K_m]:
                    if (info.visibility == True):
                        info.visibility = False
                    else:
                        info.visibility = True
                if (keys[K_p]):
                    car.reset()
                    target.reset()
                if (keys[K_q]):
                    pygame.quit()
                    sys.exit(0)

            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False
                break

#Check for key input. (KEYDOWN, trigger often)
        keys = pygame.key.get_pressed()
        if (target.timeleft > 0):
            if keys[K_LEFT]:
                car.steerleft()
            if keys[K_RIGHT]:
                car.steerright()
            if keys[K_UP]:
                car.accelerate()
            else:
                car.soften()
            if keys[K_DOWN]:
                car.deaccelerate()

        cam.set_pos(car.x, car.y)

#Show text data.
        text_fps = font.render('FPS: ' + str(int(clock.get_fps())), 1, (224, 16, 16))
        textpos_fps = text_fps.get_rect(centery=25, centerx=60)

        text_score = font.render('Score: ' + str(target.score), 1, (224, 16, 16))
        textpos_score = text_fps.get_rect(centery=45, centerx=60)

        text_timer = font.render('Timer: ' + str(int((target.timeleft / 60)/60)) + ":" + str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16))
        textpos_timer = text_fps.get_rect(centery=65, centerx=60)

#Render Scene.
        screen.blit(background, (0,0))

        #cam.set_pos(car.x, car.y)

        map_s.update(cam.x, cam.y)
        map_s.draw(screen)
        
#Conditional renders/effects
        car.grass(screen.get_at(((int(CENTER_W-5), int(CENTER_H-5)))).g)
        if (car.tracks):
            tracks_s.add(tracks.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir))

#Just render..
        tracks_s.update(cam.x, cam.y)
        tracks_s.draw(screen)
        
        player_s.update(cam.x, cam.y)
        player_s.draw(screen)

        traffic_s.update(cam.x, cam.y)
        traffic_s.draw(screen)

        target_s.update(cam.x, cam.y)
        target_s.draw(screen)

        pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x, target.y)
        pointer_s.draw(screen)

#Conditional renders.
        if (bounds.breaking(car.x+CENTER_W, car.y+CENTER_H) == True):
            bound_alert_s.update()
            bound_alert_s.draw(screen)
        if (target.timeleft == 0):
            timer_alert_s.draw(screen)
            car.speed = 0
            text_score = font.render('Final Score: ' + str(target.score), 1, (224, 16, 16))
            textpos_score = text_fps.get_rect(centery=CENTER_H+56, centerx=CENTER_W-20)
        if (info.visibility == True):
            menu_alert_s.draw(screen)
            
#Blit Blit..       
        screen.blit(text_fps, textpos_fps)
        screen.blit(text_score, textpos_score)
        screen.blit(text_timer, textpos_timer)
        pygame.display.flip()

#Check collision!!!
        if pygame.sprite.spritecollide(car, traffic_s, False):
            car.impact()
            target.car_crash()

        if pygame.sprite.spritecollide(car, target_s, True):
            target.claim_flag()
            target.generate_finish()
            target_s.add(target)
            
        clock.tick(64)
Beispiel #7
0
def main():
    #initialize objects.
    clock = pygame.time.Clock()
    running = True
    font = pygame.font.Font(None, 32)
    car = player.Player()
    cam = camera.Camera()
    target = gamemode.Finish()
    bound_alert = bounds.Alert()
    time_alert = timeout.Alert()
    celular_alert = leis_transito.CelularAlert()
    stop_alert = leis_transito.StopAlert()
    info = menu.Alert()
    pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2))

    #create sprite groups.
    map_s = pygame.sprite.Group()
    player_s = pygame.sprite.Group()
    celular_s = pygame.sprite.Group()
    stop_s = pygame.sprite.Group()
    traffic_s = pygame.sprite.Group()
    tracks_s = pygame.sprite.Group()
    target_s = pygame.sprite.Group()
    pointer_s = pygame.sprite.Group()
    timer_alert_s = pygame.sprite.Group()
    bound_alert_s = pygame.sprite.Group()
    menu_alert_s = pygame.sprite.Group()

    #generate tiles
    for tile_num in range(0, len(maps.map_tile)):
        maps.map_files.append(load_image(maps.map_tile[tile_num], False))
    for x in range(0, 10):
        for y in range(0, 10):
            map_s.add(
                maps.Map(maps.map_1[x][y], x * 1000, y * 1000,
                         maps.map_1_rot[x][y]))

#load tracks
    tracks.initialize()
    #load finish
    target_s.add(target)
    #load direction
    pointer_s.add(pointer)
    #load alerts
    timer_alert_s.add(time_alert)
    bound_alert_s.add(bound_alert)
    celular_s.add(celular_alert)
    stop_s.add(stop_alert)

    menu_alert_s.add(info)
    #load traffic
    traffic.initialize(CENTER_W, CENTER_H)
    for count in range(0, TRAFFIC_COUNT):
        traffic_s.add(traffic.Traffic())

    player_s.add(car)

    cam.set_pos(car.x, car.y)

    while running:
        #Render loop.

        #Check for menu/reset, (keyup event - trigger ONCE)
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if keys[K_m]:
                    if (info.visibility == True):
                        info.visibility = False
                    else:
                        info.visibility = True
                if (keys[K_p]):
                    car.reset()
                    target.reset()
                if (keys[K_q]):
                    pygame.quit()
                    sys.exit(0)
                if (keys[K_z]):  # atende ligacao
                    celular_alert.atender = True

            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:

                from source.menu import Menu as menu2
                aux = menu2()
                aux.main_menu()
                running = False
                break

#Check for key input. (KEYDOWN, trigger often)
        keys = pygame.key.get_pressed()
        if (target.timeleft > 0):
            if keys[K_LEFT]:
                car.steerleft()
            if keys[K_RIGHT]:
                car.steerright()
            if keys[K_UP]:
                car.accelerate()
            else:
                car.soften()
            if keys[K_DOWN]:
                car.deaccelerate()

        cam.set_pos(car.x, car.y)

        #Show text data.

        text_fps = font.render('', 1, (224, 16, 16))
        textpos_fps = text_fps.get_rect(centery=25, centerx=60)

        text_score = font.render('Pontuacao: ' + str(target.score), 1,
                                 (224, 16, 16))
        textpos_score = text_fps.get_rect(centery=45, centerx=60)

        text_timer = font.render(
            'Tempo: ' + str(int((target.timeleft / 60) / 60)) + ":" +
            str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16))
        textpos_timer = text_fps.get_rect(centery=65, centerx=60)

        #Render Scene.
        screen.blit(background, (0, 0))

        #cam.set_pos(car.x, car.y)

        map_s.update(cam.x, cam.y)
        map_s.draw(screen)

        #Conditional renders/effects
        car.grass(screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g)
        if car.tracks:
            tracks_s.add(
                tracks.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir))

#Just render..
        tracks_s.update(cam.x, cam.y)
        tracks_s.draw(screen)

        player_s.update(cam.x, cam.y)
        player_s.draw(screen)

        traffic_s.update(cam.x, cam.y)
        traffic_s.draw(screen)

        target_s.update(cam.x, cam.y)
        target_s.draw(screen)

        pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x,
                         target.y)
        pointer_s.draw(screen)

        # stop_alert.updateTimer(time.time())
        #Conditional renders.
        if (bounds.breaking(car.x + CENTER_W, car.y + CENTER_H) == True):
            bound_alert_s.update()
            bound_alert_s.draw(screen)

        if (target.timeleft == 0):
            timer_alert_s.draw(screen)
            car.speed = 0
            text_score = font.render('Pontuacao Final: ' + str(target.score),
                                     1, (224, 16, 16))
            textpos_score = text_fps.get_rect(centery=CENTER_H + 56,
                                              centerx=CENTER_W - 20)

        celular_alert.grass(
            screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g,
            car.speed)
        if (int((target.timeleft / 60) % 60) % 20 == 0):
            celular_alert.visibility = True

        if (target.timeleft > 0 and celular_alert.visibility is True):
            celular_s.draw(screen)
            keys = pygame.key.get_pressed()
            celular_alert.startTimer()
            if (keys[K_c] and car.speed > 0.4):
                target.score -= 15
                celular_alert.visibility = False
            if (keys[K_c] and car.speed <= 0.4):
                target.score += 200
                celular_alert.visibility = False
            if (not celular_alert.cel_time()):
                target.score -= 100
                celular_alert.visibility = False

        stop_alert.stop_car(car.speed)
        if (int((target.timeleft / 60) % 60) % 15 == 0 and car.speed >= 0.1):
            stop_alert.visibility = True

        if (target.timeleft > 0 and stop_alert.visibility is True):
            stop_s.draw(screen)
            if (car.speed < 0.1):
                target.score += 200
                stop_alert.visibility = False
            else:
                target.score -= 1

        if (info.visibility == True):
            menu_alert_s.draw(screen)

#Blit Blit..
        pygame.draw.rect(screen, (216, 216, 216), [40, 30, 200, 55])
        screen.blit(text_fps, textpos_fps)
        screen.blit(text_score, textpos_score)
        screen.blit(text_timer, textpos_timer)
        pygame.display.flip()

        #Check collision!!!
        if pygame.sprite.spritecollide(car, traffic_s, False):
            car.impact()
            target.car_crash()
            target.score -= 5

        if pygame.sprite.spritecollide(car, target_s, True):
            target.claim_flag()
            target.score += 150
            target.generate_finish()
            target_s.add(target)

        clock.tick(64)
Beispiel #8
0
def main():

    #database
    record_switch = False
    text_rank = []
    textpos_rank = []

    #start music
    pygame.mixer.music.load('./media/background.wav')
    pygame.mixer.music.play(-1)

    #initialize objects.
    clock = pygame.time.Clock()
    running = True
    font = pygame.font.Font(None, 32)
    car = player.Player()
    cam = camera.Camera()
    target = gamemode.Finish()
    item = []
    traffics = []
    bound_alert = bounds.Alert()
    time_alert = timeout.Alert()
    info = menu.Alert()
    pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2))
    #create sprite groups.
    map_s = pygame.sprite.Group()
    player_s = pygame.sprite.Group()
    traffic_s = pygame.sprite.Group()
    tracks_s = pygame.sprite.Group()
    target_s = pygame.sprite.Group()
    item_s = pygame.sprite.Group()
    pointer_s = pygame.sprite.Group()
    timer_alert_s = pygame.sprite.Group()
    bound_alert_s = pygame.sprite.Group()
    menu_alert_s = pygame.sprite.Group()

    #generate tiles     기본 맵 초원
    for tile_num in range(0, len(maps.map_tile[0])):
        maps.map_files.append(load_image(maps.map_tile[0][tile_num], False))
    for x in range(0, 10):
        for y in range(0, 10):
            map_s.add(
                maps.Map(maps.map_1[x][y], x * 1000, y * 1000,
                         maps.map_1_rot[x][y]))

#load tracks
    tracks.initialize()
    #load finish
    target_s.add(target)
    #load items
    items.initialize()
    for count in range(0, ITEM_COUNT):
        item.append(items.Items())
        item_s.add(item[count])
#load direction
    pointer_s.add(pointer)
    #load alerts
    timer_alert_s.add(time_alert)
    bound_alert_s.add(bound_alert)
    menu_alert_s.add(info)
    #load traffic
    traffic.initialize(CENTER_W, CENTER_H)
    for count in range(0, TRAFFIC_COUNT):
        traffics.append(traffic.Traffic())
        traffic_s.add(traffics[count])

    player_s.add(car)

    cam.set_pos(car.x, car.y)

    while running:
        #Render loop.

        #Check for menu/reset, (keyup event - trigger ONCE)
        for event in pygame.event.get():
            if event.type == pygame.KEYUP:
                if keys[K_m]:
                    if (info.visibility == True):
                        info.visibility = False
                    else:
                        info.visibility = True

                if info.visibility == True:
                    if (keys[K_p]):
                        car.reset()
                        target.reset()
                        for i in range(0, ITEM_COUNT):
                            item[i].reset
                        record_switch = False

                    if (keys[K_q]):
                        pygame.quit()
                        sys.exit(0)

                    if (keys[K_1]):
                        change_map(map_s, maps, 0)
                        car.reset()
                        target.reset()
                        for i in range(0, ITEM_COUNT):
                            item[i].reset

                    if (keys[K_2]):
                        change_map(map_s, maps, 1)
                        car.reset()
                        target.reset()
                        for i in range(0, ITEM_COUNT):
                            item[i].reset

                    if (keys[K_3]):
                        change_map(map_s, maps, 2)
                        car.reset()
                        target.reset()
                        for i in range(0, ITEM_COUNT):
                            item[i].reset

            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False
                break

#Check for key input. (KEYDOWN, trigger often)
        keys = pygame.key.get_pressed()
        if (target.timeleft > 0):
            if keys[K_LEFT]:
                car.steerleft()
            if keys[K_RIGHT]:
                car.steerright()
            if keys[K_UP]:
                car.accelerate()
            else:
                car.soften()
            if keys[K_DOWN]:
                car.deaccelerate()

        cam.set_pos(car.x, car.y)

        #Show text data.
        text_fps = font.render('FPS: ' + str(int(clock.get_fps())), 1,
                               (224, 16, 16))
        textpos_fps = text_fps.get_rect(centery=175, centerx=260)

        text_score = font.render('Score: ' + str(target.score), 1,
                                 (224, 16, 16))
        textpos_score = text_fps.get_rect(centery=205, centerx=260)

        text_timer = font.render(
            'Timer: ' + str(int((target.timeleft / 60) / 60)) + ":" +
            str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16))
        textpos_timer = text_fps.get_rect(centery=235, centerx=260)

        text_red_ball = font.render(
            'Red Ball: ' + str(int(car.red_timer / 64)), 1, (224, 16, 16))
        textpos_red_ball = text_fps.get_rect(centery=265, centerx=260)

        text_blue_ball = font.render(
            'Blue Ball: ' + str(int(car.blue_timer / 64)), 1, (224, 16, 16))
        textpos_blue_ball = text_fps.get_rect(centery=295, centerx=260)

        text_green_ball = font.render(
            'Green Ball: ' + str(int(car.green_timer / 64)), 1, (224, 16, 16))
        textpos_green_ball = text_fps.get_rect(centery=325, centerx=260)

        text_ranking = font.render('TOP 5 Ranking', 1, (16, 224, 16))
        textpos_ranking = text_fps.get_rect(centery=CENTER_H,
                                            centerx=CENTER_W - 20)

        text_boundtimer = font.render(
            'Game over After ' + str(int(bound_alert.timeleft / 64)) +
            ' Second.', 1, (224, 16, 16))
        textpos_boundtimer = text_fps.get_rect(centery=CENTER_H + 30,
                                               centerx=CENTER_W - 100)

        #Render Scene.
        screen.blit(background, (0, 0))

        #cam.set_pos(car.x, car.y)

        map_s.update(cam.x, cam.y)
        map_s.draw(screen)

        #Conditional renders/effects
        car.grass(screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g)
        if (car.tracks):
            tracks_s.add(
                tracks.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir))

#Just render..
        tracks_s.update(cam.x, cam.y)
        tracks_s.draw(screen)

        player_s.update(cam.x, cam.y)
        player_s.draw(screen)

        traffic_s.update(cam.x, cam.y)
        traffic_s.draw(screen)

        target_s.update(cam.x, cam.y)
        target_s.draw(screen)

        item_s.update(cam.x, cam.y)
        item_s.draw(screen)

        pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x,
                         target.y)
        pointer_s.draw(screen)

        #Conditional renders.

        if (bounds.breaking(car.x + CENTER_W, car.y + CENTER_H) == True):
            bound_alert_s.update()
            bound_alert_s.draw(screen)
            bound_alert.update()
            screen.blit(text_boundtimer, textpos_boundtimer)
            if (bound_alert.timeleft == 0):
                target.timeleft = 0

        else:
            bound_alert.timeleft = 640

        if (target.timeleft == 0):
            timer_alert_s.draw(screen)
            car.speed = 0
            text_score = font.render('Your Score: ' + str(target.score), 1,
                                     (224, 16, 16))
            textpos_score = text_fps.get_rect(centery=CENTER_H - 56,
                                              centerx=CENTER_W - 20)

            if record_switch == False:
                cur.execute("SELECT * From rank ORDER BY Point DESC")
                dataList = []
                dataList = cur.fetchall()
                rank = 0

                if len(dataList) == 0:
                    cur.execute("INSERT INTO rank VALUES(?,?)",
                                (1, target.score))

                elif len(dataList) < 5:
                    for i in dataList:
                        if i[1] < target.score:
                            rank = i[0]
                            break

                    if rank != 0:
                        cur.execute(
                            "UPDATE rank SET Rank=Rank+1 WHERE Point < %d" %
                            (target.score))
                        cur.execute("INSERT INTO rank VALUES(?,?)",
                                    (rank, target.score))

                    else:
                        cur.execute("INSERT INTO rank VALUES(?,?)",
                                    (len(dataList) + 1, target.score))

                elif len(dataList) >= 5:
                    for i in dataList:
                        if i[1] < target.score:
                            rank = i[0]
                            break

                    if rank != 0:
                        cur.execute(
                            "UPDATE rank SET Rank=Rank+1 WHERE Point<%d" %
                            (target.score))
                        cur.execute("INSERT INTO rank VALUES(?,?)",
                                    (rank, target.score))
                        cur.execute("DELETE FROM rank WHERE Rank > 5")

                con.commit()
                cur.execute("SELECT * From rank ORDER BY Rank ASC")
                dataList = cur.fetchall()

                cnt = 0

                text_rank.clear()
                textpos_rank.clear()

                for i in dataList:
                    cnt = cnt + 1
                    text_rank.append(
                        font.render(
                            str(i[0]) + '. ' + str(i[1]), 1, (16, 224, 16)))
                    textpos_rank.append(
                        text_fps.get_rect(centery=CENTER_H + 25 + cnt * 25,
                                          centerx=CENTER_W - 20))

                record_switch = True

        if (info.visibility == True):
            menu_alert_s.draw(screen)

#Blit Blit..
        screen.blit(text_fps, textpos_fps)
        screen.blit(text_score, textpos_score)
        screen.blit(text_timer, textpos_timer)
        screen.blit(text_red_ball, textpos_red_ball)
        screen.blit(text_blue_ball, textpos_blue_ball)
        screen.blit(text_green_ball, textpos_green_ball)
        if record_switch == True:

            screen.blit(text_ranking, textpos_ranking)
            for i in range(0, cnt):
                screen.blit(text_rank[i], textpos_rank[i])
        pygame.display.flip()

        #Check collision!!!

        #traffic 과의 충돌 확인. 블루볼 먹었을 시에는 충돌 없음
        for i in range(0, TRAFFIC_COUNT):
            if traffics[i].collision_check(car) == True:
                if car.blue_ball == True:
                    pygame.mixer.Sound.play(point_sound)
                    traffics[i].respawn()
                else:
                    car.impact()
                    target.car_crash()

        if pygame.sprite.spritecollide(car, target_s, True):
            pygame.mixer.Sound.play(point_sound)
            target.claim_flag()
            target.generate_finish()
            target_s.add(target)

        for i in range(0, ITEM_COUNT):
            if item[i].collision_check(car) == True:
                pygame.mixer.Sound.play(point_sound)
                car.get_item(item[i].color)
                item[i].generate_items()
                item_s.add(item[i])

        clock.tick(64)