Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Help Space Invaders

  1. #1
    psycho is offline Newbie
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Help Space Invaders

    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:
    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    psycho is offline Newbie
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Re: Help Space Invaders

    Apparently I was using the 'keypressed' funcion un-accordingly. It works!
    Now it llooks like this:
    Code:
    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.

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Help Space Invaders

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    psycho is offline Newbie
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Re: Help Space Invaders

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

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Help Space Invaders

    If you have '|' on screen, to "erase" it you need to write ' ' in the same place.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    psycho is offline Newbie
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Re: Help Space Invaders

    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!

  8. #7
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Help Space Invaders

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #8
    witroy is offline Newbie
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Re: Help Space Invaders

    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

  10. #9
    psycho is offline Newbie
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Re: Help Space Invaders

    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:
    Code:
    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:
    Code:
    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...
    Code:
    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 Attached Files

  11. #10
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Help Space Invaders

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help with Space Invaders Coursework
    By Tezelia in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-23-2011, 06:23 AM
  2. Problem with simple Space Invaders clone
    By eafkuor in forum Java Help
    Replies: 6
    Last Post: 10-30-2010, 04:53 AM
  3. Lost Space?
    By turbocharged123 in forum Linux/Unix General
    Replies: 7
    Last Post: 07-09-2009, 12:37 PM
  4. Add more swap space?
    By Tor in forum Linux Hardware
    Replies: 8
    Last Post: 06-21-2008, 01:08 PM
  5. HD Space Left
    By Prog in forum Linux/Unix General
    Replies: 2
    Last Post: 06-18-2007, 03:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts