begin process at 2012 05 27 16:02:44
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Applications Linux

 > ENCORE UN PONG (LINUX, CURSES) [GCC]

ENCORE UN PONG (LINUX, CURSES) [GCC]


 Information sur la source

Note :
Aucune note
Catégorie :Applications Linux Classé sous :pong, linux, jeu Niveau :Débutant Date de création :23/11/2004 Date de mise à jour :11/03/2008 02:54:24 Vu :7 668

Auteur : frzburn

Ecrire un message privé
Site perso
Commentaire sur cette source (5)
Ajouter un commentaire et/ou une note

 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

SOCKETS CLIENT/SERVER [GCC]
Source avec Zip Source avec une capture COPIER UN FICHIER, LOG DES ACTIONS [GCC]

 Sources de la même categorie

Source avec Zip TRAITEMENT D'IMAGE PGM par Jios
Source avec une capture COLORIMÈTRE NUMÉRIQUE LINUX par valchek
Source avec Zip TRAITEMENTS D'IMAGES AU FORMAT PGM AVEC LES ALGORITHMES DE C... par lemout
Source avec Zip ALGORITHME ACO INTERFACE GTK par RyBeN
Source avec Zip COMPRESSER SES SAUVEGARDES SMSBACKUPRESTORE (ANDROID) EN C A... par ThalLab

 Sources en rapport avec celle ci

Source avec Zip TRAITEMENT D'IMAGE PGM par Jios
Source avec une capture COLORIMÈTRE NUMÉRIQUE LINUX par valchek
Source avec Zip Source avec une capture SOKOBAN EN C POUR DÉBUTANT (VERSION AMÉLIORÉE BASÉE SUR LE T... par eustatika
Source avec Zip Source avec une capture PONG EN SDL par cynix
Source avec Zip Source avec une capture PONG AVEC GDI par feanor11

Commentaires et avis

Commentaire de frzburn le 23/11/2004 03:40:50

Désolé pour l'alignement (dans les switch), je sais pas pourquoi, mais c'est tout croche une fois posté ici... Et quand je regarde pour l'editer, ben c'est correct...
En tout cas...

Commentaire de spidermario le 14/02/2007 17:12:46

Qu'est-ce qu'il a, l'alignement dans les switch ?

Commentaire de smok1360 le 10/03/2008 23:09:49

1 ans aprés...
Je suis tombé sur cette source (d'autres aussi) et ça me choque enfin c'est plutôt louche...

Pourquoi commenter en anglais ? Aprés si c'est pour que plus de monde puissent en profiter...
Pourquoi aprés avoir codé tu dis que tu aimerais changer les X en rectangles moi perso je l'aurais fait au moment venu pas aprés la source enfin...
Le commentaire perdu pour le switch ?

Bref mettez au moins le nom du programmeur original ;)

Commentaire de frzburn le 11/03/2008 02:59:36

Hey allo! J'avais complètement oublié que j'avais poster des sources ici depuis le temps :O
Pour l'alignement dans les switch, tout semble correct. Je crois qu'à l'origine le code était mal aligné, comme si il y avait trop de 'tab' ou pas assez. Mais peu importe, tout est bien là.

Pour les commentaires en anglais, et bien essentiellement tout ce que je fais est en anglais. Mon OS est en anglais, mes logiciels, je travail en anglais et c'est comme ça que j'aime ça. Et oui, en même temps ça rend le code (les commentaires en fait) compréhensibles pour plus de monde.

Pour les X changés en rectangle, sérieusement je ne sais pas, du moins je ne me rapelle plus :O  Probablement que je ne savais pas comment faire!! ;P

Finalement, c'est noté pour le nom de l'auteur original. Voilà donc mon vrai nom plutôt que mon 'nickname'.


Merci!

Commentaire de clampin le 03/06/2011 15:52:02

Je tiens à dire qu'il fonctionne très bien sur Mac Os X :)

 Ajouter un commentaire


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


Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,889 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales