我对C#和XNA很陌生......对于一般的编程来说也是新手。
所以,作为一项任务,我需要使用xna制作乒乓球游戏。
我做得很好,直到我意识到球在屏幕底部像疯了一样弹跳,原因是我无法识别。
以下是我的代码
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 PongGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
SpriteFont score;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D leftPaddle;
Texture2D rightPaddle;
Texture2D centerLine;
private Texture2D ball;
private Vector2 leftPaddlePosition = new Vector2();
private Vector2 rightPaddlePosition = new Vector2();
private Vector2 centerLinePosition = new Vector2();
private Vector2 ballPosition = new Vector2();
private Vector2 ballVelocity = new Vector2();
private int WIDTH, HEIGHT;
private int scoreOne=0;
private int scoreTwo=0;
private int round = 1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
WIDTH = GraphicsDevice.Viewport.Width;
HEIGHT = GraphicsDevice.Viewport.Height;
leftPaddlePosition = new Vector2(0, (HEIGHT / 2)-40);
rightPaddlePosition = new Vector2(WIDTH-30, (HEIGHT / 2)-40);
centerLinePosition = new Vector2(WIDTH / 2, 0);
ballPosition = new Vector2((WIDTH / 2)-15, (HEIGHT / 2)-15);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
leftPaddle = new Texture2D(GraphicsDevice, 1, 1);
leftPaddle.SetData(new[] { Color.White });
rightPaddle = new Texture2D(GraphicsDevice, 1, 1);
rightPaddle.SetData(new[] { Color.White });
centerLine = new Texture2D(GraphicsDevice, 1, 1);
centerLine.SetData(new[] { Color.White });
score = Content.Load<SpriteFont>("score1");
ball = Content.Load<Texture2D>("ball2");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (round % 2 != 0)
//when the round is an odd numbered round,
{
ballVelocity = new Vector2(4f, 4f);
}
else if (round % 2 == 0)
{
ballVelocity = new Vector2(-4f, 4f);
}
KeyboardState key;
key = Keyboard.GetState();
if (key.IsKeyDown(Keys.W))
{
leftPaddlePosition.Y -= 5f;
}
if (key.IsKeyDown(Keys.S))
{
leftPaddlePosition.Y += 5f;
}
if (key.IsKeyDown(Keys.Up))
{
rightPaddlePosition.Y -= 5f;
}
if (key.IsKeyDown(Keys.Down))
{
rightPaddlePosition.Y += 5f;
}
//Left paddle manipulation
if (leftPaddlePosition.Y < 0)
{
leftPaddlePosition.Y = 0;
}
if (leftPaddlePosition.Y > HEIGHT-80)
{
leftPaddlePosition.Y = HEIGHT-80;
}
//Right paddle manipulation
if (rightPaddlePosition.Y < 0)
{
rightPaddlePosition.Y = 0;
}
if (rightPaddlePosition.Y > HEIGHT - 80)
{
rightPaddlePosition.Y = HEIGHT - 80;
}
//Making sure that the ball stays within the window
if (ballPosition.Y >= HEIGHT - 30)
{
ballVelocity.Y *= -1;
}
if (ballPosition.Y <= 0)
{
ballVelocity.Y *= -1;
}
//Checking if the ball goes over the left gutter
if (ballPosition.X < 29.9f)
{
ballPosition.X = 29.9f;
ballVelocity = new Vector2(0, 0);
scoreTwo++;
round++;
Initialize();
}
//Checking if the ball goes over the right gutter
if (ballPosition.X > WIDTH - 29.9f-29.9f)
{
ballPosition.X = WIDTH - 29.9f-29.9f;
ballVelocity = new Vector2(0, 0);
scoreOne++;
round++;
Initialize();
}
ballPosition.X += ballVelocity.X;
ballPosition.Y += ballVelocity.Y;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(ball, ballPosition, Color.White);
spriteBatch.Draw(leftPaddle, leftPaddlePosition, null, Color.White, 0f, Vector2.Zero, new Vector2(30f, 80f), SpriteEffects.None, 0f);
spriteBatch.Draw(rightPaddle, rightPaddlePosition, null, Color.White, 0f, Vector2.Zero, new Vector2(30f, 80f), SpriteEffects.None, 0f);
spriteBatch.Draw(centerLine, centerLinePosition, null, Color.White, 0f, Vector2.Zero, new Vector2(1f, HEIGHT), SpriteEffects.None, 0f);
spriteBatch.DrawString(score, System.Convert.ToString(scoreOne), new Vector2(WIDTH/4-10, 10f), Color.White);
spriteBatch.DrawString(score, System.Convert.ToString(scoreTwo), new Vector2((WIDTH/4)*3-10, 10f), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
我知道这里发布的代码很长,但由于我不知道问题出在哪里,我认为最好问你们。
我还将游戏上传到了一个Dropbox帐户。 download link on dropbox
答案 0 :(得分:0)
如果没有实际运行代码,我无法分辨,但是不是在这里为你编写代码。
如果是我,我会在这里看一下(看起来最明显的嫌疑人一眼)...
//Making sure that the ball stays within the window
if (ballPosition.Y >= HEIGHT - 30)
{
ballVelocity.Y *= -1;
}
if (ballPosition.Y <= 0)
{
ballVelocity.Y *= -1;
}
答案 1 :(得分:0)
你应该将速度向量乘以最后一帧除以某个速度常数(大约200)的毫秒数。 这个时间包含在updatemethond的参数gameTime中。