So I am trying to make a game that has collation in it and I have been trying and looking for what works for me.
This is the class.cpp file
#include "Player.h"
#include <iostream>
#include <string>
#include <allegro5/allegro.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro.h>
#include <locale>
#include <sstream>
#include <Windows.h>
#include <math.h>
#include "allegro5/allegro_primitives.h"
#include <fstream>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <stdio.h>
using namespace std;
Player::Player()
{
}
Player::~Player()
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool _Touch(int allegro_key) {
al_install_keyboard();
ALLEGRO_KEYBOARD_STATE keyState;
al_get_keyboard_state(&keyState);
if (allegro_key >= 256) return false;
if (al_key_down(&keyState, allegro_key)) {
cout << allegro_key << endl;
return true;
}
else {
return false;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Player::_Draw_cross(ALLEGRO_EVENT &event, ALLEGRO_EVENT_QUEUE *queue, ALLEGRO_BITMAP *CROSS, int &x, int &y) {
Cross = CROSS;
al_draw_bitmap(CROSS, x, y, 0);
if (_FLAG_s) {
if (_Touch(ALLEGRO_KEY_W)) {
y -= 5;
}
if (_Touch(ALLEGRO_KEY_A)) {
x -= 5;
}
if (_Touch(ALLEGRO_KEY_D)) {
x += 5;
}
}
else if (_FLAG_t) {
if (_Touch(ALLEGRO_KEY_S)) {
y += 5;
}
if (_Touch(ALLEGRO_KEY_A)) {
x -= 5;
}
if (_Touch(ALLEGRO_KEY_D)) {
x += 5;
}
}
else if (_FLAG_x) {
cout << "DD" << endl;
if (_Touch(ALLEGRO_KEY_W)) {
y -= 5;
}
if (_Touch(ALLEGRO_KEY_S)) {
y += 5;
}
if (_Touch(ALLEGRO_KEY_A)) {
x -= 5;
}
}
else if (_FLAG_y) {
if (_Touch(ALLEGRO_KEY_W)) {
y -= 5;
}
if (_Touch(ALLEGRO_KEY_S)) {
y += 5;
}
if (_Touch(ALLEGRO_KEY_D)) {
x += 5;
}
}
else {
if (_Touch(ALLEGRO_KEY_W)) {
y -= 5;
}
if (_Touch(ALLEGRO_KEY_S)) {
y += 5;
}
if (_Touch(ALLEGRO_KEY_A)) {
x -= 5;
}
if (_Touch(ALLEGRO_KEY_D)) {
x += 5;
}
}
}
bool Player::_Draw_wall(float x, float y, float x2, float y2, ALLEGRO_COLOR color, int s, int t) {
al_draw_filled_rectangle(x, y, x2, y2, color);
int s1 = (al_get_bitmap_height(Cross) + s), t1 = (al_get_bitmap_width(Cross) + t);
if (s1 >= x && s <= x2 && t1 >= y && t <= y2) {
if (s <= x) {
_FLAG_x = true;
}
else if (s >= x2) {
_FLAG_y = true;
}
else if (t1 <= y) {
_FLAG_s = true;
}
else if (t >= y2) {
_FLAG_t = true;
}
else {
_FLAG_x = false, _FLAG_y = false, _FLAG_s = false, _FLAG_t = false;
}
return true;
}
else {
_FLAG_x = false, _FLAG_y = false, _FLAG_s = false, _FLAG_t = false;
return false; }
}
and I am trying to use
player._Draw_wall(0, 0, 20, 1080, al_map_rgb(255, 255, 255), x, y);
player._Draw_wall(0, 0, 1920, 20, al_map_rgb(255, 255, 255), x, y);
but the only one that works is the last one.
can anyone help with getting more then 1 player._Draw_wall to work (I need a undefined amount of them).
I'm not really sure what you're exactly trying to achieve, and what is your problem, but by looking at your code, I immediately spot some issues:
al_install_keyboard
(as the name suggests) installs the keyboard subsystem and should be only called once in your game initialization (happening at the start of the program)
getting the keyboard state and checking that is bad practise, you should look into the Event system of allegro. There should be a main loop, which checks for new events (for example with al_get_next_event
) and handles them via a switch
based on their type. In this switch you call the functions which deal with these events (for example Player::moveLeft
should be called when recieving a ALLEGRO_EVENT_KEY_DOWN
Event from the key A. This function should then change the state of the player object (decreasing the x).
Compare these to methods to continually running to and opening your front door, to check if someone is there, or installing a doorbell and just reacting when that rings.
game logic and rendering should happen in different functions. A draw wall function should only draw a wall, not change the state of the object.
If you could specify further, what your original problem is (does the first wall not get drawn?), i'll look into that as well.