PDA

View Full Version : C Programmers Come Here



DWk
01-26-2004, 01:59 AM
Hey what up. Well if you didn't read before, I'm learning C right now (and should have learned it in 1 week lol). Anyways, the only C project the teachers are leaving is either making a Galaga-type of game or a text editor. I went for the game, since it looks easier (specially since the text editor has to be a notepad wannabe).

Here's my code for the game


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>

int x = 37;
int y = 25;
char b;


void ship (int x, int y);
void shoot (int x, int y);
void menu ();
void salida ();

void salida(){
clrscr();
delay(1000);
printf("Gracias por jugar\n");
exit(0);
}


void shoot(int x, int y){
int i=21;
while (i>0){
 int ejex = x;
 gotoxy(ejex+3, i);
 printf("|");
 delay (10);
 i--;

}
ship(x, y);

}

void ship(int x, int y){
clrscr();
gotoxy(x,y);
printf("*-----*");
gotoxy(x,y-1);
printf(" *---* ");
gotoxy(x,y-2);
printf("  *-*  ");
gotoxy(x,y-3);
printf("   *   ");


b = getch();

switch (b){

 case 'a':
  ship(x-7,y);
  break;
 
 case 'd':
  ship(x+7,y);
  break;
 
 case 's':
  shoot(x, y);
  break;
 
 case 'm':
  menu();
  break;

 case 'z':
  salida();
  break;

 default:
  ship(x, y);
}

}

void menu(){

clrscr();
gotoxy(22,10);
printf("Izquierda   A\n");
gotoxy(22,11);
printf("Disparar   S\n");
gotoxy(22,12);
printf("Derecha    D\n");
gotoxy(22,13);
printf("Menu    M\n");
gotoxy(22,14);
printf("Salir    Z\n");
gotoxy(22,16);
printf("Para empezar a jugar, presione E\n");
gotoxy(22,17);
printf("Para salir, presione Z\n");

b = getch();

switch (b){

 case 'e':
  ship(x, y);
  break;

 case 'z':
  salida();
  break;

 default:
  menu();
   
}

}


void main(){

menu();

}


The menu and stuff is in spanish. Just press E to go playing or Z to exit. While playing, you use A and D to move left and right, S to shoot, M to return to the menu, and Z to exit.

As you would obviously see, the game is at no point done. The only thing the ship can do now is move and shoot. However my question would be this:

1. Try moving to the far right or far left, beyond the screen. Any ideas on how to set the limits? I have an idea of putting an if statement in the switch in the ship function, but if you have any more ideas, please share.

2. Any ideas on how to create random evil ships? And as well, how to actually make em die?

Thanks, it's all in the name of knowledge :)

By the way, compile with Borland 3.1, cuz I don't think the other compilers will work (since it's not an ANSI program, it contains other functions and libs).

Bye :) B)

h1
01-26-2004, 02:30 AM
DWk, I believe you said your course was ANSI C...

abu_has_the_power
01-26-2004, 02:35 AM
C is a bit old. at least C++ man

DWk
01-26-2004, 02:35 AM
Originally posted by haxor41789@25 January 2004 - 19:30
DWk, I believe you said your course was ANSI C...
That noob-ass teacher really confused us. They explained a lot about ANSI (well not really), and didn't explain there was MORE to ANSI.

And I was trying to compile my program with DJGPP (GCC for windows), then a friend told me that I had to compile with TurboC or BorlandC. Pissed me off.

But nothing I can do anymore, just shut up and do it. :D

EDIT - Abu, I can't go with the university teacher and tell him to change it ;)

shn
01-26-2004, 03:51 AM
Originally posted by abu_has_the_power@25 January 2004 - 20:35
C is a bit old. at least C++ man
It may be old but its one the best languages around <_<

h1
01-26-2004, 05:39 AM
Originally posted by shn+26 January 2004 - 04:51--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (shn @ 26 January 2004 - 04:51)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-abu_has_the_power@25 January 2004 - 20:35
C is a bit old. at least C++ man
It may be old but its one the best languages around <_< [/b][/quote]
True that.

cselik
01-26-2004, 09:57 AM
Originally posted by DWk@26 January 2004 - 02:35
And I was trying to compile my program with DJGPP (GCC for windows), then a friend told me that I had to compile with TurboC or BorlandC. Pissed me off.

But nothing I can do anymore, just shut up and do it. :D

yes there is something u can do, is write programs properly in the start by saying what to do on different platforms


#ifdef &nbsp;WIN32
#include &#60;winsock.h&#62;
#else
#include &#60;netdb.h&#62;
#include &#60;sys/types.h&#62;
#include &#60;sys/socket.h&#62;
#include &#60;netinet/in.h&#62;
#include &#60;netinet/protocols.h&#62;
#include &#60;arpa/inet.h&#62;
#endif


in exams i took one would fail if his programs didn&#39;t compile on any random compiler (yea and do whats it defined to do)

DWk
01-26-2004, 11:03 AM
But I&#39;m actually compiling in Windows, so that&#39;s not the problem.

In the university, I have to do so as well (not SHH and use Putty and GCC), I&#39;ll ask today why do we have to. I think it is because TC and BC include some libraries that GCC doesn&#39;t. Let me ask and I&#39;ll get back at you :)

BTW anyone tried the program?

cselik
01-26-2004, 12:44 PM
Originally posted by DWk@26 January 2004 - 11:03
But I&#39;m actually compiling in Windows, so that&#39;s not the problem.

In the university, I have to do so as well (not SHH and use Putty and GCC), I&#39;ll ask today why do we have to. I think it is because TC and BC include some libraries that GCC doesn&#39;t. Let me ask and I&#39;ll get back at you :)

not TC and BC you did.

&#60;dos.h&#62;



BTW anyone tried the program?
where&#39;s the enemies? :huh:

Try my game :
arkanoid II (http://www.hszk.bme.hu/~cd197/Programzas/VisC/arkanoid2/index.html)

what u think?

DWk
01-26-2004, 11:02 PM
Originally posted by cselik+26 January 2004 - 05:44--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (cselik @ 26 January 2004 - 05:44)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-DWk@26 January 2004 - 11:03
But I&#39;m actually compiling in Windows, so that&#39;s not the problem.

In the university, I have to do so as well (not SHH and use Putty and GCC), I&#39;ll ask today why do we have to. I think it is because TC and BC include some libraries that GCC doesn&#39;t. Let me ask and I&#39;ll get back at you :)

not TC and BC you did.

&#60;dos.h&#62;



BTW anyone tried the program?
where&#39;s the enemies? :huh:

Try my game :
arkanoid II (http://www.hszk.bme.hu/~cd197/Programzas/VisC/arkanoid2/index.html)

what u think? [/b][/quote]
Enemies, bleh I said I only did the moving and shooting part.

That&#39;s why I&#39;m asking help here :ph34r:

shn
01-27-2004, 01:13 AM
ROFL............notepad wannabe? What do you think most text editors are. Ms has put out some shitty apps before but notepad is pretty good. It has all the basic features that a text editor should have and its has a few others that dont make it bloated.

I use text editors for editing config files not writing novels. Thats what word processors are for. And with all do respect if you cant code a simple notepad wannabe then you really shouldnt be trying to code anything thats more advanced than that because most of your time will be debugging it instead of actually finishing it.


Hope that didnt offend you or anyone else but its the truth ;)

DWk
01-27-2004, 01:27 AM
:cry1:

Well I&#39;m not making the text editor. I&#39;m just making the game. That&#39;s where I need help at :)

cselik
01-27-2004, 01:43 PM
my suggestion:
(as i don&#39;t see x changing, throw ejex out)


case &#39;d&#39;&#58;
if &#40;x&#60;whaeva_limit_fits&#41;
{
&nbsp;x+=7;
&nbsp;ship&#40;x,y&#41;;
}
....


word of advice: dont use delay, use some sort of time calc to make it smooth.
then declaration shold be (for no warnings)


void salida &#40;void&#41;;
void menu &#40;void&#41;;


if you want enemies you should rename your prog to cpp and


class Enemy{
int x,y,size,alive,etc...
Enemy &#40;x,y&#41;{ alive=-1; /*constructor stuff....*/}
~Enemy &#40;&#41;{alive_=0; /*destructor stuff...*/}
Draw&#40;&#41;{ ...}...
}


and you can call it from prog
Enemy sharman(1,0);
if (sharman.alive) { sharman.Draw(); }...

DWk
01-28-2004, 02:36 AM
Lololol Sharman. Ok, I just understood a little of what you said.

Why should I rename it to .cpp? I&#39;m doing C not C++ (although I know they can be compiled the same).

cselik
01-28-2004, 07:42 AM
Originally posted by DWk@28 January 2004 - 02:36
Lololol Sharman. Ok, I just understood a little of what you said.

Why should I rename it to .cpp? I&#39;m doing C not C++ (although I know they can be compiled the same).
because u need objects to decribe behaviour.
if u don&#39;t use objects you&#39;re going to be spending 20x more time writing your game in ansi, and will be filling up the memory with unnecessary variables.

no .c and .cpp do not compile the same, from extension the bc knows how to parameter the compiler.

DWk
01-29-2004, 12:50 AM
Ok then....can you explain again then, please :)

DWk
01-31-2004, 04:23 AM
Bumping this.... I&#39;m gonna have to do SOMETHING this weekend and I wanna make the enemy ships :D

DWk
01-31-2004, 03:11 PM
:angry:

Come on now :)

cselik
01-31-2004, 03:37 PM
sorry, i was out in the nature playing, with no comp.
so "explain again?"

ok you need objects. i am not going to write them for u as u have an alternative not to do them OO-way, and i&#39;m not gona be there on your exam.

in order for OO&#39;s to work you need to rename a .c source to .cpp so compiler is called properly.

you describe the behavior of each object inside a &#39;class&#39;
class Name
{
Name(){}
or/and
Name(int a){}
or /and
Name(char *t, int a){}
are constuctors. you can have as many as u want, they are automatically called as object come and go in your game.
automatically means if u call it with int the second constructor will generate the ojject

~Name(){}
is a destructor. is automatically called when theres no need for the object to exist. btw you can call on it if u want to delete an object manually, is good when sth dies.

Other_functions(){}
can be added as well
}


you create an object like this:
(class name is &#39;Name&#39; and u want an object named &#39;willy&#39; then:)



Name willy;
or
Name willyz&#40;2&#41;;

these eill execute constuctor

when u allready have willy named &#39;Name&#39;
then u can
willy.Other_finctions;

so if u understand these u can easily rewrite your game to be 50 lines and easily understandable, almost english (or spanish) -like statements.

i really am not going to write it, nobody wrote my c homeworks back then. this is a thing you gotta go through and use with ease in the future.

DWk
01-31-2004, 04:29 PM
Hmm..... ok I thought classes were in Java, not C.

If indeed that is C and not Java, I assume they are defined almost the same way as in Java? With the exception that you don&#39;t put public..... etc. lol :unsure: