Accueil > > > ENCORE UN PONG (LINUX, CURSES) [GCC]
ENCORE UN PONG (LINUX, CURSES) [GCC]
Information sur la source
Description
Ben oui, encore un clone de pong...! Bah, si on peut appeler sa un clone... C'est encore plus laid!! Et comme j'utilise les curses, bah pour que la balle bouge, faut appuyer sur une touche :S Enfin... utilisez 'w' et 's' pour le pad de gauche, et 'o' et 'l' pour le pad de droit. Le jeu quitte lorsque vous marquez un point. ** Petite amelioration : La balle continue de bouger meme sans appuyer sur un touche. J'ai trouver comment continuer un traitement dans une boucle tout en reagissant si une touche est enfoncee!!
Source
- /****************************************\
- | Pong - main.c |
- | Jeu tres simple base sur le |
- | classique pong. Utilise les curses. |
- | |
- | Auteur: Martin Boulianne |
- | e-mail: frzburn@gmail.com |
- \****************************************/
-
- #include <unistd.h>
- #include <stdlib.h>
- #include <curses.h>
- #include <termios.h>
- #include <term.h>
-
-
- /* Constants definitions */
- #define SCREEN_HEIGHT 40 /* Set the screen height */
- #define SCREEN_WIDTH 120 /* Set the screen width */
- #define LOCAL_ESCAPE_KEY 27 /* Local escape key code */
- #define BALL_SPEED 1 /* Set the ball movement speed */
-
- static struct termios initial_settings, new_settings;
- static int peek_character = -1;
-
-
- /* Functions prototypes */
- void printTable(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1]);
- void init_keyboard();
- void close_keyboard();
- int move_ball(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1]);
- int kbhit();
- int readch();
-
-
- /* Global variables */
- int pad1, pad2; /* Actual pads position */
- int pad1_, pad2_; /* Old pads position */
- int ball_x, ball_y; /* Actual ball position */
- int ball_xi, ball_yi; /* Ball deplacement vector */
- int ball_x_, ball_y_; /* Old ball position */
-
-
- int main(int argc, char *argv[])
- {
- int i, j, key;
- char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1];
-
- /* Variables initialisation */
- pad1 = SCREEN_HEIGHT / 2 - 2;
- pad2 = pad1;
- pad1_ = pad1;
- pad2_ = pad1;
- ball_y = 16;
- ball_x = 70;
- ball_y_ = ball_y;
- ball_x_ = ball_x;
- ball_xi = -2; /* Giving initial ball movement */
- ball_yi = -1;
-
-
- /* Puts empty character in each table case */
- for(i = 0; i < SCREEN_HEIGHT; i++) {
- for(j = 0; j < SCREEN_WIDTH; j++) {
- table[i][j] = ' ';
- }
- }
-
-
- /* Make the borders of the table */
- for(i = 0; i <= SCREEN_HEIGHT; i++) {
- table[i][0] = 'X';
- table[i][SCREEN_WIDTH] = 'X';
- }
-
- for(i = 0; i <= SCREEN_WIDTH; i++) {
- table[0][i] = 'X';
- table[SCREEN_HEIGHT][i] = 'X';
- }
-
-
- /* Put the ball in the table */
- table[ball_y][ball_x] = 'O';
-
- /* Put the pads in the table */
- for(i = 0; i < 8; i++) {
- table[pad1 + i][SCREEN_WIDTH - 4] = 'H';
- table[pad2 + i][4] = 'H';
- }
-
-
- initscr();
-
- printTable(table);
-
- /* Now we're ready to manage the keys pressed */
- crmode();
- keypad(stdscr, TRUE);
- noecho();
-
- key = getch();
- init_keyboard();
-
- while(1) {
- clrtoeol();
-
- if(kbhit())
- key = readch();
-
- switch(key) {
- case 'o':
- if(pad1 > 1)
- pad1 -= 1;
- break;
- case 'l':
- if(pad1 < SCREEN_HEIGHT - 8)
- pad1 += 1;
- break;
- case 'w':
- if(pad2 > 1)
- pad2 -= 1;
- break;
- case 's':
- if(pad2 < SCREEN_HEIGHT - 8)
- pad2 += 1;
- break;
- }
-
- switch(move_ball(table)) {
- case 1:
- move(SCREEN_HEIGHT + 3, 5);
- printw("%s", "Player 1 won!");
- getch();
- endwin();
- exit(EXIT_SUCCESS);
- case 2:
- move(SCREEN_HEIGHT + 3, 5);
- printw("%s", "Player 2 won!");
- getch();
- endwin();
- exit(EXIT_SUCCESS);
- }
-
- printTable(table);
-
- pad1_ = pad1;
- pad2_ = pad2;
- ball_y_ = ball_y;
- ball_x_ = ball_x;
-
- key = 0;
-
- usleep(100000);
- }
- close_keyboard();
-
- getch();
- endwin();
- exit(EXIT_SUCCESS);
- }
-
- /* Refresh the ball position in the table and manage collisions */
- int move_ball(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1])
- {
- /* If the ball reches the top or the bottom */
- if(((ball_y + ball_yi * BALL_SPEED) <= 0) || ((ball_y + ball_yi * BALL_SPEED) >= (SCREEN_HEIGHT)))
- ball_yi = -ball_yi;
-
- /* If the ball hit the left paddle */
- if(((ball_x + ball_xi * BALL_SPEED) <= 4) && (((ball_y + ball_yi * BALL_SPEED) >= pad2))) {
- if((ball_y + ball_yi * BALL_SPEED) <= pad2 + 8)
- ball_xi = -ball_xi;
- }
-
- /* If the ball hit the right paddle */
- if(((ball_x + ball_xi * BALL_SPEED) >= SCREEN_WIDTH - 4) && (((ball_y + ball_yi * BALL_SPEED) >= pad1))) {
- if((ball_y + ball_yi * BALL_SPEED) <= pad1 + 8)
- ball_xi = -ball_xi;
- }
-
- /* If it passes either paddle */
- if((ball_x + ball_xi * BALL_SPEED) <= 0)
- return 1;
-
- if((ball_x + ball_xi * BALL_SPEED) >= SCREEN_WIDTH)
- return 2;
-
-
- ball_y += ball_yi * BALL_SPEED;
- ball_x += ball_xi * BALL_SPEED;
-
- table[ball_y_][ball_x_] = ' ';
- table[ball_y][ball_x] = 'O';
-
- return 0;
- }
-
- /* Print the whole table on the screen */
- void printTable(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1])
- {
- int i, j;
-
- /* Refresh pads position */
- for(i = 0; i < 8; i++) {
- table[pad1_ + i][SCREEN_WIDTH - 4] = ' ';
- table[pad2_ + i][4] = ' ';
- }
- for(i = 0; i < 8; i++) {
- table[pad1 + i][SCREEN_WIDTH - 4] = 'H';
- table[pad2 + i][4] = 'H';
- }
-
- /* Print the whole table on the screen */
- for(i = 0; i <= SCREEN_HEIGHT; i++) {
- for(j = 0; j <= SCREEN_WIDTH; j++) {
- move(i, j);
- addch(table[i][j]);
- }
- }
-
- /* Move the cursor away from the table */
- move(SCREEN_HEIGHT + 5, SCREEN_WIDTH + 5);
- addch(' ');
-
- refresh();
- }
-
- void init_keyboard()
- {
- tcgetattr(0,&initial_settings);
- new_settings = initial_settings;
- new_settings.c_lflag &= ~ICANON;
- new_settings.c_lflag &= ~ECHO;
- new_settings.c_lflag &= ~ISIG;
- new_settings.c_cc[VMIN] = 1;
- new_settings.c_cc[VTIME] = 0;
- tcsetattr(0, TCSANOW, &new_settings);
- }
-
- void close_keyboard()
- {
- tcsetattr(0, TCSANOW, &initial_settings);
- }
-
- int kbhit()
- {
- char ch;
- int nread;
-
- if(peek_character != -1)
- return 1;
- new_settings.c_cc[VMIN]=0;
- tcsetattr(0, TCSANOW, &new_settings);
- nread = read(0, &ch, 1);
- new_settings.c_cc[VMIN]=1;
- tcsetattr(0, TCSANOW, &new_settings);
-
- if(nread == 1) {
- peek_character = ch;
- return 1;
- }
- return 0;
- }
-
- int readch()
- {
- char ch;
-
- if(peek_character != -1) {
- ch = peek_character;
- peek_character = -1;
- return ch;
- }
- read(0, &ch, 1);
- return ch;
- }
-
/****************************************\
| Pong - main.c |
| Jeu tres simple base sur le |
| classique pong. Utilise les curses. |
| |
| Auteur: Martin Boulianne |
| e-mail: frzburn@gmail.com |
\****************************************/
#include <unistd.h>
#include <stdlib.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
/* Constants definitions */
#define SCREEN_HEIGHT 40 /* Set the screen height */
#define SCREEN_WIDTH 120 /* Set the screen width */
#define LOCAL_ESCAPE_KEY 27 /* Local escape key code */
#define BALL_SPEED 1 /* Set the ball movement speed */
static struct termios initial_settings, new_settings;
static int peek_character = -1;
/* Functions prototypes */
void printTable(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1]);
void init_keyboard();
void close_keyboard();
int move_ball(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1]);
int kbhit();
int readch();
/* Global variables */
int pad1, pad2; /* Actual pads position */
int pad1_, pad2_; /* Old pads position */
int ball_x, ball_y; /* Actual ball position */
int ball_xi, ball_yi; /* Ball deplacement vector */
int ball_x_, ball_y_; /* Old ball position */
int main(int argc, char *argv[])
{
int i, j, key;
char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1];
/* Variables initialisation */
pad1 = SCREEN_HEIGHT / 2 - 2;
pad2 = pad1;
pad1_ = pad1;
pad2_ = pad1;
ball_y = 16;
ball_x = 70;
ball_y_ = ball_y;
ball_x_ = ball_x;
ball_xi = -2; /* Giving initial ball movement */
ball_yi = -1;
/* Puts empty character in each table case */
for(i = 0; i < SCREEN_HEIGHT; i++) {
for(j = 0; j < SCREEN_WIDTH; j++) {
table[i][j] = ' ';
}
}
/* Make the borders of the table */
for(i = 0; i <= SCREEN_HEIGHT; i++) {
table[i][0] = 'X';
table[i][SCREEN_WIDTH] = 'X';
}
for(i = 0; i <= SCREEN_WIDTH; i++) {
table[0][i] = 'X';
table[SCREEN_HEIGHT][i] = 'X';
}
/* Put the ball in the table */
table[ball_y][ball_x] = 'O';
/* Put the pads in the table */
for(i = 0; i < 8; i++) {
table[pad1 + i][SCREEN_WIDTH - 4] = 'H';
table[pad2 + i][4] = 'H';
}
initscr();
printTable(table);
/* Now we're ready to manage the keys pressed */
crmode();
keypad(stdscr, TRUE);
noecho();
key = getch();
init_keyboard();
while(1) {
clrtoeol();
if(kbhit())
key = readch();
switch(key) {
case 'o':
if(pad1 > 1)
pad1 -= 1;
break;
case 'l':
if(pad1 < SCREEN_HEIGHT - 8)
pad1 += 1;
break;
case 'w':
if(pad2 > 1)
pad2 -= 1;
break;
case 's':
if(pad2 < SCREEN_HEIGHT - 8)
pad2 += 1;
break;
}
switch(move_ball(table)) {
case 1:
move(SCREEN_HEIGHT + 3, 5);
printw("%s", "Player 1 won!");
getch();
endwin();
exit(EXIT_SUCCESS);
case 2:
move(SCREEN_HEIGHT + 3, 5);
printw("%s", "Player 2 won!");
getch();
endwin();
exit(EXIT_SUCCESS);
}
printTable(table);
pad1_ = pad1;
pad2_ = pad2;
ball_y_ = ball_y;
ball_x_ = ball_x;
key = 0;
usleep(100000);
}
close_keyboard();
getch();
endwin();
exit(EXIT_SUCCESS);
}
/* Refresh the ball position in the table and manage collisions */
int move_ball(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1])
{
/* If the ball reches the top or the bottom */
if(((ball_y + ball_yi * BALL_SPEED) <= 0) || ((ball_y + ball_yi * BALL_SPEED) >= (SCREEN_HEIGHT)))
ball_yi = -ball_yi;
/* If the ball hit the left paddle */
if(((ball_x + ball_xi * BALL_SPEED) <= 4) && (((ball_y + ball_yi * BALL_SPEED) >= pad2))) {
if((ball_y + ball_yi * BALL_SPEED) <= pad2 + 8)
ball_xi = -ball_xi;
}
/* If the ball hit the right paddle */
if(((ball_x + ball_xi * BALL_SPEED) >= SCREEN_WIDTH - 4) && (((ball_y + ball_yi * BALL_SPEED) >= pad1))) {
if((ball_y + ball_yi * BALL_SPEED) <= pad1 + 8)
ball_xi = -ball_xi;
}
/* If it passes either paddle */
if((ball_x + ball_xi * BALL_SPEED) <= 0)
return 1;
if((ball_x + ball_xi * BALL_SPEED) >= SCREEN_WIDTH)
return 2;
ball_y += ball_yi * BALL_SPEED;
ball_x += ball_xi * BALL_SPEED;
table[ball_y_][ball_x_] = ' ';
table[ball_y][ball_x] = 'O';
return 0;
}
/* Print the whole table on the screen */
void printTable(char table[SCREEN_HEIGHT + 1][SCREEN_WIDTH + 1])
{
int i, j;
/* Refresh pads position */
for(i = 0; i < 8; i++) {
table[pad1_ + i][SCREEN_WIDTH - 4] = ' ';
table[pad2_ + i][4] = ' ';
}
for(i = 0; i < 8; i++) {
table[pad1 + i][SCREEN_WIDTH - 4] = 'H';
table[pad2 + i][4] = 'H';
}
/* Print the whole table on the screen */
for(i = 0; i <= SCREEN_HEIGHT; i++) {
for(j = 0; j <= SCREEN_WIDTH; j++) {
move(i, j);
addch(table[i][j]);
}
}
/* Move the cursor away from the table */
move(SCREEN_HEIGHT + 5, SCREEN_WIDTH + 5);
addch(' ');
refresh();
}
void init_keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}
int kbhit()
{
char ch;
int nread;
if(peek_character != -1)
return 1;
new_settings.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0, &ch, 1);
new_settings.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &new_settings);
if(nread == 1) {
peek_character = ch;
return 1;
}
return 0;
}
int readch()
{
char ch;
if(peek_character != -1) {
ch = peek_character;
peek_character = -1;
return ch;
}
read(0, &ch, 1);
return ch;
}
Conclusion
Bon, c'est encore incomplet, je viens tout juste de finir de le faire fonctionner. Et le code pourrait etre optimisé, sa ne fait aucun doute =P Ha, ya les characteres aussi que je veux changer, mettre un rectangle plein a place des 'X' et des 'H'... Mais pour l'instant c'est ca... Je suis tanné pour aujourd'hui.... Pour compiler, oubliez pas de mettre les curses. Sa devrait ressembler a : cc main.c -o pong -lcurses (et -Wall et tout ca si vous voulez =P)
Historique
- 14 avril 2005 02:09:34 :
- Amelioration
- 14 avril 2005 02:10:40 :
- 11 mars 2008 02:54:24 :
- Mises à jour du code source: nom de l'auteur.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
(allegro) Voici un jeu dont je voudrais votre avis [ par mooolo ]
Salut tout le mondeDésolé, encore un autre snake, ce n'est pas l'executable que je vous propose mais la source, il se trouve que cette dernière pèse 1
CMD-Comment créer un petit Jeu (pong/snake/...) [ par Actares1456 ]
Avant tout, j'ai bien compris que me lancer dans des projet beaucoup trop poussé ne me servirait à rien. Alors j'ai étudié le c++ et petit peu d'autre
Cours de linux [ par utharkao ]
[color=purple][size=200]Salut, Je suis débutante en linux,et comme je n'arrive à rien comprendre des cours de notre prof et nos exams approchent, je d
pause jeu allegro [ par souhayebyoussef ]
Salut, je suis entrain de programmer le fameux jeu Snake en C avec allegro et j'aimerai faire une pause pour le jeu en appuyant sur une touche. J'en
compilation d'un noyau linux [ par neotom40 ]
bonjour, Je doit compiler un noyau linux en croisé pour un PPC. j'ai donc installé une version de fedora en double boot sur mon PC pour pouvoir le
[Linux/ Cpp] Récupérer la source d'un html avec curl avec proxy [ par ce18ce ]
Bonjour, moi mon problème avec curl c'est que je ne sais pas comment récupérer le code source d'une page web à cause d'un proxy. Quelqu'un saurait com
Multithread linux [ par windowsfashion ]
HelloJe passe enfin sous linux et j'aimerai savoir comment créer des threads...Je sais comment faire sous windows mais voilà même aprés pas mal de rec
Lenteur selon processeur [ par Mastersam ]
Bonjour à tous je développe actuellement un jeu en OpenGL et C, tout ceci marchait bien sur différents PC jusqu'à ce que j'effectue plusieurs changeme
Erreurs sockets sous Linux ... [ par Ange44 ]
Salut ! Je dois faire du code portable utilisant les sockets, et j'ai un petit problème face au test de la perte de connexion ... Sous Windows,
SOCKET ASYNCHRONE LINUX [ par nicompx ]
Salut, Je cherche un exemple de communication par socket en C sous LINUX! J'aimerais que plusieurs client puissent laisser des messages au serveur, le
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|