標籤:
今天是練習日,通過幾天的系統學習,我對基礎的一些知識有了簡單的瞭解,今天用了一天的時間通過整理幾天所學到的東西,成功編寫了一個簡單的小遊戲。遊戲中幾乎用到了學到的所有控制項功能,Label,text,UiImageView,UiButton,NsTimer等。 今天我自己編寫的遊戲是實現一個角色圖片通過設定好的四個button 按鈕點擊控制它的移動,中間有三個障礙圖片,碰到障礙會減少血量,並且回到起始點。檢測碰撞的地方用到了NsTimer的知識,其中還有一個雷區,雷區裡通過問李國斌老師學會了新知識,[Image removeFromSubview];實現了把之前的產生圖片抹除。雖然是一個很簡單的小遊戲,雖然是一個很多BUG的遊戲,但是我還是很高興,畢竟是自己獨立編寫的下面是其中一段代碼:
//實現邊框左右的排布
for (int j=0; j<<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">700; j=j+367) {
for (int i=0; i<<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">500; i++) {
UILabel*biankuang2=[[UILabel alloc]initWithFrame:CGRectMake(j, i*10, 8, 8)];
if (i%3==0) {
biankuang2 .backgroundColor=[UIColor redColor];
}else if (i%3==1){
biankuang2 .backgroundColor=[UIColor blueColor];
}else{
biankuang2 .backgroundColor=[UIColor blackColor];
}
[self.view addSubview:biankuang2];
}
}
////////////////////////////////////////////////////////////////////////
// 下面的_time1是控制三個障礙圖片運動
_time1=[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(move1) userInfo:nilrepeats:YES];
// 下面的_time2時控制雷區出現地雷
_time2=[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(diLei)userInfo:nil repeats:YES];
//判斷是否相撞
_time3=[NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(crash)userInfo:nil repeats:YES];
}
///////////////////////////////////////跳出頁面程式
// 碰撞檢測
-(void)fly{
_snowFlake.center=CGPointMake(_snowFlake.center.x-_qq, _snowFlake.center.y+_qq);
if (_snowFlake.center.x<<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">5) {
_snowFlake.center=CGPointMake(388, _snowFlake.center.y);
}else if (_snowFlake.center.y>666){
_snowFlake.center=CGPointMake(_snowFlake.center.x, 66);
}
}
-(void)crash{
if (CGRectIntersectsRect(_iv4.frame, _iv3.frame)||CGRectIntersectsRect(_iv4.frame,_iv2.frame)||CGRectIntersectsRect(_iv4.frame, _iv.frame)||CGRectIntersectsRect(_iv4.frame, _iv5.frame)) {
_iv4.center=CGPointMake(120, 650);
//實現了發生碰撞一次剩餘血量就減一
_blood--;
//這裡的text一定要放在這裡寫。顯示剩餘剩餘血量。
[email protected](_blood).stringValue;
if (_blood<<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8">1) {
UILabel*over=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 400, 666 )];
[email protected]"大菜逼,重來吧!";
over.font=[UIFont systemFontOfSize:50 weight:33];
over.textColor=[UIColor redColor];
over.backgroundColor=[UIColor blueColor]
;
[self.view addSubview:over];
}
_level++;
[email protected](_level).stringValue;
// ********每個_kk的值在控製圖片運動的時候有負值,所以在這裡加上
// if判斷是否為正數,如果是正數就加,負的就減
if (_kk>0) {
_kk+=10;
}else{
_kk+=-10;
}
if (_kk2>0) {
_kk2+=10;
}else{
_kk2+=-10;
}
if (_kk3>0) {
_kk3+=10;
}else{
_kk3+=-10;
}
////////
if (_level>=2) {
UIImageView*good=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 380, 666)];
good.image=[UIImage imageNamed:@"08.jpg"];
[self.view addSubview:good];
}
藍懿iOS編寫小遊戲實現新功能