def do_missions(mission_tuple, base_coordinate=None):
    global controller
    timer = time.clock()
    while True:
        try:
            if time.clock() - timer > 300:
                restart_bluestacks()
            if mission_tuple is not MISSION_EXCEPTION:
                controller = Controller(get_screenshot())
                do_missions(MISSION_EXCEPTION)
            for m in mission_tuple:
                if mission(m, base_coordinate):
                    controller = Controller(get_screenshot())
        except BreakMissions:
            break
        except LoopDone:
            timer = time.clock()
            while True:
                controller = Controller(get_screenshot())
                if get_coordinates(SHOP_BUTTON):
                    break
                if time.clock() - timer > 20:
                    break
            trophies = trophy_recognizer.recognize()
            print('{} trophies'.format(trophies))
            if 0 < trophies < TROPHY_TARGET:
                break
        except GameException as game_exception:
            if game_exception.args[0] == EXCEPTION_FOR_EXCEPTION_LOOP:
                raise GameException(EXCEPTION_FOR_MAIN_LOOP)
            else:
                do_missions(MISSION_START_GAME)
                do_missions(mission_tuple, base_coordinate)
                break
Beispiel #2
0
def produce_army():
    global controller
    while True:
        controller = Controller(get_screenshot())
        barracks_coordinates = get_coordinates(BARRACKS_LV10)
        if len(barracks_coordinates) >= BARRACKS_NUMBER:
            break
    for coordinate in barracks_coordinates[:4]:
        do_missions(MISSION_PRODUCE_ARMY, [coordinate])
        controller.click_blank()
def produce_army():
    global controller
    while True:
        controller = Controller(get_screenshot())
        barracks_coordinates = get_coordinates(BARRACKS_LV10)
        if len(barracks_coordinates) >= BARRACKS_NUMBER:
            break
    for coordinate in barracks_coordinates[:4]:
        do_missions(MISSION_PRODUCE_ARMY, [coordinate])
        controller.click_blank()
Beispiel #4
0
def restart_bluestacks():
    global controller
    while get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        click_points_by_name(BLUESTACKS_EXIT_BUTTON)
    time.sleep(1)
    while not get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        if get_coordinates(START_BLUESTACKS):
            controller.click(get_coordinates(START_BLUESTACKS)[0], click_times=2)
        time.sleep(10)
    raise GameException(EXCEPTION_FOR_MAIN_LOOP)
Beispiel #5
0
    def __init__(self, port):
        self.zoom = 1
        self.disp = Display(self.zoom)

        #robot displays by default at 100 pixels square.
        #read robot is approx 26cm square
        #multiply default scaled distances by scale to get cm

        #internally the model works in default scaled pixels.  But it returns
        #cm on the UI and TCP interfaces.
        self.scale = 26.0 / 100
        timenow = time.time()
        self.robot = RobotModel(self.disp, 100, self.scale, timenow)
        self.world = WorldModel(self.disp, self.robot, timenow)
        self.world.add_default_walls()
        if (port != 0):
            self.controller = Controller(port, self.robot, 1)
        else:
            self.controller = Controller(55443, self.robot, 0)
        self.robot.set_controller(self.controller)
        self.robot.init_robot_posn(500, 350, 0)
        self.robot.set_ir_angles(0, 0)
        self.robot.stop()
        self.active = True
def restart_bluestacks():
    global controller
    while get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        click_points_by_name(BLUESTACKS_EXIT_BUTTON)
    time.sleep(1)
    while not get_coordinates(BLUESTACKS_EXIT_BUTTON):
        controller = Controller(get_screenshot())
        if get_coordinates(START_BLUESTACKS):
            controller.click(get_coordinates(START_BLUESTACKS)[0],
                             click_times=2)
        time.sleep(10)
    raise GameException(EXCEPTION_FOR_MAIN_LOOP)
Beispiel #7
0
class Simulator():
    def __init__(self, port):
        self.zoom = 1
        self.disp = Display(self.zoom)

        #robot displays by default at 100 pixels square.
        #read robot is approx 26cm square
        #multiply default scaled distances by scale to get cm

        #internally the model works in default scaled pixels.  But it returns
        #cm on the UI and TCP interfaces.
        self.scale = 26.0 / 100
        timenow = time.time()
        self.robot = RobotModel(self.disp, 100, self.scale, timenow)
        self.world = WorldModel(self.disp, self.robot, timenow)
        self.world.add_default_walls()
        if (port != 0):
            self.controller = Controller(port, self.robot, 1)
        else:
            self.controller = Controller(55443, self.robot, 0)
        self.robot.set_controller(self.controller)
        self.robot.init_robot_posn(500, 350, 0)
        self.robot.set_ir_angles(0, 0)
        self.robot.stop()
        self.active = True

    def poll_controller(self):
        cont_active = self.controller.poll()
        if cont_active:
            self.disp.set_active()

    def process_gui(self):
        self.active = self.disp.process_gui_events()

    def run(self):
        #local variables are fastest and this is the inner loop
        world = self.world
        robot = self.robot
        realtime = time.time()
        starttime = realtime
        cur_time = 0
        iter_time = 0
        sleep_time = 0.005
        try:
            while True:
                #five phases in this loop to allow more accurate position
                #updates than display updates

                #phase 1
                #first update after pygame's sleep
                realtime = time.time()
                if self.active:
                    self.robot.update_position()
                    self.world.check_wheel_drag()
                self.poll_controller()

                #phase 2
                #process the gui
                self.process_gui()
                cur_time = cur_time + 1
                world.set_time(realtime)
                x_offset, y_offset = robot.self_update(cur_time, realtime)
                if self.active:
                    self.world.check_wheel_drag()
                zoom = self.disp.zoom
                x_offset = x_offset - (500 / zoom)
                y_offset = y_offset - (350 / zoom)
                self.poll_controller()

                #phase 3
                #update sensors, update display
                if self.active:
                    world.sample_all_sensors()
                self.disp.update(x_offset, y_offset)
                if self.active:
                    self.robot.update_position()
                    self.world.check_wheel_drag()
                self.poll_controller()

                #phase 4
                #check collisions
                world.check_collision()
                if self.active:
                    self.robot.update_position()
                    self.world.check_wheel_drag()
                self.poll_controller()

                #phase 5
                #draw the screen
                world.redraw()
                if self.active:
                    self.robot.update_position()
                    self.world.check_wheel_drag()
                self.poll_controller()
                #may sleep in flip
                self.disp.flip()
        except KeyboardInterrupt:
            sys.exit()
Beispiel #8
0
from src.control import Controller

if __name__ == '__main__':
    Controller().run()
__author__ = 'CRVV'

import time

from src.control import Controller
from src.image_process import get_coordinates
from src.image_process import get_screenshot
from src.trophy import TrophyRecognizer
from src.resource import *

controller = Controller(get_screenshot())
trophy_recognizer = TrophyRecognizer()


def do_missions(mission_tuple, base_coordinate=None):
    global controller
    timer = time.clock()
    while True:
        try:
            if time.clock() - timer > 300:
                restart_bluestacks()
            if mission_tuple is not MISSION_EXCEPTION:
                controller = Controller(get_screenshot())
                do_missions(MISSION_EXCEPTION)
            for m in mission_tuple:
                if mission(m, base_coordinate):
                    controller = Controller(get_screenshot())
        except BreakMissions:
            break
        except LoopDone:
            timer = time.clock()