PDA

View Full Version : Help Space Invaders Shooting



psycho
11-10-2009, 10:26 PM
As the title implies, I need help with programming in the shooting function for the space ship. 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. As of now I managed to code-in the spacebar function to create the bullet, but what I want to do now is to move the bullet upwards.


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
REPEAT
Shooting := readkey;
IF Shooting = Spacebar AND Can_Shoot THEN
BEGIN
gotoxy(9,9);

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

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.

Thanks in advance!
Psycho.

PD: As you can see, the gameplay field is supposed to be a matrix.

chronozphere
12-10-2009, 06:54 AM
You should make a (dynamic) array containing information about all the bullets. Something like this:


TBullet = record
Used: Boolean; //Whether the bullet is in use (visible and moving)
X,Y: Integer; //Position of the bullet
DX,DY: Integer; //Direction in which the bullet travels (you can leave this out when you want all bullets to travel upwards).
//etc etc...
end;

MyBullets: array of TBullet;


Once in a while, you should loop through this array and move all the bullets one step. After you have done this (or before you make the step), you should check if the bullet has hit something.

Good luck :)

psycho
12-10-2009, 05:45 PM
Ok I kinda get what you're saying, what I don't understand is how do I make it go through the array if i have not defined the array's dimension?
And I came up with a different scheme, basically with a For loop I make it write the bullet one step up and then one step down I make it erase it. The only problem is that the bullet goes down instead of going up...


PROCEDURE Bullet_Creation(var Shoot : char);

var X,Y : integer;
BEGIN
X := 20;

Shoot := readkey;
IF Shoot = Spacebar THEN
BEGIN
FOR Y:= 0 to max_col DO
BEGIN
GOTOXY(X,Y+1);
writeln('|');

GOTOXY(X,Y-1);
writeln(' ');
delay(50);
END;
END;


END;


Thanks again! hope you can cooperate...

pjpdev
12-10-2009, 07:07 PM
Ok I kinda get what you're saying, what I don't understand is how do I make it go through the array if i have not defined the array's dimension?
And I came up with a different scheme, basically with a For loop I make it write the bullet one step up and then one step down I make it erase it. The only problem is that the bullet goes down instead of going up...


PROCEDURE Bullet_Creation(var Shoot : char);

var X,Y : integer;
BEGIN
X := 20;

Shoot := readkey;
IF Shoot = Spacebar THEN
BEGIN
FOR Y:= 0 to max_col DO
BEGIN
GOTOXY(X,Y+1);
writeln('|');

GOTOXY(X,Y-1);
writeln(' ');
delay(50);
END;
END;


END;


Thanks again! hope you can cooperate...


Do it the other way around. Remember that the Y axis moves from top to bottom, so you should minus to move to the top.


GOTOXY(X, Y-1);
writeln('|');

GOTOXY(X, Y+1);
writeln(' ');
delay(50);

psycho
12-10-2009, 07:32 PM
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.

User137
12-10-2009, 09:07 PM
Do not draw anything on screen at Bullet_Creation, instead draw all bullets same time in the main loop. You should have the coordinates of each bullet saved in array.

There is example of using dynamic array (i am not sure if all pascal compilers support this, especially oldest):
http://www.delphibasics.co.uk/RTL.asp?Name=Array

But you can just define, say 100 bullets max and have the array size static. If you can't use record such as described in previous posts you can have 2 arrays:
BulletX,BulletY: array[0..99] of integer;

psycho
26-10-2009, 03:38 AM
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('0123456789012345678921234567893123456789412 34567895123456789612345678971234567');

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,filn av,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...
Please help me! I need to finish this by Thursday or im screwed! THANK YOU ALL
PS Is it possible to attach a file?

psycho
27-10-2009, 04:20 AM
Never mind mates, I got it going 8)
I just embedded the if statement that checks for the spacebar in the ship's movement procedure.
Now to finish off the martians...