Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help Space Invaders

pascal

  • Please log in to reply
11 replies to this topic

#1 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 11 October 2009 - 07:21 AM

Hey peeps, how's it going?
I need help with a college project I'm working on. I'm supposed to program a Space Invaders clone in good ol' pascal using non other than the CRT library; that means no graphics, just characters to interpret the game's graphics. I'm trying to program in the ship's shooting function, but I can't seem to make it work...
What I wanna do is check whether the player is pressing the spacebar key and then create the bullet (using the character 'I') at a certain spot. The gameplay field is supposed to be a Matrix.
Here's the code:
USES crt;

CONST
	max_row = 40;   //Matrix dimensions
	max_col = 80;
        Spacebar = #32;

// Can_Shoot checks the posibility to shoot, not yet implemented
//	Shooting checks if the user y pressing the spacebar key 
PROCEDURE Bullet_Creation(var Can_Shoot : boolean; var Shooting : char);
BEGIN
IF keypressed AND Can_Shoot THEN
	BEGIN
                Shooting := readkey;
			IF Shooting = Spacebar THEN
				BEGIN
                                 gotoxy(9,9);

                                 writeln('I');
                                 Can_Shoot := false;
				END;
	END;
END;

// Main Program
var Can_Shoot : boolean;
    Shooting : char;
    MATRIX : ARRAY[1..max_row,1..max_col] OF char;
    i,j : byte;
BEGIN
	Bullet_Creation(Can_Shoot,Shooting);


	//Print Matrix
    for i:=1 to max_row do
    begin
    for j:=1 to max_col do
      write (MATRIX[i,j]);
    writeln;
    end;

        readln();

END.

Now my other doubt is:
Is there any way to disable the typing so you are only allowed to use the arrow keys and spacebar?
Thanks a mil, Psycho
  • 0

#2 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 11 October 2009 - 02:13 PM

Apparently I was using the 'keypressed' funcion un-accordingly. It works!
Now it llooks like this:
PROCEDURE Bullet_Creation(var Can_Shoot : boolean; var Shooting : char);
BEGIN
REPEAT
                Shooting := readkey;
			IF Shooting = Spacebar AND Can_Shoot THEN
				BEGIN
                                 gotoxy(9,9);

                                 writeln('I');
                                 Can_Shoot := false;
				END;
UNTIL keypressed;
END;


But now, I want it to 'fire up' , not just stand still on the screen...
Is there a way of implementing the 'gotoxy' function to do as above? Or should I make it go up through the rows of the matrix?
Thanks again,
Psycho.
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 October 2009 - 03:20 PM

If I recall correctly, you will need to use gotoxy to write the shot, then go back to write space, then go up one to write the shot's track , etc.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 11 October 2009 - 06:53 PM

Cool, thanks for helping out, but I don't quite follow. What do you mean by writing space?
  • 0

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 October 2009 - 09:18 AM

If you have '|' on screen, to "erase" it you need to write ' ' in the same place.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 12 October 2009 - 11:35 AM

GREAT!!! Finally got it goin' !!
Now, there's one more thing I want to establish before I leave...
I know that sooner or later I will have to check for collisions and I am supposed to make the bullet go through the matrix, all I'm doing here with the gotoxy is printing it on a given coordinate.
What I want to know is:
Is it possible for the bullet to go through the columns of the matrix?
For instance 'J = J + 1' I don't know, make it take a step through the dimensions.
Thanks a lot by the way!
  • 0

#7 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 October 2009 - 12:19 PM

I'm not clear on what you're asking. You will definitely want the action onscreen to correspond with an array of positions that can be used for collision detection.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#8 witroy

witroy

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 25 October 2009 - 02:50 PM

Psycho, did you finish the game?

Could you upload the code somewhere so i could see it?

I would really appreciate it

Thanks in advance
  • 0

#9 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 25 October 2009 - 07:28 PM

Guess who's back!
I'm almost done with the game, I still have to finish the movement for the enemies, their shooting and as always, fix the freaking bugs! I managed to get around the shooting function for the spaceship, and I am pleased to announce that I did not have to use an array or a record for such action. All I did was declare two separate variables containing the x,y coordinates of the ship and then applying them to the 2D array.
Now here's the problem: I can't make the ship shoot and move at the same time, meaning that until you press the space bar, the ship won't move unless the bullet is drawn on the screen... Could it be because I am calling the readkey and keypressed functions from two different procedures? (One to move the ship and the other to shoot).
Here's some of the code, mainly the shooting procedure and the moving one:
procedure crear_bala(variable_dario_x,variable_dario_y:byte;var bala_x,bala_y:byte;var puntaje : integer; var Poder_Disparar : boolean; var matriz : tm_matriz; var punta_a:byte);
Begin

if keypressed then
 begin
 if (not Poder_Disparar) and (readkey = spacebar) then
    
	  	 begin
          Poder_Disparar := true;
          bala_x:=punta_a;
          bala_y:=37; {variable que tomo del proc de dario}
	    end;
    end;

  IF (Poder_Disparar) and (bala_y>1) THEN
    begin
      dec(bala_y);
      if ((matriz[bala_x,bala_y]<>'M') and (matriz[bala_x,bala_y]<>'R')
            and (matriz[bala_x,bala_y]<>'^')) then
        begin
          matriz[bala_x,bala_y] := 'I';
          gotoxy(bala_x,bala_y);
          write(matriz[bala_x,bala_y]);
          if matriz[bala_x,bala_y+1]='I' then
            begin
              matriz[bala_x,bala_y+1]:=' ';
              gotoxy (bala_x,bala_y+1);
              write (matriz[bala_x,bala_y+1]);
            end;
        end;
BTW that's for the bullet, it's in spanish i know... crear_bala = bullet creation poder_disparar = canshoot
And for the movemente:
procedure Mover_Nave(var matriz: tm_matriz; var punta_d, punta_i, punta_a: byte; var tecla:char);
var
        fil, col, i: byte;
begin
        i:=0;
        if (tecla='d') then
                begin
                col:=punta_d;
                for fil:=38 downto 36 do
                        begin
                        while (matriz[col,fil]='^') do
                                begin
                                int(matriz[col,fil],matriz[col+1,fil]);
                                gotoxy(col+1,fil);
                                write(matriz[col+1,fil]);
                                gotoxy (col,fil);
                                write (matriz[col,fil]);
                                dec(col);
                                end;
                        inc(i);
                        col:=punta_d-i;
                        end;
                inc(punta_d);
                inc(punta_i);
                inc(punta_a);
                end;
        i:=0;
        if (tecla='a') then
                begin
                col:=punta_i;
                for fil:=38 downto 36 do
                        begin
                        while (matriz[col,fil]='^') do
                                begin
                                int(matriz[col,fil],matriz[col-1,fil]);
                                gotoxy(col-1,fil);
                                write(matriz[col-1,fil]);
                                gotoxy (col,fil);
                                write (matriz[col,fil]);
                                inc(col);
                                end;
                        inc(i);
                        col:=punta_i+i;
                        end;
                dec(punta_d);
                dec(punta_i);
                dec(punta_a);
                end;
end;
MAIN LOOP this is where I need help also...
begin
  dis_mars_fil:=6;
  dis_mars_col:=6;
  clrscr;
  perdiste:=false;
  colicion:=false;
  hay_tiro:=false;
  cant_vidas:=3;
  cargar(m_matriz,punta_marcianos_x,punta_marcianos_y);
  mostrar(m_matriz);
  cargar_nave(m_matriz,pd,pi,pa);
  vida (m_matriz);
  gotoxy (1,2);
  write('012345678901234567892123456789312345678941234567895123456789612345678971234567');

  while not perdiste do
    begin
      delay(50);
      resta_vida (colicion,cant_vidas,perdiste,m_matriz);
      p_disparo_marciano (m_matriz,hay_tiro,colicion,punta_marcianos_x,filnav,punta_marcianos_y,colnav,x_bala,y_bala);
		crear_bala(variable_dario_x,variable_dario_y,bala_x,bala_y,puntaje,Poder_Disparar,m_matriz,pa);
      if keypressed then
                        begin
                        teclapresionada:=readkey;
								mover_nave(m_matriz, pd, pi, pa, teclapresionada);
                        end;


    end;
end.

All variables are declared correctly, I just need help with the keypressed and readkey functions. I only posted the code I need help with cause the rest is too long, I uploaded it as an attacthment if you guys wanna take a glander at the whole thing...
Please help me! I need to finish this by Thursday or im screwed! THANK YOU ALL

Attached Files


  • 0

#10 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 October 2009 - 03:11 PM

Have you traced through your code in a debugger to see what's happening? I've looked at it a little, but nothing's obvious at the moment. I think the keypressed function acts more like a typewriter than a normal hook into the keyboard, so it just registers keystrokes. Think of it like typing in Word, you can only type one of 'a', 'd', or ' ' at a time.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#11 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 26 October 2009 - 05:34 PM

Yes, I've used the debugger function to see what's up, but I can't find the logical error behind all this.
Maybe I need to insert the readkey function within a loop?
If I call the bullet procedure from the keypressed statement in the main program (like the ship's movement for instance), the bullet will only move when you press a key, but the ship will move without a problem...
Anyway, I'm out of ideas :closedeyes:
Thanks again wingedpanther
  • 0

#12 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 26 October 2009 - 08:16 PM

Never mind mates, I got it going :thumbup1:
I just embedded the if statement that checks for the spacebar in the ship's movement procedure.
Now to finish off the martians...
  • 0





Also tagged with one or more of these keywords: pascal

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download