![]() |
|
Newbie Allegro programming |
dsds dsds
Member #13,430
August 2011
|
hi everyone! i'm sorry for my bad english but i'm not a master in this language xD thx all!
|
Audric
Member #907
January 2001
|
See the allegro demo game (the small shoot-them-up). It should already give you a good example of a game loop, and it will show you which allegro4 functions a game usually uses. dsds dsds said: is it possible to create a map (in C) without classes Of course. Instead of a 2D array of classes, you'll use a 2D array of struct, etc. About platform games examples/tutorials, I don't have specific example in mind. I checked on the depot, here's list of platform games with source code available: Of these, I checked Alex4 as the game and its author are pretty famous, and the source code seems rather pretty : |
type568
Member #8,381
March 2007
![]() |
You could find some examples here. Compiling them(and modifying in order to understand them better) could be a good start. Good luck.
|
dsds dsds
Member #13,430
August 2011
|
i see many of these games but they are too complex for me thx all
|
Neil Walker
Member #210
April 2000
![]() |
Perhaps this is a good case for writing a book before that dreadful Game Programming All In One is updated Or hey, why not email the authors asking if they want it updated and donate 10% of commission to Matthew's hosting bill. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Audric
Member #907
January 2001
|
You can look at the Mappy Tile Map Editor : For designing levels, it's pretty handy to have an already-made editor that supports cut and paste, undo, etc... The library is designed so you don't have to understand how it is implemented, but you can still look in mappyal.c : for example MapDrawBG() is a classic loop on x and y, and for each cell it calls one of the allegro functions blit() or masked_blit(). Just one critic about these examples: They were written long ago, so they attempt to open tiny modes like 320x240 in 16bpp. If they don't run at all on your machine, first try to modify them to call set_color_depth(32) right before they call set_gfx_mode(). |
J-Gamer
Member #12,491
January 2011
![]() |
Better yet: set_color_depth(desktop_color_depth()) " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
dsds dsds
Member #13,430
August 2011
|
can you write for me a small code of a tilemap?
|
l j
Member #10,584
January 2009
![]() |
The most simple tilemap I can think of.
|
dsds dsds
Member #13,430
August 2011
|
edit: ok it works so with this technique i can draw a map? thx
|
l j
Member #10,584
January 2009
![]() |
Well replace rectfill with al_draw_filled_rectangle al_draw_filled_rectangle(t * 32, i *32, t * 32 +32, i *32 + 32, al_map_rgb(255,255,255), 1);
I assume you know how to get a basic game loop running (if you don't, there should be an allegro 5 tutorials about this, this one should be good http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial ).
|
dsds dsds
Member #13,430
August 2011
|
thx taron another question:
|
l j
Member #10,584
January 2009
![]() |
You don't have to create another matrix for objects on the map. I'd call it a 2d-array though, in programming it's mostly called a 2d array unless you actually use matrix operations on it. In order to make objects collide with the tiles on the map you'll have to use simple maths. I'll create a simple player struct for this. I did not test this, but this is the basic idea, it's very simple, this way however your sprites can not be larger than 2 tiles wide or 2 tiles high, if they are, the collision (and response) might not work properly. If something does not work I think you can fix it, the collision detection and movement are also 'slow', not optimized at all 1typedef struct PLAYER{
2 int x;
3 int y;
4 int width; // For your bounding box
5 int height;
6 int gravity;
7 // Add other data here, like for example the state the player is in (on ground, dead, whatever)
8 // Perhaps a bitmap to represent the player
9};
10
11void create_player(PLAYER *player, int x, int y, int width, int height){
12 // * means pointer
13 // This variable does not actually contain the value of PLAYER
14 // But it contains the address to a PLAYER
15 player->x = x;
16 player->y = y;
17 player->width = width;
18 player->height = height;
19}
20
21void move_player(PLAYER *player, int map[4][4], int speed_x, int speed_y){
22 int left; // Bounding box
23 int right;
24 int top = player->y / 32; // Divided by the tile height
25 // If your tiles are not 32x32 change this
26 int bottom = (player->y + height) / 32; // On a screen, the top left is 0x0, not the bottom left
27
28 // Divide pixel coordinates by the height and width of the tiles to get the tile coordinates.
29 while (speed_x > 0){ // moving to the right
30 speed_x--;
31 player->x++;
32 right = (player->x + player->width) / 32;
33 if (map[top][right] || (map[bottom][right]){ // if this cell is not zero, the player will have collided
34 player->x--; // collision response
35 break; // jump out the while loop
36 }
37 }
38 while (speed_x < 0){
39 speed_x++;
40 player->x--;
41 left = player->x / 32;
42 if (map[top][left] || (map[bottom][left]){
43 player->x++;
44 break;
45 }
46 }
47 right = (player->x + player->width) / 32;
48 left = player->x / 32;
49 while (speed_y > 0){
50 speed_y--;
51 player->y++;
52 bottom = (player->y + height) / 32;
53 if (map[bottom][left] || (map[bottom][right]){
54 player->y++;
55 player->gravity = 0; // Setting the gravity to zero if player lands
56 break;
57 }
58 }
59 while (speed_y < 0){
60 speed_y++;
61 player->y--;
62 top = player->y / 32;
63 if (map[top][left] || map[top][right]){
64 player->++;
65 break;
66 }
67}
68
69void step_player(PLAYER *player){ // should be called every frame
70 player->gravity++;
71 move_player(player, 0, player->gravity);
72 // You could add your controls here
73 // If you use allegro 4, you can use the key array
74 // If you use allegro 5, you can use al_get_keyboard_state()
75 // if (key[KEY_RIGHT]){
76 // move_player(player, 2, 0);
77 // }
78 }
79}
80
81void draw_player(PLAYER *player){ // Should be called after step_player
82// It's strongly recommended to first to the logic in your game and then draw everything in your game
83 rectfill(buffer, player->x, player->, player->x + player->width, player->y + player->height, makecol(0,255,0)); // Add your own drawing procedure here
84}
And how to use // Somewhere in your initialization PLAYER player; create_player(&player, 32, 32, 16, 32); // Use whatever values you like // the & symbol means address of // We only pass the address of the player to the functions, not the whole player structure itself // In your main loop step_player(&player); // after doing all the logic draw_player(&player);
|
dsds dsds
Member #13,430
August 2011
|
Ok i'll study it soon xD thx taron you are very kind. 1 75 // if (key[KEY_RIGHT]){
2 76 // move_player(player, 2, 0);
|
l j
Member #10,584
January 2009
![]() |
Sorry made a mistake there. You'll also have to pass your map as a parameter.
|
Steve Terry
Member #1,989
March 2002
![]() |
You can port anything in C++ to C or anything in C to C++, you just have to change the constructs. Don't be so discouraged of other projects just because they have several "files". Separating your classes/methods into several files helps organize the project and makes it much easier to manage. You should be doing the same. Anyway, don't discourage yourself because you think it's too hard, making a game takes time, lots of time, and persistence. Writing a good game with well managed source and classes/structs takes even longer and requires experience. Start with something smaller than a tilemap and work your way up. ___________________________________ |
|