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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2023/12/10 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#小游戏_C#小游戏—钢铁侠VS太空侵略者 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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


?????本篇博文也是因《復(fù)仇者聯(lián)盟1》的啟發(fā),C#語(yǔ)言實(shí)現(xiàn)的一個(gè)小游戲,所以游戲命名就叫“鋼鐵俠VS太空侵略者》了!

???? 先上一個(gè)游戲原型圖:

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

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

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

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

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 左右移動(dòng):

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

????? ?子彈發(fā)射:

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);}

????? ?侵略者移動(dòng):

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(); }}

????? ?子彈移動(dòng)效果:

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(); } } }}

????????主要核心代碼如上,下面看下運(yùn)行效果圖:

效果圖一

效果圖二

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

總結(jié)

以上是生活随笔為你收集整理的c#小游戏_C#小游戏—钢铁侠VS太空侵略者的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。