Obsah fóra
PravidláRegistrovaťPrihlásenie




Odpovedať na tému [ Príspevkov: 3 ] 
AutorSpráva
Offline

Užívateľ
Užívateľ
Obrázok užívateľa

Registrovaný: 15.12.07
Prihlásený: 14.12.07
Príspevky: 2
Témy: 1
Príspevok NapísalOffline : 15.12.2007 0:55

Ahoj na internete som našiel tento script:

Kód:
/* ppong.c
* bounce a character (default is 'o') around the screen
* defined by some parameters
*
* user input: s slow down x component, S: slow y component
* f speed up x component, F: speed y component
* Q quit
*
* blocks on read, but timer tick sends SIGALRM caught by ball_move
* build: cc ppong.c set_ticker.c -lcurses -o ppong
*/

#include <curses.h>
#include <signal.h>
#include "ppong.h"

struct ppball the_ball ;

/** the main loop **/

void set_up();
void wrap_up();

int main()
{
int c;

set_up();

while ( ( c = getchar()) != 'Q' ){
if ( c == 'f' ) the_ball.x_ttm--;
else if ( c == 's' ) the_ball.x_ttm++;
else if ( c == 'F' ) the_ball.y_ttm--;
else if ( c == 'S' ) the_ball.y_ttm++;
}

wrap_up();
}


void set_up()
/*
* init structure and other stuff
*/
{
void ball_move(int);

the_ball.y_pos = Y_INIT;
the_ball.x_pos = X_INIT;
the_ball.y_ttg = the_ball.y_ttm = Y_TTM ;
the_ball.x_ttg = the_ball.x_ttm = X_TTM ;
the_ball.y_dir = 10 ;
the_ball.x_dir = 10 ;
the_ball.symbol = DFL_SYMBOL ;

initscr();
noecho();
crmode();

signal( SIGINT , SIG_IGN );
mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
refresh();

signal( SIGALRM, ball_move );
set_ticker( 1000 / TICKS_PER_SEC ); /* send millisecs per tick */
}

void wrap_up()
{

set_ticker( 0 );
endwin(); /* put back to normal */
}


void ball_move(int signum)
{
int y_cur, x_cur, moved;

signal( SIGALRM , SIG_IGN ); /* dont get caught now */
y_cur = the_ball.y_pos ; /* old spot */
x_cur = the_ball.x_pos ;
moved = 0 ;

if ( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ){
the_ball.y_pos += the_ball.y_dir ; /* move */
the_ball.y_ttg = the_ball.y_ttm ; /* reset*/
moved = 1;
}

if ( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ){
the_ball.x_pos += the_ball.x_dir ; /* move */
the_ball.x_ttg = the_ball.x_ttm ; /* reset*/
moved = 1;
}

if ( moved ){
mvaddch( y_cur, x_cur, BLANK );
mvaddch( y_cur, x_cur, BLANK );
mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
bounce_or_lose( &the_ball );
move(LINES-1,COLS-1);
refresh();
}
signal( SIGALRM, ball_move); /* for unreliable systems */

}

int bounce_or_lose(struct ppball *bp)
{
int return_val = 0 ;

if ( bp->y_pos == TOP_ROW ){
bp->y_dir = 1 ;
return_val = 1 ;
} else if ( bp->y_pos == BOT_ROW ){
bp->y_dir = -1 ;
return_val = 1;
}
if ( bp->x_pos == LEFT_EDGE ){
bp->x_dir = 1 ;
return_val = 1 ;
} else if ( bp->x_pos == RIGHT_EDGE ){
bp->x_dir = -1;
return_val = 1;
}

return return_val;
}




/* ppong.h */

/* some settings for the game */

#define BLANK ' '
#define DFL_SYMBOL 'o'
#define TOP_ROW 5
#define BOT_ROW 20
#define LEFT_EDGE 10
#define RIGHT_EDGE 70
#define X_INIT 10 /* starting col */
#define Y_INIT 10 /* starting row */
#define TICKS_PER_SEC 50 /* affects speed */

#define X_TTM 5
#define Y_TTM 8

/** the ping pong ball **/

struct ppball {
int y_pos, x_pos,
y_ttm, x_ttm,
y_ttg, x_ttg,
y_dir, x_dir;
char symbol ;

} ;






#include <stdio.h>
#include <sys/time.h>
#include <signal.h>

/*
* set_ticker.c
* set_ticker( number_of_milliseconds )
* arranges for the interval timer to issue
* SIGALRM's at regular intervals
* returns -1 on error, 0 for ok
*
* arg in milliseconds, converted into micro seoncds
*/


set_ticker( n_msecs )
{
struct itimerval new_timeset;
long n_sec, n_usecs;

n_sec = n_msecs / 1000 ;
n_usecs = ( n_msecs % 1000 ) * 1000L ;

new_timeset.it_interval.tv_sec = n_sec; /* set reload */
new_timeset.it_interval.tv_usec = n_usecs; /* new ticker value */
new_timeset.it_value.tv_sec = n_sec ; /* store this */
new_timeset.it_value.tv_usec = n_usecs ; /* and this */

return setitimer(ITIMER_REAL, &new_timeset, NULL);
}


Môžete mi prosím vás povedať čo s tým aby to fungovalo ako to rozdeliť a tak, vďaka (som lama do c, ale potrebujem to)


Offline

Užívateľ
Užívateľ
Pomoc: Pong v C

Registrovaný: 30.05.06
Prihlásený: 28.06.15
Príspevky: 2278
Témy: 45
Bydlisko: ZA
Príspevok NapísalOffline : 15.12.2007 10:50

rozdel to na 3 subory, mas to predsa v komentoch[aj nazvy]
zaciatok subor poznas podla #include || #define


Offline

Užívateľ
Užívateľ
Obrázok užívateľa

Registrovaný: 15.12.07
Prihlásený: 14.12.07
Príspevky: 2
Témy: 1
Príspevok Napísal autor témyOffline : 15.12.2007 11:17

Takže ak som to dobre pochopil rozdelím to na tieto 3 súbory:

Kód:
#include <curses.h>
#include <signal.h>
#include "ppong.h"

struct ppball the_ball ;

/** the main loop **/

void set_up();
void wrap_up();

int main()
{
int c;

set_up();

while ( ( c = getchar()) != 'Q' ){
if ( c == 'f' ) the_ball.x_ttm--;
else if ( c == 's' ) the_ball.x_ttm++;
else if ( c == 'F' ) the_ball.y_ttm--;
else if ( c == 'S' ) the_ball.y_ttm++;
}

wrap_up();
}


void set_up()
/*
* init structure and other stuff
*/
{
void ball_move(int);

the_ball.y_pos = Y_INIT;
the_ball.x_pos = X_INIT;
the_ball.y_ttg = the_ball.y_ttm = Y_TTM ;
the_ball.x_ttg = the_ball.x_ttm = X_TTM ;
the_ball.y_dir = 10 ;
the_ball.x_dir = 10 ;
the_ball.symbol = DFL_SYMBOL ;

initscr();
noecho();
crmode();

signal( SIGINT , SIG_IGN );
mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
refresh();

signal( SIGALRM, ball_move );
set_ticker( 1000 / TICKS_PER_SEC ); /* send millisecs per tick */
}

void wrap_up()
{

set_ticker( 0 );
endwin(); /* put back to normal */
}


void ball_move(int signum)
{
int y_cur, x_cur, moved;

signal( SIGALRM , SIG_IGN ); /* dont get caught now */
y_cur = the_ball.y_pos ; /* old spot */
x_cur = the_ball.x_pos ;
moved = 0 ;

if ( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ){
the_ball.y_pos += the_ball.y_dir ; /* move */
the_ball.y_ttg = the_ball.y_ttm ; /* reset*/
moved = 1;
}

if ( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ){
the_ball.x_pos += the_ball.x_dir ; /* move */
the_ball.x_ttg = the_ball.x_ttm ; /* reset*/
moved = 1;
}

if ( moved ){
mvaddch( y_cur, x_cur, BLANK );
mvaddch( y_cur, x_cur, BLANK );
mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
bounce_or_lose( &the_ball );
move(LINES-1,COLS-1);
refresh();
}
signal( SIGALRM, ball_move); /* for unreliable systems */

}

int bounce_or_lose(struct ppball *bp)
{
int return_val = 0 ;

if ( bp->y_pos == TOP_ROW ){
bp->y_dir = 1 ;
return_val = 1 ;
} else if ( bp->y_pos == BOT_ROW ){
bp->y_dir = -1 ;
return_val = 1;
}
if ( bp->x_pos == LEFT_EDGE ){
bp->x_dir = 1 ;
return_val = 1 ;
} else if ( bp->x_pos == RIGHT_EDGE ){
bp->x_dir = -1;
return_val = 1;
}

return return_val;
}



Ten uložím ako ppong.c

Kód:
#define BLANK ' '
#define DFL_SYMBOL 'o'
#define TOP_ROW 5
#define BOT_ROW 20
#define LEFT_EDGE 10
#define RIGHT_EDGE 70
#define X_INIT 10 /* starting col */
#define Y_INIT 10 /* starting row */
#define TICKS_PER_SEC 50 /* affects speed */

#define X_TTM 5
#define Y_TTM 8

/** the ping pong ball **/

struct ppball {
int y_pos, x_pos,
y_ttm, x_ttm,
y_ttg, x_ttg,
y_dir, x_dir;
char symbol ;

} ;




Tento ako ppong.h

Kód:
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>

/*
* set_ticker.c
* set_ticker( number_of_milliseconds )
* arranges for the interval timer to issue
* SIGALRM's at regular intervals
* returns -1 on error, 0 for ok
*
* arg in milliseconds, converted into micro seoncds
*/


set_ticker( n_msecs )
{
struct itimerval new_timeset;
long n_sec, n_usecs;

n_sec = n_msecs / 1000 ;
n_usecs = ( n_msecs % 1000 ) * 1000L ;

new_timeset.it_interval.tv_sec = n_sec; /* set reload */
new_timeset.it_interval.tv_usec = n_usecs; /* new ticker value */
new_timeset.it_value.tv_sec = n_sec ; /* store this */
new_timeset.it_value.tv_usec = n_usecs ; /* and this */

return setitimer(ITIMER_REAL, &new_timeset, NULL);
}


A toto ako set_ticker.c

A teraz? Nejde to skompilovať :oops:


Odpovedať na tému [ Príspevkov: 3 ] 


Podobné témy

 Témy  Odpovede  Zobrazenia  Posledný príspevok 
V tomto fóre nie sú ďalšie neprečítané témy. pong game

v Assembler, C, C++, Pascal, Java

5

452

02.11.2011 17:25

nBXXL Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. 3D Ping Pong

v Počítačové hry

17

1275

27.04.2007 20:11

Holup Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. Herna konzola / pong z roku 1976. Typ Binatone Tv master mk6

v Predám

3

542

10.08.2017 0:00

jericho79 Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc pomoc prosim nefunguje internet

v Ovládače

7

1137

09.09.2017 20:37

Smith Wesson Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. Pomoc pri pomoc pri skladaní PC - od 1 500 do 1 800 Eur (rozlíšenie 1440p)

v PC zostavy

13

802

28.09.2023 20:23

johny314 Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. POMOC

[ Choď na stránku:Choď na stránku: 1, 2 ]

v Ostatné

30

2268

27.09.2009 20:01

workoholik16 Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc

v PHP, ASP

2

786

02.02.2008 17:17

Punker661 Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc

v Ostatné

13

952

08.08.2009 22:16

malcolm Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc

v PHP, ASP

6

710

01.08.2008 16:27

Darkeye18 Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc !

v Ovládače

1

955

18.08.2007 16:07

johny128 Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. Pomoc

v ATI/AMD grafické karty

3

863

02.01.2008 7:51

Daron Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc

v Monitory, televízory a projektory

2

874

19.02.2008 13:40

patqo_he Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc

v nVidia grafické karty

1

1032

02.02.2007 10:51

Harlequin Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. Pomoc

v Zvuk

2

839

24.11.2007 17:30

optyx Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. pomoc

v ATI/AMD grafické karty

10

1059

10.01.2008 8:48

kasom Zobrazenie posledných príspevkov

V tomto fóre nie sú ďalšie neprečítané témy. POMOC ???

v Delphi, Visual Basic

2

1258

27.03.2008 14:32

Martinek6 Zobrazenie posledných príspevkov


Nemôžete zakladať nové témy v tomto fóre
Nemôžete odpovedať na témy v tomto fóre
Nemôžete upravovať svoje príspevky v tomto fóre
Nemôžete mazať svoje príspevky v tomto fóre

Skočiť na:  

Powered by phpBB Jarvis © 2005 - 2024 PCforum, webhosting by WebSupport, secured by GeoTrust, edited by JanoF
Ako väčšina webových stránok aj my používame cookies. Zotrvaním na webovej stránke súhlasíte, že ich môžeme používať.
Všeobecné podmienky, spracovanie osobných údajov a pravidlá fóra