Shooting?

Previous topic - Next topic

Hotshot

I would like to know how you mkae shooting that upward so often.....so if player tap spacebar so often then it shoot up quite lots

so from what I know.....there must be abit of delay when come to shooting......like every 2 seconds per shot would be good because for every arcade shoot em up games that I have played are like that:)


mentalthink

Hi take a look to this thread:

http://www.glbasic.com/forum/index.php?topic=8212.0

Works fine, sure  =D =D :good:

Ian Price

You need to prevent the key from being repeatedly read by giving it a value once you've pressed it. Decrease this value every frame. Your code needs to ensure that the value is zero before it can fire again. Some code -

Code (glbasic) Select


If KEY(fire) AND press=0
// fire_bullet()
press=10
ENDIF

IF press>0 THEN DEC press



Increase or decrease the "press=10" value to speed up/slow down repeat speed of firing.
I came. I saw. I played.

Hotshot

this is what I have done so far.....but not perfect at the moment

Code (glbasic) Select


FUNCTION Shoot:

         // Press Spacebar to Shoot
         IF KEY(57) AND press=0
            DRAWSPRITE  Bang,MX+265,270-SY
         ENDIF


         SY=SY+2

         IF press>0
            DEC press
            SY=270    // IF Player shoot off then restart the gun at top of spaceship to shoot even more :)
         ENDIF
         

ENDFUNCTION



bigsofty

Little bit ahead of where you are but this is one way to go for multiple bullets.

I'd maybe consider creating an array to hold your bullet X and Y coordinates.

Every time you press fire add a new set of X and Y coordinates to the array.

To draw your bullets simple go through the array one row at a time for the X and Y coordinates.

To move your bullets go through your array and change your Y coordinate.

You can use DELETE to remove something from an array.

You can use DIMPUSH to add something  to an array.

A slightly more complex way but easier to manage, is to make and array of custom types to hold each bullets information within and array, see TYPE and ENDTYPE.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Hotshot

If we going take from Type then

Code (glbasic) Select

Type Shot
        SX
        SY
endtype

GLOBAL shots[] AS SHOT


I am not sure how type deal with Functions I used because I used functions to break small code down

okee

#6
Something like

Code (glbasic) Select
TYPE TShip
Sprite // Ship Sprite
X // X position
Y // Y position
BulletCharge
Bullet[] AS TBullet //Bullet is an array of the type bullet

// setup the ship once only
FUNCTION Initalise:
self.Sprite = GENSPRITE(); LOADSPRITE "ship.png", self.Sprite
self.X = 400
self.Y = 300
ENDFUNCTION

// Draw the ship
FUNCTION Draw:
DRAWSPRITE self.Sprite, self.X, self.Y
ENDFUNCTION

FUNCTION Move:
IF KEY(203)
self.X = self.X -2 //left
ELSEIF KEY(205)
self.X = self.X +2 //right
ELSEIF KEY(200)
self.Y = self.Y -2 //Up
ELSEIF KEY(208)
self.Y = self.Y +2 //down
ENDIF
ENDFUNCTION


ENDTYPE

TYPE TBullet
Sprite
X
Y

FUNCTION Draw:
DRAWSPRITE self.Sprite, self.X, self.Y
ENDFUNCTION

ENDTYPE

GLOBAL Ship AS TShip

// main loop
WHILE KEY(1) <> 1

Ship.Draw()
Ship.Move()
Ship.Shoot()


SHOWSCREEN
WEND


Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

Hotshot

#7
I didnt know you can used function inside Type in Glbasic :)

Should I Rewrite the code again and used Function inside type or would end up confusing myself?


Hemlos

Why not use a timed itteration?
Bing ChatGpt is pretty smart :O

Hotshot

Quote
Why not use a timed itteration?

What is that as that never heard of it until now :-[

Hemlos

Its just food for thought, using timers for bullets fired.
It can be built very simple, with gettimer() function.
This command reports showscreen time elapsed in milliseconds.
Add up the itterations, and you can build a timer to reset(shoot) at any time.


Here is an example of a function that keeps track of multiple timers,link below.
The only thing about these( i didnt think about it before ) is it keeps track of seconds, not milliseconds.
In case you need milliseconds you can eleiminate this entire function, and use raw gettimer() output to increment.
Or perhaps you can rewrite the function to handle milliseconds, up to you.
Like i said just an idea, derived from your notion about shooting every 2 seconds.

have a look at a multi timer tracking function:
http://www.glbasic.com/forum/index.php?topic=86.msg284#msg284


Bing ChatGpt is pretty smart :O

Schranz0r

#11
Code (glbasic) Select

SYSTEMPOINTER TRUE
GLOBAL mx,my,b1,b2

TYPE tShot
x%;y% //position
speed#
delay = 500

FUNCTION NEW: speed = 1
self.speed = speed+RND(1)
self.x = mx
self.y = my
DIMPUSH _tShot[], self
ENDFUNCTION

FUNCTION UPDATE:
DEC self.y, self.speed
ENDFUNCTION
ENDTYPE

GLOBAL _tShot[] AS tShot
LOCAL timer%


WHILE TRUE

MOUSESTATE mx,my,b1,b2


IF b1 AND timer < GETTIMERALL()
LOCAL s AS tShot
s.NEW() // or s.NEW(2)
timer = GETTIMERALL()+s.delay
ENDIF


FOREACH s IN _tShot[]

s.UPDATE()

PRINT "!", s.x, s.y

IF s.y < -10 THEN DELETE s

NEXT

PRINT LEN(_tShot[]), 10,10

SHOWSCREEN
WEND
END
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Hotshot

thanks man :)

I was thinking of doing this

as Monk up

Code (glbasic) Select

Type Game
       
        Function Title_Screen:

        endfunction

       Function Game_Screen:
             
            Show_Background()
            Show_Spaceship()
            Shoot()

       endfunction

      Function Exit_Screen:
         Exit Window
     endfunction
Endtype

global _Game[] as Game

Game_Start=false

Repeat
           if Key(spacebar)
              g.Title_Screen
           elseif
              g.Game_Screen
           elseif Key(Escape)
              g.Exit_Screen
          endif

           showscreen
         
UNTIL KEY(01) = 1 // ESC


Do think the code above is good idea or have you better way of design it to make more cleaner?