2 questions C#

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

2 questions C#

Post by mattykins »

So as some of you may know i'm making a roguelike in visual C# Windows forms. And so far its coming along pretty good, however I have two questions.

1.How do I get windows forms to check for keyboard input. For example if the player presses the W key I want the game to run the code I have to move the player up.

2. How should I go about doing collision detection? In my first attempt at the game I had a bunch of if statements checking to see if the player was at the edge of the map or below a tile. However this got to be overwhelming because as most of you probably know, roguelike genre games are all randomly generated. So there would be a LOT of if statements.

Someone said on my original post to use arrays for the tiles. But how do I get an array to hold an x, a y, and a boolean value. For examlpe, if a tile at 10, 14 has a wall on it, I want the x to be 10, the y to be 14 and the bool to be true.

Thanks :)
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: 2 questions C#

Post by Chris »

1. Shouldn't be too hard to achieve, I've never developed with C# however it does remind me an awful lot of Java.

Code: Select all

private void controls(  object sender, KeyPressEventArgs e )
{
    switch( e.KeyChar )
    {
        case 87 : // off the top of my head w is 87 (at least in JavaScript)
            player.position.moveUp(); // move player up
        break;
    }
}
2. As for the player position and collision detection, I'd advise going down the vector route, this will make it really easy to compare locations for collision detection.
http://msdn.microsoft.com/en-us/library/bb199660.aspx

All you need to do is store objects and their locations in an array. When it comes to moving, make sure there are no objects on the x, y axes that block movement.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: 2 questions C#

Post by mattykins »

Thanks :) However, I'm not using XNA for this game. I'm using windows forms and picture boxes...heh... Its working out very well so far tho.
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: 2 questions C#

Post by Chris »

Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: 2 questions C#

Post by mattykins »

Oh thanks, how would I use the vector class tho? I know i create a new class called Vector2 and copy/paste that code.

Then when I wanna use it do I call it like:

Code: Select all

Vector2 Playertile = new Vector2(100, 100)
EDIT: Also do you know how to get transparent backgrounds for sprites?

Like I want my background to show through it.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2 questions C#

Post by Jackolantern »

As usual, whenever you are contemplating writing a ton of IF statements, that should always be a sign that the collection of objects need to be stored in an array or collection of some other kind, and you need to write a general-usage method that accepts an object from this array (maybe of type "Block", "Wall", or however you have it set up in your game) and the player character and can see if they are touching. Then you use a FOR loop so that you can iterate through all of the objects in the array, and pass each one into the method as well as the player to compare it.

As for the transparent sprite backgrounds, I have no idea how to do that in Windows Forms. The last few times I have worked in .NET desktop applications, I have used WPF.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: 2 questions C#

Post by mattykins »

Hmm yeah. The problem is this game is a roguelike so everything is randomly generated. essentially my map is 40x40 tiles, so 1600 tiles.
and they are divided into 8x8 rooms, the 8th tile always being a wall. However inside the 7x7 it can be completely random, how do I store each individual tile in an array, Also the tiles are 50 pixels wide each, so how do I get a tile thats at 150, 150 pixels to be at the array[3][3]

EDIT: I would also need the array to hold a couple bool values, Like if there is a wall there, if there is a treasure chest there, Or if there is a monster, etc.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2 questions C#

Post by Jackolantern »

Create a tile class that contains any info you need in it. A class can hold anything you want. Then create another method (maybe a static method would be appropriate) to randomly generate rooms, taking in whatever values you need to carry out the random generation. The tiles could contain their own location info that is used to place them. Then store the tiles inside an array or collection. For simplicity, you could even store the tiles into the array inside the method to randomly generate the levels, although really that should be 2 different methods.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: 2 questions C#

Post by mattykins »

Hm, so how would the tile class look in general?
simlar to:

Code: Select all

static private bool wall;
static private bool treasure;
static private bool stairs;
static private bool monster;
static private bool boss;
//More bool values for more things
//And then have the random generation method like

static public void floorGen
{
//method to generate the rooms
}
Also another thing I would like your opinion on is how to go about the random generation, I was thinking of having a string of numbers and letters generated and based on that string it would create new picture boxes and move them into place. Is there a way to create a new instance of a picture box class and put it in certain locations? Because if there isn't the only thing I can think of is have a ton of wall tiles, treasure tiles, etc laying offscreen and just move them on screen when the map generates.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2 questions C#

Post by Jackolantern »

Without knowing the specifics of your game, that looks pretty good! Of course you need () on the static method. Maybe the tile class could look something like:

Code: Select all

class Tiles {
   private int row;
   private int column;

   private bool wall;
   private bool treasure;
   private bool stairs;
   private bool monster;
   private bool boss;
   //etc...

   public Tile(int rowIn, int col, bool isWall, bool isTreas, bool isStairs, bool isMonst, bool isBoss) {
      this.row = rowin;
      this.column = col;
      this.wall = isWall;
      this.treasure = isTreas;
      this.stairs = isStairs;
      this.monster = isMonst;
      this.boss = isBoss;
   }
}
This way you can create the tiles through its constructor. You would probably create the tiles inside of the room generation method, or maybe a method that works with the generation method (adding another later of abstraction).

EDIT: Oh yeah, and you can't make the Tile fields static, because that would make them shared between all instances of the Tile class. That isn't what you want since you want to have many different Tiles.
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”