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
CarMovement{
///
/// This is the main type for your game
///
publicclassCarGame1 : Microsoft.Xna.Framework.Game{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D carTexture;
Texture2D SShipTexture;
Texture2D ShotTexture;
Texture2D background;
Vector2 carOrigin =
new Vector2(64, 32);Vector2 position =
new Vector2(0, 200);Vector2 position2 =
new Vector2(10, 150);Vector2 position3 =
new Vector2(20, 300);Vector2 position1 =
new Vector2(700, 259);
float angle = -MathHelper.PiOver2;
float speed;
int x = 0;
int shuttle = 1;
bool ShotFlying = false;Vector2 ShotPosition =
new Vector2(700, 290);Vector2 ShotDirection;
Vector2 SShipPosition;
Vector2 SShipDirection;
float ShotAngle;
float ShotScaling = 0.1f;
int screenWidth;
int screenHeight;
public CarGame1(){
graphics =
new GraphicsDeviceManager(this);Content.RootDirectory =
"Content";}
///
/// 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.
///
protectedoverridevoid Initialize(){
// TODO: Add your initialization logic here
base.Initialize();}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protectedoverridevoid LoadContent(){
// Create a new SpriteBatch, which can be used to draw textures.spriteBatch =
new SpriteBatch(GraphicsDevice);background = Content.Load(
"stars"); // change these names to the names of your imagescarTexture = Content.Load(
"Car");SShipTexture = Content.Load(
"SShip");ShotTexture = Content.Load(
"Bubble2");
// TODO: use this.Content to load your game content here}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protectedoverridevoid UnloadContent(){
// TODO: Unload any non ContentManager content here}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
///Provides a snapshot of timing values.
protectedoverridevoid Update(GameTime gameTime){
KeyboardState keyboard = Keyboard.GetState();
keyboard = Keyboard.GetState();
if (keyboard.IsKeyDown(Keys.Left)){
SShipPosition.X -= 5;
}
elseif (keyboard.IsKeyDown(Keys.Right)){
SShipPosition.X += 5;
}
if (keyboard.IsKeyDown(Keys.Up)){
SShipPosition.Y -= 5;
}
elseif (keyboard.IsKeyDown(Keys.Down)){
SShipPosition.Y += 5;
}
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic hereangle = 0.00f;
// KeyboardState keyboard = Keyboard.GetState();
//if (keyboard.IsKeyDown(Keys.Space))
//{
// position = new Vector2(GraphicsDevice.Viewport.Width,
// GraphicsDevice.Viewport.Height) / 2;
// angle = -MathHelper.PiOver2;
//}position += speed *
new Vector2((float)Math.Cos(angle),(
float)Math.Sin(angle));position2 += speed *
new Vector2((float)Math.Cos(angle),(
float)Math.Sin(angle));position3 += speed *
new Vector2((float)Math.Cos(angle),(
float)Math.Sin(angle));x = x + 1;
//speed *= 0.95f;
if ((x == 4)){
position.X = 0;
position.Y = 200;
spriteBatch.Begin();
spriteBatch.Draw(carTexture, position,
null, Color.White, angle, carOrigin,0.5f, SpriteEffects.None, 0.0f);
spriteBatch.End();
//DrawCar();}
speed += 0.10f;
//KeyboardState keyboard = Keyboard.GetState();
//if (keyboard.IsKeyDown(Keys.Enter) || keyboard.IsKeyDown(Keys.Space))
//{
//DrawShot();
//// ShotFlying = true;
//}
//DrawShot();
//// if (ShotFlying==true)
// ShotPosition.X -= 6;ProcessKeyboard();
base.Update(gameTime);}
privatevoid ProcessKeyboard(){
KeyboardState keyboard = Keyboard.GetState();
keyboard = Keyboard.GetState();
if (keyboard.IsKeyDown(Keys.Left)){
SShipPosition.X -= 5;
}
elseif (keyboard.IsKeyDown(Keys.Right)){
SShipPosition.X += 5;
}
if (keyboard.IsKeyDown(Keys.Up)){
SShipPosition.Y -= 5;
}
elseif (keyboard.IsKeyDown(Keys.Down)){
SShipPosition.Y += 5;
}
if (keyboard.IsKeyDown(Keys.Enter) || keyboard.IsKeyDown(Keys.Space)){
//DrawShot();
//{ShotFlying =
true;ShotPosition = position1;
//////ShotAngle = players[currentPlayer].Angle;
//Vector2 front = new Vector2(0, -1);Matrix rotMatrix = Matrix.CreateRotationZ(ShotAngle);
//ShotDirection = Vector2.Transform(up, rotMatrix);
//ShotDirection *= shot[currentPlayer].Power / 50.0f;}
if (ShotFlying)ShotPosition.X -= 6;
ShotFlying =
true;
}
privatevoid DrawShot(){
//KeyboardState keyboard = Keyboard.GetState();
//if (keyboard.IsKeyDown(Keys.Enter) || keyboard.IsKeyDown(Keys.Space))
//{
// spriteBatch.Begin();
if (ShotFlying)spriteBatch.Draw(ShotTexture, ShotPosition,
null, Color.White, ShotAngle, new Vector2(42, 240), 0.3f, SpriteEffects.None, 1);
//spriteBatch.End();
// ShotFlying = true;
//}
//if (ShotFlying == true)
// ShotPosition.X -= 6;}
//private void DrawCar()
//{
// spriteBatch.Begin();
// spriteBatch.Draw(carTexture, position, null, Color.White, angle, carOrigin,
// 0.5f, SpriteEffects.None, 0.0f);
// spriteBatch.End();
//}
//private void UpdateShot()
//{
// if (ShotFlying)
// {
// ShotPosition.X -= 4;
// }
//}
///
/// This is called when the game should draw itself.
///
///Provides a snapshot of timing values.
///
protectedoverridevoid Draw(GameTime gameTime){
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code hereGraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// To Draw the backgroud for the ScreenspriteBatch.Draw(background,
new Rectangle(0, 0, 1200, 1200), Color.White);
//spriteBatch.Draw(carTexture, position, null, Color.White, angle, carOrigin,
// 0.5f, SpriteEffects.None, 0.0f);spriteBatch.Draw(carTexture, position,
null, Color.White, angle, carOrigin,0.5f, SpriteEffects.None, 0.0f);
spriteBatch.Draw(carTexture, position2,
null, Color.White, angle, carOrigin,0.5f, SpriteEffects.None, 0.10f);
spriteBatch.Draw(carTexture, position3,
null, Color.White, angle, carOrigin,0.5f, SpriteEffects.None, 0.20f);
//DrawCar();spriteBatch.Draw(SShipTexture, position1,
null, Color.White, MathHelper.PiOver2, carOrigin,1.00f, SpriteEffects.None, 0.0f);
DrawShot();
//spriteBatch.Draw(ShotTexture, ShotPosition, null, Color.White, ShotAngle, new Vector2(42, 240), 0.5f, SpriteEffects.None, 1);spriteBatch.End();
base.Draw(gameTime);}
privatevoid DrawScenery(){
Rectangle screenRectangle =
new Rectangle(0, 0, screenWidth, screenHeight);spriteBatch.Draw(background, screenRectangle, Color.White);
}
//private void shuttles()
//{
// foreach (shuttles item in shuttle)
// {
// }}
}
x & y
سوف نعمل على الx
حيث انه المسئول عن تحريك السياره يمين ويسار عليك معرفة, x
1- عندما تكون السياره يسار الشاشه فلنفترض ان الx=0
عندما تكون السياره يمين الشاشه فلنفترض ان الx=800
اذاً سوف يكون الكودif x > 800 then x = 0
جرب وقولى ما النتيجة.