Jump to content

  • Log In with Google      Sign In   
  • Create Account

different way of learning


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
31 replies to this topic

#21 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 October 2012 - 11:15 AM

the only thing i could think of is creating an array for storing bullets, lets say there can be 10 on the screen at a time. and whenever spacebar is pressed, create a bullet at shipPos.X and shipPos.Y, and send the bullet straight up by subracting its y value.

i guess my problem is that i really only know one side to doing things. I have a hard time thinking of different ways to program if my first option doesnt work because I get stuck in a loop trying to fix something that might not even work at all

Edited by burnt_casadilla, 30 October 2012 - 11:19 AM.

If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#22 Slushy   Members   -  Reputation: 168

Like
0Likes
Like

Posted 30 October 2012 - 11:35 AM

the only thing i could think of is creating an array for storing bullets, lets say there can be 10 on the screen at a time. and whenever spacebar is pressed, create a bullet at shipPos.X and shipPos.Y, and send the bullet straight up by subracting its y value.

i guess my problem is that i really only know one side to doing things. I have a hard time thinking of different ways to program if my first option doesnt work because I get stuck in a loop trying to fix something that might not even work at all

That'll work just fine. Just keep checking it for collisions and when it goes outside of the playing area (screen) remove it from the array (This could be done using null). Making it possible to shoot another bullet.

Edited by Slushy, 30 October 2012 - 11:35 AM.


#23 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 October 2012 - 12:58 PM

I don't even know where to start with the code for this. So far I have
[source lang="csharp"]Texture2D ship, bullet, alien; Texture2D[] aliens = new Texture2D[20]; Texture2D[] bullets = new Texture2D[10]; Vector2 shipPos = new Vector2(200, 540); Vector2 alienPos = new Vector2(200, 400); Vector2 bulletPos = new Vector2(200, 450); Vector2[] aliensPos = new Vector2[20]; Vector2[] bulletsPos = new Vector2[10]; int alienCount = 0; int bulletCount = 0; KeyboardState state = Keyboard.GetState(); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 400; graphics.PreferredBackBufferHeight = 600; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); alien = Content.Load<Texture2D>("alien"); bullet = Content.Load<Texture2D>("bullet"); ship = Content.Load<Texture2D>("ship"); for (int x = 0; x < 5; x++) { for (int y = 0; y < 4; y++) { aliens[alienCount] = Content.Load<Texture2D>("alien"); aliensPos[alienCount].X = 100 + (x * 40); aliensPos[alienCount].Y = 20 + (y * 40); alienCount++; } } } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (state.IsKeyDown(Keys.Right) && shipPos.X < 360) { shipPos.X += 5; } if (state.IsKeyDown(Keys.Left) && shipPos.X > 0) { shipPos.X -= 5; } if (state.IsKeyDown(Keys.Space)) { } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); spriteBatch.Draw(ship, shipPos, Color.White); spriteBatch.Draw(bullet, bulletPos, Color.White); spriteBatch.Draw(alien, alienPos, Color.White); for (int alienCount = 0; alienCount < 20; alienCount++) { spriteBatch.Draw(aliens[alienCount], aliensPos[alienCount], Color.White); } spriteBatch.End(); base.Draw(gameTime); } }}[/source]

I know that the update method runs 60 times a second, so that would create more bullets than I need per each press of the spacebar. And I have no idea how to get the position of the ship and how to create a bullet there

If you see a post from me, you can safely assume its C# and XNA :)


#24 Haps   Members   -  Reputation: 1315

Like
0Likes
Like

Posted 30 October 2012 - 01:08 PM

You can use a variable in conjunction with gameTime.ElapsedTime to make a cooldown on how often the bullet actually gets fired.

ShipPos.X and ShipPos.Y are keeping track of your ship's position. When you make a new bullet, relate it to those coordinates.

#25 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 October 2012 - 01:26 PM

so something like this?
[source lang="csharp"] if (state.IsKeyDown(Keys.Space)) { for (int i = 0; i < 10; i++) { bullets[bulletCount] = Content.Load<Texture2D>("bullet"); bulletsPos[bulletCount].X = shipPos.X; bulletsPos[bulletCount].Y = shipPos.Y; bulletCount++; } }[/source]
[source lang="java"] for (int bulletCount = 0; bulletCount < 10; bulletCount++) { spriteBatch.Draw(bullets[bulletCount], bulletsPos[bulletCount], Color.White); }[/source]

i try this and it says texture does not expect null

Edited by burnt_casadilla, 30 October 2012 - 01:35 PM.

If you see a post from me, you can safely assume its C# and XNA :)


#26 Tasaq   Members   -  Reputation: 1235

Like
1Likes
Like

Posted 30 October 2012 - 02:36 PM

You don't need to create array of textures for bullets. They look all the same, so you just need one texture.
so in drawing you do just:
[source lang="csharp"]for(int i = 0; i < bulletCount; i++){ spriteBatch.Draw(bullet, bulletPos[i], Color.White);}[/source]
For bullets I suggest you creating a class for this. It would contain a List of Vector2, time delay constant(to not spam bullets), bullet velocity, method to handle shooting bullets, method to update each bullets position, and one to destroy bullets (when they are out of screen or when they colide). Then, instead of using for loop with a fixed number of bullets, you can use foreach and thus have 'limitless' number of them. It's the easiest way i believe, in more advanced scenario you would also create a class for bullets, but leave that for later Posted Image

Edited by Tasaq, 30 October 2012 - 02:37 PM.


#27 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 October 2012 - 02:44 PM

oh crap. im completely hopeless when it comes to creating classes. I know how they work and how to call methods from in them but the bulk of the code confuses me

If you see a post from me, you can safely assume its C# and XNA :)


#28 Tasaq   Members   -  Reputation: 1235

Like
1Likes
Like

Posted 30 October 2012 - 04:44 PM

oh crap. im completely hopeless when it comes to creating classes. I know how they work and how to call methods from in them but the bulk of the code confuses me


But that's the essence of programming in languages like C# or Java. I suggest you to make some simple classes in windows console application. Create classes like calculator. Just to befriend OOP.

#29 Slushy   Members   -  Reputation: 168

Like
1Likes
Like

Posted 30 October 2012 - 04:46 PM

oh crap. im completely hopeless when it comes to creating classes. I know how they work and how to call methods from in them but the bulk of the code confuses me

Then I'd recommend doing alot of testing and trying different approaches. Simply messing around with classes, before you go and start making games. Classes are an essential part of games/programming overall. No point in rushing making games when you have to learn the basics first, I know it sucks. But sometimes life does.Posted Image

Edited by Slushy, 30 October 2012 - 04:47 PM.


#30 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 October 2012 - 06:42 PM

Can you point me to good, thorough tutorials on classes in c#? I can't stress thorough enough. If one detail is missed I get confused -.-

If you see a post from me, you can safely assume its C# and XNA :)


#31 Haps   Members   -  Reputation: 1315

Like
1Likes
Like

Posted 30 October 2012 - 06:47 PM

RB Whitaker's tutorials are a pretty good place to start. Classes are covered in #15 and onwards, but it's not a bad idea to go back a few more and cover the basics.

You are going to miss some things, eventually. It's inevitable with a non-standardized education, and the more thorough a resource is, the more people tend to gloss over it. The more concise it is, the more likely something gets left out.

Any time you run into trouble, try going back over the related sections in your books or tutorials to see if anything jumps out that you might have missed the first time.

Edited by Haps, 30 October 2012 - 06:52 PM.


#32 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 30 October 2012 - 06:57 PM

Thats actually the tutorial I "learned" c# from. I already know java and a little bit of c++ tho, so i may have just skimmed over it

If you see a post from me, you can safely assume its C# and XNA :)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS