This game is about a ball randomly moving on the screen, can be manipulated with a finger to make a ball, if two balls collide together, it means a lost, very simple game
Define two Uiimageview and a Start button in storyboard
and link the two Uiimageview with the code.
@property (weak, nonatomic)iboutlet uiimageview *player;
@property (weak, nonatomic)iboutlet uiimageview *enemy;
@property (weak, nonatomic)iboutlet UIButton *start;
Define the rate of ball movement in Viewdidload
pos =cgpointmake(5.0,4.0);
If you click the Start button
-(Ibaction) Start: (ID) Sender {[Self.start sethidden:yes]; Start a timer and let player this ball move randomly randommain = [Nstimer scheduledtimerwithtimeinterval:0.03 target:self selector: @selector ( OnTimer) Userinfo:nil Repeats:yes]; }-(void) ontimer{[self checkcollision]; Self.enemy.center = Cgpointmake (self.enemy.center.x+pos.x, SELF.ENEMY.CENTER.Y+POS.Y); if (Self.enemy.center.x > | | self.enemy.center.x < 0) {pos.x =-pos.x; } if (Self.enemy.center.y > 480 | | self.enemy.center.y < 0) {pos.y =-pos.y; }}-(void) checkcollision{//detects if two balls have collided together if (Cgrectintersectsrect (Self.player.frame, Self.enemy.frame)) {[R Andommain invalidate]; [Self.start Sethidden:no]; CGRect frame = [Self.player frame]; frame.origin.x = 137.0f; FRAME.ORIGIN.Y = 326.0f; [Self.player Setframe:frame]; CGRect frame2 = [Self.enemy frame]; frame2.origin.x = 137.0f; Frame2.origin.y = 20.0f; [Self.enemy setframe:frame2]; Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Ball was catched" message:@ "you had to play again" delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:nil]; [Alert show]; }}
The code can be downloaded