diff --git a/Rulebased.py b/Rulebased.py index 5dff514..06820f0 100644 --- a/Rulebased.py +++ b/Rulebased.py @@ -4,39 +4,29 @@ import cv2 as cv import numpy as np -# code for locating objects on the screen in super mario bros -# by Lauren Gee +# Rule-Based Agent implementation for Gym Super Mario Bros World 1 Level 1 + +# 23599356 Sersang Ngedup +# 22957747 Kirsty Straiton # Template matching is based on this tutorial: # https://docs.opencv.org/4.x/d4/dc6/tutorial_py_template_matching.html -################################################################################ - #Screen Height, width and match threshold constants SCREEN_HEIGHT = 240 SCREEN_WIDTH = 256 MATCH_THRESHOLD = 0.9 -################################################################################ # TEMPLATES FOR LOCATING OBJECTS # ignore sky blue colour when matching templates MASK_COLOUR = np.array([252, 136, 104]) -# (these numbers are [BLUE, GREEN, RED] because opencv uses BGR colour format by default) - -# You can add more images to improve the object locator, so that it can locate -# more things. For best results, paint around the object with the exact shade of -# blue as the sky colour. (see the given images as examples) -# -# Put your image filenames in image_files below, following the same format, and -# it should work fine. # filenames for object templates image_files = { "mario": { "small": ["marioA.png", "marioB.png", "marioC.png", "marioD.png", "marioE.png", "marioF.png", "marioG.png"], - "tall": ["tall_marioA.png", "tall_marioB.png", "tall_marioC.png"], }, "enemy": { "goomba": ["goomba.png"], @@ -46,12 +36,8 @@ "block": ["block1.png", "block3.png"], "floorblock":["block2.png"], "stair":["block4.png"], - "question_block": ["questionA.png", "questionB.png", "questionC.png"], "pipe": ["pipe_upper_section.png", "pipe_lower_section.png"], }, - "item": { - "mushroom": ["mushroom_red.png"], - } } def _get_template(filename): @@ -109,7 +95,6 @@ def _locate_object(screen, templates, stop_early=False, threshold=MATCH_THRESHOL if stop_early and locations: break - # [((x,y), (width,height))] return [( loc, locations[loc]) for loc in locations] def _locate_pipe(screen, threshold=MATCH_THRESHOLD): @@ -187,9 +172,12 @@ def make_action(screen, info, step, env, prev_action): # List of locations of blocks, pipes, etc: block_locations = object_locations["block"] + ################Above code was written by Lauren Gee and modified by us########################## -################Below code is our rule based agent########################## + + +################ Below code is our rule based agent ########################## #DEFAULT ACTION action = 3 @@ -198,13 +186,11 @@ def make_action(screen, info, step, env, prev_action): ##Code for jumping according to enemy locations if len(enemy_locations)>0: for enemy in enemy_locations: - if enemy[0][0]>mario_locations[0][0][0]: + if enemy[0][0]>mario_locations[0][0][0]: ##dont need this if(enemy[0][0]-mario_locations[0][0][0]<40) and (enemy[0][1]>=180) and mario_locations[0][0][0] <= enemy[0][0]: - print(enemy) - print(mario_locations) action=4 - #-------------------PIPE CODE-------------------# + #-------------------PIPE CODE------------------------------# pipeindex = 0 pipelist = [] pipe = False @@ -216,11 +202,9 @@ def make_action(screen, info, step, env, prev_action): #Code for jumping with pipe if pipe: for pipeindex in pipelist: - # print("pipe", pipeindex, block_locations[pipeindex]) if block_locations[pipeindex][0][0] 0: - action = 4 + for stair in stairs: + if stair[0][1] == 192 and stair[0][0]>mario_locations[0][0][0]: + if(stair[0][0]-mario_locations[0][0][0]<40) and mario_locations[0][0][0] <= stair[0][0]: + action=4 + if step % 20 == 0: if prev_action == 4: action = 0 @@ -263,7 +250,7 @@ def make_action(screen, info, step, env, prev_action): #Stops mario from flying off screen when he's at the top of the stairs if mario_locations[0][0][1] <= 66: action = 3 - print(action) + return action ####################Implementing the rule based agent#################################