We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So i'm making a turn based role-playing game for a project and i'm not exactly sure how to set some of it up. The game uses WASD to move and mouse functions to make decisions (start game, choose character, attack, use potion etc...). My problem is initiating battles based off where the character is positioned. Initially I was going to spawn 3 enemies in somewhat random areas of the screen and have the battle initiate when the character moves within a certain vicinity of any given enemy. Would this be possible? Or is there an easier way to initiate battles? Keep in mind, this is my very first year in processing and most of my class-mates are remaking pong or snake... Any ideas are greatly appreciated. I should also mention that I am using the currentScreen variable to make sure buttons can only be clicked and your character can only be moved when they are visible (when you are on the right screen). Here are some parts of my code that may be useful to know:
if (keyPressed) {
if (battle == false) {
if (key == 'w' || key == 'w') {
playerPosY = playerPosY - moveSpeed;
} else if (key == 's' || key == 's') {
playerPosY = playerPosY + moveSpeed;
} else if (key == 'a' || key == 'a') {
playerPosX = playerPosX - moveSpeed;
} else if (key == 'd' || key == 'd') {
playerPosX = playerPosX + moveSpeed;
}
}
}
if (currentScreen == 3) {
if (ranEnemy1 == 1) {
image(goblin, enemy1PosX, enemy1PosY, 80, 80);
} else if (ranEnemy1 == 2) {
image(skeleton2, 310, 510, 110, 110);
} else if (ranEnemy1 == 3) {
image(skeleton, 530, 360, 80, 80);
}
if (ranEnemy2 == 1) {
image(goblin, 600, 330, 80, 80);
} else if (ranEnemy2 == 2) {
image(skeleton2, 720, 190, 110, 110);
} else if (ranEnemy2 == 3) {
image(skeleton2, 570, 160, 110, 110);
}
if (ranEnemy3 == 1) {
image(goblin, 390, 480, 80, 80);
} else if (ranEnemy3 == 2) {
image(skeleton2, 620, 240, 110, 110);
} else if (ranEnemy3 == 3) {
image(skeleton2, 810, 420, 110, 110);
}
}
As you can see, I really had to complicate things already to randomize what kind of enemy would spawn and other related details. Some tips on how to simplify this would also be appreciated.
Answers
You need to select it after pasting and hit Ctrl+o.
Hope I did it right this time? lol
Good enough for me. When is your assignment due? Are there limitations on your code? Could you, for example, use arrays and objects?
It's due on the 27th and there aren't really any specified limitations but we haven't learned arrays or objects so I would just have no clue how to use them. I wouldn't mind learning though.
arrays or objects would simplify things. A lot. But let's leave this aside for the moment. Let's use arrays here.
you wrote
I suggest that you have a
list of vicinity
in an Array (this isbefore
setup()
! ):(the two arrays are parallel in that sense that index 0 or 1 hold information on the 0 or 1st vicinity (position and if it's fighting), ok? Think of two shopping lists lying next to each other.)
now when does the player enter the region ?
Check this with
dist()
against the player position:loop over all vicinities
ok.
What then?
You already have currentScreen / state of the program. Very good.
Similar to this I recommend a var gameMode what tells you if you currently fight.
gameMode
behaves similar to currentScreen in that it tells the program what to do (on a higher level).You might even give the gameModes names (constants)
so in the code above enter
So your program knows when to start a fight.
do the fight. You have to program this... with dice...?
When all enemies are dead (or the player) say
gameMode = gameModeRunAround ;
andvicinityIsFighting[fightIndex]=false;
andfightIndex=-1;
(with fightIndex storing the i from first loop).Alright well thank you for the quick and awesome response. Like I said, this is almost all new to me so forgive me if I don't understand some things at first. So, one thing I don't understand is the beginning of the for loop - when you said for (int i..............) am I supposed to replace this with something else? You used the i variable throughout the code many times and i'm not exactly sure what it represents. I also don't fully understand how PVector vicinities works, how is the program going to know where the enemies are/decide where exactly the vicinities are? Again, sorry if I sound like a total noob.. I only started coding in November.
Yes. It just loops over the array. So start by 0 increase by one until vicinities.length is reached.
The idea of PVector is that it represents a point with x and y.
since it holds x and y you reach them with pv.x and pv.y (with the dot)
with the array it's similar
just enter the locations where you want the battles to happen in
setup()
(and before
setup()
just the declaration as shown in last post)i
represents the index of the arraythink of a shopping list,
i
is the line number.array is like a shopping list, with the line number (e.g.
i
) you reach the item (e.g. milk or vinity position / vector / PVector)you wrote
you have to enter the vicinities in
setup()
as shown abovewhen the player meets a vicinity, the battle starts
I suggest you spawn the enemies around the vicinity (in a circle) :
[EDITED]
for (int i..............)
was a shortening offor (int i = 0; i<vicinities.length, i++)
Read about for loops.i
here is a counter and it is used to access the elements of array it goes each cycle 0 then 1 then 2 until it reaches the last element in the array. Read this topic.PVector is a variable that holds x and y and represents mathematical vector. If you don't know about using vectors in geometry, you may use 2 arrays instead for x and y. Read about PVector in reference and in very good book.
Well.. That kind of clears things up but this is still so confusing to me :/ I hate to ask this of you but I think what's confusing me is that I can't see the code together and in the right order.. Any chance you could put it all together including void setup, void draw and all that? If it would help, I can also paste more of my code, just let me know. Thanks so much for your help thus far
actually....
I suggest you give it a try and post it....
I mean, we wrote this is in setup()..... and that is before setup()... so I bet you can do it....
try it.
post it.
and ask more.
we are here
Well I can't really try it out at the moment, I'm in class but when I get home I'll see what I can do and I'll let you know.
you should implement this
Sure wish I knew what that did :-?
try it!
it just a flashing "BATTLE" for one second just shows that your battle begins ;-)
just a small gimmick...
Sounds cool. Just gotta wait till I'm home before I can try it
I can't believe you asked us to put the code together for you just because you're in school now ;-)
-1 point
I'm a noob I'm sorry :((
you're forgiven
So I haven't added everything yet but I added the variables and the for loop (including what Ater said) and I get a syntax error maybe missing a semicolon expecting SEMI, found "," i'm assuming this is just because im not very good at making for loops...
Use semicolons in your for loop:
post your entire code please
Ok now it says consider adding "=" and when I run it it says IdentifierOrNew" to complete ReferenceExpression
All of it? it's 220 lines :P It's also not commented yet which I know is a big no no in processing but for reasons I cant really explain, i'm gonna have to comment it later
220 lines is not that big really and I think everyone will be able to understand what you're doing even wiothout comments. Try to implement some of suggestions given and post it. The code just needs to be runnable.
This is pretty much my code when it worked fine, with a few of the suggestions added but I couldn't really get any of them to work xD Its quite buggy and I'm gonna have to fix things like movement in the future... Like how as soon as I implemented the title screen, my character got really laggy and the buttons don't always work which is strange... I'm working on getting the image links one sec.. image links: - I turned this one into a png - turned this one into a png too And for some reason I couldn't find warrior and title page so just replace those with something else
all loadImage lines must be setup()
since this belongs to the preparation part of the game, ok ?
same goes for loadFont
your draw() got very long
since you use currentScreen
why not use functions and say
==========================================================
so the code would like this
I corrected some other smaller stuff so it runs now
Alright I can arrange that Edit: I put them all in setup and the lag is gone :) thanks!
in the meantime I made a new version for you, see above
you need this
Hmm.. for some reason when I run it there is a problem with the medieval font that I used. Could not run the sketch (target VM failed to initialize). Also, should I keep the comments such as:
or are they not important?
don't need the comments
it's better to use createFont instead of loadFont
just place
,45);
at the end of createFontOk so I fixed the font issue fairly easily but now when I choose a character in game, the screen goes black and I get target VM failed to initialize :( i'm starting to think I should have done an easier game xD The error highlights this line btw :
if ( dist (playerPosX, playerPosY, vicinities [i].x, vicinities [i].y) < 30 ) {
Edit: it also says NullPointerException if that helps
you need this
in setup()
you need to move this
into functionGamePlay() I think
Yup I just figured that out. Now I need to make it so that a battle starts whenever the character goes near en enemy. Right now it starts a battle when it goes to certain areas on screen. Is this going to be difficult? If it's complicated I can simply make the enemies spawn in specific locations every time
no, it's not hard .
just enter the places of the enemies here:
meaning: change the numbers in the () brackets which are x,y
Yeah I understand that, my problem is that I want to randomize what kind of enemy spawns in each of those 3 spots. What do you think would be the easiest way to do this?
I'm off now
similar to Char=1
have
enemyChar1 = 0 or 1 or 2
enemyChar2 = 0 or 1 or 2
enemyChar3 = 0 or 1 or 2
set those via random
then act accordingly and display a different image (as you did with if(Char==1....)
Well thanks so much for your help! I feel like I should be crediting you at the end of my game :P
The easiest way to do it would be to learn how to do Object Oriented Programming.
That way, you can define a "Combat Zone" object that HAS A position (which would be a PVector), and HAS An array of Enemy objects. And then you could define and "Enemy" object to keep track of that enemies's relative position in the combat zone (PVector), as well as it's health (int), max health (int), level (int), name (String), attack patterns (functions), type (int), etc, etc, etc.
Guess ill look into it :) thanks for your help too :D
Here's a start on an enemy (well, I called it a foe) class:
Really study this example. It's going to save you a ton of time.
approach without objects:
it's important that those numbers match with where the enemies must stand later:
now, next:
this is a variable :
int a = 1;
it is a box named
a
that holds a value 1. it is allowed to hold only typeint
(these are integer numbers)now PVector:
PVector is a special box for a point. Since a point needs x and y, the box has two chambers / compartements with a value each. To get those values (x=23, y=21) we say
b.x
andb.y
.try it:
this is a complete sketch (entire sketch)
Nice!
;-)
now we can like we have a var a also a list / array of vars
the array is called c
so it's a shopping list and at position/ line number 0 we hold the value 17
smilar we can make a array from PVector
list has 3 lines (each holds a PVector)
now since you can for-loop over an array and do something with each item in a list, a list or array is very powerful tool.