日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C#小游戏—钢铁侠VS太空侵略者

發布時間:2023/12/4 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#小游戏—钢铁侠VS太空侵略者 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??????身為漫威迷,最近又把《鋼鐵俠》和《復仇者聯盟》系列又重溫了一遍,真的是印證了那句話:“讀書百遍,其意自現”。看電影一個道理,每看一遍,都有不懂的感受~ 不知道大伙是不是也有同樣的感受,對于好的電影,真的是回味無窮!


?????本篇博文也是因《復仇者聯盟1》的啟發,C#語言實現的一個小游戲,所以游戲命名就叫“鋼鐵俠VS太空侵略者》了!

???? 先上一個游戲原型圖:

??????Talk is Cheap,Show me the Code!

?????? 代碼方面沒有太難的操作,主要依賴于Timer控件:

?????分別用來監控游戲中Iron man 子彈移動,侵略者左右移動,往下移動,侵略者子彈移動,子彈碰撞,以及觀察者監控(主要校驗生命值),具體代碼如下:

????? ?侵略者界面生成:

private void CreateControl(Form p) {PictureBox pb = new PictureBox();pb.Location = new Point(x, y);pb.Size = new Size(width, height);pb.BackgroundImage = Properties.Resources.invader;pb.BackgroundImageLayout = ImageLayout.Stretch;pb.Name = "Alien";p.Controls.Add(pb); } public void CreateSprites(Form p) {for(int i = 0; i < rows; i++){for(int j = 0; j < columns; j++){CreateControl(p);x += width + space; }y += height + space;x = 150; } }

???????鍵盤事件綁定:

private void Pressed(object sender, KeyEventArgs e) {if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left){moveLeft = true;}else if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right){moveRight = true;}else if (e.KeyCode == Keys.Space && game && !fired){Missile();fired = true;} } private void Released(object sender, KeyEventArgs e) {if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left){moveLeft = false;}else if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right){moveRight = false;}else if (e.KeyCode == Keys.Space){fired = false;} }

????? ?Iron man 左右移動:

private void PlayerMove(object sender, EventArgs e){if (moveLeft && Player.Location.X >= 0){Player.Left--;}else if (moveRight && Player.Location.X <= limit){Player.Left++;}}

????? ?子彈發射:

private void FireBullet(object sender, EventArgs e) {foreach (Control c in this.Controls){if (c is PictureBox && c.Name == "Bullet"){PictureBox bullet = (PictureBox)c;bullet.Top -= 5;if (bullet.Location.Y <= 0){this.Controls.Remove(bullet); }foreach(Control ct in this.Controls){if (ct is PictureBox && ct.Name == "Laser"){PictureBox laser = (PictureBox)ct;if (bullet.Bounds.IntersectsWith(laser.Bounds)){this.Controls.Remove(bullet);this.Controls.Remove(laser);pts++;Score(pts);}}}foreach(Control ctrl in this.Controls){if (ctrl is PictureBox && ctrl.Name == "Alien"){PictureBox alien = (PictureBox)ctrl;if (bullet.Bounds.IntersectsWith(alien.Bounds) && !Touched(alien)){this.Controls.Remove(bullet);this.Controls.Remove(alien);aliens.Remove(alien);pts += 5;Score(pts);CheckForWinner();}else if (bullet.Bounds.IntersectsWith(alien.Bounds) && Touched(alien)){this.Controls.Remove(bullet);this.Controls.Remove(alien);delay.Add(alien);pts += 5;Score(pts);CheckForWinner();}}}}} }

????? ?子彈

private void Missile() {PictureBox bullet = new PictureBox();bullet.Location = new Point(Player.Location.X + Player.Width / 2, Player.Location.Y - 20);bullet.Size = new Size(5, 20);bullet.BackgroundImage = Properties.Resources.bullet;bullet.BackgroundImageLayout = ImageLayout.Stretch;bullet.Name = "Bullet";this.Controls.Add(bullet); }

????? ?侵略者移動:

private void AlienMove() { foreach(PictureBox alien in aliens){alien.Location = new Point(alien.Location.X + left, alien.Location.Y + top);SetDirection(alien);Collided(alien); } } private void Collided(PictureBox a) {if (a.Bounds.IntersectsWith(Player.Bounds)){gameOver();} }

????? ?子彈移動效果:

private void Beam(PictureBox a) {PictureBox laser = new PictureBox();laser.Location = new Point(a.Location.X + a.Width / 3,a.Location.Y + 20);laser.Size = new Size(5, 20);laser.BackgroundImage = Properties.Resources.laser;laser.BackgroundImageLayout = ImageLayout.Stretch;laser.Name = "Laser";this.Controls.Add(laser); } private void StrikeSpan(object sender, EventArgs e) {Random r = new Random();int pick;if (aliens.Count > 0){pick = r.Next(aliens.Count);Beam(aliens[pick]);} } private void DetectLaser(object sender, EventArgs e) {foreach(Control c in this.Controls){if (c is PictureBox && c.Name == "Laser"){PictureBox laser = (PictureBox)c;laser.Top += 5;if (laser.Location.Y >= limit){this.Controls.Remove(laser); }if (laser.Bounds.IntersectsWith(Player.Bounds)){this.Controls.Remove(laser); LoseLife(); } }} }

????????主要核心代碼如上,下面看下運行效果圖:

效果圖一

效果圖二

??????何以解憂唯有擼碼,歡迎有興趣的朋友,聯系我一起探討。如需源碼,也請聯系我,源碼免費贈送,最后感謝您的耐心觀看,贈人玫瑰,手留余香,覺得本文有些許意思和啟發的,記得關注博主,您的支持,就是我寫作莫大的動力!

總結

以上是生活随笔為你收集整理的C#小游戏—钢铁侠VS太空侵略者的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。