3 * Released under Creative Commons
7 * - ansi terminal with resolution >= 80x24 characters
10 /* You can use this code to control hPong with Arduino (and potentiometer):
11 ------------------------ CUT HERE -----------------------------------------
12 // Pong controller (for use with hPong)
16 int min = 65535, max = 0;
19 float val = 0, oldval = -255;
21 void setup() // run once, when the sketch starts
27 void loop() // run over and over again
29 val = analogRead(inpin);
32 val=(((val-(min))/(max-(min)))*stadium_max)-offset;
35 //Serial.print(min, DEC); Serial.print("-"); Serial.print(max, DEC); Serial.print("\n");
36 //Serial.print(val, DEC); Serial.print("\n");
37 Serial.print(val, BYTE);
41 ------------------------ CUT HERE -----------------------------------------
51 int turntime
= 25; //Time of turn in ms (inverted speed)
52 int debugturntime
= 0; //Turntime for debuging
54 unsigned char padsize
= 1; //Size of pad
55 char padspeed
= 2; //Speed of pad (pixels per keypress)
57 unsigned char ui_skill_l
= 120; //Skill of UI = (0-255); 0=disabled, 255=godmode
58 unsigned char ui_skill_r
= 0; //Skill of UI = (0-255); 0=disabled, 255=godmode
59 unsigned char debug
= 0; //UI Debug mode
61 unsigned char music
= 0; //Enable music? (Bool)
62 char music_cmd
[] ="while true; do mplayer /usr/share/chiptunes/* >/dev/null 2>&1; done;";
64 unsigned char arduino
= 0; //Enable Arduino? (Bool)
65 char arduinodev
[] = "/dev/ttyUSB0";
68 unsigned char *stadium_bitmap
[] = {
69 "#### hPong 0.3 ################################################",
85 "########################################### <~~Harvie 2oo8 ####"};
86 char stadium_lines
= 17; //15+2
87 char stadium_width
= 63; //60+2+1
90 unsigned char banner_start
[] =
91 "\n\n\n\n\n\n\n\n\n\n\n"
92 " ## #### by: <~~Harvie 2oo8\n"
94 " ## ## ## #### ### #### ###\n"
95 " ####### ###### # # # # # # #\n"
96 " ## ## ## # ### # # ####\n"
98 " ## Press any key to start! ###\n";
100 unsigned char banner_lose
[] =
101 "\n\n\n\n\n\n\n\n\n\n\n\n\n"
102 " ### # # ### # # # ### #\n"
103 " # # # # # # # # # #\n"
104 " # # ### ### ### # # #\n"
105 " # # # # # # # # #\n"
106 " ### # # ### # # # # #\n"
109 " (Press Any Key to continue...)\n";
112 char stadium_offset
= 7;
113 char score_space
[] = " ";
114 unsigned char *num_bitmaps
[]={
115 "#### ## ## ####", //0
124 "#### #### ####"}; //9
127 char stadium_max
; //Set by init()
131 char ballmove_c
= 1; //x
132 char ballmove_l
= 1; //y
134 char player_l
= 7; //Left
135 char player_r
= 7; //Right
137 unsigned int score_l
= 0;
138 unsigned int score_r
= 0;
143 FILE *musicfd
= NULL
;
144 FILE *arduinofd
= NULL
;
148 printf("\033[2J"); //Clear screen
149 printf("\033[0;0H"); //L:C = 0:0
153 if(arduino
) fclose(arduinofd
);
155 system("stty icanon");
157 printf("hPong exited! Scrore was %d : %d\n<~~Harvie 2oo8\n", score_l
, score_r
);
159 if(music
) pclose(musicfd
);
163 if( (musicfd
= popen(music_cmd
, "r"))==NULL
) {
164 printf("Cannot initialize music\n");
169 void win(char player
) {
173 ball_l
= 2; ball_c
= stadium_width
-3; ballmove_c
= -1;
178 ball_l
= 2; ball_c
= 3; ballmove_c
= 1;
182 printf(banner_lose
, score_l
, score_r
);
184 if(!arduino
&& (ui_skill_l
== 0 || ui_skill_r
== 0)) getchar();
189 printf("\033[0;0H"); //L:C = 0:0
192 void pixel(char c
, char line
, char col
) {
193 printf("\033[%d;%dH", line
, col
);
195 printf("\033[0;0H"); //L:C = 0:0
198 void draw_stadium() {
199 unsigned char i
, x
, sl
, sr
;
200 sl
= score_l
%10; sr
= score_r
%10;
201 for(i
=0;i
<stadium_offset
;i
++) {
205 printf("%s%c%c%c . %c%c%c", score_space
,
206 num_bitmaps
[sl
][x
], num_bitmaps
[sl
][x
+1], num_bitmaps
[sl
][x
+2],
207 num_bitmaps
[sr
][x
], num_bitmaps
[sr
][x
+1], num_bitmaps
[sr
][x
+2]
212 for(i
=0;i
<stadium_lines
;i
++) printf("%s\033[K\n", stadium_bitmap
[i
]);
218 if((unsigned char)rand() < ui_skill_l
) { //Left
219 if(player_l
< ball_l
) player_l
++;
220 if(player_l
> ball_l
) player_l
--;
223 if((unsigned char)rand() < ui_skill_r
) { //Right
224 if(player_r
< ball_l
) player_r
++;
225 if(player_r
> ball_l
) player_r
--;
229 ball_c
+= ballmove_c
;
230 ball_l
+= ballmove_l
;
233 if(ball_c
== 1) { //left
234 if(abs(ball_l
-player_l
) <= padsize
+1) {
235 ballmove_c
= -ballmove_c
;
242 if(ball_c
== stadium_width
-3) { //right
243 if(abs(ball_l
-player_r
) <= padsize
+1) {
244 ballmove_c
= -ballmove_c
;
251 //Bounce ball (vertical)
252 if(ball_l
>= stadium_max
|| ball_l
<= 0) ballmove_l
= -ballmove_l
;
263 //Check player pos limits
264 if(player_r
< 0) player_r
= 0;
265 if(player_r
> stadium_max
) player_r
= stadium_max
;
267 if(player_l
< 0) player_l
= 0;
268 if(player_l
> stadium_max
) player_l
= stadium_max
;
271 for(i
=-padsize
;i
<padsize
+1;i
++) pixel(pixchar
, stadium_offset
+2+i
+player_l
, 2);
272 for(i
=-padsize
;i
<padsize
+1;i
++) pixel(pixchar
, stadium_offset
+2+i
+player_r
, stadium_width
-1);
275 pixel(pixchar
, stadium_offset
+2+ball_l
, 2+ball_c
);
286 void control(unsigned char key
) {
288 case 'A': player_r
-=padspeed
; break; //A = Arrow UP
289 case 'B': player_r
+=padspeed
; break; //B = Arrow DOWN
294 case 'e': case 'E': player_l
-=padspeed
; break; //e key
295 case 'd': case 'D': player_l
+=padspeed
; break; //D key
301 void alarm_handle(int signo
) {
304 if(signo
== SIGTERM
|| signo
== SIGINT
|| signo
== SIGQUIT
) quit();
310 //Init random generator
312 //Disable both input buffers
314 system("stty -icanon");
315 //Handle alarm (timer)
316 signal(SIGALRM
, alarm_handle
);
317 signal(SIGINT
, alarm_handle
); signal(SIGTERM
, alarm_handle
); signal(SIGQUIT
, alarm_handle
);
318 //Compute lower sprite position in stadium
319 stadium_max
= stadium_lines
- 3;
321 if(debug
) turntime
= debugturntime
;
323 if( arduino
&&((arduinofd
=fopen(arduinodev
,"rb"))==NULL
) ) {
324 printf("Cannot open %s\n", arduinodev
);
329 //Main... Short & simple ;)
335 if(music
) music_start();
338 ualarm(turntime
*1000+1, turntime
*1000+1);
340 if(arduino
) player_r
= getc(arduinofd
); //Arduino
341 else control(getchar()); //Keyboard
This page took 0.56696 seconds and 4 git commands to generate.