![]() |
|
Autoshooter |
AleX-G Squadron
Member #13,593
October 2011
![]() |
Hi! 2) EDIT: I accidentally found how to pause the game. www.anothergames.com |
GullRaDriel
Member #3,861
September 2003
![]() |
1) then start a timer and trigger a new bullet shot each time it reach 1.2 seconds. Pseudo code:
"Code is like shit - it only smells if it is not yours" |
AleX-G Squadron
Member #13,593
October 2011
![]() |
I tried something like this 1case ALLEGRO_KEY_SPACE:
2 count++;
3 while(count % 3000 == 0)
4 {
5 ShipShot(bullet, BULLET_NUMBER, ship);
6 count=0;
7 }
8 break;
The game launches but no bullet launch. I tried this one and i get problems 1case ALLEGRO_KEY_SPACE:
2ALLEGRO_TIMER *spaceshoot = NULL;
3spaceshoot = al_create_timer(1.0);
4al_start_timer(spaceshoot);
5if(spaceshoot > 1000.0)
6{
7GjuajPlumb(plumb, NUMRI_PLUMBAVE, anija);
8}
9break;
www.anothergames.com |
l j
Member #10,584
January 2009
![]() |
Try pressing space 3000 times it will work.
|
J-Gamer
Member #12,491
January 2011
![]() |
It won't be true even once, since he increments count before it.(assuming count started at 0) " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
AleX-G Squadron
Member #13,593
October 2011
![]() |
Looks like i got it to work somehow, but it doesnt work as needed. 1case ALLEGRO_KEY_SPACE:
2al_start_timer(spaceshoot);
3if(spaceshoot)
4{
5SpaceShoot(bullet, BULLET_NUMBER, ship);
6}
7break;
@taron www.anothergames.com |
Dizzy Egg
Member #10,824
March 2009
![]() |
int bulletTimer = 0; while(game_running){ if(key_space && bulletTimer == 0){ fire_bullet(); bulletTimer = 20; } if(bulletTimer > 0){ bulletTimer -= 1; } }
---------------------------------------------------- |
AleX-G Squadron
Member #13,593
October 2011
![]() |
I wrote your idea, but the timer is not an int. EDIT: I actually modified the counter = 20 to counter = 0 and it worked launching infinite bullets. www.anothergames.com |
Temia Eszteri
Member #14,459
July 2012
![]() |
If your code editor has debugging support, drop a breakpoint on the line of code that's decrementing the timer and run it in the debugger to see if it breaks. If it doesn't, something's wrong with the block. “Nothing is withheld from us what we have conceived to do.” —Russell Kirsch |
AleX-G Squadron
Member #13,593
October 2011
![]() |
After dropping a breakpoint with F9 on visual studio 2012, the program went on and the game started like always. The ship could not use any bullets. This is the REAL coding i made for the autoshooting piece. 1else if (ev.type == ALLEGRO_EVENT_KEY_DOWN && counter == 0)
2 {
3 GjuajPlumb(plumb, NUMRI_PLUMBAVE, anija);
4 counter = 0;
5 }
6 else if (counter > 0)
7 {
8 counter -= 1;
9 }
I have searched everywhere on the internet and i only got a reply from someone who has made autoshooter telling me i have to make a % 5 with the counter. I did that myself because that is the basic logic at first, but it doesnt happen. I learned one thing though. How to freeze the ship when i press space! www.anothergames.com |
l j
Member #10,584
January 2009
![]() |
else if (counter > 0) Move that code to your timer event and remove the else too.
|
AleX-G Squadron
Member #13,593
October 2011
![]() |
Did it like you said, it only shoots one time. If i keep the button pressed, it wont shoot anymore. 1else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
2 {
3 if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
4 GjuajPlumb(plumb, NUMRI_PLUMBAVE, anija);
5 counter = 20;
6 if (counter > 0)
7 counter -= 1;
8 }
Actually i created a kind of debugging. I added text and used it to show the counter. After some other research i found this piece of code 1 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
2 {
3 if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
4 if(counter % 5 == 0)
5 {
6 GjuajPlumb(plumb, NUMRI_PLUMBAVE, anija);
7 }
8 if (counter > 0)
9 counter -= 1;
10 }
But this is even worse. The ship shoots only when the space is pressed and the frame is % 5 == 0. So you have to press it several times for the ship to shoot! After many attempts i found out something else even more amazing 1else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
2 {
3 if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
4 if(counter = counter + 15000)
5 {
6 GjuajPlumb(plumb, NUMRI_PLUMBAVE, anija);
7 }
8 if (counter > 0)
9 counter -= 1;
10 }
It raises the frames by 15000! I found a minimized version when the ships shoots every time the frames % 5 == 0, but the problem is that you have to press the key! 1else if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
2 {
3 if(counter % 5 == 0)
4 GjuajPlumb(plumb, NUMRI_PLUMBAVE, anija);
5 }
I tried the while cycle but that crashes the whole game! www.anothergames.com |
l j
Member #10,584
January 2009
![]() |
1// Initialize stuff
2// Code assumes your event queue is simply called queue
3bool isFiring = false;
4
5ALLEGRO_TIMER *timer = al_create_timer(1.0 / 60.0); // 60 ticks per second
6al_register_event_source(queue, timer);
7
8while (true) // Begin of gameloop, statement can differ
9{
10 al_wait_for_event(queue, &ev); // Or your preffered way to poll the events
11 if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
12 {
13 if (ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
14 {
15 isFiring = true;
16 }
17 }
18 else if (ev.type == ALLEGRO_EVENT_KEY_UP)
19 {
20 if (ev.keyboard.keycode == ALLEGRO_KEY_SPACE)
21 {
22 isFiring = false;
23 }
24 }
25 else if (ev.type == ALLEGRO_EVENT_TIMER) // executes 60 times per second
26 {
27 if (counter > 0)
28 {
29 counter--; // Subtract one from your counter
30 }
31 if (isFiring && counter == 0)
32 {
33 counter = 5; // Now you'll shoot every 5 frames
34 // since the game runs at 60 ticks/frames per second
35 // you'll have a fire rate of 720 bullets per minute
36 // Fire bullet
37 GjuajPlumb(plumb, NUMRI_PLUMBAVE, anija); // I guess
38
39 }
40 }
41
42 // I'm pretty sure you can do the drawing yourself.
43}
This code is completely untested. Also you can use a switch statement for your event handling instead of all those if statements, whatever you prefer. EDIT: AleX-G Squadron said: It raises the frames by 15000! there is nothing surprising about this.
|
AleX-G Squadron
Member #13,593
October 2011
![]() |
I get only an error with the timer. I added mine and deleted yours and still on both cases the timer is labeled with red al_register_event_source(event_queue, timer); My event is event_queue so no problem there, only timer is red. IntelliSense: argument of type "ALLEGRO_TIMER *" is incompatible with parameter of type "ALLEGRO_EVENT_SOURCE *" www.anothergames.com |
l j
Member #10,584
January 2009
![]() |
oops, forgot something. al_register_event_source(queue, al_get_timer_event_source(timer));
|
AleX-G Squadron
Member #13,593
October 2011
![]() |
The game freezes EDIT: After entering the code again, i got it shooting only 1 time! www.anothergames.com |
l j
Member #10,584
January 2009
![]() |
1#include <allegro5/allegro.h>
2#include <cstdlib>
3
4const int MAX_BULLETS = 200,
5 SCREEN_WIDTH = 800,
6 SCREEN_HEIGHT = 600;
7
8const float PLAYER_X_VELOCITY = 1.5f;
9const float PLAYER_Y_VELOCITY = 1.5f;
10
11const float BULLET_Y_VELOCITY = 3;
12
13class Player
14{
15
16public:
17 float x, y;
18 bool movingUp,
19 movingDown,
20 movingRight,
21 movingLeft;
22 bool isFiring;
23
24 Player()
25 {
26 movingUp =
27 movingDown =
28 movingRight =
29 movingLeft = false;
30
31 isFiring = false;
32 }
33
34 void Update()
35 {
36 if (movingUp)
37 {
38 y -= PLAYER_Y_VELOCITY;
39 }
40 else if (movingDown)
41 {
42 y += PLAYER_Y_VELOCITY;
43 }
44
45 if (movingRight)
46 {
47 x += PLAYER_X_VELOCITY;
48 }
49 else if (movingLeft)
50 {
51 x -= PLAYER_X_VELOCITY;
52 }
53 }
54
55};
56
57class Bullet
58{
59public:
60 float x, y;
61 bool alive;
62};
63
64void HandleInput(Player &player, int keyCode, bool keyDown)
65{
66 switch (keyCode)
67 {
68 case ALLEGRO_KEY_Z:
69 player.isFiring = keyDown;
70 break;
71 case ALLEGRO_KEY_LEFT:
72 player.movingLeft = keyDown;
73 break;
74 case ALLEGRO_KEY_UP:
75 player.movingUp = keyDown;
76 break;
77 case ALLEGRO_KEY_RIGHT:
78 player.movingRight = keyDown;
79 break;
80 case ALLEGRO_KEY_DOWN:
81 player.movingDown = keyDown;
82 break;
83 }
84}
85
86int main(int argc, char *argv[])
87{
88 al_init();
89 al_install_keyboard();
90 ALLEGRO_DISPLAY *display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
91 ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue();
92 ALLEGRO_TIMER *timer = al_create_timer(1.0 / 60.0);
93
94 al_register_event_source(queue, al_get_display_event_source(display));
95 al_register_event_source(queue, al_get_keyboard_event_source());
96 al_register_event_source(queue, al_get_timer_event_source(timer));
97
98 ALLEGRO_EVENT event;
99
100 // Bullet stuff
101 int fireCooldown = 0;
102 int bulletsAlive = 0;
103 Bullet bulletArray[MAX_BULLETS];
104 memset(bulletArray, 0, sizeof(Bullet) * MAX_BULLETS); // Initialize bullet memory to 0
105
106 // Player stuff
107 Player player;
108 player.x = SCREEN_WIDTH / 2;
109 player.y = SCREEN_HEIGHT - 20;
110
111 bool invalidated = false;
112 al_start_timer(timer);
113 while (true)
114 {
115 bool keyDown = false;
116 al_wait_for_event(queue, &event);
117 switch (event.type)
118 {
119 case ALLEGRO_EVENT_DISPLAY_CLOSE:
120 return 0;
121
122 case ALLEGRO_EVENT_KEY_DOWN:
123 HandleInput(player, event.keyboard.keycode, true);
124 break;
125
126 case ALLEGRO_EVENT_KEY_UP:
127 HandleInput(player, event.keyboard.keycode, false);
128
129 case ALLEGRO_EVENT_TIMER:
130 invalidated = true;
131 if (fireCooldown > 0)
132 {
133 fireCooldown--;
134 }
135
136 player.Update();
137
138 if (player.isFiring && fireCooldown == 0)
139 {
140 // Replace first dead bullet with a 'living' one
141 for (int i = 0; i < MAX_BULLETS; i++)
142 {
143 if (!bulletArray[i].alive)
144 {
145 bulletArray[i].alive = true;
146 bulletArray[i].x = player.x;
147 bulletArray[i].y = player.y;
148 break;
149 }
150 }
151 fireCooldown = 5;
152 }
153
154 // Update all living bullets
155 for (int i = 0; i < MAX_BULLETS; i++)
156 {
157 if (bulletArray[i].alive)
158 {
159 bulletArray[i].y -= BULLET_Y_VELOCITY;
160 // Kill bullet if it has gone off the screen
161 if (bulletArray[i].y < 0)
162 {
163 bulletArray[i].alive = false;
164 }
165 }
166 }
167 break;
168 }
169 if (invalidated && al_is_event_queue_empty(queue)) // Redraw
170 {
171 al_clear_to_color(al_map_rgb(0, 0, 0));
172 al_draw_pixel(player.x, player.y, al_map_rgb(0,255, 0)); // player is a green dot
173
174 for (int i = 0; i < MAX_BULLETS; i++)
175 {
176 if (bulletArray[i].alive)
177 {
178 al_draw_pixel(bulletArray[i].x, bulletArray[i].y, al_map_rgb(255, 0, 0));
179 }
180 }
181
182 al_flip_display();
183 invalidated = false;
184 }
185 }
186 return 0;
187}
if this code freezes, it's your computer.
|
AleX-G Squadron
Member #13,593
October 2011
![]() |
FINALLY IT WORKED!!!! www.anothergames.com |
l j
Member #10,584
January 2009
![]() |
You should try to understand how and why the code works, otherwise you'll run into problems again real fast.
|
AleX-G Squadron
Member #13,593
October 2011
![]() |
You are totally right. EDIT: I am trying to understand your code, but that seems to me a bit strange. www.anothergames.com |
|