Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » [MinorHack] This Saturday?

This thread is locked; no one can reply to it. rss feed Print
 1   2 
[MinorHack] This Saturday?
Zaphos
Member #1,468
August 2001

Okay, here's my updated version -- the planets bounce, and you have a very erratic flight path. The premise is that you are some sort of space fly, I guess.

1#include <allegro.h>
2#include <cmath>
3#include <list>
4#include <iostream>
5 
6using namespace std;
7 
8int lt = 0;
9void timer() {
10 {lt ++;}
11} END_OF_FUNCTION(timer)
12 
13int cx, cy;
14 
15struct circ {
16 float r, theta;
17 float rate;
18 float rad;
19 int col;
20 float rvel;
21
22 circ(float r, float theta, float rate, float rad, int col) : r(r), theta(theta), rate(rate), rad(rad), col(col), rvel(0) {}
23
24 void draw(BITMAP *buf) {
25 circlefill(buf, cx + cos(theta)*r, cy + sin(theta)*r, rad, col);
26 }
27};
28 
29 
30int main() {
31 allegro_init();
32 install_keyboard();
33 install_timer();
34 LOCK_FUNCTION(timer);
35 LOCK_VARIABLE(lt);
36 install_int_ex(timer, BPS_TO_TIMER(30));
37
38 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
39
40 BITMAP *buf = create_bitmap(SCREEN_W, SCREEN_H);
41
42 cx = SCREEN_W /2;
43 cy = SCREEN_H + 100;
44
45 circ sun(0, 0, 0, 300, makecol(255,200,0));
46 list<circ> planets;
47 for (int i = 0; i < 20; i++) {
48 float r = (rand()%21 - 10) / 10.0;
49 planets.push_back(circ(350 + 20*r, .314 * i + .15*r - (3.5/2.0), .01 + .003*r, 20 + 5*r, makecol(51+r*50,255,51+r*50)));
50 }
51 circ me(0, -3.14/2.0, 3.14/2.0, 10, makecol(0,0,255));
52 list<circ>::iterator mine = planets.begin();
53 me.r = mine->rad + me.rad;
54 float racc = 0;
55 int beats = 0;
56
57 while(!key[KEY_ESC]) {
58 while (lt> 0) {
59 me.theta -= (!key[KEY_RIGHT] - !key[KEY_LEFT]) * .06;
60 
61 if (me.r > mine->rad + me.rad) {
62 me.r -= racc;
63 racc+=.2;
64 beats = 0;
65 } else {
66 racc = 0;
67 me.r = mine->rad + me.rad;
68 me.theta -= (!key[KEY_RIGHT] - !key[KEY_LEFT]) * .1;
69 beats++;
70 if (/*beats > 5 || */key[KEY_SPACE]) {
71 me.r += 10;
72 racc = -10;
73 }
74 }
75
76 float distToMine = me.r;
77 float mex = cx + cos(mine->theta)*mine->r + cos(me.theta)*me.r;
78 float mey = cy + sin(mine->theta)*mine->r+sin(me.theta)*me.r;
79
80 if ( sqrt( (mex - cx) * (mex -cx) + (mey -cy) * (mey -cy) ) < sun.rad + me.rad)
81 {
82 return 1;
83 } else if (mex > SCREEN_W || mex < 0) {
84 return 1;
85 }
86
87 for (list<circ>::iterator i = planets.begin(); i != planets.end(); ++i){
88 if (i != mine) {
89 float youx = cx + cos(i->theta)*i->r;
90 float youy = cy + sin(i->theta)*i->r;
91 float dist = sqrt( (youx - mex) * (youx - mex) + (youy - mey) * (youy - mey) );
92 if (dist < distToMine) {
93 distToMine = dist;
94 mine = i;
95 me.r = dist;
96 me.theta = atan2(mey - youy, mex - youx);
97 if (racc < 0)
98 racc = -racc;
99 }
100 }
101 }
102
103 for (list<circ>::iterator i = planets.begin(); i != planets.end(); ++i){
104 i->theta += i->rate;
105
106 i->rvel -= .5;
107 i->r += i->rvel;
108 if (i->r < 290) {
109 i->r = 290;
110 float r = (rand()%21 - 10) / 10.0;
111 i->rvel = 7 * r*2;
112 }
113 }
114
115
116 lt--;
117 }
118
119 for (list<circ>::iterator i = planets.begin(); i != planets.end(); ++i){
120 i->draw(buf);
121 }
122 circlefill(buf, cx + cos(mine->theta)*mine->r + cos(me.theta)*me.r,
123 cy + sin(mine->theta)*mine->r+sin(me.theta)*me.r, me.rad, me.col);
124 sun.draw(buf);
125 blit(buf, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
126 clear(buf);
127 }
128
129 return 0;
130} END_OF_MAIN()

edit: And for ranking, I'll go with Kikaru, CGames, Dennis
Kikaru and CGames are both really strong entries imo (I'm impressed you both had time to add intro text things ...) but Kikaru's was more surprising. Dennis's looked like it would have been interesting -- looking forward to the revised entry! :)

edit2: changed the starting theta to -PI/2 like CGames suggested (below)

CGamesPlay
Member #2,559
July 2002
avatar

Ugh, the revision (the one on the site at least) makes it impossible :)

Much harder, and I tend to die in the first half second. I change the initial player theta to -pi/2, which helped some, but staying alive 5 seconds is a good record for me :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Kikaru
Member #7,616
August 2006
avatar

Ok, everyone has voted, so here are the results:
Zaphos: 7 pts.
Kikaru: 5 pts.
Dennis: 3 pts.
CGamesPlay: 7 pts.

It's a tie between Zaphos and CGames. Good games everyone! :D

CGamesPlay
Member #2,559
July 2002
avatar

Ohmigosh! This is like the second time I've come close to winning :P I think last time I wasn't tied, though :)

FYI, the next bi-weekly competition has been scheduled. I won't be able to attend, because I will be in London.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Zaphos
Member #1,468
August 2001

Woo!

Quote:

Much harder, and I tend to die in the first half second. I change the initial player theta to -pi/2, which helped some, but staying alive 5 seconds is a good record for me :)

Ah, the rand() I was getting managed to hide some of the problems (and difficulty!) -- note to self: test with different srand()s!

I think I'll go back and mess with the entry some more later. The way the blue guy exists in polar coordinates makes him control really oddly; I think I want to see how it plays if he moves more normally.

edit: also -- hope we can make this a semi-regular event again. Everybody come back to play again in 2 weeks!

CGamesPlay
Member #2,559
July 2002
avatar

I am adding the email support right now, as we speak. I just finished the "send me emails when a competition is proposed." :)

[append]
The top 3 options on this page work!

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Kikaru
Member #7,616
August 2006
avatar

I would love to start doing this again. What's funny is that I joined late, finished early, and still had a good game... :o

Now, to work some more on it! :D

CGamesPlay
Member #2,559
July 2002
avatar

All of the email options except one are now working.

If I added SMS support, how many of you would use it? (i.e. SMS me 1 hour before competition begins, and SMS me when a competition begins)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Dennis
Member #1,090
July 2003
avatar

I fixed my collision logic and made the paddle shrink after each hit.
There is still no game over condition though, as the only thing bugging me was the collision bug and since that's fixed, I can sleep well now.

1// Title: My 1st Minorhack participation
2// Author: Dennis Busch
3// Date: 17th of February 2007
4// Time spent: 1h + 1h bugfixing the faulty collision logic :P
5 
6// didn't finish in time!
7// collision bug fixed, paddle shrinks after every hit
8// (only the center pixel of the enemy is checked for collision)
9// still no game over condition
10 
11// Rule: 1 source file, game has to use polar coordinates instead of rect. coords.
12 
13// this is quick and dirty code without too much(if any at all) error checking
14 
15#include <allegro.h>
16#include <math.h>
17 
18// variables
19bool debug_info = true;
20bool game_over = false;
21bool use_screen = false;
22BITMAP *back = NULL;
23const int W = 640;
24const int H = 480;
25const int timing = 60;
26const float PI = 3.14159;
27 
28const float to_fix = 128.0/180.0;
29const float to_rad = PI/180.0;
30 
31float ply_angle = 0.0; // :P
32float ply_angle_step = 3.0;
33float ply_length = W/6.0; // distance to center
34float ply_size = 60.0;
35float ply_size_min = 5.0;
36float ply_size_step = 5.0;
37float ply_arc_end = ply_angle + ply_size;
38float center_x = W/2.0;
39float center_y = H/2.0;
40 
41float enemy_angle = 0.0;
42float enemy_length = W/2; // distance to center
43float enemy_size = W / 100.0;
44float enemy_step = W / 320.0;
45int enemy_state = 0; // 0 incoming, 1 offgoing
46int enemy_reset_delay = 240;
47int enemy_delay = enemy_reset_delay;
48 
49void update_display()
50{
51 BITMAP *drawto = NULL;
52 
53 if(use_screen)
54 drawto = screen;
55 else
56 drawto = back;
57
58 // drawing
59 clear_bitmap(drawto);
60
61 // draw debug info
62 if(debug_info)
63 {
64 textprintf_ex(drawto, font, 0, 0, makecol(255,255,255), -1, "player_angle: %f / player_angle+player_size: %f", ply_angle, ply_arc_end);
65 textprintf_ex(drawto, font, 0,10, makecol(255,255,255), -1, "enemy_angle: %f / enemy_length: %f", enemy_angle, enemy_length);
66
67 }
68
69 // draw the player
70 circlefill(drawto, center_x, center_y, ply_length / 2, makecol(255,255,255));
71 arc(drawto, center_x,center_y,itofix(int(ply_angle*to_fix)),itofix(int(ply_angle*to_fix+ply_size*to_fix)),ply_length, makecol(255,255,255));
72
73 // draw the enemy
74 circlefill(drawto, center_x + cos(enemy_angle*to_rad)*enemy_length
75 , center_y - sin(enemy_angle*to_rad)*enemy_length
76 , enemy_size, makecol(255,0,0));
77 putpixel(drawto, center_x + cos(enemy_angle*to_rad)*enemy_length
78 , center_y - sin(enemy_angle*to_rad)*enemy_length
79 , makecol(0,0,0));
80
81 if(!use_screen)
82 blit(back, screen, 0,0,0,0,W,H);
83}
84 
85void update_game_logic()
86{
87 // read input
88 if(key[KEY_ESC])
89 game_over = true;
90 
91 if(key[KEY_LEFT]||key[KEY_UP])
92 ply_angle += ply_angle_step;
93 if(key[KEY_RIGHT]||key[KEY_DOWN])
94 ply_angle -= ply_angle_step;
95 
96 if (ply_angle > 360.0)
97 ply_angle -= 360.0;
98 if (ply_angle < 0.0)
99 ply_angle = 360.0 + ply_angle;
100 
101 if(ply_angle == 360.0)
102 ply_angle = 0.0;
103 
104 ply_arc_end = ply_angle + ply_size;
105 if (ply_arc_end > 360.0)
106 ply_arc_end -= 360.0;
107 if (ply_arc_end < 0.0)
108 ply_arc_end = 360.0 + ply_arc_end;
109 
110 if(ply_arc_end == 360.0)
111 ply_arc_end = 0.0;
112 
113 // update enemy
114 switch(enemy_state)
115 {
116 case 0:
117 enemy_length -= enemy_step;
118 if((abs(enemy_length) <= (ply_length + enemy_size)) // in player paddle radius?
119 && (abs(enemy_length) >= (ply_length - enemy_size)))
120 {
121 bool hit = false;
122
123 if(ply_angle < ply_arc_end)
124 if((enemy_angle >= ply_angle)&&(enemy_angle <= (ply_arc_end)))
125 hit = true;
126
127 if(ply_angle > ply_arc_end)
128 if( ((enemy_angle >= 0.0)&&(enemy_angle<=ply_arc_end))
129 ||((enemy_angle >= ply_angle)&&(enemy_angle <= 360.0)))
130 hit = true;
131
132 if(hit)
133 {
134 if(ply_size > ply_size_min)
135 {
136 ply_size -= ply_size_step;
137 ply_angle += ply_size_step / 2.0;
138 }
139 enemy_state = 1;
140 }
141 }
142
143 if((abs(enemy_length) >= W/2)||(abs(enemy_length) <= ply_length/2 ))
144 {
145 // reset enemy
146 enemy_angle = float(rand()%360);
147 enemy_length = W/2;
148 }
149 break;
150 case 1:
151 enemy_length += enemy_step;
152
153 if((abs(enemy_length) >= W/2)||(abs(enemy_length) <= ply_length/2 ))
154 {
155 // reset enemy
156 enemy_angle = float(rand()%360);
157 enemy_length = W/2;
158 enemy_state = 0;
159 }
160 break;
161 }
162 
163}
164 
165// copy/pasted from the FAQ
166volatile int speed_counter = 0;
167void increment_speed_counter()
168{speed_counter++;}
169END_OF_FUNCTION(increment_speed_counter)
170 
171void play_the_game()
172{
173 
174 LOCK_VARIABLE(speed_counter);
175 LOCK_FUNCTION(increment_speed_counter);
176 
177 install_int_ex(increment_speed_counter, BPS_TO_TIMER(timing));
178 
179 while(!game_over)
180 {
181 while (speed_counter > 0)
182 {
183 update_game_logic();
184 speed_counter--;
185 }
186 update_display();
187 }
188}
189// END OF copy/pasted from the FAQ
190 
191int main(void)
192{
193 bool failed = false;
194 if(allegro_init() != 0)failed = true;
195 if(install_timer() != 0)failed = true;
196 if(install_keyboard() != 0)failed = true;
197 if(install_mouse() == -1)failed = true;
198
199 if(!failed)
200 {
201 set_color_depth(32);
202 if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,W,H,0,0)==0)
203 {
204 back = create_bitmap(W,H);
205 if(back==NULL)
206 use_screen = true;
207
208 play_the_game();
209
210 }
211 }
212 allegro_exit();
213 return 0;
214}
215END_OF_MAIN()

Zaphos
Member #1,468
August 2001

Quote:

Zaphos: 7 pts.
Kikaru: 5 pts.
Dennis: 3 pts.
CGamesPlay: 7 pts.

Wait ... how did you calculate that? It adds to 22, but 4 * (3+2+1) is 24 ...

edit: Hmm -- also, in admin on the webpage, we can set the "Minimum players for competition"? I thought that was something global, not per user?

Dennis -- nice to see it working! It'd be cool if the same ball bounced around the screen edges and came back, but I guess that's rather hard to do with polar coordinates.

Kikaru
Member #7,616
August 2006
avatar

What do you mean? I gave 3 points for first, 2 points for second, and 1 point for third. :)

Zaphos
Member #1,468
August 2001

I mean that if you have 4 people voting, and each gives out 3 points for first, 2 points for second, and 1 point for third, then the total number of points given out should be 4*(3+2+1) = 24. But 7+5+3+7 = 22.

CGamesPlay
Member #2,559
July 2002
avatar

Zaphos: 8
CGamesPlay: 7
Kikaru: 6
Dennis: 3

I'll get you one day!

[append]
The minimum is 3. He can't give points to himself.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Zaphos
Member #1,468
August 2001

Quote:

The minimum is 3. He can't give points to himself.

Yeah -- that's referring to an edit that I put in and then quickly removed. Momentary brain failure.

edit: also, hooray!

Kikaru
Member #7,616
August 2006
avatar

Maybe you could make it not reuse rules for a certain number of events? Like, 6 or 7? We have enough rules for that. :)

Zaphos
Member #1,468
August 2001

Quote:

Maybe you could make it not reuse rules for a certain number of events? Like, 6 or 7? We have enough rules for that. :)

Does it re-use rules at all? It doesn't seem like it's been a problem, yet, at any rate ...

Kikaru
Member #7,616
August 2006
avatar

The rule we just did was... Oh, wait, it was different. Hehe. ;D

Dennis
Member #1,090
July 2003
avatar

Since this was my first participation, I can't judge about the previous rules but this one certainly was challenging.

Anyway, I'm not going to revise my entry again, because it inspired me to something else, which I hope I'll have the time and motivation to start working on someday.:)

For me, this MinorHack turned out to be the most cool thing I did in a while. I will surely participate in the next one as well. It's fun!:)

CGamesPlay
Member #2,559
July 2002
avatar

Good thing you IRC, or else you wouldn't have heard about it! :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

 1   2 


Go to: