2014-11-28 09:17:13

Hello everyone!
Please tell me how to create an enemy in bgt, I have a field x 20 how to create the position of 20? that he was left with tub, reproducing the sound of his voice, when he came with tub or I to him, so he attacked and I take away -5 hp, and I, too, could attack a space, write example code here Please standard field:
#include "dynamic_menu.bgt"

const int board = 20;
int player_position = 1;
int hp = 20;
sound music1;
sound music;
sound shag;

void main()
{
music.load("music.wav");
music.volume = -30;
music1.load("music1.wav");
music1.volume = -10;
shag.load("shag.wav");
shag.volume = -10;
tts_voice voice;
dynamic_menu menu;
menu.allow_escape = true;
menu.wrap = true;
menu.add_item_tts("Start game");
menu.add_item_tts("Test speaks");
menu.add_item_tts("Exit game");
show_game_window("Enemy");
voice.speak_wait("Welcome to Enemy");
music.play_looped();
int choice;
do
{
choice = menu.run("Please choose a menu item with the arrow keys, then hit enter to activate it.", true);
if(choice==1)
{
music.stop();
game_play();
}
else if(choice==2)
{
music.stop();
alert("Game", "unfinished game");
music.play_looped();
}
}
while(choice!=0 and choice!=3);
music.stop();
voice.speak_wait("Thanks for playing.");
}

void game_play()
{
while(true)
{
music1.play_looped();
if(key_pressed(KEY_LEFT) and player_position>0) {
player_position--;
shag.play();
}
if(key_pressed(KEY_RIGHT) and player_position<20) {
player_position++;
shag.play();
}
if(player_position<0)
{
player_position++;
}
if(player_position>20)
{
player_position--;
}
}
}

2014-11-30 13:16:16

please help three days there is no answer

2014-11-30 19:10:27

A couple of things, while I can see your code.
1. The way your menu is set up is not great. You have it in your main function, which is a big problem should you want to reuse it. My advice to you is to define another function to hold your menu, like so:
void menu()
{
//menu code goes here
}
At that point you can simply call your menu function when it needs calling. You also put your menu in a do while loop, which I found a bit puzzling. To me it seems like you're unnecessarily nesting a loop inside another loop here. The dynamic_menu object will loop while the menu is waiting for a choice anyway, and I"d just put checks for 0 or 3 inside your loop and have done with it.
2. Your enemy. The best way would be to creat ean enemy class. I'll give you a dummy class definition just so you can see how you might go about it.
class enemy
{
int HP;
int speed;
int position;
void move(int direction)
{
if(position+direction > 20 || position+direction < 0)
return; //exit the function before it increments the position
position += direction;
}
}
This is definitely a skeleton of what you might want. You'd probably also want a timer for checking when the enemy would move. The timer would only let the enemy move once its elapsed property is greater-than or equal to a specified value, ,which you'd control with the speed property. Then there's the sounds the enemy might make--attacking, movement, etc. Such sounds are best controlled by the sound_pool object, which is documented in the helper layer part of the BGT reference.
I hope this was what you wanted; the code you wrote kind of suggested to me that you had a thorough enough knowledge of the language to grasp objects and how they would be implemented.

Best Regards,
Hayden

2014-11-30 20:39:35

Hello everyone.
Thank you, but still I'm interested how to make it sound reproduction of the enemy, and the functions of weapons, shooting, I do not understand.
Time is short, you need to know about it immediately on blastbay forum do not respond, Philip, too, help please, please, please!

2014-11-30 20:42:02

At least give an example or a game where it can be viewed

2014-12-01 22:38:23

well, please help

2014-12-01 23:20:51

We can't.  This is unique to every game that is written.  I'm not even 100% sure what you want, I'm afraid, but throwing source code at you won't help.  Have you tried the BGT tutorials?  I could be mistaken, but I thought they covered this kind of thing.  If you haven't read them, you need to start there.

My Blog
Twitter: @ajhicks1992

2014-12-02 01:35:21

Every game is different.
If I pasted the zombie code from the Swamp Platformer, it would probably be useless for you.
I can't really paste code from Sengoku Jidai or JLA because they are big and complicated and I decided to be minimalist with the classes in those cases.


Here's a commented sidescroller example I threw together at some point. I seem to remember it having problems or something, but it gets the idea across.

#include "sound_pool.bgt"
#include "dynamic_menu.bgt"

// The above lines add data from external script files. Those two come with BGT.

/*
 * Multi-line comments like this one will appear at the beginning of each major section of this file.
 * You can search for /* (slash asterisk) to jump to the next section.
 */

// These variables can be referenced by any part of the program:
int level;
int lives;
double x; // The player's position.
double health; // The player's health.
double y; // The player's y position.
double recovery; // How long before the player can attack again.
bool has_key; // Whether or not the player has found the key.

// The following variables are all objects.
sound_pool pool; // The simplest way to play lots of sounds.
tts_voice voice; // For text-to-speech.
dynamic_menu menu;

// Let's set some constants for the map:
const uint8 clear=0, pit=1, fan=2, door=3, heal=4, key=5;
// The map itself is an array of integers.
 uint8[] map; // We don't define a size yet; the map can change size as you advance in the game.
int[] slots; // Parallel to map, keeps track of the sound slots. Only so we can silence items that the player collects.

/* Enemy class.
 * This class defines an enemy, and all of its properties and methods.
 */

class enemy {
double health; // How much damage is needed to kill this enemy.
double strength; // How much damage this enemy does to the player.
double speed; // How quickly this enemy moves.
double x; // Enemy position.
double recovery; // How long before this enemy can attack again.
double attack_speed; // When an enemy attacks, recovery will be set to this value.
bool flies; // If true, this enemy stays in the air.
bool dodges; // If true, this enemy is immune to pits and fans, otherwise it can die just as easily as the player.
int slot; // This could be a sound object, but it's simpler to reference the sound that the enemy makes in the sound pool.

// The constructor sets the properties randomly.
enemy() {
 x=random(1, 10);
health=random(1, 10);
slot=-1; // -1 means it has not been matched to a sound.
speed=random(1, 5);
strength=random(1, 15);
recovery=0.0;
attack_speed=random(250, 1500);

// Set flies and dodges randomly:
int number=random(0, 5);
if(number==5) flies=true;
else flies=false;
number=random(0, 5);
if(number>3) dodges=true;
else dodges=false;
// The above could have been accomplished without using ifs, but this way should make more sense.

}

// This method updates the enemy's sound, if it has one.
void update_sound() {
// First, we make sure that the slot corresponds to a sound.
 if(slot>=0) {
// Move the sound to the enemy's current position:
pool.update_sound_1d(slot, x);
}
else {
// The enemy should probably be making sound.
// The sound should depend on whether or not the enemy is flying.
string toplay="sounds/enemy.wav";
if(flies) toplay="sounds/flying.wav";

// Since the class contains a variable called x, we use two colons (::) to reference the player's position when playing the sound.
slot=pool.play_1d(toplay, ::x, x, true);
}
}

// This method moves the enemy, and attacks the player if in range.
// It takes a double as an argument, as otherwise there is no way for the method to know how much time has passed.
void step(double time)
{
// Save the player position as something more distinct.
// We use two colons (::) to distinguish the global variable x from the enemy property of the same name.
double target=::x;

if(recovery>0.0) {
recovery-=time;
}
else {
// Let's back up x. This will be useful later.
int backup_x=round(x, 0); // We also rounded it to the nearest integer.

// The enemy moves toward the player.
// Notice that in both cases, we divide time by 1000, to convert it to seconds.
if(x>target) {
x=x-(speed*time/1000.0);
}
else if(x<target) {
x=x+(speed*time/1000.0);
}

// Now we use backup_x to see if we should play a step sound.
// We only do this if the enemy is on the ground, and if the integer part of x changed, so we combine two conditions:
if((!flies)&&(backup_x!=round(x, 0))) {
// Add a random number in the middle of the filename, so we use different step sounds each time.
 pool.play_1d("sounds/step" + random(1, 5) + ".wav", target, x, false);
}

// If the enemy is close enough to the player, attack.
if(absolute(x-target)<=1.0) {
// They must be at the same vertical position:
if((flies)||(y<=0.0)) {
pool.play_stationary("sounds/hit_player.wav", false);
hurt_player(strength);
}
else pool.play_1d("sounds/miss.wav", target, x, false);
recovery=attack_speed;
}

}

}

// This method tells us if this enemy is alive.
// We'll use this later to determine when to step enemies, and when to remove them.
bool alive() {
 return health>0;
}
}

// This array contains the enemies in the game:
// We use an @ sign to indicate that we're using handles; otherwise, lots of things could go wrong.
enemy@[] enemies;


/*
 * The main function is where the program starts.
 */

void main() {

// Lets begin by setting up the menu.

// Use the same tts we're already using.
menu.set_tts_object(voice);

// Add our options:
menu.add_item_tts("Start game");
menu.add_item_tts("Set voice rate");
menu.add_item_tts("Help");
menu.add_item_tts("exit");
menu.wrap=true;

// Now, create the window:
show_game_window("Simplest Side Scroller Ever!");

// This loop lasts forever; the only way to get out of it is by selecting exit from the menu.

while(true) {
int value=menu.run("Use arrow keys and enter to select an option.", true);

if(value==1) new_game();
else if(value==2) {
voice.speak_interrupt("Enter voice rate, -5 to 5");
string text=input_box("Voice rate", "Enter the desired voice rate (-5 to 5): ");
// Assume that if the input is empty, the user canceled.
if(text.is_empty()) voice.speak_interrupt("Canceled");
else voice.rate=string_to_number(text);
// Notice that the above does not guard against invalid values!
}
else if(value==3) {
voice.speak_interrupt("In this game, you must unlock doors to reach the next level. Enemies will try to stop you. Use the left and right arrow keys to move, up to jump, spacebar to attack, down to pick up items, and h to hear your health. Press any key to continue.");
// Let the player skip this message by pressing a key.
keys_pressed();
bool has_pressed=false;
while(!has_pressed) {
 int[] keys=keys_pressed();
 if(keys.length()>0) has_pressed=true;
}
voice.stop();
}
else {
voice.speak_interrupt("Thanks for playing.");
exit();
}
wait(5);
}
}


/*
 * The new game function initializes all the values, and turns over control to the game itself.
 */

void new_game() {
level=1;
lives=3;
recovery=0;
x=0;
y=0;

start_level();
timer gametimer;
while(lives>0) {
// Let the player exit the game:
if(key_pressed(KEY_ESCAPE)) {
pool.destroy_all();
return;
}
// Checking health:
if(key_pressed(KEY_H)) voice.speak_interrupt("Health: " + round(health, 0));

// Only let the player do anything else while not recovering from an attack.
if(recovery>0) recovery-=gametimer.elapsed;
else {

// Backup an int version of x.
int backup_x=round(x, 0);
// We use 0.004 as our speed, because gametimer.elapsed is in milliseconds. This means we'll move roughly 4 tiles per second.
 if((key_down(KEY_LEFT))&&(x>0.0)) x-=0.004*gametimer.elapsed;
else if((key_down(KEY_RIGHT))&&(x<map.length())) x+=0.004*gametimer.elapsed;
else if((key_pressed(KEY_UP))&&(y<=0.0)) {
 y=1000.0;
 pool.play_stationary("sounds/jump.wav", false);
}
else if((key_pressed(KEY_DOWN))&&(y<=0.0)) {
int value=map[backup_x];
if(value==heal) {
health+=random(10, 50);
if(health>100) health=100;
voice.speak_interrupt("Healed");
map[backup_x]=clear;
if(slots[backup_x]>=0) pool.destroy_sound(slots[backup_x]);
}
else if(value==key) {
has_key=true;
map[backup_x]=clear;
voice.speak_interrupt("You found the key!");
if(slots[backup_x]>=0) pool.destroy_sound(slots[backup_x]);
}
}// Get item.
else if(key_pressed(KEY_SPACE)) {
recovery=333;
pool.play_stationary("sounds/attack.wav", false);
// Check enemies for a hit.
for(uint i=0; i<enemies.length(); i++) {
 if(absolute(backup_x-enemies[i].x)<=2.0) {
// Be sure that the vertical positions match!
if((enemies[i].flies)&&(y>0)) {
 enemies[i].health--;
 pool.play_1d("sounds/hit_flying.wav", x, enemies[i].x, false);
}
 else if((y<=0)&&(!enemies[i].flies)) {
pool.play_1d("sounds/hit_enemy.wav", x, enemies[i].x, false);
}
}// In range.
}// Enemies loop.

}// Attacking.

if(round(x, 0)!=backup_x) pool.play_stationary("sounds/step" + random(1, 5) + ".wav", false);
if(x<0.0) {
x=0.0;
pool.play_1d("sounds/wall.wav", x, -1, false);
}
else if(round(x, 0)>=map.length()) {
x=map.length()-1;
pool.play_1d("sounds/wall.wav", x, x+1, false);
}

// Check for traps.
int value=map[round(x, 0)];
 if((value==pit)&&(y<=0)) {
pool.destroy_all();
voice.speak_interrupt_wait("You fell in a pit.");
kill_player();
}
else if((value==fan)&&(y>200)) {
pool.destroy_all();
voice.speak_interrupt("You hit a deadly fan!");
kill_player();
}
else if((value==door)&&(has_key)) {
pool.destroy_all();
voice.speak_interrupt_wait("You have unlocked the door! Congratulations!");
level++;
start_level();
}

}

for(uint i=0; i<enemies.length(); i++) {
if(@(enemies[i])!=null) {
if(enemies[i].alive()) {
enemies[i].update_sound();
enemies[i].step(gametimer.elapsed);
}
else {
// The enemy is dead, remove it.
if(enemies[i].slot>=0) pool.destroy_sound(enemies[i].slot);
if(enemies[i].flies) pool.play_1d("sounds/kill_flyer.wav", x, enemies[i].x, false);
else pool.play_1d("sounds/kill_enemy.wav", x, enemies[i].x, false);
enemies.remove_at(i);
}// Dead enemy.
}
}


if(health<=0.0) kill_player();
else pool.update_listener_1d(x);
gametimer.restart(); gametimer.resume();
wait(5);
}
voice.speak_interrupt_wait("Game over. You reached level " + level + ".");
}

/*
 * This function initializes the level.
 */

void start_level() {

// Stop sound.
pool.destroy_all();
voice.speak("Level " + level + ". " + lives + " lives remaining.");

map.resize(level*10);
enemies.resize(level*2);
slots.resize(level*10);
for(uint i=1; i<map.length(); i++) {
map[i]=clear;
int trap=random(0, 15);
if(trap==4) map[i]=pit;
else if(trap==2) map[i]=fan;
else if(trap==8) map[i]=heal;

}// Initialize map.
// Position the door and the key.
int door_x=random(0, map.length()-1);
int key_x=level*9;
// The key should appear randomly, but not at the same position as the door:
do {
key_x=random(0, map.length()-1);
} while(key_x==door_x);
map[door_x]=door;
map[key_x]=key;

// Reset player.
x=0;
if(health<10) health=100;
else if(health>100) health=100;
recovery=0;
y=0;
has_key=false;

// Add enemies.
for(uint i=0; i<enemies.length(); i++) {
enemy en;
en.x=random(2, map.length());
@(enemies[i])=en;
}

// BGT cleans up memory automatically, but this is a good place to run garbage collection.
// There shouldn't be any lag in a game like this, but taking advantage of garbage-collection opportunities is generally a good idea:
garbage_collect();
// Now, wait for the voice to finish speaking. We didn't call speak_wait, so that the game could do other things while sapi speaks.
while(voice.speaking) {
wait(50);
}

// Start sounds.
for(uint i=0; i<map.length(); i++) {
int slot=-1;
if(map[i]==pit) slots[i]=pool.play_1d("sounds/pit.wav", 0, i, true);
else if(map[i]==fan) pool.play_1d("sounds/fan.wav", 0, i, true);
else if(map[i]==heal) slot=pool.play_1d("sounds/heal.wav", 0, i, true);
 else if(map[i]==door) slot=pool.play_1d("sounds/door.wav", 0, i, true);
else if(map[i]==key) slot=pool.play_1d("sounds/key.wav", 0, i, true);
slots[i]=slot;
}// Sounds.


}

/*
 * Kill the player.
 *
 */
void kill_player() {
pool.play_stationary("sounds/death.wav", false);
wait(250);
voice.speak_wait("You have been killed.");
lives--;
if(lives>0) start_level();
}

/*
 * Decreases the player's health.
 */
void hurt_player(double dmg) {
health=health-dmg;
}
看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2014-12-02 21:35:43

damn it's not.
Here's the code:
class enemy
{
int hp;
int speed;
}
enemy()
{
hp = 100;
}
how to make the enemy's movement and attack?

2014-12-02 21:58:19

You probably need to throw a tick method in.
But you are asking us for something we cannot give you.  The question you are asking at this point is "Can you write my game for me?"  And the answer is yes, but you'll never learn to write games.  implementing this stuff is different for every game, sometimes even depending on what you want to do with it in future, so you need to read the tutorials and the code of others, figuring out how to borrow some of the ideas for yourself.  If you are not finding enough stuff inside BGT, I promise that Googling for basic game design tutorials will bring up many, many good results to read through.

My Blog
Twitter: @ajhicks1992

2014-12-02 22:27:44

in the textbook does not describe how to create a side-scroller

2014-12-03 19:21:07

By the code you're showing, I'd say the first thing  you need to do is to run through the language tutorial again. This is the class you wrote:
class enemy
{
int hp;
int speed;
}
enemy()
{
hp = 100;
}
You already have a problem here; you've defined the constructer for your class outside the class definition, something I assure you the compiler will not take kindly to in the least. As a note to your comment, there is a mini sidescroller example in the BGT reference, sound_pool object section. So perhaps you should read through the entire reference before stating which examples are and are not given.

Best Regards,
Hayden

2014-12-03 23:05:13

In this side-scroller, I do not see the enemy, and I asked them about it.
about shots, etc.

2014-12-05 02:01:35

so help me someone or not?

2014-12-05 05:43:24

As has been said by several people, we cannot just arbitrarily help you. This isn't our game, and we have no way of knowing precisely what it is you want. Just saying "I want the enemy to shoot: tells us nothing. And as I said in an early post, you really need to look more into the tutorial. Of course it's possible that you inadvertently stuck a brace in there. But from what I've so far gleaned of your knowledge of the language and programming in general, you really should be looking into a smaller project than one which requires moving and shooting enemies. Have you also considered the fact that you're going to need some sort of artificial intelligence in this game?

Best Regards,
Hayden

2014-12-05 07:05:59 (edited by jonikster 2014-12-05 07:07:26)

slapjack, I told you, I need an example, on the algorithm I did do, I understand how important.
watched source games BattleZone, scrolling battles all wrong.

2014-12-05 15:23:46 (edited by camlorn 2014-12-05 15:24:35)

We simply can't.
I get the feeling you're trying to tell us what you want and that we're trying to tell you why we can't, but both sides are getting lost as they cross the language barrier.  Looking at random source code will not help you.  Googling for game design tutorials will possibly help you.  Starting with something very, very simple that doesn't involve enemies like a guessing game and building up will help you much more than anything we can do here.  I'm sorry, but there is no shortcut.
And if you can find anyone speaking your language who is a programmer and willing to teach you some, even if said person is sighted, that will quite possibly help you more than any of this.

My Blog
Twitter: @ajhicks1992