CGRectGetMaxY

本文详细介绍了UIKit坐标系统中CGRectGetMinX、CGRectGetMaxX、CGRectGetMinY和CGRectGetMaxY方法的功能。这些方法用于获取视图在其父视图中的边界位置,包括左、右、上、下的坐标值。

CGRectGetMaxY(View)

CGRectGetMinX方法的作用得到目前view在当前屏幕中相对于整个View的最小值(位于屏幕的最左边)

CGRectGetMaxX方法的作用得到目前view在当前屏幕中相对于整个View的最大值(位于屏幕的最右边)

CGRectGetMinY方法的作用得到目前view在当前屏幕中相对于整个View的最小值(位于屏幕的最上边)

CGRectGetMaxY方法的作用得到目前view在当前屏幕中相对于整个View的最大值(位于屏幕的最下边)

#import <Foundation/Foundation.h> #import <CoreGraphics/CoreGraphics.h> NS_ASSUME_NONNULL_BEGIN #pragma mark - Models /// 游戏方向枚举 typedef NS_ENUM(NSUInteger, SnakeDirection) { SnakeDirectionUp, SnakeDirectionDown, SnakeDirectionLeft, SnakeDirectionRight }; /// 游戏点模型 @interface Point : NSObject @property (nonatomic, assign) NSInteger x; @property (nonatomic, assign) NSInteger y; - (instancetype)initWithX:(NSInteger)x y:(NSInteger)y; + (instancetype)pointWithX:(NSInteger)x y:(NSInteger)y; @end @implementation Point - (instancetype)initWithX:(NSInteger)x y:(NSInteger)y { self = [super init]; if (self) { _x = x; _y = y; } return self; } + (instancetype)pointWithX:(NSInteger)x y:(NSInteger)y { return [[self alloc] initWithX:x y:y]; } - (BOOL)isEqual:(id)object { if (self == object) return YES; if (![object isKindOfClass:[Point class]]) return NO; Point *other = (Point *)object; return self.x == other.x && self.y == other.y; } - (NSUInteger)hash { return self.x * 31 + self.y; } - (NSString *)description { return [NSString stringWithFormat:@"(%ld, %ld)", (long)self.x, (long)self.y]; } @end /// 食物模型 @interface Food : NSObject @property (nonatomic, strong) Point *position; @end @implementation Food @end /// 蛇模型 @interface Snake : NSObject @property (nonatomic, strong) NSMutableArray<Point *> *body; @property (nonatomic, assign) SnakeDirection direction; @property (nonatomic, assign) BOOL hasEaten; - (void)move; - (void)changeDirection:(SnakeDirection)newDirection; - (void)grow; @end @implementation Snake - (instancetype)init { self = [super init]; if (self) { _body = [NSMutableArray array]; _direction = SnakeDirectionRight; _hasEaten = NO; // 初始化蛇身 [_body addObject:[Point pointWithX:5 y:5]]; [_body addObject:[Point pointWithX:4 y:5]]; [_body addObject:[Point pointWithX:3 y:5]]; } return self; } - (void)move { if (self.body.count == 0) return; Point *head = self.body.firstObject; NSInteger newX = head.x; NSInteger newY = head.y; switch (self.direction) { case SnakeDirectionUp: newY--; break; case SnakeDirectionDown: newY++; break; case SnakeDirectionLeft: newX--; break; case SnakeDirectionRight: newX++; break; } Point *newHead = [Point pointWithX:newX y:newY]; if (!self.hasEaten) { [self.body removeLastObject]; } else { self.hasEaten = NO; } [self.body insertObject:newHead atIndex:0]; } - (void)changeDirection:(SnakeDirection)newDirection { // 防止直接反向移动 if ((self.direction == SnakeDirectionUp && newDirection == SnakeDirectionDown) || (self.direction == SnakeDirectionDown && newDirection == SnakeDirectionUp) || (self.direction == SnakeDirectionLeft && newDirection == SnakeDirectionRight) || (self.direction == SnakeDirectionRight && newDirection == SnakeDirectionLeft)) { return; } self.direction = newDirection; } - (void)grow { self.hasEaten = YES; } - (BOOL)checkCollisionWithWall:(CGRect)wall { Point *head = self.body.firstObject; return head.x < CGRectGetMinX(wall) || head.x >= CGRectGetMaxX(wall) || head.y < CGRectGetMinY(wall) || head.y >= CGRectGetMaxY(wall); } - (BOOL)checkSelfCollision { Point *head = self.body.firstObject; for (NSInteger i = 1; i < self.body.count; i++) { Point *bodyPart = self.body[i]; if ([head isEqual:bodyPart]) { return YES; } } return NO; } @end #pragma mark - ViewModel /// 游戏状态枚举 typedef NS_ENUM(NSUInteger, GameState) { GameStateNotStarted, GameStatePlaying, GameStatePaused, GameStateOver }; /// 游戏视图模型 @interface GameViewModel : NSObject @property (nonatomic, assign) GameState gameState; @property (nonatomic, strong) Snake *snake; @property (nonatomic, strong) Food *food; @property (nonatomic, assign) CGRect gameArea; @property (nonatomic, assign) NSInteger score; @property (nonatomic, assign) NSInteger highScore; @property (nonatomic, copy, nullable) void (^onGameStateChanged)(GameState); @property (nonatomic, copy, nullable) void (^onScoreChanged)(NSInteger); @property (nonatomic, copy, nullable) void (^onSnakeMoved)(void); @property (nonatomic, copy, nullable) void (^onFoodGenerated)(void); - (void)startGame; - (void)pauseGame; - (void)resumeGame; - (void)resetGame; - (void)changeSnakeDirection:(SnakeDirection)direction; - (void)updateGame; @end @implementation GameViewModel - (instancetype)init { self = [super init]; if (self) { _gameState = GameStateNotStarted; _gameArea = CGRectMake(0, 0, 20, 20); _score = 0; _highScore = 0; [self setupGame]; } return self; } - (void)setupGame { self.snake = [[Snake alloc] init]; self.food = [[Food alloc] init]; [self generateFood]; } - (void)startGame { if (self.gameState == GameStateNotStarted || self.gameState == GameStateOver) { [self resetGame]; } self.gameState = GameStatePlaying; if (self.onGameStateChanged) { self.onGameStateChanged(self.gameState); } } - (void)pauseGame { if (self.gameState == GameStatePlaying) { self.gameState = GameStatePaused; if (self.onGameStateChanged) { self.onGameStateChanged(self.gameState); } } } - (void)resumeGame { if (self.gameState == GameStatePaused) { self.gameState = GameStatePlaying; if (self.onGameStateChanged) { self.onGameStateChanged(self.gameState); } } } - (void)resetGame { [self setupGame]; self.score = 0; if (self.onScoreChanged) { self.onScoreChanged(self.score); } } - (void)changeSnakeDirection:(SnakeDirection)direction { [self.snake changeDirection:direction]; } - (void)updateGame { if (self.gameState != GameStatePlaying) return; [self.snake move]; // 检查是否吃到食物 Point *snakeHead = self.snake.body.firstObject; if ([snakeHead isEqual:self.food.position]) { [self.snake grow]; self.score += 10; if (self.score > self.highScore) { self.highScore = self.score; } if (self.onScoreChanged) { self.onScoreChanged(self.score); } [self generateFood]; if (self.onFoodGenerated) { self.onFoodGenerated(); } } //
最新发布
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值