-
Jun 29th, 2014, 12:43 PM
#1
Thread Starter
New Member
Newbie Space invaders
im super new at programming and im trying to make a space invaders game on visual studio ive got alot of errors i cant fix or understand why they are errors any help showing me what i did wrong would be appreciated. please be nice
Code:
namespace SpaceInvaders
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D invader, ship, bullet;
Rectangle rectship, rectbullet;
Rectangle[,] rectinvader;
int rows = 5;
int cols = 10;
String direction = "RIGHT";
String bulletvisible;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
invader = Content.Load<Texture2D>("invader");
rectinvader = new Rectangle[rows, cols];
for (int r = 0;r < rows;r++)
for (int c = 0; c < cols; c++)
{
rectinvader[r, c].Width = invader.Width;
rectinvader[r, c].Height = invader.Height;
rectinvader[r, c].X = 60 * c;
rectinvader[r, c].Y = 60 * r;
}
ship = Content.Load<Texture2D>("ship");
rectship.Width = ship.Width;
rectship.Height = ship.Height;
rectship.X = 0;
rectship.Y = 400;
bullet= Content.Load<Texture2D>("bullet");
rectbullet.Width = bullet.Width;
rectbullet.Height = bullet.Height;
}
}
protected override void UnloadContent()
{
}
protected override Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
int rightside = graphics.GraphicsDevice.Viewport.Width;
int leftside = 0;
//
for (int r = 0;r < rows;r++)
for (int c = 0; c < cols; c++)
{
if (direction.Equals("RIGHT"))
rectinvader[r, c].X = rectinvader[r, c].X + 1;
if (direction.Equals("LEFT"))
rectinvader[r, c].X = rectinvader[r,c].X - 1;
}
//
{
String changedirection = "N";
for (int r = 0;r < rows;r++)
for (int c = 0; c < cols; c++)
}
{
if (rectinvader[r, c].X + rectinvader[r, c].Width > rightside)
}
{
direction = "LEFT";
changeddirection = "Y";
}
if (rectinvader[r, c] .X < leftside)
{
direction = "RIGHT";
changeddirection = "Y";
}
}
if (changeddirection.Equals("Y"))
{
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
rectinvader[r, c].Y = rectinvader[r, c].Y + 3;
}
KeyboardState kb = Keyboard.Getstate();
if (kb.Iskeydown(keys.Left))
rectship.X = rectship.X + 2
if (kb.IsKeyDown(keys.Right)0
rectship.X
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
spriteBatch.Draw(ship, rectship, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Last edited by Joacim Andersson; Jul 3rd, 2014 at 07:24 AM.
Reason: Added code tags.
-
Jun 29th, 2014, 01:31 PM
#2
Re: Newbie Space invaders
Hi you have posted in the wrong section. This is for VB related questions only. A couple of points though please always post code inside of code or highlight tags. The second is most members wont help if it's not specific. You have simply posted your code and said "find the error and work out why it's not working. Also what it should be doing". Why can't you take the time to explain?
Thanks
-
Jun 29th, 2014, 03:41 PM
#3
Thread Starter
New Member
Re: Newbie Space invaders
sorry where should i have posted it ?
protected override void UnloadContent()
{
}
protected override Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
int rightside = graphics.GraphicsDevice.Viewport.Width;
int leftside = 0;
//
for (int r = 0;r < rows;r++)
for (int c = 0; c < cols; c++)
{
if (direction.Equals("RIGHT"))
rectinvader[r, c].X = rectinvader[r, c].X + 1;
if (direction.Equals("LEFT"))
rectinvader[r, c].X = rectinvader[r, c].X - 1;
}
//
{
its just this bit it carries on saying expect class not found and the name space r cannot be found the namespace c cannot be found are you missing an assembly reference. and for the . it says expected class or enum or delegate etc. sorry if this makes no sense i just started last week.
-
Jun 29th, 2014, 03:57 PM
#4
Re: Newbie Space invaders
I repeat A couple of points though please always post code inside of code or highlight tags. This is a visual basic section.
-
Jun 30th, 2014, 07:03 AM
#5
Re: Newbie Space invaders
You're missing your opening bracket for your first for loop (the one that declares r).
There may be other errors, but you've only shown a small snippet of code and that is all I can see wrong right now.
The code should look like below:
Code:
for (int r = 0;r < rows;r++)
{
for (int c = 0; c < cols; c++)
{
if (direction.Equals("RIGHT"))
rectinvader[r, c].X = rectinvader[r, c].X + 1;
if (direction.Equals("LEFT"))
rectinvader[r, c].X = rectinvader[r, c].X - 1;
}
}
-
Jun 30th, 2014, 10:29 AM
#6
Re: Newbie Space invaders
Thread moved to the Games and Graphics forum.
Last edited by Joacim Andersson; Jun 30th, 2014 at 10:33 AM.
-
Jul 3rd, 2014, 07:02 AM
#7
Re: Newbie Space invaders
You're missing your opening bracket for your first for loop
Unless I'm missing something (always a possibility) that doesn't matter. The whole of the subsequent loop (the one that declares c) will be considered a single statement by the compiler and therefore doesn't need to be wrapped in brackets. That said, I've always felt that putting the brackests in as you suggest improves readability.
name space r cannot be found the namespace c cannot be found
That means it's looking for a namespace called r and a namespace called c. I can't see anything in your code that should cause it to do that. It's difficult to read, though, becasue you're not using the code tags as Ident suggested. Click the Go Advanced button. You'll go to a posting page with a few more options. One of them is a code option. If you use it it will preserve the whitespace in your code which will make it much easier to read. I'd suggest posting the whole of your code again, using the coding tags.
Also, double click on the error message. This should take the cursor to the offending line. If you can tell us which line it is that will help us alot.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Jul 3rd, 2014, 07:24 AM
#8
Re: Newbie Space invaders
I've updated the first post to add code tags.
-
Jul 3rd, 2014, 10:01 AM
#9
Re: Newbie Space invaders
I've updated the first post to add code tags.
Now why didn't I think of that
Ganhad, now there's code tags on there I can what looks like a problem. In the Update method you've got this:-
Code:
}
//
{
String changedirection = "N";
for (int r = 0;r < rows;r++)
for (int c = 0; c < cols; c++)
} <-- What this doing Here?
{
if (rectinvader[r, c].X + rectinvader[r, c].Width > rightside)
}
{
direction = "LEFT";
changeddirection = "Y";
}
That looks to me like a closing curly bracket has crept in. That's going to push all the code after it into an incorrect scope and could result in... well, just about anything. Try removing that and see if you've still got issues. Or even better, I'd go through the whole code paying particular attention to indentation and curly brackets. That's the only one that looks flat out wrong but I can see several pairs that just don't need to be there and could be removed.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|