XNA开发一个2D的小游戏
这是我大一下半年时,写的一个程序,觉得现在这个在wp上也有在用,所以和大家一起分享了下,程序写的不好,请大家多多指教
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame2
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D background;
Rectangle rect; //正方形
Texture2D canon;
float canon_rotation;
List<gameobject> UFOs = new List<gameobject>();
Random rd = new Random();
List<gameobject> balls = new List<gameobject>();
List<gameobject> exploreok = new List<gameobject>();
KeyboardState prevkeyboarStart;
const int explorerwidth = 320;
const int explorerheith = 240;
Point explorersize = new Point(4,5);
float timer = 0;
float interal = 1000f/60;
AudioEngine engine; //创建AudioEngine
SoundBank soundbank; //创建SoundBank对像
WaveBank wavebank; //创建WaveBank对像
Cue mycue; //这主要用于播放意思的对像
float bump = 0; //记录使用炸弹的个数
float score = 0; //记录击中飞机的个数
SpriteFont gamefont; //创建游戏字体(这环境不支撑中文)
string sin ="1"; //标记
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
Form1 f1 = new Form1();
protected override void LoadContent()
{
f1.ShowDialog();
canon_rotation = 0;
spriteBatch = new SpriteBatch(GraphicsDevice);
background = Content.Load<Texture2D>("mypic//7132481"); //加载2D的图片背景
rect =
new Rectangle(0,0,graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height);
canon = Content.Load<Texture2D>("cannon"); //加载炮
canon_rotation = -MathHelper.PiOver2; //炮的位置
for (int i = 0; i < Convert.ToInt16(f1.a); i++) //随机产生n个
{
gameobject ufo = new gameobject();
ufo.sprite = Content.Load<Texture2D>("mypic//f-16c04");
ufo.position=new Vector2(
0,MathHelper.Lerp(0f,200f,(float)rd.NextDouble()));
ufo.velocity = new Vector2(MathHelper.Lerp(Convert.ToSingle(f1.pd), Convert.ToSingle(f1.pg), (float)rd.NextDouble()),
0);
UFOs.Add(ufo);
}
for (int i = 0; i <Convert.ToInt16(f1.ps); i++) //随机产生5枚炮弹
{
gameobject ball = new gameobject();
ball.sprite = Content.Load<Texture2D>("cannonball");
ball.alive = false;
balls.Add(ball);
}
for (int i = 0; i < 5; i++)
{
gameobject ex = new gameobject();
ex.alive = false;
ex.currenexplosionx = 0;
ex.currentexplosiony = 0;
ex.sprite = Content.Load<Texture2D>("mypic//explosion");
exploreok.Add(ex);
}
engine = new AudioEngine("Content/AudioProject.xgs");
wavebank = new WaveBank(engine, "Content/Wave Bank.xwb");
soundbank = new SoundBank(engine, "Content/Sound Bank.xsb");
mycue = soundbank.GetCue("background");
mycue.Play();
gamefont = Content.Load<SpriteFont>("gamefont");
}
protected override void UnloadContent()
{
}
bool ok = true;
//Form1 f2 = new Form1();
protected override void Update(GameTime gameTime)
{
if (f1.i == "1")
{
sin = "1";
bump = 0;
score = 0;
f1.i = "0";
ok = true;
}
if (score==Convert.ToSingle(f1.jbb)*10-1)
{
Form2 f2 = new Form2();
f2.Show();
sin = "0";
//bump = 0;
score +=1;
f1.i = "0";
ok = true;
}
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState keyboar = Keyboard.GetState();
if ((keyboar.IsKeyDown(Keys.Left) || keyboar.IsKeyDown(Keys.A))&&sin=="1")
{
canon_rotation -= 0.1f;
}
if ((keyboar.IsKeyDown(Keys.Right) || keyboar.IsKeyDown(Keys.D)) && sin=="1")
{
canon_rotation += 0.1f;
}
canon_rotation = MathHelper.Clamp(
canon_rotation,
-MathHelper.Pi, 0);
Updateufo();
if (keyboar.IsKeyDown(Keys.Space)
&& !prevkeyboarStart.IsKeyDown(Keys.Space) && sin=="1")
{
foreach (var ball in balls)
{
if (!ball.alive)
{
ball.alive = true;
ball.position=new Vector2(390, 440) ;
ball.velocity = new Vector2
((float)Math.Cos(canon_rotation) * 5,
(float)Math.Sin(canon_rotation) * 5);
soundbank.PlayCue("fire");
bump += 1;
break;
}
}
}
updateba();
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interal)
{
upxiguo();
timer = 0;
}
prevkeyboarStart = keyboar;
base.Update(gameTime);
}
private void upxiguo()
{
foreach (var ex in exploreok)
{
if (ex.alive)
{
ex.currenexplosionx++;
if (ex.currenexplosionx> explorersize.X)
{
ex.currenexplosionx = 0;
ex.currentexplosiony++;
if (ex.currentexplosiony > explorersize.Y)
{
ex.currentexplosiony = 0;
}
}
if (ex.currenexplosionx == 0 && ex.currentexplosiony == 0)
{
ex.alive = false;
}
else
{
ex.sourcerect = new Rectangle(ex.currenexplosionx * explorerwidth, ex.currentexplosiony * explorerheith,
explorerwidth,
explorerheith);
}
}
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(background,rect,Color.White);
spriteBatch.Draw(canon,
new Vector2(400, 450),
null,
Color.White,
canon_rotation,
new Vector2(canon.Width / 2, canon.Height / 2),
1f,
SpriteEffects.None,
0f);
foreach (var item in UFOs)
{
if(item.alive)
spriteBatch.Draw(item.sprite,item.position,Color.White);
}
foreach (var ball in balls)
{
if(ball.alive)
spriteBatch.Draw(ball.sprite,ball.position,Color.White
);
}
foreach (var ex in exploreok)
{
spriteBatch.Draw(ex.sprite, ex.position, ex.sourcerect, Color.White);
}
spriteBatch.DrawString(gamefont,"score:"+score.ToString(),new Vector2( 50,50),Color.Blue);
spriteBatch.DrawString(gamefont, "bump:" +bump.ToString(), new Vector2(50, 80), Color.Red);
spriteBatch.End();
base.Draw(gameTime);
}
private void Updateufo()
{
//throw new NotImplementedException();
foreach (var item in UFOs)
{
if (item.alive)
{
item.position += item.velocity;
if (item.position.X > 800)
item.alive = false;
}
if (!item.alive)
{
item.alive = true;
item.position = new Vector2(
0, MathHelper.Lerp(0f, 200f, (float)rd.NextDouble()));
item.velocity = new Vector2(MathHelper.Lerp(0.1f, 1f, (float)rd.NextDouble()),
0);
}
}
}
private void updateba()
{
foreach (var ball in balls)
{
if (ball.alive==true)
{
ball.position += ball.velocity;
if (!rect.Contains(new Point((int)ball.position.X, (int)ball.position.Y)))
{
ball.alive = false;
}
else
{
Rectangle rectball = new Rectangle(
(int) ball.position.X,
(int)ball.position.Y,
ball.sprite.Width,
ball.sprite.Height);
foreach (var ufo in UFOs)
{
if (ufo.alive)
{
Rectangle recufo = new Rectangle(
(int)ufo.position.X,
(int)ufo.position.Y,
ufo.sprite.Width,
ufo.sprite.Height);
if (rectball.Intersects(recufo))
{
ball.alive = false;
ufo.alive = false;
foreach (var ex in exploreok)
{
if (!ex.alive)
{
ex.alive = true;
ex.currentexplosiony = 0;
ex.currenexplosionx = 0;
ex.position = new Vector2(
ufo.position.X - (explorerwidth - ufo.sprite.Width) / 2,
ufo.position.Y - (explorerheith - ufo.sprite.Height) / 2);
soundbank.PlayCue("bao");
score += 1;
break;
}
}
break;
}
}
}
}
}
}
}
}
}
XNA开发一个2D的小游戏相关推荐
- ***JAVA*和*Eclipse*开发一个换装小游戏**
JAVA和Eclipse开发一个换装小游戏** 家有一女如有一宝,最近女朋友的少女心有点爆棚,作为一个计算机专业的人,我的情商简直是低到吓人,但是我还是想到了,亲自给女朋友做一个换装的小游戏,来满足女 ...
- 编程语言用 Java 开发一个打飞机小游戏(附完整源码)
编程语言用 Java 开发一个打飞机小游戏(附完整源码) 上图 写在前面 技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习.java确实不适合写桌面应用,这里只是通过 ...
- 使用 Python 开发一个恐龙跑跑小游戏,玩起来
作者 | 周萝卜 来源 | 萝卜大杂烩 相信很多人都玩过 chrome 浏览器上提供的恐龙跑跑游戏,在我们断网或者直接在浏览器输入地址"chrome://dino/"都可以进入游戏 ...
- 使用Python开发一个恐龙跳跳小游戏,玩起来
相信很多人都玩过 chrome 浏览器上提供的恐龙跑跑游戏,在我们断网或者直接在浏览器输入地址"chrome://dino/"都可以进入游戏 今天我们就是用 Python 来制作一 ...
- 使用Python开发一个恐龙跑跑小游戏,玩起来
相信很多人都玩过 chrome 浏览器上提供的恐龙跑跑游戏,在我们断网或者直接在浏览器输入地址"chrome://dino/"都可以进入游戏 今天我们就是用 Python 来制作一 ...
- Python开发一个炸金花小游戏,注意别玩上瘾了
众所周知扑克牌可谓是居家旅行.桌面交友的必备道具,今天我们用 Python 来实现一个类似炸金花的扑克牌小游戏,先来看一下基本的游戏规则. 炸(诈)金花又叫三张牌,是在全国广泛流传的一种民间多人纸牌 ...
- 用python开发一个炸金花小游戏,注意别玩上瘾了
扑克牌可谓是居家旅行.桌面交友的必备道具,今天我们用 Python 来实现一个类似炸金花的扑克牌小游戏,先来看一下基本的游戏规则. 炸(诈)金花又叫三张牌,是在全国广泛流传的一种民间多人纸牌游戏.游戏 ...
- 熬了三个大夜利用Python开发一个炸金花小游戏,注意别玩上瘾了~~(附完整源码)
大家好,我是你们的好朋友王老师~ 众所周知扑克牌可谓是居家旅行.桌面交友的必备道具,今天我们用 Python 来实现一个类似炸金花的扑克牌小游戏,先来看一下基本的游戏规则. 炸(诈)金花又叫三张牌, ...
- 用python开发一个炸金花小游戏,注意别玩上瘾了~~
众所周知扑克牌可谓是居家旅行.桌面交友的必备道具,今天我们用 Python 来实现一个类似炸金花的扑克牌小游戏,先来看一下基本的游戏规则. 炸(诈)金花又叫三张牌,是在全国广泛流传的一种民间多人纸牌 ...
最新文章
- MySql的安装、配置(转)
- 烂泥:文件服务器搭建与使用详解,minio文件服务器搭建(单机版)
- SSM项目使用GoEasy 获取客户端上下线实时状态变化及在线客户列表
- mysql错误码 1045_MySql 错误代码 1045
- 理解什么是前后端分离
- C++ queue和priority_queue
- 基于Sklearn实现LDA算法
- 如何给绘制好的CAD图纸设置密码?
- C 语言编码规范(MISRA-C-:2004)
- 【PS】制作水彩画效果
- 公众号网课答案查询系统
- 西瓜视频稳定性治理体系建设三:Sliver 原理及实践
- Reincarnation
- 敏捷Scrum框架最全总结!
- 大数据求签,人工智能算命,技术革新下传统行业还有灵魂吗
- 2022.9.26初识c语言
- 统计 DHCP 服务器 ip 分配情况的脚本
- python从ftp下载文件时问题解决
- opencv+gpu硬解码
- Qt:解析xml文件