Unit 8
Production Part 3 - Time to Battle
Tutorials used:
Rock,Paper, Scissors -
Health -
Now I needed to create the actual gameplay that I want my game to follow. To do this I wanted to have the player touch a character on the overworld and get taken to a battle screen. The player would then have three actions to choose from in order to 'fight' the opponent.
The first step was to create a room for the battle. this is where the player will be taken to when they touch a character on the overworld. I used some objects to design the look of the room. This reusing of assets allows me to use more time developing the game rather than creating new assets.
Next I needed to prepare the code to allow the player to make there choice on what they wanted to do, as well as code that lets the computer choose as well. To begin I made a create event and a draw event. I began with the create event.
//Choice
text = "What will you do?";
This is setting a variable that will make the computer draw some text in the room. This is due to the "" around the text.
//Wins, Draws and Losses
wins = 0;
draws = 0;
losses = 0;
This is telling the computer that the win, draw and lose variables will be zero when the room starts.
//Enemy choice
enemyText = "";
enemySprite = 3;
This is setting up the text that will appear for the opponent to tell the player what the computer chose.
//Result text
resultText = "";
This is a variable that will allow some text to appear to tell the player the result of the action and whether they won, lost or drew.
The next list is something known as a 2D array, as it works with two variables instead of one. This allows more complex strings.
//Player choice
choice[0,0] = "Attack";
choice[0,1] = attack_spr;
choice[0,2] = 0;
choice[1,0] = "Block";
choice[1,1] = block_spr;
choice[1,2] = 74;
choice[2,0] = "Break";
choice[2,1] = break_spr;
choice[2,2] = 148;
The list is split up int three parts and each part has three different pieces of infomation for the computer to use. The first is some text to represent the players choice, next is the name of the sprite for said choice and the final is a number that is where the sprite will be placed in accordance to the location of the object.
//Result
result[0,0] = "Draw";
result[0,1] = "Lose";
result[0,2] = "Win";
result[1,0] = "Win";
result[1,1] = "Draw";
result[1,2] = "Lose";
result[2,0] = "Lose";
result[2,1] = "Win";
result[2,2] = "Draw";
This 2D array will be used to write text to tell the player whether they won, lost or drew.
once I had finished the create event, I moved on to the draw event. This is where I will use the code from the create event and some new pieces to create my combat system.
draw_set_halign(fa_left);
draw_set_valign(fa_middle);
draw_set_font(menu_fnt);
draw_set_color(c_white);
This is just some code to change the appearance of the text. This was used in almost all apects of my game.
//Choose your action
Right... This next bit looks confusing so I will try to explain... This is called a for-loop.
for (<statement1> ; <expresion>;<statement2>) <statement3>
First statement1 is executed, then the expression is checked and if true, statement3 is executed. Then statement2 is executed and if the expresion is still true then statement1 is executed...
for(value = 0;value < 3; value += 1)
First it checks some values...
{
///battle_scr
draw_sprite(choice[value,1],0,x+choice[value,2],y);
... If they are true then it will draw a sprite based on two things, first what was the value equal to and then it will check the choice array I set up in the create event and pick one that's value is equal to the value of [value,1].
Next it checks choice[value,2] to determine the location of the sprites.
if mouse_x >= x+choice[value,2] and mouse_x < x+63+choice[value,2]
and mouse_y >= y and mouse_y < y+63
Then it checks if the mouse is on one of the sprites...
{
draw_rectangle(x+choice[value,2],y,x+63+choice[value,2],y+63,1);
if mouse_check_button_released(mb_left)
... And if it is then it will draw a box around the sprite the mouse is on.
{
text = choice[value,0];
enemy = irandom(2);
This is all that is needed to make the computer choose a random choice...
enemyText = choice[enemy,0];
enemySprite = choice[enemy,1]
resultText = result[value,enemy];
switch (resultText)
These are some more variables that I set to allow the game to show text that will say what the player chose, as well as what the enemy chose as well.
{
case "Win": wins += 1; global.ehp--; break;
case "Draw": draws += 1; break;
case "Lose": losses += 1; global.hp--; break;
These are are called case statements. They are basically if statements that work by checking to see if something is true and if so perform a series of actions.
In this it is telling the computer that if the player:
Wins then add 1 to wins variable, lower the enemy's health (Explained later) then stop.
Draws then add 1 to draws then stop
Losses then add one to losses, lower players health (Explained later) then stop.
}
}
}
}
//Player Interface
draw_text(x,y-50,text); // Player Selection
draw_text(150,550,"Wins: "+string(wins));
draw_text(150,600,"Draws: "+string(draws));
draw_text(150,650,"Losses: "+string(losses));
draw_text(608,350,resultText); // Player Result
This code tells the computer where to draw the text of the battle interface
//Enemy Interface
draw_text(608,100,enemyText); //Enemy Selection
if enemySprite < 3
{
draw_sprite(enemySprite,0,608,200);
}
This draws the text that shows what the enemy chose.
Now its time to add the health system to the game. First I created a sprite that I wanted to act as my health indicator, I chose a heart as I felt that it would be clear that it is supposed to be health. I also made an empty heart, that would replace a full one if damage was taken.
Then I created an object called hp_obj and added four events:
Create
Step
Draw
Draw GUI
Draw GUI is the same as a draw event, but it is supposed to be for a Graphical User Interface, so it places it on a different layer.
First one is nice and easy. On the draw event just add a comment. This overrides the sprite so that it wont interfere with the draw GUI event.
This is also not too complex...
///Initalise the HP
global.hp = 5;
global.maxhp = 5;
This just sets the variables that tell the game how much life the player has and what is the maximum that they can get.
The global. part before each variable just tells the computer that these variables can be used in any part of the game. This allows the variables to be referenced easily in other parts of the coding.
//set the size of the GUI layer
display_set_gui_size(view_wview[0],view_hview[0]);
If you go into a room such as the overworld, that uses views, Then the GUI layer will change to match that, but will also distort anything on the layer as well. This code tells the game to keep the size of the objects the same.
This is the draw GUI event. This is where the sprites will be drawn
/// Draw the hearts
var xoffset = 48;
This variable will allow me to easily tell the game how far each heart will be from each other.
// Draw empty hearts
for (var i=0; i<global.maxhp; i++) {
draw_sprite(emptyheart_spr, 0, xstart+(xoffset*i), ystart);
}
This for-loop tells the game to draw the empty heart sprite if the hp variable is less than the max hp and add one for each number below max.
// Use a for loop
for (var i=0; i<global.hp; i++) {
draw_sprite(hp_spr, 0, xstart+(xoffset*i), ystart);
}
This tells the game to add one heart sprite equal to the global.hp variable. It places these sprites in correlation to the location of the objects placement in the room and spaces them out based on the xoffset variable.
This is the step event and is where we tell the game to add or subtract health and what to do if the health reaches a certain number.
/// Add or subtract health
//Add
if (keyboard_check_pressed(vk_up))
{
global.hp++;
}
Now... The game was not supposed to have any way of adding health, but I decided to put it in to help in testing the game and making sure that the health works correctly.
// Make sure hp does not go higher than max
global.hp = clamp(global.hp, 0, global.maxhp)
This is called a clamp statement. It tells the game not to raise a variable above a certain amount, in this case I am telling the game not to raise the players hp above the maxhp variable.
if global.hp = 0
{
instance_create(0,0,fadetogameover_obj)
}
This is telling the game that if the players hp reaches zero then to create an object that I made. This object will take the player to a game over screen, but I will talk about that in my next part...
Next I duplicated this object and replaced all of the hp variables with ehp and all of the maxhp with emax hp. This was to be the enemys health.
If placed into the battle room, this object would allow the game to raise and lower the health of the player and their opponent.
Next I duplicated this object and replaced all of the hp variables with ehp and all of the maxhp with emax hp. This was to be the enemys health.
If placed into the battle room, this object would allow the game to raise and lower the health of the player and their opponent.
No comments:
Post a Comment