473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Videogame

cassyhoz
Hi, I was asked in school to make this sort of videogame in C basic where a little bar defends a city (which is in the bottom) from falling thunders.

I'm working on Visual Basic 2005 and this is the code I've been able to develop. However I have a problem 'cause the little "defender" bar won't move at the same time as the thunders fall.
I have absolutely no idea of how to make both things happen at the same time and not sequencially. Can somebody help me out pleaseeeeeeeeeee? I just can't continue working on it with out that, I just need a little explanation..

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <dos.h>
#include <time.h>
#define AMARILLO (14)
#define ROSA (5)
#define GRIS (8)
void presentacion(void)
{
HANDLE s= GetStdHandle (STD_OUTPUT_HANDLE);
system("cls");
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_INTE NSITY);
printf("DEFENDER\n");
printf("\n");
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),ROSA|FOREGROUND_INTENSITY);

printf("Press any key to continue");
}
void ciudad(void)
{ int a, b, c;
HANDLE s= GetStdHandle (STD_OUTPUT_HANDLE);
HANDLE q= GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);
system("cls");
coord.Y = 23;
for (a=0; a<=78; a++)
{
coord.X= a;
SetConsoleCursorPosition(q, coord);printf("%c\n", 220);
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);
}
c=10;
for (b=-10; b<60; b++) //ciclo principal
{

b=b+c;
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);

coord.X = 0+b;
for (a=23; a>=20; a--)
{ coord.Y= a;
SetConsoleCursorPosition(q, coord);printf("%c\n", 219);
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);
}
coord.Y = 19;
for (a=0; a<=3; a++)
{ coord.X= a+b;
SetConsoleCursorPosition(q, coord);printf("%c\n", 220);
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);
}
coord.X = 3+b;
for (a=23; a>=20; a--)
{ coord.Y= a;
SetConsoleCursorPosition(q, coord);printf("%c\n", 219);
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);
}
coord.Y = 21;
for (a=4; a<=9; a++)
{ coord.X= a+b;
SetConsoleCursorPosition(q, coord);printf("%c\n", 220);
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);
}
coord.X = 9+b;
for (a=22; a<24; a++)
{ coord.Y =a ;
SetConsoleCursorPosition(q, coord);printf("%c\n", 219);
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),GRIS|FOREGROUND_INTENSITY);
}
}
fflush(stdout);
}
void random(void)
{
int n, M, R, y, numero;
HANDLE s= GetStdHandle (STD_OUTPUT_HANDLE);
HANDLE q= GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_INTE NSITY);
y=0;
coord.Y = y;
R=0;
n=78;
M=1;
srand( (unsigned)time( NULL ) );
while (R<1)
{
numero = rand () % (n);
coord.X = numero;
SetConsoleCursorPosition (q, coord);
printf("%c\n", 31);
while (y<18)
{
y = y+1;
coord.Y = y;
SetConsoleCursorPosition (q, coord);
Sleep(100);
printf("%c\n", 31);
}
if (y=18)
{
y=0;
coord.X = numero;
SetConsoleCursorPosition (q, coord);
printf("%c\n", 255);
while (y<18)
{
coord.Y = y;
SetConsoleCursorPosition (q, coord);
printf("%c\n", 255);
y = y+1;
}
}
R=R+1;
}
fflush(stdin);
}
void gotoxy0(int m, int y)
{
COORD coord;
coord.X = m;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}
void gotoxy1(int j, int y)
{
COORD coord;
coord.X = j;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}
void gotoxy2(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}
void gotoxy3(int r, int y)
{
COORD coord;
coord.X = r;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}
void gotoxy4(int t, int y)
{
COORD coord;
coord.X = t;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}
main()
{
int tecla;
int salir=0;
int y=12,m=38,j=39,x=40,r=41,t=42;
presentacion();
getch();
ciudad ();
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),AMARILLO|FOREGROUND_INTENSITY) ;
printf("Move with: z left, c right ... go out with escape");
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_INTE NSITY);
gotoxy2(x,y);
while(salir==0)
{
tecla=getch();

switch(tecla)
{
case 122://izquierda
if (j>0)
{

m-=1;
j-=1;
x-=1;
r-=1;
t-=1;
gotoxy0(m,y);
printf("%c",255);
gotoxy1(j,y);
printf("%c", 22);
gotoxy2(x,y);
printf("%c", 22);
gotoxy3(r,y);
printf("%c", 22);
gotoxy4(t,y);
printf("%c",255);
}
else
{
j=0;
x=1;
r=2;
}
break;

case 99://derecha
if (r<79)
{
m+=1;
j+=1;
x+=1;
r+=1;
t+=1;
gotoxy4(t,y);
printf("%c",255);
gotoxy3(r,y);
printf("%c", 22);
gotoxy2(x,y);
printf("%c", 22);
gotoxy1(j,y);
printf("%c", 22);
gotoxy0(m,y);
printf("%c",255);
}
else
{
j=77;
x=78;
r=79;
}

break;

default:
salir=1;
break;
}
random();
}

return 0;
}
Dec 2 '08 #1
6 2704
vmpstr
63
The explanation for your problem is that you have the following happening:

get the user input
move the bar
generate a thunder, and make it fall all the way down
repeat.

What you should have is this:

get the user input
move the bar
make the thunder fall one square
if the thunder has fallen all the way, remove it
repeat

this requires a fairly significant change in your code. If you can move the bar faster than once every 100 ms, then you need to get rid of Sleep, because it will always pause for 100 ms.

You also don't need a separate gotoxy for each variable name that do the same thing. The variable names do not matter. Just have gotoxy(int x, int y) and then you can call gotoxy(r, y) and gotoxy(m, y) just fine.
Dec 2 '08 #2
but if I make the thunder fall one square that'd only make my bar move quickly, it would still depend on me moving the bar for the thunder to fall down wouldn't it?

I'm sorry if I didn't understand quite well, I'm very ignorant on this, only been working on it for about a month :S sry
Dec 2 '08 #3
I took out the sleep function and now I can move the bar normally, but the thunders fall too fast. You said I should make them move square by square, but how do I do that?
Dec 2 '08 #4
vmpstr
63
Suppose you only have one thunder at any point in time. Say you have thunderx and thundery for it's position and thunderexists as a flag meaning it exists (this should all be stored in a struct, if you have studied that)

Then you can have something along these lines (and forgive me if it doesn't compile, I'm just typing it in, not testing it)

Expand|Select|Wrap|Line Numbers
  1. // infinite loop
  2. for(;;)
  3. {
  4.    if(thunderexists == 0) 
  5.    {
  6.       thunderx = rand()%field_width;
  7.       thundery = top_of_the_screen;
  8.       thunderexists = 1;
  9.    }
  10.    // get the user input
  11.    // if getch waits (blocks), then it is not good here
  12.    // I remember borland had kbhit that would tell you if input is available
  13.    // I don't know if you have that function, though
  14.    input = getch();
  15.  
  16.    // move the bar
  17.    switch(input)
  18.    {
  19.        ...
  20.    }
  21.    // move the thunder
  22.    if(thunderexists == 1)
  23.    {
  24.        thundery++; // or --, depending how it is moving
  25.        if(thundery > very_bottom_of_the_field)
  26.        {
  27.             thunderexists = 0;
  28.         }
  29.     }
  30.     Sleep(100);
  31. }   
  32.  
Do you see what I mean? You have to move the thunder and the bar in the same iteration of the loop, not move the bar and then keep moving the thunder

EDIT:
it seems that some version of kbhit does exist.

So you would have

Expand|Select|Wrap|Line Numbers
  1. if(kbhit())
  2.   input = getch();
  3. else
  4.   input = 0; // or some other value not handled in the switch
  5.  
Dec 2 '08 #5
I'm very sorry to ask, but is there a simpler way to do that? I haven't learned much in school yet and I ignore most of what you wrote in the last post ): I'm ashamed to say I need something simpler..

They told me that maybe I could somehow improve it if I called the defender bar each time the thunder's added another piece, cause there's always a thunder and it's always increasing, so I should be able to move with the bar all the time.

Is it possible to call for a void outside the main?

Do you have any other suggestion? :S I'm so sorry to keep asking :(
Dec 2 '08 #6
vmpstr
63
We're here to answer, so don't be sorry for asking.

Umm... Well, from the code you have, I don't see a simple way out...

you have function A and function B (A happens to be main)

in A, you move the bar and call B
in B, you move the thunder all the way down.

So you see, you can't move the bar while the thunder is moving down, because the control isn't going to return to A until the thunder is all the way down.

To say in other words, once you are inside the function called random, the thunder will go ALL the way down. Once you are outside the random function, you can move the bar and then you are again, inside random (which means the thunder will move ALL the way down).

You need to move the thunder and the bar at the same time. If you think about it in chess terms, what you have is player1 makes one move, then player2 makes as many moves as he wants, then player1 makes one move, then player 2 makes as many moves as he wants... Clearly player2 is going to win.

What you need to have is player1 makes a move, then player2 makes one move, and so on.

So, you need to move the bar, then move the thunder, move the bar, then move the thunder. NOT move the bar, then let the thunder make many moves.

I mean, there isn't a much simpler way to do it than what I said... Maybe ask a question about my post? What don't you understand out of what I said?
Dec 2 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Marcelo A. Camelo | last post by:
Hi! I will be presenting Python to an audience of game developers, mostly C/C++ programmers. In my presentation I will talk about using python and C/C++ extension instead of pure C/C++ to write...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
36
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at...
16
by: fernandez.dan | last post by:
Hey I'm sorry if this is not the appropriate news group for this question. I was wondering if anyone has any recommendation for embbedding a script engine in a c++ application. I want to feed my...
16
by: Jeff Flinn | last post by:
At the risk of raising the OT ire of some here, I'd like to know what might be done to raise the awareness of the boost libraries. I've found boost and it's libraries invaluable in my work for ~5...
31
by: somebody | last post by:
No, I'm not a troll, just angry. I just started learning C++, and find it extremely lacking compared to Java. For example, I had to write my own functions to do something as simple as a case...
14
by: Magcialking | last post by:
somebody told me that c++ is superior to c while others insist that c++ is just a junk. I don't know which to follow.I want to know exactly the difference between them, and their respective...
2
by: martoncho | last post by:
Hi all, I am facing what is for me a really complicated situation. I program C but not this advanced (I mean direct hardware access etc). Here is the background: I am using DJGPP. I am...
9
by: zensunni | last post by:
I'd like to create a game like a simple RPG. For this, I want something beyond just console output. I'd like to start working with something that's "industry standard", meaning that it is flexible,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.