Thread: Videogame

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    Exclamation Videogame

    Hi, I'm trying to make a videogame called "defender" in C. The point of the game is to defend the city drawn at the bottom from falling thunder with a little bar; it's sort like ping pong.

    Well the problem is I can make the "defender" bar move at the same time as the thunder falls, therefore it cannot be played and I can't proceed with it.
    I'd very much appreciate if someone could tell me how to call 2 voids in order for them to reproduce at the same time and not sequencially. If this is not possible maybe you could give me some advice in what to do or how to code it.
    Thank you so very much, here's my code.

    Btw, I'm working on Visual Studio 2005.

    Code:
    #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_INTENSITY);
    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_INTENSITY);
    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_HANDLE), coord); 
    }
    void gotoxy1(int j, int y) 
    { 
    COORD coord; 
    coord.X = j; 
    coord.Y = y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
    }
    void gotoxy2(int x, int y) 
    { 
    COORD coord; 
    coord.X = x; 
    coord.Y = y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
    }
    void gotoxy3(int r, int y) 
    { 
    COORD coord; 
    coord.X = r; 
    coord.Y = y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
    }
    void gotoxy4(int t, int y) 
    { 
    COORD coord; 
    coord.X = t; 
    coord.Y = y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), 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_INTENSITY);
    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;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent the code, for love of the gods!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    indent it? what do you mean? :S

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    not indented:

    Code:
    int main()
    {
    puts("BAD VERY BAD");
    return 0;
    }
    indented:

    Code:
    int main()
    {
       puts("Much better to read!");
    
       return 0;
    }
    Double Helix STL

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cassyhoz View Post
    indent it? what do you mean? :S
    Don't tell me you've been writing code like that all the time...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Sorry, I indented it now:

    Code:
    #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_INTENSITY);
    		printf("DEFENDER\n");
    		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);
    			(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);
    				(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_INTENSITY);
    		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_HANDLE), coord); 
    	}
    
    void gotoxy1(int j, int y) 
    	{ 
    		COORD coord; 
    		coord.X = j; 
    		coord.Y = y; 
    		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
    	}
    
    void gotoxy2(int x, int y) 
    	{ 
    		COORD coord; 
    		coord.X = x; 
    		coord.Y = y; 
    		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
    	}
    
    void gotoxy3(int r, int y) 
    	{ 
    		COORD coord; 
    		coord.X = r; 
    		coord.Y = y; 
    		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
    	}
    void gotoxy4(int t, int y) 
    	{ 
    		COORD coord; 
    		coord.X = t; 
    		coord.Y = y; 
    		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), 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 left with z and right with c, press any other key to exit");
    		SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_INTENSITY);
    		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;
    	}

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think the idea is that one function shouldn't make the thunder all the way down (I wasn't able to quite follow exactly what you were doing, but that's my guess). You should have a function that draws things as they are right this instant -- the player bar and the lightning and everything else. Then you can update positions -- let the lightning fall one spot, check if a key is down and if so move the player bar one spot. Then go back and draw again.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Elysia View Post
    Indent the code, for love of the gods!
    But if he does that we cannot include some sound-bytes I have prepared for his game. Such as:

    "DANGER! DANGER! Defend yourself from my unreadable code!"
    "This game was written using a simple encryption method."
    "An exception has been thrown from line 2 near character 10,355."

    Ok, I am done being a total ass. I gotta show some love to anyone who writes code in spanish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Videogame Memories - Contest
    By Sentral in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-05-2008, 04:43 PM