Спасибо за помощь)) Нашел вариант попроще на С. Кому надо:
Код | #include <conio.h> #include <stdlib.h> #include <graph.h>
void DrawField() { _setcolor(3); _rectangle( _GFILLINTERIOR, 635, 0, 640,480/2-50); _rectangle( _GFILLINTERIOR, 635, 480, 640,480/2+50); _rectangle( _GFILLINTERIOR, 0, 0, 5, 480/2-50); _rectangle( _GFILLINTERIOR, 0, 480, 5, 480/2+50); }
void DrawBall(double x, double y) { _setcolor(4); _ellipse( _GFILLINTERIOR, x + 10, y + 10, x - 10, y - 10); }
void EraseBall(double x, double y) { _setcolor(0); _ellipse( _GFILLINTERIOR, x - 10, y - 10, x + 10, y + 10); }
void DrawRacket(double x, double y) { _setcolor(5); _rectangle( _GFILLINTERIOR, x - 5, y - 40, x + 5, y + 40 ); }
void EraseRacket(double x, double y) { _setcolor(0); _rectangle( _GFILLINTERIOR, x - 5, y - 40, x + 5, y + 40 ); }
double ballx=150, bally=200, balldx=2, balldy=2; double racketx=120/2, rackety=480/2; int oldballx=0, oldbally=0; int oldracketx=0, oldrackety=0; int redraw_field=0;
int ProcessBall() { int res=0; ballx+=balldx; bally+=balldy; if (ballx+10>633){ if (bally-10>480/2-50 && bally+10<480/2+50 ) res=1; balldx=-balldx; ballx+=balldx; redraw_field=1; } if (ballx-10<7) { if (bally-10>480/2-50 && bally+10<480/2+50 ) res=2; balldx=-balldx; ballx+=balldx; redraw_field=1; } if (bally+10>480) balldy=-balldy; if (bally-10<10) balldy=-balldy;
if (ballx>racketx) { if (ballx-10<racketx+5 && bally+10>rackety-40 && bally-10<rackety+49) { balldx=-balldx; ballx+=balldx; DrawRacket(racketx, rackety); } } else if (ballx+10>racketx-5 && bally+10>rackety-40 && bally-10<rackety+49) { balldx=-balldx; ballx+=balldx; DrawRacket(racketx, rackety); } return res; }
int should_exit = 0, goal=0; int goals1=0, goals2=0; char s[20]=""; char ch; main() { short mode = _VRES16COLOR;
_setvideomode( mode ); DrawField(); while (!should_exit) { if (oldballx!=(int)ballx || oldbally!=(int)bally) { DrawBall(ballx, bally); oldballx = ballx; oldbally = bally; }
if (oldracketx!=(int)racketx || oldrackety!=(int)rackety) { DrawRacket(racketx, rackety); oldracketx = racketx; oldrackety = rackety; }
goal = ProcessBall(); if (goal){ if (goal==2) goals1++; else goals2++; } if (oldballx!=(int)ballx || oldbally!=(int)bally) EraseBall(oldballx, oldbally);
if (kbhit()) { ch = getch(); if (ch==27) should_exit = 1; if (ch==80) rackety+=5.0; if (ch==72) rackety-=5.0; }
_settextcolor( 7 ); sprintf(s, "Goals: %d \n Loses: %d", goals1, goals2 ); _settextposition(1, 3); _outtext( s );
if (oldracketx!=(int)racketx || oldrackety!=(int)rackety) EraseRacket(oldracketx, oldrackety); if (redraw_field) { DrawField(); redraw_field = 0; } } exit( !_setvideomode( _DEFAULTMODE ) ); }
|
|