Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Pokemon Enemy Random Generator  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently I'm trying to make a pokemon simulator using Java and have a question for this(so far I only managed to make the basic JFrame with a moving oval(will be modified later as the moving character) in a window). How do I code random encounter with enemies like one in a pokemon game(using Java)? Thanks a lot. I'm new and will be pleased if anyone is willing to guide me on this game making.
 
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not being familiar with Pokemon I'd need to know the rules for an "encounter". Do "enemies" reside in a fixed location and an "encounter" occurs when the player attempts to move onto that location? Do enemies appear at random locations and an encounter occurs when that location happens to hold the player? Or something else?
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The enemies are going to spawn hidden at random location when the program runs(at the beginning), and the encounter happens when the character reaches a specific location where an enemy stays. The enemies wont move.
 
Ranch Hand
Posts: 95
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.  

And also some code will help to show us where you are stuck. Its easier to help that way.
 
Joseph Mokenela
Ranch Hand
Posts: 95
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you might want to define a Move class or something along that name to encapsulate the moves allowed in  the game.  Define some battlefield class, perhaps make it multidimensional array. the location will be the coordinates where each player is at a given point. Then you can define a method to randomly select the location based on the rules you define.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joseph Mokenela wrote:Welcome to the Ranch.  

And also some code will help to show us where you are stuck. Its easier to help that way.


Below is the code for the main class to initiate window



This is an object class Character



Thanks for all the replies! =D
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that the game is played on a 2D grid, meaning that Pokemon and enemies have an X,Y position. Or, can the position be anywhere on the screen where the X,Y coordinate is a pixel location? Are enemies visible? I assume that Pokemon and enemies have a fixed size relative to the grid.

So, you'd have a Sprite that may be either the Pokemon or one of the enemies. A Sprite has an X,Y position. A sprite has a width and height (which may just be 1). And you want to know if two sprites occupy overlapping space. Is this correct?

You can see where I'm going don't you? You need to break down your problem, "the game", into smaller and smaller requirements until they are small enough to be solvable with some code. Fortunately for me I have no prior knowledge so I can ask these kinds of questions. You, on the other hand, need to put down in English (or your native language) a detailed set of requirements so that somebody like me could implement them.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...or... you at least have to be specific enough that I can understand where you're stuck and begin to have a clue as to how to guide you.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I assume that the game is played on a 2D grid, meaning that Pokemon and enemies have an X,Y position. Or, can the position be anywhere on the screen where the X,Y coordinate is a pixel location? Are enemies visible? I assume that Pokemon and enemies have a fixed size relative to the grid.

So, you'd have a Sprite that may be either the Pokemon or one of the enemies. A Sprite has an X,Y position. A sprite has a width and height (which may just be 1). And you want to know if two sprites occupy overlapping space. Is this correct?

You can see where I'm going don't you? You need to break down your problem, "the game", into smaller and smaller requirements until they are small enough to be solvable with some code. Fortunately for me I have no prior knowledge so I can ask these kinds of questions. You, on the other hand, need to put down in English (or your native language) a detailed set of requirements so that somebody like me could implement them.


Yes it is a 2D grid game like an old time pokemon battle game.
I'm planning to create a pokemon simulator with the following requirements:
Program runs and a character(will be controlled by user later using arrow keys) pops up in a location(fixed or random anything will be fine to me).
At the same time, enemies(or known as bots) will pop up hidden at random locations and stay at the specific locations.
When the character runs into the enemies battle will be initiated, each picks three pokemons and the battle ends when all the pokemons from one of the sides reach 0 hp.
Every pokemon has its own unique attributes(speed, skills with elements, damage, health etc)
Speed accumulator appears with a value of 0 for each side since the beginning of the battle and increases by the speed of the pokemon every round. The pokemon is allowed to attack when the speed exceeds 100, then the accumulator -100 after attacking.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will also be skill accuracy for each skill in the form of percentage.
The character can switch pokemon anytime they want and precious pokemon's health points are retained, while the enemy bot casts skills randomly.
The user can choose not to attack for a specific round(skip) and the speed accumulator will not be reduced in value.
There is still space for improvement but that's all that I want for now.
Thanks!
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the game played on a coarse grid of, say, 20x20 where each entity wholly occupies a grid location such that two entities with the same X,Y location are considered to have collided?

Or are we talking about pixels of, say, 1920x1080 where each entity has an X,Y location and a width and height used to determine if there is a collision?
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Is the game played on a coarse grid of, say, 20x20 where each entity wholly occupies a grid location such that two entities with the same X,Y location are considered to have collided?

Or are we talking about pixels of, say, 1920x1080 where each entity has an X,Y location and a width and height used to determine if there is a collision?



It will be the latter, as I'm planning to assign width and height for the character. Can be the former one too if the image used to represent the character(represented by an oval in the code above) can fit into a grid location. I'm just nt sure whether it's possible.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I'm planning to save an image to the background as a map(but there will be no obstacles, only hidden enemy bots). So I basically just have to find an image and save it to the background and make it look like a map right?
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question, so I was planning to set an image as the background. I created a class named Map, initiated a variable 'map' in the main class(Field) and added the map variable to the paint method. But nothing appeared when I run the code, please help thanks~
class Map


class Field(main class)

 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't gone through this thoroughly but it doesn't appear that you're setting the Field width and height anywhere.

Regarding grid vs pixels, I think this would depend on if a move goes from cell to cell or from pixel to pixel. Does "speed" indicate the number of pixels moved with each call to move() or how many grid positions.

I would caution against naming one of your classes "Map". This is a very significant component in Java Collections that you may need/want to use as your design becomes clearer. Sorry, off hand I can't think of a replacement name for it at the moment.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To determine if two things collide in pixel space you'll need to compute a bounding box for each one and store it with the object. If an object moves then the bounding box would need to be recalculated. A bounding box is essentially: min-x, min-y, max-x, max-y. You can get this from the X,Y coordinate of the upper left corner and the width and height. Then you write a method to see if two bounding boxes overlap. If you draw this out with pencil and paper you'll see that this test is not to hard to compute.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Haven't gone through this thoroughly but it doesn't appear that you're setting the Field width and height anywhere.

Regarding grid vs pixels, I think this would depend on if a move goes from cell to cell or from pixel to pixel. Does "speed" indicate the number of pixels moved with each call to move() or how many grid positions.

I would caution against naming one of your classes "Map". This is a very significant component in Java Collections that you may need/want to use as your design becomes clearer. Sorry, off hand I can't think of a replacement name for it at the moment.



Oh ya I didnt set any width and height for field, perhaps I should use frame indtead of field? (Trying to get the image to fit the size of the window frame) Thanks for pointing me out.

If for speed as in the speed of the pokemon, it has nothing to do with moving of the character, every pokemon has a unique speed and pokemons will only be picked during battle.
I cant tell the difference between cell to cell and pixel to pixel. Do they have contrast or some sort of pros and cons? Which one is recommended for a simple mini Java game like this?

Okay I will change the name of class Map to another one, thanks.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:To determine if two things collide in pixel space you'll need to compute a bounding box for each one and store it with the object. If an object moves then the bounding box would need to be recalculated. A bounding box is essentially: min-x, min-y, max-x, max-y. You can get this from the X,Y coordinate of the upper left corner and the width and height. Then you write a method to see if two bounding boxes overlap. If you draw this out with pencil and paper you'll see that this test is not to hard to compute.


Is it something like the ones used in creating a tennis game(collision(), getBounds(), getTop())with the help of the Rectangle class?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathaniel Oyt wrote:

Carey Brown wrote:To determine if two things collide in pixel space you'll need to compute a bounding box for each one and store it with the object. If an object moves then the bounding box would need to be recalculated. A bounding box is essentially: min-x, min-y, max-x, max-y. You can get this from the X,Y coordinate of the upper left corner and the width and height. Then you write a method to see if two bounding boxes overlap. If you draw this out with pencil and paper you'll see that this test is not to hard to compute.


Is it something like the ones used in creating a tennis game(collision(), getBounds(), getTop())with the help of the Rectangle class?

Exactly.

Pixel/pixel thing:
Think of it this way, if you had a piece of graph paper you could draw a circle in one of the cells on the paper then you could draw another one in an adjacent cell. This would be the equivalent of moving one space in grid-space. If on the other hand, you draw a circle just slightly to the right so that part of it is still in the original graph paper cell and part of it is in the adjacent cell, this would be more like pixel to pixel movement.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I c, thanks for the explanation.
Both will work for me, but I may opt for the option that's easier to be implemented as I don't have much time left haha
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day, I set the size of field variable to be the same as the frame's as I couldn't put the frame in class Field into Backg class. Still, nothing happened. How do I solve this?
Another question is to reconfirm regarding the collision thing. So I just have to basically create a class for enemy bots that are randomly painted in locations at the run of program, and coat them with Rectangle so that when collision happens battle mode is initiated?
Lots of thanks.

Below is my code for the first first question.



 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the painting bots, should I just generate random Rectangles that trigger battle instead? I think that makes things easier, the bots will only come in during battle phase.(I dont know the way to print invisible shapes on screen and thought there's no necessity to do so if just printing Rectangle s which are originally invisible is plausible). Or not?
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathaniel Oyt wrote:Good day, I set the size of field variable to be the same as the frame's as I couldn't put the frame in class Field into Backg class. Still, nothing happened. How do I solve this?
Another question is to reconfirm regarding the collision thing. So I just have to basically create a class for enemy bots that are randomly painted in locations at the run of program, and coat them with Rectangle so that when collision happens battle mode is initiated?
Lots of thanks.

Below is my code for the first first question.




Even after manually setting the width and height of background to 500, 500 nothing is painted in the background. Why? Sorry for the dumb questions
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used new File("...").exists() to check if the path is correct and the output is true so there seems to be no problem with the path.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I recall correctly, asking swing to load an image will create a background task to do the loading and then immediately continue on whether the image has been loaded or not. There's a way to test if the image has been completely loaded before continuing but I can't remember the steps right at the moment. You may need to google swing and image loading.
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's your problem

The 2nd and 3rd arguments are the X,Y coordinates of the upper left corner, not the size.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright will try that later, thanks!
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:If I recall correctly, asking swing to load an image will create a background task to do the loading and then immediately continue on whether the image has been loaded or not. There's a way to test if the image has been completely loaded before continuing but I can't remember the steps right at the moment. You may need to google swing and image loading.

Turns out that this is not the problem. This works.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah I c thanks, do I have to replace something with the code or add it somewhere in class Field?
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it! Thanks a lot for all the help! =D
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there! Sorry I have another question. So I'm now going to create a class that contains the information about the pokemons that will be used later in the battle.(The bot is going to pick the pokemons randomly while the user chooses from among them). The stats of the pokemons will be needed, which are skills(and stats of skills), damage, health, defense, speed etc. What is the best way for me to create this class?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've already got the "Character" class, you could add those fields to that. Alternatively you could have a "Stats" class to hold those items and have your Character class hold an instance of Stats. The potential down side of the 2nd approach would be another layer of verbosity but it is better in terms of separating responsibilities.
 
Marshal
Posts: 61727
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You've already got the "Character" class . . .

Since there is a class in java.base/java.lang with the same name, ins't a Character class going to store up trouble for the future?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:You've already got the "Character" class . . .

Since there is a class in java.base/java.lang with the same name, ins't a Character class going to store up trouble for the future?

Yeah, already got bit by that when I copied the code into my playpen directory. I ended up re-naming to "PokemonCharacter", "PokemonPanel", etc.. I also put all the classes into a single "Pokemon" file (not recommending this but it does make it easier on us to replicate any problems).
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:You've already got the "Character" class . . .

Since there is a class in java.base/java.lang with the same name, ins't a Character class going to store up trouble for the future?


Thanks, I will change the name.
 
Nathaniel Oyt
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You've already got the "Character" class, you could add those fields to that. Alternatively you could have a "Stats" class to hold those items and have your Character class hold an instance of Stats. The potential down side of the 2nd approach would be another layer of verbosity but it is better in terms of separating responsibilities.



Sorry, I'm a little confused here. if I use character.getStats() does it assign the stats to the character? Or just obtaining the stats of the pokemon picked by the character to use in battle?
Actually I'm planning to create another two classes(one for conditions in battle and another for the bot that randomly picks pokemons and uses skills)
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use getters and setters. To SET a Stat parameter it would be
 
Campbell Ritchie
Marshal
Posts: 61727
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't there a way to design the interface so as not to use several dots in the expression?
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathaniel Oyt wrote:Actually I'm planning to create another two classes(one for conditions in battle and another for the bot that randomly picks pokemons and uses skills)

I'm not familiar with the game but as you go through the design phase you look for nouns: bot, pokemon, stats, etc., they become your classes. Verbs become your methods: move, draw, setSpeed, etc..
 
Carey Brown
Saloon Keeper
Posts: 5144
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm not familiar with the game but as you go through the design phase you look for nouns: bot, pokemon, stats, etc., they become your classes. Verbs become your methods: move, draw, setSpeed, etc..

Then you look for relationships: is-a, has-a. A game (panel?) has-zero-or-more Bots implies a Collection.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!