简体   繁体   中英

- (void)drawRect:(CGRect)rect , draw a maze and detect its boundary

I'm trying to implement a maze game that will draw its own levels by a code developed on drawRect , I just need to know how to detect the boundary of the content of the image that will be drawn by the code,

I'm using an UIButton wich is draggle using this code:

- (void)viewDidLoad
{
    [super viewDidLoad];
     map = [[mapView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    [self.view addSubview:map];

    self.view.backgroundColor = [UIColor blackColor];
    meButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [meButton setTitle:@"Drag me!" forState:UIControlStateNormal];
    meButton.backgroundColor = [UIColor yellowColor];
    [meButton addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];

    meButton.frame = CGRectMake(100,100,50, 50);

    [self.view addSubview:meButton];
 }



- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:button] anyObject];

    CGPoint previousLocation = [touch previousLocationInView:button];
    CGPoint location = [touch locationInView:button];
    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

    // move button
    button.center = CGPointMake(button.center.x + delta_x,
                            button.center.y + delta_y);
    if(CGRectIntersectsRect(button.frame, map.frame)) {

        NSLog(@"CGRectIntersectsRect");

    }
}

and this code is for drawing the map(still not implemented correctly )

- (void)drawRect:(CGRect)rect;
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line

    CGContextBeginPath(context);

    CGContextMoveToPoint(context, 50.0, 50.0); //start point
    CGContextAddLineToPoint(context, 250.0, 100.0);
    CGContextAddLineToPoint(context, 250.0, 350.0);
    CGContextAddLineToPoint(context, 50.0, 350.0); // end path

    CGContextClosePath(context); // close path

    CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it

    CGContextStrokePath(context); // do actual stroking

    CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent
    CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left-hand corner
}

So you have two ways of really achieving this. The first, and in my opinion the harder, option is to test the pixels where you currently are in the maze and see if the color corresponds to a wall or not. There are some subtle issues that can crop up with this method that mean it's sub-optimal.

The better approach is to divide your screen up into a grid of tiles (say, 10x10px in size) and use this to hold a map of your maze (for example, using a two dimensional boolean array). That way you can simply look up the current tile you are at and see which directions of movement are allowable. This is a fairly standard approach for dealing with mazes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM