def __init__(self, scenario): """Create a BeeBot.""" # Initial position of the BeeBot in terms of square on the Board. self.start_logical_position = Point( scenario.get_beebot_start_position()) # The amount the BeeBot moves. self.step = scenario.get_board_step() # The BeeBot's position of the BeeBot in terms of pixels. self.screen_location = self.start_logical_position.scale(self.step) # The BeeBot's position of the BeeBot in terms of the Board. self.logical_position = self.start_logical_position.copy() # Read the sprite and assume it is the "NORTH" sprite. self.original_sprite = scenario.get_beebot_sprite() self.sprite = self.original_sprite # Which way is the BeeBot facing. self.start_heading = scenario.get_beebot_heading() self.heading = self.start_heading # Rotate sprite based on start heading if self.start_heading == Heading.EAST: self.sprite = self.rotate(self.sprite, 270) elif self.start_heading == Heading.WEST: self.sprite = self.rotate(self.sprite, 90) elif self.start_heading == Heading.SOUTH: self.sprite = self.rotate(self.sprite, 180) # set fail sprite from scenario self.fail_sprite = scenario.get_beebot_fail_sprite() # Store MOVE_BEEBOT_* events here. self.memory = []
from src.Point import Point BaseImgPath = "img/" imgNumberOfEnemies = { "1b.png": 3, "2b.png": 2, "3b.png": 1, "5a.png": 1, "8a.png": 1, "9a.png": 8, "10a.png": 5 } imgCentersOfEnemies = { "1b.png": [Point(88, 372), Point(256, 329), Point(295, 310)], "2b.png": [Point(1086, 331), Point(1214, 267)], "3b.png": [Point(670, 582)], "5a.png": [Point(1129, 229)] } creatureMode_OffOn = { "1a.png": "1b.png", "2a.png": "2b.png", "3a.png": "3b.png" } tacticalMode_Off_On = { "6a.png": "6b.png",
def test_create_vector(self): p1=Point(1,1) p2=Point(2,2) v=Vector.create_vector_from_2points(p1,p2) self.assertEquals(v.X,1) self.assertEquals(v.Y,1)
class BeeBot(pygame.sprite.Sprite): """This class defines the BeeBot.""" def __init__(self, scenario): """Create a BeeBot.""" # Initial position of the BeeBot in terms of square on the Board. self.start_logical_position = Point( scenario.get_beebot_start_position()) # The amount the BeeBot moves. self.step = scenario.get_board_step() # The BeeBot's position of the BeeBot in terms of pixels. self.screen_location = self.start_logical_position.scale(self.step) # The BeeBot's position of the BeeBot in terms of the Board. self.logical_position = self.start_logical_position.copy() # Read the sprite and assume it is the "NORTH" sprite. self.original_sprite = scenario.get_beebot_sprite() self.sprite = self.original_sprite # Which way is the BeeBot facing. self.start_heading = scenario.get_beebot_heading() self.heading = self.start_heading # Rotate sprite based on start heading if self.start_heading == Heading.EAST: self.sprite = self.rotate(self.sprite, 270) elif self.start_heading == Heading.WEST: self.sprite = self.rotate(self.sprite, 90) elif self.start_heading == Heading.SOUTH: self.sprite = self.rotate(self.sprite, 180) # set fail sprite from scenario self.fail_sprite = scenario.get_beebot_fail_sprite() # Store MOVE_BEEBOT_* events here. self.memory = [] def move(self, event): """Move the BeeBot.""" if event.type == CustomEvent.MOVE_BEEBOT_UP: self.move_forward() sleep(0.5) # wait to simulate BeeBot movement if event.type == CustomEvent.MOVE_BEEBOT_DOWN: self.move_backward() sleep(0.5) # wait to simulate BeeBot movement if event.type == CustomEvent.MOVE_BEEBOT_LEFT: self.move_left() sleep(0.5) # wait to simulate BeeBot movement if event.type == CustomEvent.MOVE_BEEBOT_RIGHT: self.move_right() sleep(0.5) # wait to simulate BeeBot movement def add_to_memory(self, event): """Add "button press" to the BeeBot's "memory".""" self.memory.append(event) def push_out_memory(self): """Act out the instructions in the BeeBot's "memory".""" for instruction in self.memory: # we use pygame's event queue because we need to check # it regularly to prevent the game from crashing. pygame.event.post(instruction) def clear_memory(self): """Clear the BeeBot's "memory".""" self.memory = [] def display(self, screen): """Display the BeeBot on screen.""" screen.blit(self.sprite, (self.screen_location.x, self.screen_location.y)) def move_backward(self): """Move the BeeBot backward.""" # The origin is in the top left corner if self.heading == Heading.SOUTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y - 1 elif self.heading == Heading.WEST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x + 1 elif self.heading == Heading.NORTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y + 1 elif self.heading == Heading.EAST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x - 1 def move_forward(self): """Move the BeeBot forward.""" # The origin is in the top left corner if self.heading == Heading.NORTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y - 1 elif self.heading == Heading.EAST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x + 1 elif self.heading == Heading.SOUTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y + 1 elif self.heading == Heading.WEST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x - 1 def move_right(self): """Turn the BeeBot right.""" # No need to move the BeeBot, just change it's sprite. sleep(0.5) # Take a copy of the sprite old_sprite = self.sprite # Rotate the copy, 1 degree more every time for i in range(0, 90): self.sprite = self.rotate(old_sprite, -(i + 1)) sleep(0.01) if self.heading == Heading.NORTH: self.heading = Heading.EAST elif self.heading == Heading.EAST: self.heading = Heading.SOUTH elif self.heading == Heading.SOUTH: self.heading = Heading.WEST elif self.heading == Heading.WEST: self.heading = Heading.NORTH sleep(0.5) def move_left(self): """Turn the BeeBot left.""" # No need to move the BeeBot, just change it's sprite. sleep(0.5) # Take a copy of the sprite old_sprite = self.sprite # Rotate the copy, 1 degree more every time for i in range(0, 90): self.sprite = self.rotate(old_sprite, i + 1) sleep(0.01) if self.heading == Heading.NORTH: self.heading = Heading.WEST elif self.heading == Heading.EAST: self.heading = Heading.NORTH elif self.heading == Heading.SOUTH: self.heading = Heading.EAST elif self.heading == Heading.WEST: self.heading = Heading.SOUTH sleep(0.5) def reset_position(self): """Reset the position of the BeeBot.""" # Initial position of the BeeBot in terms of square on the Board. self.logical_position = self.start_logical_position.copy() self.heading = self.start_heading # The BeeBot's position in terms of pixels. self.screen_location = self.start_logical_position.scale(self.step) # Reset BeeBot sprite self.sprite = self.original_sprite # From the original sprite, rotate to the start_heading if self.start_heading == Heading.EAST: self.sprite = self.rotate(self.sprite, 270) elif self.start_heading == Heading.WEST: self.sprite = self.rotate(self.sprite, 90) elif self.start_heading == Heading.SOUTH: self.sprite = self.rotate(self.sprite, 180) self.heading = self.start_heading def crash(self): """Set the sprite to be displayed to the fail sprite.""" self.sprite = self.fail_sprite @classmethod def rotate(cls, image, angle): """Rotate given image by given angle.""" image_copy = image.copy() # this seems to stop thread errors orig_rect = image_copy.get_rect() rot_image = pygame.transform.rotate(image_copy, angle) rot_rect = orig_rect.copy() rot_rect.center = rot_image.get_rect().center return rot_image.subsurface(rot_rect)
def __init__(self, x1, y1, x2, y2): self.top_left = Point(x1, y1) self.bottom_right = Point(x2, y2)
def RandomTerrain(n_people, size=DEFAULT_SIZE, exits=DEFAULT_EXITS): dimRows, dimCols = size _map = [[0 for _ in range(dimCols)] for _ in range(dimRows)] # 1) to make sure at least MAX_NUMBER_OF_PEOPLE places are free totalPlaces = dimCols * dimRows totalPlaces -= MAX_NUMBER_OF_PEOPLE totalPlaces -= len(DEFAULT_EXITS) #GENERATING OBSTACLES print("GENERATING OBSTACLES...") obstaclesList = [] stopGeneratingObstacles = False for i in range(dimRows): if stopGeneratingObstacles: break for j in range(dimCols): # 2) to make sure at least MAX_NUMBER_OF_PEOPLE places are free if totalPlaces - MAX_OBSTACLE_HEIGHT * MAX_OBSTACLE_WIDTH <= MAX_NUMBER_OF_PEOPLE: stopGeneratingObstacles = True break if Terrain.__isPlaceOk( _map, i, j) and Terrain.__isThereObstacleSurrounding( _map, i, j) == False: makeANewObstacleProbability = random.random() if makeANewObstacleProbability > 0.90: sizeRow = random.randint(1, MAX_OBSTACLE_HEIGHT) sizeCols = random.randint(1, MAX_OBSTACLE_WIDTH) for r in range(i, min(i + sizeRow, dimRows)): for c in range(j, min(j + sizeCols, dimCols)): if Terrain.__isPlaceOk(_map, r, c): entryRow, entryCol = i, j if r == i and Terrain.__isThereObstacleUp( _map, r, c): sizeCols = abs(c - j) break if Terrain.__isThereObstacleRight( _map, r, c): sizeCols = abs(c - j) break _map[r][c] = OBSTACLE_IDENTIFIER totalPlaces -= 1 leaveRow, leaveCol = r, c #print(entryRow,entryCol , leaveRow , leaveCol) obstaclesList.append( Obstacle(entryRow, entryCol, leaveRow, leaveCol)) #for obstacle in obstaclesList : # print (obstacle) for j in range(0, 128): _map[255][j] = EMPTY_IDENTIFIER _map[256][j] = EMPTY_IDENTIFIER for i in range(0, 512): _map[i][63] = EMPTY_IDENTIFIER _map[i][64] = EMPTY_IDENTIFIER print("GENERATING OBSTACLES COMPLETE!") print("GENERATING PEOPLE...") #Generating People totalNumberOfPeople = 2**n_people PeopleSpawned = 0 emptyPlaces = [] peopleList = [] for i in range(dimRows): for j in range(dimCols): if _map[i][j] == EMPTY_IDENTIFIER: emptyPlaces.append(Point(i, j)) while PeopleSpawned < totalNumberOfPeople: randomEmptyPlaceIndex = random.randint(0, len(emptyPlaces) - 1) if _map[emptyPlaces[randomEmptyPlaceIndex].x][ emptyPlaces[randomEmptyPlaceIndex].y] == 0: #PeopleSpawned +1 represents the id of the person _map[emptyPlaces[randomEmptyPlaceIndex].x][ emptyPlaces[randomEmptyPlaceIndex]. y] = PERSON_IDENTIFIER # PeopleSpawned + 1 PeopleSpawned += 1 peopleList.append( Person(PeopleSpawned, emptyPlaces[randomEmptyPlaceIndex])) print("GENERATING PEOPLE COMPLETE!") for exit in exits: _map[exit.x][exit.y] = EXIT_IDENTIFIER #for p in peopleList: # print(p) #now we have SIZE, list of obstacles, list of exists, list of people, and the map return Terrain(obstaclesList, peopleList, _map, size, exits) # a = np.array(_map) '''fig, ax = plt.subplots()
class BeeBot(pygame.sprite.Sprite): """This class defines the BeeBot.""" def __init__(self, scenario): """Create a BeeBot.""" # Initial position of the BeeBot in terms of square on the Board. self.start_logical_position = Point( scenario.get_beebot_start_position()) # The amount the BeeBot moves. self.step = scenario.get_board_step() # The BeeBot's position of the BeeBot in terms of pixels. self.screen_location = self.start_logical_position.scale(self.step) # The BeeBot's position of the BeeBot in terms of the Board. self.logical_position = self.start_logical_position.copy() # Read the sprite and assume it is the "NORTH" sprite. self.original_sprite = scenario.get_beebot_sprite() self.sprite = self.original_sprite # Which way is the BeeBot facing. self.start_heading = scenario.get_beebot_heading() self.heading = self.start_heading # Rotate sprite based on start heading if self.start_heading == Heading.EAST: self.sprite = self.rotate(self.sprite, 270) elif self.start_heading == Heading.WEST: self.sprite = self.rotate(self.sprite, 90) elif self.start_heading == Heading.SOUTH: self.sprite = self.rotate(self.sprite, 180) # set fail sprite from scenario self.fail_sprite = scenario.get_beebot_fail_sprite() # Store MOVE_BEEBOT_* events here. self.memory = [] def move(self, event): """Move the BeeBot.""" if event.type == CustomEvent.MOVE_BEEBOT_UP: self.move_forward() sleep(0.5) # wait to simulate BeeBot movement if event.type == CustomEvent.MOVE_BEEBOT_DOWN: self.move_backward() sleep(0.5) # wait to simulate BeeBot movement if event.type == CustomEvent.MOVE_BEEBOT_LEFT: self.move_left() sleep(0.5) # wait to simulate BeeBot movement if event.type == CustomEvent.MOVE_BEEBOT_RIGHT: self.move_right() sleep(0.5) # wait to simulate BeeBot movement def add_to_memory(self, event): """Add "button press" to the BeeBot's "memory".""" self.memory.append(event) def push_out_memory(self): """Act out the instructions in the BeeBot's "memory".""" for instruction in self.memory: # we use pygame's event queue because we need to check # it regularly to prevent the game from crashing. pygame.event.post(instruction) def clear_memory(self): """Clear the BeeBot's "memory".""" self.memory = [] def display(self, screen): """Display the BeeBot on screen.""" screen.blit(self.sprite, (self.screen_location.x, self.screen_location.y)) def move_backward(self): """Move the BeeBot backward.""" # The origin is in the top left corner if self.heading == Heading.SOUTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y - 1 elif self.heading == Heading.WEST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x + 1 elif self.heading == Heading.NORTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y + 1 elif self.heading == Heading.EAST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x - 1 def move_forward(self): """Move the BeeBot forward.""" # The origin is in the top left corner if self.heading == Heading.NORTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y - 1 elif self.heading == Heading.EAST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x + 1 elif self.heading == Heading.SOUTH: for _ in range(0, self.step): self.screen_location.y = self.screen_location.y + 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.y = self.logical_position.y + 1 elif self.heading == Heading.WEST: for _ in range(0, self.step): self.screen_location.x = self.screen_location.x - 1 sleep(0.01) # sleep prevents the BeeBot moving too quickly self.logical_position.x = self.logical_position.x - 1 def move_right(self): """Turn the BeeBot right.""" # No need to move the BeeBot, just change it's sprite. sleep(0.5) # Take a copy of the sprite old_sprite = self.sprite # Rotate the copy, 1 degree more every time for i in range(0, 90): self.sprite = self.rotate(old_sprite, -(i+1)) sleep(0.01) if self.heading == Heading.NORTH: self.heading = Heading.EAST elif self.heading == Heading.EAST: self.heading = Heading.SOUTH elif self.heading == Heading.SOUTH: self.heading = Heading.WEST elif self.heading == Heading.WEST: self.heading = Heading.NORTH sleep(0.5) def move_left(self): """Turn the BeeBot left.""" # No need to move the BeeBot, just change it's sprite. sleep(0.5) # Take a copy of the sprite old_sprite = self.sprite # Rotate the copy, 1 degree more every time for i in range(0, 90): self.sprite = self.rotate(old_sprite, i+1) sleep(0.01) if self.heading == Heading.NORTH: self.heading = Heading.WEST elif self.heading == Heading.EAST: self.heading = Heading.NORTH elif self.heading == Heading.SOUTH: self.heading = Heading.EAST elif self.heading == Heading.WEST: self.heading = Heading.SOUTH sleep(0.5) def reset_position(self): """Reset the position of the BeeBot.""" # Initial position of the BeeBot in terms of square on the Board. self.logical_position = self.start_logical_position.copy() self.heading = self.start_heading # The BeeBot's position in terms of pixels. self.screen_location = self.start_logical_position.scale(self.step) # Reset BeeBot sprite self.sprite = self.original_sprite # From the original sprite, rotate to the start_heading if self.start_heading == Heading.EAST: self.sprite = self.rotate(self.sprite, 270) elif self.start_heading == Heading.WEST: self.sprite = self.rotate(self.sprite, 90) elif self.start_heading == Heading.SOUTH: self.sprite = self.rotate(self.sprite, 180) self.heading = self.start_heading def crash(self): """Set the sprite to be displayed to the fail sprite.""" self.sprite = self.fail_sprite @classmethod def rotate(cls, image, angle): """Rotate given image by given angle.""" image_copy = image.copy() # this seems to stop thread errors orig_rect = image_copy.get_rect() rot_image = pygame.transform.rotate(image_copy, angle) rot_rect = orig_rect.copy() rot_rect.center = rot_image.get_rect().center return rot_image.subsurface(rot_rect)
def test_init(cls): """Test the init method of the Goal class.""" _unused_goal = Goal([None], Point(0, 0), 0)
def setUp(self): """Create a simple Goal for testing.""" self.test_goal = Goal(["A", "B"], Point(0, 0), 0)
def collision(b1, b2): q1 = Point(b1.x + b1.vx, b1.y + b1.vy) q2 = Point(b2.x + b2.vx, b2.y + b2.vy) return dointersect(b1, q1, b2, q2)
def test_init(cls): """Test the init method of the Component class.""" _unused_component = Component([None], Point(0, 0), 0)
def setUp(self): """Create a simple Component for testing.""" self.test_component = Component(["A", "B"], Point(0, 0), 0)