- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 XNA Break Out 克隆教程,我正在为 Windows Phone 编程。本教程在球和砖 block 的碰撞检测完成之前结束。积木按行和列放在屏幕上。我得到了 Racket 的边界框检测,它从 Racket 上反射球,但砖 block 需要相同的能力。我正在粘贴我的主要游戏类球和积木的代码。如果有人能告诉我如何实现这一点,我将不胜感激。我是否需要一组矩形,或者是否有不同的方法来完成这项工作?
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.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace bouncingBlocks
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture1;
Texture2D texture2;
public Vector2 spritePosition1;
Vector2 spritePosition2;
Texture2D mainBackground;
Vector2 mainBackgroundPos;
Vector2 spriteSpeed1 = new Vector2(50.0f, 50.0f);
Vector2 spriteSpeed2 = new Vector2(100.0f, 100.0f);
int sprite1Height;
int sprite1Width;
int sprite2Height;
int sprite2Width;
Texture2D paddleTexture;
Rectangle screenRectangle;
int bricksWide;
int bricksHigh;
Texture2D brickImage;
Block[,] bricks;
Vector2 ballPos;
SoundEffect soundEffect;
//objects
paddle paddle;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 320;
graphics.PreferredBackBufferHeight = 480;
screenRectangle = new Rectangle(
0,
0,
graphics.PreferredBackBufferWidth,
graphics.PreferredBackBufferHeight
);
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
TouchPanel.EnabledGestures = GestureType.FreeDrag;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteBatch = new SpriteBatch(GraphicsDevice);
texture1 = Content.Load<Texture2D>("Ball");
texture2 = Content.Load<Texture2D>("Ball");
mainBackground = Content.Load<Texture2D>("Background");
mainBackgroundPos.X = 0;
mainBackgroundPos.Y = 0;
paddleTexture = Content.Load<Texture2D>("GlowPaddle");
paddle = new paddle(paddleTexture, screenRectangle);
brickImage = Content.Load<Texture2D>("BlueBlock");
bricksWide = 4;
bricksHigh = 2;
soundEffect = Content.Load<SoundEffect>("Windows Ding");
spritePosition1.X = 0;
spritePosition1.Y = 0;
spritePosition2.X = graphics.GraphicsDevice.Viewport.Width - texture1.Width;
spritePosition2.Y = graphics.GraphicsDevice.Viewport.Height - texture1.Height;
sprite1Height = texture1.Bounds.Height;
sprite1Width = texture1.Bounds.Width;
sprite2Height = texture2.Bounds.Height;
sprite2Width = texture2.Bounds.Width;
StartGame();
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
ButtonState.Pressed)
this.Exit();
// Move the sprite around.
UpdateSprite(gameTime, ref spritePosition1, ref spriteSpeed1);
UpdateSprite(gameTime, ref spritePosition2, ref spriteSpeed2);
CheckForCollision();
paddCollision(paddle.GetBounds());
paddle.Update();
// TODO: Add your update logic here
base.Update(gameTime);
}
public Rectangle BallGetBounds()
{
return new Rectangle(
(int)spritePosition1.X,
(int)spritePosition1.Y,
texture1.Width,
texture1.Height);
}
private void StartGame()
{
paddle.SetInStartPosition();
bricks = new Block[bricksWide, bricksHigh];
for (int y = 0; y < bricksHigh; y++)
{
Color tint = Color.White;
switch (y)
{
case 0:
tint = Color.Blue;
break;
case 1:
tint = Color.Red;
break;
case 2:
tint = Color.Green;
break;
case 3:
tint = Color.Yellow;
break;
case 4:
tint = Color.Purple;
break;
}
for (int x = 0; x < bricksWide; x++)
{
bricks[x, y] = new Block(
brickImage,
new Rectangle(
x * brickImage.Width,
y * brickImage.Height,
brickImage.Width,
brickImage.Height),
tint);
}
}
}
public void blockCollision(Rectangle ballRec)
{
Rectangle newballLocation = new Rectangle(
(int)spritePosition1.X,
(int)spritePosition1.Y,
texture1.Width,
texture1.Height);
if (ballRec.Intersects(newballLocation))
{
spritePosition1.Y = ballRec.Y - texture1.Height;
spriteSpeed1.Y *= -1;
}
}
public void paddCollision(Rectangle paddleRec)
{
Rectangle ballLocation = new Rectangle(
(int)spritePosition1.X,
(int)spritePosition1.Y,
texture1.Width,
texture1.Height);
if (paddleRec.Intersects(ballLocation))
{
spritePosition1.Y = paddleRec.Y - texture1.Height;
spriteSpeed1.Y *= -1;
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(mainBackground, mainBackgroundPos, Color.White);
spriteBatch.End();
spriteBatch.Begin();
foreach (Block brick in bricks)
brick.Draw(spriteBatch);
paddle.Draw(spriteBatch);
spriteBatch.End();
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(texture1, spritePosition1, Color.White);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque);
spriteBatch.Draw(texture2, spritePosition2, Color.Gray);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
void UpdateSprite(GameTime gameTime, ref Vector2 spritePosition, ref Vector2 spriteSpeed)
{
// Move the sprite by speed, scaled by elapsed time.
spritePosition +=
spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
int MaxX =
graphics.GraphicsDevice.Viewport.Width - texture1.Width;
int MinX = 0;
int MaxY =
graphics.GraphicsDevice.Viewport.Height - texture1.Height;
int MinY = 0;
// Check for bounce.
if (spritePosition.X > MaxX)
{
spriteSpeed.X *= -1;
spritePosition.X = MaxX;
}
else if (spritePosition.X < MinX)
{
spriteSpeed.X *= -1;
spritePosition.X = MinX;
}
if (spritePosition.Y > MaxY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MaxY;
}
else if (spritePosition.Y < MinY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MinY;
}
}
void CheckForCollision()
{
BoundingBox bb1 = new BoundingBox(new Vector3(spritePosition1.X - (sprite1Width / 2),
spritePosition1.Y - (sprite1Height / 2), 0), new Vector3(spritePosition1.X + (sprite1Width / 2),
spritePosition1.Y + (sprite1Height / 2), 0));
BoundingBox bb2 = new BoundingBox(new Vector3(spritePosition2.X - (sprite2Width / 2),
spritePosition2.Y - (sprite2Height / 2), 0), new Vector3(spritePosition2.X + (sprite2Width / 2),
spritePosition2.Y + (sprite2Height / 2), 0));
if (bb1.Intersects(bb2))
{
soundEffect.Play();
}
}
}
}
//// block 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace bouncingBlocks
{
class Block
{
Texture2D texture;
Rectangle location;
Color tint;
bool alive;
private Texture2D brickImage;
private Rectangle rectangle;
Rectangle BallRec;
public Block(Texture2D brickImage, Rectangle rectangle, Color tint)
{
// TODO: Complete member initialization
this.texture = brickImage;
this.location = rectangle;
this.tint = tint;
this.alive = true;
}
public Rectangle Location
{
get { return location; }
}
public void CheckCollision()
{
}
public void Draw(SpriteBatch spriteBatch)
{
if (alive)
spriteBatch.Draw(texture, location, tint);
}
}
}
//球类
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.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace bouncingBlocks
{
class Ball
{
Texture2D texture1;
Texture2D texture2;
int sprite1Height;
int sprite1Width;
public Vector2 spritePosition1;
Vector2 spriteSpeed1 = new Vector2(50.0f, 50.0f);
public Ball()
{
}
}
}
最佳答案
如果您知道砖 block 的位置和尺寸,您可以简单地添加将 BoudingBox 返回到 Brick 类的属性。您还可以将该属性添加到 ball. (BoundingSphere 而不是 BoundingBox)
public BoundingBox Boundingbox
{
return new BoundingBox(new Vector3(this.position.X, position.Y, 0f),
new Vector3(this.position.X + spritewidth,position.Y+spriteheight, 0f));
}
然后当您检查碰撞时,它看起来像这样。
foreach(Brick b in bricks)
{
if(ball.BoundingSphere.Intersects(b.Boundingbox))
{
b.Alive=false;
}
}
关于c# - 突破 Windows Phone Dev 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9153890/
我开发了一个使用 mspn 服务的 windows phone 应用程序。它在 Windows 手机模拟器上运行良好。现在我想在 Windows Phone 设备中测试它。我有一个 Windows P
我正在考虑针对 Windows Phone 7 进行开发。为了测试我的应用程序,我需要能够模拟电话调用。在模拟器中可以吗? 最佳答案 不,不可能模拟您的应用程序被接收到的电话中断。还宣布为 the R
我看到 Windows Phone 8 支持 Windows Phone 7 应用程序。我在 Windows Phone 7 上开发了一些应用程序,我想在 Windows Phone 8 模拟器上运行
我正在Windows 8上使用Visual Studio2013。我有三个Windows Phone设备,lumia 620、920、1320,这些都是开发人员解锁的设备。 620设备已安装Windo
非常直接的问题。我的公司开发了一个 windows phone 7 应用程序,并一直在 windows phone 7 设备上对其进行测试。我们能否安全地假设同一个应用程序将向后兼容并在 Window
标题几乎概括了它。我已经编写了一个 Windows Phone 7 应用程序,现在我想将它部署到 Windows Phone 8 设备 (HTC Windows Phone 8X)。我已经注册并解锁了
我一直在开发 Windows Phone 8 应用程序,现在我想将其更改为 Windows Phone 8.1。我该怎么做? 是否应该创建一个新的 Windows Phone 8.1 应用并将所有现有
我有一个针对 Windows Phone 8 的应用程序。现在我想使用 this compression library其中指出: Supported Platforms: .NET Framewor
有什么区别Windows Phone 7.1 (NoDo) 和 Windows Phone 7.5 (芒果) ? 我已经为 Mango 安装了 SDK,但是当我尝试创建一个项目时,它显示为 7.1 版
我最近更新了我的 Visual Studio 2013,现在它允许开发 Windows Phone 8.1 应用程序。 但是现在,当我创建一个 Windows Phone 项目时,它是针对 WP 8.
我最近购买了 Lumia 630 双 SIM 卡,并一直在尝试注册我的手机以进行开发,但一直收到此错误“无法连接到手机。请确保通过 USB 传输服务的 Windows Phone IP 正在运行”。
我正在尝试使用 documentation from MSDN 在我现有的 Windows Phone OS 7.1 应用程序中支持新的 Windows Phone 磁贴功能。 .但是,我似乎无法通过
我在搜索如何启动Windows Phone 10模拟器时迷路了。 我已经做了: 我下载并安装了仿真器镜像(我知道flash.vhd文件的位置) 我正在运行Windows 8.1 x64专业版。 Hyp
我想制作像图片一样的动态磁贴。我该怎么做? 最佳答案 上面创建的图 block 很可能是在服务器端创建的。据说 Windows Phone 7.1 和 Windows Phone 8 之间发生了很多变
我刚刚将它安装在我的 Windows 8 上。它不是在 VM 上运行,而是在带有 bootcamp 的 macbook 上运行。 当我尝试模拟我的应用程序时,出现第一个错误: 但是后来我单击“重试”并
Microsoft 刚刚发布了结合了 Windows 8.1 和 Windows Phone 8.1 的 Windows App 8.1,因此您可以创建一个通用的应用程序。但是,将 Microsoft
是否有可用于WP7的日历控件(不是日期选择器和时间选择器)? 最佳答案 我还在CodePlex上启动了新项目。我刚刚研究了日历控件的Alpha版本。 http://wpcontrols.codeple
我在XAML中有一个文本框: .cs文件声明了以下事件: private void blah_OnKeyDown(object sender, KeyEventArgs e) {
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在为Windows Phone 7创建一个多人游戏。如何调试模拟器的多个实例以便对其进行调试? 最佳答案 实际上,您可以同时运行Windows Phone 7模拟器的多个实例,甚至可以同时调试它们
我是一名优秀的程序员,十分优秀!