Skip to content

Instantly share code, notes, and snippets.

@Pr3d4dor
Last active August 29, 2015 14:24
Show Gist options
  • Save Pr3d4dor/fd642bf7244ac348c83a to your computer and use it in GitHub Desktop.
Save Pr3d4dor/fd642bf7244ac348c83a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define y 20
#define x 70
int main()
{
int i=0,j=0,ball_x=x/2,ball_y=y/2,p1_ini=7,p1_end=y-7,p2_ini=7,p2_end=y-7,control=0,move;
int movementballx=1,movementbally=1,pointsp1=0,pointsp2=0;
char field[y][x];
system("color 70");
srand(time(NULL));
printf ("To start playing the game enter 1: \n");
scanf ("%d", &control);
do{
//Sleep(35);
system ("cls");
//Field configuration
for (i=0;i<y;i++){
for (j=0;j<x;j++){
field[i][j]=' ';
if (j==(x-1))
field[i][j]='*';
if (j==0)
field[i][j]='*';
if (i==(y-1))
field[i][j]='*';
if (i==0)
field[i][j]='*';
}
}
//Ball configuration and ball movement
field[ball_y][ball_x]='o';
ball_x+=movementballx;
ball_y+=movementbally;
if (ball_x==1){
movementballx*=-1;
pointsp2++;
ball_x=x/2;
ball_y=((rand()%17)+1);
movementballx*=-1;
}
if (ball_x==x-1){
movementballx*=-1;
pointsp1++;
ball_x=x/2;
ball_y=((rand()%17)+1);
movementballx*=-1;
}
if ((ball_y==y-2) || (ball_y==1)){
movementbally*=-1;
}
if ((ball_x==2) && (ball_y==p1_ini || ball_y==p1_ini+1 || ball_y==p1_ini+2 || ball_y==p1_ini+3 || ball_y==p1_ini+4 || ball_y==p1_ini+5 || ball_y==p1_end)){
movementballx*=-1;
}
if ((ball_x==x-3) && (ball_y==p2_ini || ball_y==p2_ini+1 || ball_y==p2_ini+2 || ball_y==p2_ini+3 || ball_y==p2_ini+4 || ball_y==p2_ini+5 || ball_y==p2_end)){
movementballx*=-1;
}
//Players configuration
for (i=p1_ini;i<=p1_end;i++){
field[i][2]='|';
}
for (i=p2_ini;i<=p2_end;i++){
field[i][x-3]='|';
}
//Keys configuration
if(kbhit()==1) {
move=getchar();
switch(move){
case 'w':
if(p1_ini!=1) {
p1_end-=1;
p1_ini-=1;
}
break;
case 's':
if(p1_end!=y-2){
p1_end+=1;
p1_ini+=1;
}
case 'i':
if(p2_ini!=1) {
p2_end-=1;
p2_ini-=1;
}
break;
case 'k':
if(p2_end!=y-2){
p2_end+=1;
p2_ini+=1;
}
break;
}
}
for (i=0;i<y;i++){
printf ("\n");
for (j=0;j<x;j++){
printf ("%c", field[i][j]);
}
}
printf ("\nScores: \n");
printf ("Player 1: %d\n", pointsp1);
printf ("Player 2: %d\n", pointsp2);
}while (control!=0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment