Verfügbare Informationen zu "XNA - Codeschnipsel - Collision"
Qualität des Beitrags: Beteiligte Poster: Snowball Forum: Webdesigner's Forenbeschreibung: Das Hilfe Forum für alle Webdesigner aus dem Unterforum: C# XNA Antworten: 1 Forum gestartet am: Freitag 20.07.2007 Sprache: deutsch Link zum Originaltopic: XNA - Codeschnipsel - Collision Letzte Antwort: vor 16 Jahren, 4 Monaten, 20 Tagen, 16 Stunden, 22 Minuten
Alle Beiträge und Antworten zu "XNA - Codeschnipsel - Collision"
Re: XNA - Codeschnipsel - Collision
Snowball - 07.11.2007, 21:08XNA - Codeschnipsel - Collision
Code: #region Using Statements
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace x9
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
SpriteBatch spriteBatch;
Texture2D tree;
Texture2D kugel;
Vector2 spritePosition = Vector2.Zero;
private MouseState mouse;
SpriteBatch Sprite;
SpriteFont Arial;
float moveSpeedPerFrame = 5.0f;
Vector2 playerPosition = new Vector2(400, 300);
Vector2 mousePosition;
Vector2 tilepos2;
Vector2 tilepos;
Texture2D c;
Texture2D b;
KeyboardState ksState;
int cX;
int cY;
int bX;
int bY;
private Vector2 PlayerPos = new Vector2(300, 400);
private Vector2 BlockPos;
private Vector2[] playerPosition2 = new Vector2[100];
Vector2 a = new Vector2(400, 300);
private float[] shotmovespeed = new float[100];
private Vector2[] shotPosition = new Vector2[100];
private Vector2[] mousePosition2 = new Vector2[100];
int x5 = 400;
int y5 = 300;
private Vector2[] direction2 = new Vector2[100];
private Vector2 playerposition2 = new Vector2(400, 300);
Rectangle safeBounds;
private Vector2[] shotPosition2 = new Vector2[100];
private Vector2[] xxx = new Vector2[100];
int shot;
Rectangle c2;
Rectangle b2;
int totalsec;
Texture2D blockTexture;
Texture2D personTexture;
Color[] personTextureData;
Color[] blockTextureData;
Vector2 personPosition;
bool personHit = false;
List<Vector2> blockPositions = new List<Vector2>();
const int BlockFallSpeed = 2;
private int text = 1;
private float BlockPosX;
private float BlockPosY;
private const float tile_hoehe = 65;
private const float tile_breite = 65;
private const int map_hoehe = 10;
private const int map_breite = 12;
private static int[,] tilemap = new int[map_hoehe, map_breite]
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
};
private Texture2D[] map_textures = new Texture2D[2];
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
graphics.IsFullScreen = true;
for (int i = 0; i < 100; i++)
{
shotPosition[i] = new Vector2(x5, y5);
playerPosition2[i] = new Vector2(x5, y5);
}
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
map_textures[0] = content.Load<Texture2D>("s1");
map_textures[1] = content.Load<Texture2D>("s4");
blockTexture = content.Load<Texture2D>("block");
personTexture = content.Load<Texture2D>("player");
c = content.Load<Texture2D>("c");
b = content.Load<Texture2D>("b");
blockTextureData =
new Color[blockTexture.Width * blockTexture.Height];
blockTexture.GetData(blockTextureData);
personTextureData =
new Color[personTexture.Width * personTexture.Height];
personTexture.GetData(personTextureData);
this.tree = Texture2D.FromFile(this.graphics.GraphicsDevice,
"tree.png");
this.kugel = Texture2D.FromFile(this.graphics.GraphicsDevice,
"kugel.png");
this.spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
this.IsMouseVisible = true;
}
Arial = content.Load<SpriteFont>("Arial");
Sprite = new SpriteBatch(graphics.GraphicsDevice);
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent)
{
content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
ksState = Keyboard.GetState();
mouse = Mouse.GetState();
totalsec++;
if (mouse.RightButton == ButtonState.Pressed)
{
if (totalsec > 15)
{
if (shot < 100)
{
shot++;
if (shot == 100)
{
shot = 0;
}
playerPosition2[shot] = new Vector2(400, 300);
mousePosition2[shot] = new Vector2(mouse.X, mouse.Y);
shotmovespeed[shot] = 16.0f;
shotPosition[shot] = new Vector2(400, 300);
totalsec = 0;
}
}
}
if (mouse.LeftButton == ButtonState.Pressed)
{
mousePosition = new Vector2(mouse.X, mouse.Y);
Vector2 direction = playerposition2 - mousePosition;
direction.Normalize();
playerPosition = playerPosition + moveSpeedPerFrame * direction;
xxx[shot] = direction * moveSpeedPerFrame;
tilepos += direction * moveSpeedPerFrame;
BlockPos += direction * moveSpeedPerFrame;
}
BlockPosX = BlockPos.X;
BlockPosY = BlockPos.Y;
Rectangle c2 = new Rectangle((int)300,
(int)400, c.Width, c.Height);
Rectangle b2 = new Rectangle((int)BlockPosX, (int)BlockPosY, b.Width, b.Height);
if (c2.Intersects(b2))
{
text = 2; // Irgendwelche Dinge, die gemacht werden sollen, wenn eine Kollision stattgefunden hat!
} //
if (ksState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
for (int i = 0; i < map_hoehe; i++)
{
for (int j = 0; j < map_breite; j++)
{
tilepos2.X = tile_breite * i + tilepos.X;
tilepos2.Y = tile_hoehe * j + tilepos.Y;
spriteBatch.Draw(map_textures[tilemap[i, j]], (tilepos2), Color.White);
}
}
for (int k = 0; k < 100; k++)
{
direction2[k] = mousePosition2[k] - playerPosition2[k];
direction2[k].Normalize();
shotPosition[k] = (shotPosition[k] + shotmovespeed[k] * direction2[k]) + xxx[shot];
this.spriteBatch.Draw(this.kugel, (shotPosition[k]), Color.White);
}
for (int m = 0; m < 100; m++)
{
xxx[m] = new Vector2(0, 0);
}
spriteBatch.Draw(c, (PlayerPos), Color.White);
spriteBatch.Draw(b, new Vector2(BlockPosX, BlockPosY), Color.White);
this.spriteBatch.End();
Sprite.Begin();
// Hostnamen und IP-Adresse abrufen.
string hostName = Dns.GetHostName();
// Die erste passende IP-Adresse abrufen.
string ipAddress = Dns.GetHostEntry(hostName).AddressList[0].ToString();
Sprite.DrawString(Arial, "Hostname: " + hostName, new Vector2(10, 60), Color.Black);
Sprite.DrawString(Arial, "IP-Adresse: " + ipAddress, new Vector2(10, 75), Color.Black);
Sprite.DrawString(Arial, "Y: " + playerPosition, new Vector2(10, 26), Color.Black);
Sprite.DrawString(Arial, "X: " + mousePosition2[shot], new Vector2(10, 45), Color.Black);
Sprite.DrawString(Arial, "Text: " + text, new Vector2(300, 400), Color.Black);
Sprite.End();
base.Draw(gameTime);
}
}
}
Mit folgendem Code, können Sie den Beitrag ganz bequem auf ihrer Homepage verlinken
Weitere Beiträge aus dem Forum Webdesigner's
WinAPI - Sachen kombinieren - gepostet von Snowball am Donnerstag 26.07.2007
Ähnliche Beiträge wie "XNA - Codeschnipsel - Collision"
XNA - Codeschnipsel - IP Adresse - Snowball (Sonntag 04.11.2007)
XNA - Codeschnipsel - Tilemap bewegen - Snowball (Donnerstag 01.11.2007)
XNA Matrices Demystified - kuroneko (Sonntag 27.05.2007)
XNA - Codeschnipsel - Move_Background - Snowball (Sonntag 28.10.2007)
XNA :-/ - dmnr4z (Mittwoch 20.12.2006)
Favorite Song von Collision Course - Hybrid (Sonntag 17.04.2005)
Angriffe von Collision,Hannibal ? - wildRat (Freitag 15.06.2007)
XNA selber Spiele Entwickeln. - Cycron (Samstag 09.09.2006)
Jay Z & Linkin Park - Collision course - shandu (Mittwoch 08.12.2004)
Collision Course - Raiden (Dienstag 28.12.2004)