I recently wrote my first game in C
As I dont know any graphics, all the graphics
that I used are based on text.
Please tell me your comments/suggestions.
www.akilla.tk
It Rocks
It Sucks
It is moderate. (Not so bad)
Who cares ?
I recently wrote my first game in C
As I dont know any graphics, all the graphics
that I used are based on text.
Please tell me your comments/suggestions.
www.akilla.tk
The game is jerky. Paddle movement can be a bit smoother.
Overall I'll say its nice considering that no graphics stuff was used.
How many ids are you going to create,dude??
akilla, learner007, moonwalker. I dont understand why you need so many of them??
-
You're right about the paddle movement.. but I am unable to increase it (too fast) or decrease it (too slow)
I think something is wrong with my code... I used the delay command for the ball to fall slowly, but that command is above the while loop of the input, this is slowing it down... you know what i mean ?
yeah, I have 2 IDs. 'Akilla' was just a guest name. I am settling with this one 'cause I like this better .
Pretty good overall, exept you might want to make it fullscreen, and the paddle movement smoother
.
awsome game! It's great considering your new and it's console.
nice job!
How can I make it full screen ?
I'll try to do it right away!!!
pretty cool...but I agree with everyone, the paddle movement is kind of jerky
Away.
Very nice! My only complaint: not the jerkiness, I'm fine with that, but the speed at which the paddle moves. it's much to difficult to catch up with that ball. If you don't mind, could you please post the source code? I've been trying a console thing like that and I can't get the collision detection to work. Brilliant work!
Brendan
If I increase the speed, it becomes more jerky (as this is wholly text and not graphics)
Here's the code anyway:
This may not be a very good code to learn from. I am a newbie and I pretty much suck when compared to most people here.PHP Code:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define paddlelength 7
struct gameinfo
{
int score;
int lives;
} gameboard;
struct gamecomponent
{
int bufx, bufy; //buffer coordinates
int oldx, oldy; //old coordinates
int newx, newy; //new coordinates
int xspeed, yspeed; //speed in x, y directions
void resetcoords();
} ball, paddle;
void gamecomponent :: resetcoords() //function to reset coordinates whenever they change
{
oldx = bufx;
oldy = bufy;
bufx = newx;
bufy = newy;
}
void waitforkeypress() //waits for a key press
{
while (!kbhit())
{
}
fflush(stdin);
}
void changecoordball() //changes the coordinates of the ball
{
if (ball.newy == paddle.newy && ball.newx >= paddle.newx && ball.newx <= (paddle.newx + paddlelength))
{
//if ball hits the paddle
if (ball.newx >= paddle.newx && ball.newx <= (paddle.newx + 3))
{
//if it falls on the left half of the paddle
if (ball.xspeed > 0)
{
ball.xspeed = -ball.xspeed; //changes direction to -ve (goes left)
}
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed; //changes y direction to -ve (goes up)
}
}
if (ball.newx > (paddle.newx + 3) && ball.newx <= (paddle.newx + paddlelength))
{
//if it falls on the right half of the paddle
if (ball.xspeed < 0)
{
ball.xspeed = -ball.xspeed; //changes x direction to +ve (goes right)
}
if (ball.yspeed > 0)
{
ball.yspeed = -ball.yspeed; //changes y direction to -ve (goes up)
}
}
}
if (ball.newx >= 79)
{
//if it hits the right border
if (ball.xspeed > 0)
{
ball.xspeed = -ball.xspeed; //changes x direction to -ve (goes left)
}
}
if (ball.newx <= 1)
{
//if it hits the left border
if (ball.xspeed < 0)
{
ball.xspeed = -ball.xspeed; //changes x direction to +ve (goes right)
}
}
if (ball.newy <= 1)
{
//if it hits the top wall
if (ball.yspeed < 0)
{
ball.yspeed = -ball.yspeed; //changes y direction to +ve (goes down)
}
}
//based on the above changes, the coordinates are affected as:
ball.newx = ball.newx + ball.xspeed;
ball.newy = ball.newy + ball.yspeed;
}
void changecoordpaddle(int direction) //changes coordinates of paddle
{
int delx;
if (direction == 1)
{
//if direction is right
delx = paddle.xspeed;
}
else
{
//if direction is left
delx = -paddle.xspeed;
}
if (paddle.newx + delx >= 1 && paddle.newx + delx + paddlelength <= 80)
{
//if paddle is free to move
paddle.newx = paddle.newx + delx;
}
}
void initialize() //initializes most variables
{
int tempposition = 1 + rand()%40; //temporary position of the ball during start
ball.bufx = tempposition;
ball.bufy = 1;
ball.newx = tempposition;
ball.newy = 1;
ball.oldx = tempposition;
ball.oldy = 1;
ball.xspeed = 1;
ball.yspeed = 1;
paddle.bufx = 35;
paddle.bufy = 25;
paddle.newx = 35;
paddle.newy = 25;
paddle.oldx = 35;
paddle.oldy = 25;
paddle.xspeed = 2;
paddle.yspeed = 1;
}
void placeball() //places the ball
{
gotoxy(ball.oldx, ball.oldy);
printf(" "); //places a space at old coordinates
gotoxy(ball.newx, ball.newy);
printf("o"); //places ball at new coordinates
}
void placepaddle() //places the paddle
{
gotoxy(paddle.oldx, paddle.oldy);
printf(" "); //places spaces at old coordinates
gotoxy(paddle.newx, paddle.newy);
printf("======="); //places paddle at new coordinates
}
int checkbounce()
{
int bounceval = 1; //bounceval = 1 assumes ball will bounce
if (ball.newy == paddle.newy)
{
if (ball.newx < paddle.newx || ball.newx > (paddle.newx + paddlelength))
{
bounceval = 0; //happens when ball falls off
}
else if (ball.newx >= paddle.newx && ball.newx <= paddle.newx + paddlelength)
{
gameboard.score++; //increases when ball hits paddle
}
}
return bounceval; //lates bounceval is returned
}
void cursor (int yn) //function hides/brings back the cursor. 0 to hide and 1 to bring back
{
CONSOLE_CURSOR_INFO c;
c.dwSize = 1;
c.bVisible = yn;
SetConsoleCursorInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ), &c );
}
void my_delay (int milliseconds) //delays in milliseconds
{
time_t end, now = clock();
for ( end = now + milliseconds; now < end; now = clock() )
continue;
}
int getpressedkey() //gets instant input
{
char c;
c = getch();
switch ((int)c)
{
case 27: //Escape key - returns 2
fflush(stdin);
return 2;
case 75: //left key - returns 0
fflush(stdin);
return 0;
case 77: //right key - returns 1
fflush(stdin);
return 1;
}
fflush(stdin);
return -1;
}
void showscore() //shows latest score
{
gotoxy(1, 1);
printf("Score: %d", gameboard.score);
gotoxy(73, 1);
printf("Lives: %d", gameboard.lives);
}
void showtitle() //title
{
my_delay(1000);
clrscr();
gotoxy(1, 24);
printf("\nProgrammed by Mahurshi Akilla\nhttp://mahurshi.tripod.com\n");
waitforkeypress();
cursor(1);
exit(1);
}
void showgameover() //shows game over
{
gotoxy(35, 13);
printf("Game Over!");
}
char playagain() //asks if he/she wants to play again
{
char yesorno;
do
{
showgameover();
gotoxy(1,24);
cursor(1);
printf("Do you want to play again? (y/n): ");
scanf("%c", &yesorno);
fflush(stdin);
} while (yesorno != 'y' && yesorno != 'Y' && yesorno != 'n' && yesorno != 'N');
return yesorno;
}
void main()
{
randomize();
int pressedkey;
int bouncer;
char yesorno;
do
{
gameboard.lives =3; //setting lives to 3
gameboard.score = 0; //setting score to 0
clrscr();
initialize(); //initializing all the variables
cursor(0);
showscore(); //show score
placeball(); //place ball
placepaddle(); //place paddle
waitforkeypress(); //wait a key to start
while (gameboard.lives >= 1 ) //as long as there's atleast 1 life, the following code runs
{
if (checkbounce() == 1) //as long as the ball hasn't fallen off
{
changecoordball(); //change the coordinates of the ball
ball.resetcoords(); //reset the old/new coordinates
placeball(); //places the ball at new coordinates
placepaddle(); //places the paddle at new coordinates
my_delay(65); //delays for a while. This can be increased to increase the ball speed
showscore(); //shows the latest score
while (kbhit()) //as long as a key is pressed, the following code runs
{
pressedkey = getpressedkey();
if (pressedkey != -1 && pressedkey != 2)
{
//if it's left or right arrow key only
changecoordpaddle(pressedkey); //changes coordinates based on arrow key pressed
paddle.resetcoords(); //resets the old/new coordinates
placepaddle(); //places the paddle in new coodinates
}
else if (pressedkey == 2)
{
//if Esc is pressed
showtitle();
}
}
}
else if (checkbounce() == 0) //if the ball has fallen off
{
gotoxy(38, 13);
printf("CRASH!"); //prints CRASH
waitforkeypress(); //waits for a key
clrscr(); //clears the screen
fflush(stdin); //clears the input buffer
initialize(); //initializes all the variables again (except lives & score)
gameboard.lives--; //lives are reduced
}
}
yesorno = playagain(); //asks if he/she wants to play again
fflush(stdin);
} while (yesorno == 'y' || yesorno == 'Y'); //if answer is yes, lives and score are reset and the game begins
showtitle(); //shows title
cursor(1); //brings back the cursor
}
Anyway, I hope this will be of some use to you.
GOOD LUCK!
Last edited by moonwalker; 07-23-2002 at 03:27 PM.
as far as the collision is conserned, just think about it..
1) the ball and the paddle should have the same y co ordinates
2) the x co-ordinate of the ball should be >= the x coordinate of the paddle and <= the x coordinate of the paddle + paddle length
try using buffered screen should be better.
research it before you ask!
¿Red Baron?
"Imagination is more important than knowledge"
-Albert Einstein (1879-1955)
Check out my games!
[code] /* dont forget code tags! */ [/code]
I don't know why everybody are complaining about the paddle movement. It's fine.
You should create a menu system too and some blocks for destroying them and a highscore table.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Great game! (considering its console and all)
I dotn think you can make the paddle movement less jerky, because its in the console and you cant exactly move the paddle by pixels, but characters rather.
"There are three kinds of people in the world...
Those that can count and those that can't."
I believe that increasing paddle movement would be diffecult, because unlike in windows games, user input and ball updates (ect) are separate checks.
What I mean is:
Although the functions run quickly, you begin to notice it when it comes to frame rate. Hey, using time.h and and integer, try to implement a fps meter.Code:clrscr(); initialize(); //initializing all the variables cursor(0); showscore(); //show score placeball(); //place ball placepaddle(); //place paddle waitforkeypress(); //wait a key to start
"If you tell the truth, you don't have to remember anything"
-Mark Twain
while (!kbhit())
AH HAH!!!!!!!! That function isn't very portable and is a big pain in the ass to universalize.
"If you tell the truth, you don't have to remember anything"
-Mark Twain