Author Topic: Shooting?  (Read 5248 times)

Offline Hotshot

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Shooting?
« on: 2012-Aug-16 »
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:)


Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3375
  • Integrated Brain
    • View Profile
Re: Shooting?
« Reply #1 on: 2012-Aug-16 »
Hi take a look to this thread:

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

Works fine, sure  =D =D :good:

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4176
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Shooting?
« Reply #2 on: 2012-Aug-16 »
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.

Offline Hotshot

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Re: Shooting?
« Reply #3 on: 2012-Aug-16 »
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


Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2792
    • View Profile
Re: Shooting?
« Reply #4 on: 2012-Aug-16 »
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)

Offline Hotshot

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Re: Shooting?
« Reply #5 on: 2012-Aug-17 »
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

Offline okee

  • Dr. Type
  • ****
  • Posts: 324
    • View Profile
Re: Shooting?
« Reply #6 on: 2012-Aug-17 »
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


« Last Edit: 2012-Aug-17 by okee »
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

Offline Hotshot

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Re: Shooting?
« Reply #7 on: 2012-Aug-17 »
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?

« Last Edit: 2012-Aug-17 by Hotshot »

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1622
  • Particle Hawk
    • View Profile
Re: Shooting?
« Reply #8 on: 2012-Aug-17 »
Why not use a timed itteration?
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Hotshot

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Re: Shooting?
« Reply #9 on: 2012-Aug-17 »
Quote
Why not use a timed itteration?

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

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1622
  • Particle Hawk
    • View Profile
Re: Shooting?
« Reply #10 on: 2012-Aug-17 »
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


Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5116
  • O Rly?
    • View Profile
Re: Shooting?
« Reply #11 on: 2012-Aug-17 »
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
« Last Edit: 2012-Aug-17 by Schranz0r »
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

Offline Hotshot

  • Mc. Print
  • *
  • Posts: 22
    • View Profile
Re: Shooting?
« Reply #12 on: 2012-Aug-18 »
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?