A game problem in Turbo C 2.0
hello,everybody.
This is a tiny game.It is like a Andre Lamothe's sample.I wrote it in
Turbo C 2.0. But it does not work well.The ball can't move itself.Only
while i hit the key,the ball just move a little.
I think the problem is the key buffer and the function key().But im a
newbie. I don't know how to use DPMI or EMS.can it work in DOS real
mode?
What can i do?
Thanks for anyone.
After is the game's src:
#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
/*---------------------------Defines---------------------------------------*/
#define SCREEN_HEIGHT 480 /*Define the screen.-----------*/
#define SCREEN_WIDTH 640
#define BALL_START_X random(30) /*Define the ball--------------*/
#define BALL_START_Y random(40)
/*Define the pannel.-----------*/
#define PANNEL_START_X (SCREEN_WIDTH/2 - 16)
#define PANNEL_START_Y (SCREEN_HEIGHT -32)
#define PANNEL_WIDTH 64
#define PANNEL_HEIGHT 16
#define KEY_ESC 27 /*Define the key map-----------*/
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_L 108
#define KEY_H 104
#define GAME_STATE_INIT 0 /*Define the game's state.-----*/
#define GAME_STATE_START_LEVEL 1
#define GAME_STATE_RUN 2
#define GAME_STATE_SHUTDOWN 3
#define GAME_STATE_EXIT 4
/*-------------------------End define--------------------------------------*/
/*---------------------------Game console----------------------------------*/
int Gema_main(void);
int key(void);
/*----------------------------Globals--------------------------------------*/
int ball_x = 0,
ball_y = 0;
int ball_dx = 0, /*velocity of the ball----*/
ball_dy = 0;
int pannel_x1 = 0,
pannel_y1 = 0,
pannel_x2 = 0,
pannel_y2 = 0;
int score = 0; /*the origin score.--------*/
int get_key = 0;
int gdriver=DETECT,
gmode;
int game_state = GAME_STATE_INIT;
/*-------------------------------main--------------------------------------*/
int
main(void)
{
ball_dx+=random(2);
ball_dy+=random(4);
while((get_key = key()) != KEY_ESC){
Game_main();
}
return 1;
}
/*-----------------------------End main------------------------------------*/
/*-----------------------process the ball---------------------------------*/
void process_ball(void)
{
cleardevice();
/*-------------Test if the ball hit the pannel----------------------------*/
if((ball_x >= pannel_x1 && ball_x <= pannel_x2) &&
(ball_y >= pannel_y1 && ball_y <= pannel_y2))
{
ball_dy = -ball_dy;
ball_y += ball_dy;
if ( get_key == KEY_RIGHT || get_key == KEY_L)
{ ball_dx -= random(3);
ball_dy -= random(3);
}
else
if ( get_key == KEY_LEFT || get_key == KEY_H)
{ ball_dx += random(3);
ball_dy += random(3);
}
else
ball_dx += -1 + random(3);
circle(ball_x,ball_y,30);
return;
}
ball_x+=ball_dx;
ball_y+=ball_dy;
} /*End process_ball*/
/*Get key map.*/
int key()
{
int key;
while((bioskey(1) == 0)){
key = bioskey(0);
key = key & 0xff ? key & 0xff : key >> 8;
return (key);
}
}/*End get keymap*/
/*--------------------------Game main--------------------------------------*/
int Game_main(void)
{
if (game_state == GAME_STATE_INIT)
{
initgraph(&gdriver,&gmode,"e:\\soft\\tc2"); /*goto the graphic mode*/
pannel_x1 = PANNEL_START_X;
pannel_y1 = PANNEL_START_Y;
pannel_x2 = pannel_x1 + PANNEL_WIDTH;
pannel_y2 = pannel_y1 + PANNEL_HEIGHT;
ball_x = 8 +random(SCREEN_WIDTH-16);
ball_y = BALL_START_Y;
ball_dx = 4 + random(8);
ball_dy = 6 + random(1);
}
game_state = GAME_STATE_RUN;
if (game_state == GAME_STATE_RUN)
{
cleardevice();
setcolor(GREEN);
rectangle(pannel_x1,pannel_y1,
pannel_x2,pannel_y2);
/*-------------------Move the pannel,and keep in the screen.---------------*/
if (get_key == KEY_RIGHT || get_key == KEY_L)
{
pannel_x1+=8;
pannel_x2 = pannel_x1 + PANNEL_WIDTH;
if (pannel_x2 > (SCREEN_WIDTH))
pannel_x1 = SCREEN_WIDTH - PANNEL_WIDTH;
}
if (get_key == KEY_LEFT || get_key == KEY_H)
{
pannel_x1-=8;
pannel_x2 = pannel_x1 + PANNEL_WIDTH;
if (pannel_x1 < 0)
pannel_x1 = 0;
}
/*---------------Test if the ball hit my screen.--------------------------*/
if (ball_x > (SCREEN_WIDTH) || ball_x < 0)
{
ball_dx = -ball_dx;
ball_x +=ball_dx;
score -= 50;
}
if(ball_y > (SCREEN_HEIGHT) || ball_y < 0)
{
ball_dy = -ball_dy;
ball_y += ball_dy;
score -= 50;
}
if (ball_dx > 8) ball_dx = 8; /*contorl the ball's velocity*/
else
if (ball_dx < -8) ball_dx = 8;
process_ball();
rectangle(pannel_x1,pannel_y1, /*Draw the pannel*/
pannel_x2,pannel_y2);
circle(ball_x,ball_y,6); /*Draw the ball*/
/*-----------------Exit the game.----------------------------------------*/
if (get_key == KEY_ESC)
{
game_state = GAME_STATE_SHUTDOWN;
}
}
else
if (game_state == GAME_STATE_SHUTDOWN)
{
closegraph();
game_state = GAME_STATE_EXIT;
}
return (1);
}
[edited by - longlong_0 on March 12, 2003 4:44:38 AM]
I never coded C++, so I don''t know if I can help you, but the
problem sounds like the ball is ''waiting'' for keyboard input.
So I would recommend making a new really short program to find out how those keyboard commands work.
Some DOS-based keyboard commands will pause your program until any keypress has occured.
So use the short program to find out exactly what each keyboard instruction does. I''m convinced the C language offers a routine which will just tell you what key is pressed or return 0 if nothing happend and continue the program.
Good Luck, NewBe
problem sounds like the ball is ''waiting'' for keyboard input.
So I would recommend making a new really short program to find out how those keyboard commands work.
Some DOS-based keyboard commands will pause your program until any keypress has occured.
So use the short program to find out exactly what each keyboard instruction does. I''m convinced the C language offers a routine which will just tell you what key is pressed or return 0 if nothing happend and continue the program.
Good Luck, NewBe
Ok, try changing your key function to...
int key() {
int key;
if(kbhit()) {
key = getch();
return (key);
} else {
return -1;
}
}
And change your main loop to...
while(1) {
get_key = key();
if(get_key == KEY_ESC) break;
Game_main();
}
I think that should help things.
[edited by - Xanth on March 12, 2003 2:59:41 PM]
int key() {
int key;
if(kbhit()) {
key = getch();
return (key);
} else {
return -1;
}
}
And change your main loop to...
while(1) {
get_key = key();
if(get_key == KEY_ESC) break;
Game_main();
}
I think that should help things.
[edited by - Xanth on March 12, 2003 2:59:41 PM]
quote:Original post by Xanth
Ok, try changing your key function to...
int key() {
int key;
if(kbhit()) {
key = getch();
return (key);
} else {
return -1;
}
}
And change your main loop to...
while(1) {
get_key = key();
if(get_key == KEY_ESC) break;
Game_main();
}
I think that should help things.
[edited by - Xanth on March 12, 2003 2:59:41 PM]
thank for your help.
i have solved the problem.
there are many things i will do .
thanks
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement