#import <Cocoa/Cocoa.h>#import "PaddleVIEw.h"#import "BallVIEw.h"@interface GameController : NSVIEw { PaddleVIEw *leftPaddle; PaddleVIEw *rightPaddle; BallVIEw * ball; CGPoint ballVeLocity; int gameState; int player1score; int player2score;}@property (retain) IBOutlet PaddleVIEw *leftPaddle;@property (retain) IBOutlet PaddleVIEw *rightPaddle;@property (retain) IBOutlet BallVIEw *ball;- (voID)reset:(BOol)newGame;@end
#import "GameController.h"#define GameStateRunning 1#define GameStatePause 2#define BallSpeedX 0.2#define BallSpeedY 0.3#define CompMoveSpeed 15#define scoreToWin 5@implementation GameController@synthesize leftPaddle,rightPaddle,ball;- (ID)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if(self) { gameState = GameStatePause; ballVeLocity = CGPointMake(BallSpeedX,BallSpeedY); [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES]; } return self;}- (voID)gameLoop { if(gameState == GameStateRunning) { [ball setFrameOrigin:CGPointMake(ball.frame.origin.x + ballVeLocity.x,ball.frame.origin.y + ballVeLocity.y)]; if(ball.frame.origin.x + 15 > self.frame.size.wIDth || ball.frame.origin.x < 0) { ballVeLocity.x =- ballVeLocity.x; } if(ball.frame.origin.y + 35 > self.frame.size.height || ball.frame.origin.y < 0) { ballVeLocity.y =- ballVeLocity.y; } } if(CGRectIntersectsRect(ball.frame,leftPaddle.frame)) { if(ball.frame.origin.x > leftPaddle.frame.origin.x) { ballVeLocity.x =- ballVeLocity.x; } } if(CGRectIntersectsRect(ball.frame,rightPaddle.frame)) { if(ball.frame.origin.x +15 > rightPaddle.frame.origin.x) { ballVeLocity.x =- ballVeLocity.x; } } if(ball.frame.origin.x <= self.frame.size.wIDth / 2) { if(ball.frame.origin.y < leftPaddle.frame.origin.y + 75 && leftPaddle.frame.origin.y > 0) { [leftPaddle setFrameOrigin:CGPointMake(leftPaddle.frame.origin.x,leftPaddle.frame.origin.y - CompMoveSpeed)]; } if(ball.frame.origin.y > leftPaddle.frame.origin.y +75 && leftPaddle.frame.origin.y < 700 - leftPaddle.frame.size.height ) { [leftPaddle setFrameOrigin:CGPointMake(leftPaddle.frame.origin.x,leftPaddle.frame.origin.y + CompMoveSpeed)]; } } if(ball.frame.origin.x <= 0) { player2score++; [self reset:(player2score >= scoreToWin)]; } if(ball.frame.origin.x + 15 > self.frame.size.wIDth) { player1score++; [self reset:(player1score >= scoreToWin)]; }}- (voID)reset:(BOol)newGame { gameState = GameStatePause; [ball setFrameOrigin:CGPointMake((self.frame.size.wIDth + 7.5) / 2,(self.frame.size.height + 7.5)/2)]; if(newGame) { if(player1score > player2score) { NSLog(@"Player 1 Wins!"); } else { NSLog(@"Player 2 Wins!"); } player1score = 0; player2score = 0; } else { NSLog(@"Press key to serve"); } NSLog(@"Player 1: %d",player1score); NSLog(@"Player 2: %d",player2score);}- (voID)moveRightPaddleUp { if(rightPaddle.frame.origin.y < 700 - rightPaddle.frame.size.height) { [rightPaddle setFrameOrigin:CGPointMake(rightPaddle.frame.origin.x,rightPaddle.frame.origin.y + 20)]; }}- (voID)moveRightPaddleDown { if(rightPaddle.frame.origin.y > 0) { [rightPaddle setFrameOrigin:CGPointMake(rightPaddle.frame.origin.x,rightPaddle.frame.origin.y - 20)]; }}- (BOol)acceptsFirstResponder { return YES;}- (voID)keyDown:(NSEvent *)theEvent { if ([theEvent modifIErFlags] & NSNumericPadKeyMask) { Nsstring *theArrow = [theEvent charactersIgnoringModifIErs]; unichar keyChar = 0; if ( [theArrow length] == 0 ) { return; // reject dead keys } if ( [theArrow length] == 1 ) { keyChar = [theArrow characteratIndex:0]; if ( keyChar == NSleftArrowFunctionKey ) { gameState = GameStateRunning; } if ( keyChar == NSRightArrowFunctionKey ) { } if ( keyChar == NSUpArrowFunctionKey ) { [self moveRightPaddleUp]; } if ( keyChar == NSDownArrowFunctionKey ) { [self moveRightPaddleDown]; } [super keyDown:theEvent]; } } else { [super keyDown:theEvent]; }}- (voID)drawRect:(NSRect)dirtyRect {}- (voID)dealloc { [ball release]; [rightPaddle release]; [leftPaddle release]; [super dealloc];}@end解决方法 如果您“消费”该事件,请不要将其传递给super. 总结
以上是内存溢出为你收集整理的objective-c – keyDown工作,但我发出哔哔声全部内容,希望文章能够帮你解决objective-c – keyDown工作,但我发出哔哔声所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)