Page 2 of 2

Re: Moving the background

Posted: Tue Aug 09, 2011 2:20 pm
by Wingman
cvirus wrote:...I want the enemies to fire so i must do somethig like this:

Code: Select all

 enemiebullet.BULLETS = New(BULLETS)
    enemiebullet\obj = CloneObject(bullet)
    CloneObjectPosition enemiebullet\obj,newenemie\obj
    CloneObjectOrientation enemiebullet\obj,newenemie\obj
    MoveObject enemiebullet\obj,24
but i´m confused in what place should i put it.

by the way, is it possible to put a variable printed in the screen with the number of enemies?

Thank you once again
You should put them around something like this (in the case you use types for enemies):

Code: Select all

repeat
    for newenemie.ENEMIES=each ENEMIES
        <moving and all that>
        if <newenemie is ready to shoot>
            enemiebullet.BULLETS=new(BULLETS)    // I think you have another type for bullets, as that'll be easier
            enemiebullet\obj = CloneObject(bullet)
            CloneObjectPosition enemiebullet\obj,newenemie\obj
            CloneObjectOrientation enemiebullet\obj,newenemie\obj
            MoveObject enemiebullet\obj,24
       endif
    next newenemie.ENEMIES
    <continuing your code>
forever

Re: Moving the background

Posted: Tue Aug 09, 2011 2:35 pm
by cvirus
So I must create a new type like so?

Code: Select all

type ENEMBULLET
                Field obj
EndType

Re: Moving the background

Posted: Tue Aug 09, 2011 3:02 pm
by cvirus
I was able to make the enemies to fire but impossible to win such a game...lol

And the game starts to be slow

Any ideas from you masters? Should i put the enemies moving forward and shout them, i´m lost can you guys help me on the right direction?

Thank you

Code: Select all

'game space shooting

SCREEN 640, 480, cbsizable

    FrameLimit 60
    
Type ENEMIES
    Field obj
    Field health As Integer
End Type

Type BULLETS
    Field obj
End Type

Type ENEMBULLETS
    Field obj
End Type

   
Gosub load
    
Repeat
SetWindow "Ship Battle Game 1"
    For newenemie.ENEMIES = Each ENEMIES
      PointObject newenemie\obj, ship
      MoveObject newenemie\obj, 0.5
      decreasehealth = False
      
      
     ' code for enemies to fire
If decreasehealth = False Then
   enemiebullets.ENEMBULLETS =New(ENEMBULLETS)
   enemiebullets\obj = CloneObject(bullet)
            CloneObjectPosition enemiebullets\obj,newenemie\obj
            CloneObjectOrientation enemiebullets\obj,newenemie\obj
            MoveObject enemiebullets\obj,2
EndIf
For enbullet.ENEMBULLETS = Each ENEMBULLETS
        MoveObject enbullet\obj,6
 Next enbullet.ENEMBULLETS
  ' end code for enemie to fire
  
  
  
      For iBullet.BULLETS = Each BULLETS
         If ObjectsOverlap (newenemie\obj, ibullet\obj) Then
            decreasehealth = True
            DeleteObject ibullet\obj
            Delete ibullet
         EndIf
      Next ibullet
      If decreasehealth Then newenemie\health -1
      If newenemie\health < 1 Then 
      PlaySound boom,100
         DeleteObject newenemie\obj
         Delete newenemie
      EndIf
     Next newenemie.ENEMIES 
       
    CloneCameraPosition ship
        TranslateObject floor, -1.4, 0 'move the floor
                If LeftKey() Then TurnObject ship, 5
                If RightKey() Then TurnObject ship,-5
                If UpKey() Then speed# = CurveValue(4.6, speed#, 13) 'if up arrow is pressed, speed# will be increased smoothly
                If DownKey() Then speed# = CurveValue(-3.5, speed#, 13) '...and if down arrow is pressed, it will be decreased smoothly
        speed# = CurveValue (0, speed#, 60) 'this will make sure the ship will eventually stop smoothly if no key is pressed
    MoveObject ship, speed# '...and finally, the ship is actually being moved
   
     If KeyDown(cbkeyspace) And reload =0
        PlaySound shoot,20
        newbullet.BULLETS = New(BULLETS)
        newbullet\obj = CloneObject(bullet)
        CloneObjectPosition newbullet\obj,ship
        CloneObjectOrientation newbullet\obj,ship
        MoveObject newbullet\obj,24
        reload=7
     EndIf
     
SetWindow "Ship Battle Game 3"

   If Rand (1, 100) = 1 Then
      newenemie.ENEMIES = New(ENEMIES)
      newenemie\obj = CloneObject (enem)
      newenemie\health = 1
      PositionObject newenemie\obj, Rand(-220,220),Rand(-300,300)
      SetupCollision ship,newenemie\obj,2,2,1
   EndIf
     SetWindow "Ship Battle Game 4"
b=0
    For ibullet.BULLETS = Each BULLETS
     MoveObject ibullet\obj,6 
    If Distance2 (ibullet\obj,ship)>400 Then
        DeleteObject ibullet\obj
        Delete ibullet
    EndIf
b=b+1
Next ibullet

If reload >0 Then reload=reload-1

Text 30,30,"Bullets: "+b
Text 30,42,"Coordinates: X"+Int(  (ship))+", Y"+Int(ObjectY (ship))
DrawScreen 

Until EscapeKey()

load:
    shoot = LoadSound ("Blaster.wav")
    boom = LoadSound ("Boom.wav")
    
    enem = LoadObject("enemy2.png")
        ShowObject enem, 0

    bullet= LoadObject("bullet.png",72)
        ShowObject bullet, 0
        
    explosion = LoadObject("explosion.png", 72)
    ShowObject explosion, 0
    
    ship = LoadObject("nave.png", 72)
        PositionObject ship, 0, 0
       
    floor=MakeObjectFloor()
          fundo=LoadImage("fundo.bmp")
    PaintObject floor,fundo
        ClsColor cbBlack
        Color cbRed
        
For i = 1 To 15
    newenemie.ENEMIES = New(ENEMIES)
    newenemie\obj = CloneObject (enem)
    newenemie\health = 1
    PositionObject newenemie\obj, Rand(-500,500),Rand(-450,450)
    SetupCollision ship, newenemie\obj,2,2,1
Next i

    SetWindow "Ship Battle Game"     
x=0:y=0
Return

Re: Moving the background

Posted: Wed Aug 10, 2011 4:23 pm
by cvirus
Any idea guys?

Anything will be appreciated.
8-)
thank you

Re: Moving the background

Posted: Thu Aug 11, 2011 6:35 pm
by cvirus
Update a little the game, still not working the shooting part correctly so i decided to disable for now, i changed the enemies sprites and add some text with number of lives but now i don´t know were to start decreasing the lives when the enemies ships collide with the main one, can anybody :P help a poor soul here.

Re: Moving the background

Posted: Thu Aug 11, 2011 11:04 pm
by Jonez
Use Distance2()-function to get the distance between the player and the enemies. If too far, the enemies won't point to the player nor shoot, but wander aimlessly (or by waypoints if you prefer). Make an additional field for the enemies, called reload. Reload is reduced by one in each frame, and when it gets zero, the enemy is allowed to shoot again.

You can use ObjectOverlap() to check when an enemy collides with the player. Use this in the ENEMIES-loop.

Re: Moving the background

Posted: Fri Aug 12, 2011 11:27 pm
by cvirus
Jonez wrote:Use Distance2()-function to get the distance between the player and the enemies. If too far, the enemies won't point to the player nor shoot, but wander aimlessly (or by waypoints if you prefer). Make an additional field for the enemies, called reload. Reload is reduced by one in each frame, and when it gets zero, the enemy is allowed to shoot again.

You can use ObjectOverlap() to check when an enemy collides with the player. Use this in the ENEMIES-loop.

Thank you Jonez, i will try that out as soon i´ll be back from 3 days weekend.

Re: Moving the background

Posted: Sat Aug 13, 2011 4:08 pm
by Wingman
Oh, slowing down is a result for not deleting the bullets. When they get out of the play area/screen, delete them

Re: Moving the background

Posted: Tue Aug 16, 2011 10:10 pm
by cvirus
Well i have changed some little code and here is the result, the enemies are scrolling to the left now.

Code: Select all

'game space shooting

SCREEN 640, 480, cbsizable

    FrameLimit 60
    
Type ENEMIES
    Field obj
    Field health As Integer
End Type

Type BULLETS
    Field obj
    
End Type

Type ENEMBULLETS
    Field obj
End Type


   
Gosub load
    
Repeat
SetWindow "Ship Battle Game 1"
    For newenemie.ENEMIES = Each ENEMIES
     ' PointObject newenemie\obj, ship
      MoveObject newenemie\obj, -0.5
      decreasehealth = False

      For iBullet.BULLETS = Each BULLETS
         If ObjectsOverlap (newenemie\obj, ibullet\obj) Then
            decreasehealth = True
            DeleteObject ibullet\obj
            Delete ibullet
         EndIf
      Next ibullet
      If decreasehealth Then newenemie\health -1
      If newenemie\health < 1 Then 
      PlaySound boom,100
         DeleteObject newenemie\obj
         Delete newenemie
      EndIf
     Next newenemie.ENEMIES
     
       
    CloneCameraPosition ship
        TranslateObject floor, -1.4, 0 'move the floor
                If LeftKey() Then TurnObject ship, 5
                If RightKey() Then TurnObject ship,-5
                If UpKey() Then speed# = CurveValue(4.6, speed#, 13) 'if up arrow is pressed, speed# will be increased smoothly
                If DownKey() Then speed# = CurveValue(-3.5, speed#, 13) '...and if down arrow is pressed, it will be decreased smoothly
        speed# = CurveValue (0, speed#, 60) 'this will make sure the ship will eventually stop smoothly if no key is pressed
    MoveObject ship, speed# '...and finally, the ship is actually being moved
   
     If KeyDown(cbkeyspace) And reload =0
        PlaySound shoot,20
        newbullet.BULLETS = New(BULLETS)
        newbullet\obj = CloneObject(bullet)
        CloneObjectPosition newbullet\obj,ship
        CloneObjectOrientation newbullet\obj,ship
        MoveObject newbullet\obj,24
        reload=7  
     EndIf
     
SetWindow "Ship Battle Game 3"

   If Rand (1, 100) = 1 Then
      newenemie.ENEMIES = New(ENEMIES)
      newenemie\obj = CloneObject (enem)
      newenemie\health = 1
      PositionObject newenemie\obj, Rand(-220,220),Rand(-300,300)
      SetupCollision ship,newenemie\obj,2,2,1
   EndIf
     SetWindow "Ship Battle Game 4"
b=0
    For ibullet.BULLETS = Each BULLETS
     MoveObject ibullet\obj,6 
    If Distance2 (ibullet\obj,ship)>400 Then
        DeleteObject ibullet\obj
        Delete ibullet
    EndIf
b=b+1
Next ibullet

If reload >0 Then reload=reload-1

Text 30,30,"Bullets: "+b
Text 0, 0, "Lives: "+lives
Text 30,42,"Coordinates: X"+Int(  (ship))+", Y"+Int(ObjectY (ship))
DrawScreen 

Until EscapeKey() Or lives = 0

load:
    shoot = LoadSound ("Blaster.wav")
    boom = LoadSound ("Boom.wav")
    
    enem = LoadObject("enemy2.png")
        ShowObject enem, 0

    bullet= LoadObject("bullet.png",72)
        ShowObject bullet, 0
        
    explosion = LoadObject("explosion.png", 72)
    ShowObject explosion, 0
    
    ship = LoadObject("nave.png", 72)
        PositionObject ship, 0, 0
       
    floor=MakeObjectFloor()
          fundo=LoadImage("fundo.bmp")
    PaintObject floor,fundo
        ClsColor cbBlack
        Color cbRed
        
For i = 1 To 15
    newenemie.ENEMIES = New(ENEMIES)
    newenemie\obj = CloneObject (enem)
    newenemie\health = 1
    PositionObject newenemie\obj, Rand(-500,500),Rand(-450,450)
    SetupCollision ship, newenemie\obj,2,2,1
Next i

    SetWindow "Ship Battle Game"     
x=0:y=0
lives =3
Return
The main problem is that the game is slowing down, i must put some function in the game so that way is lot easy to change the code, can anyone give some hint´s how to divide the game in functions?

Thanks :P

Re: Moving the background

Posted: Tue Aug 16, 2011 10:20 pm
by valscion
cvirus wrote:Well i have changed some little code and here is the result, the enemies are scrolling to the left now.

Code: Select all

---cut---
The main problem is that the game is slowing down, i must put some function in the game so that way is lot easy to change the code, can anyone give some hint´s how to divide the game in functions?

Thanks :P
The slowing down happens because you spawn new enemies and position them outside the screen. This is the problematic part:

Code: Select all

PositionObject newenemie\obj, Rand(-220,220),Rand(-300,300)
And why this is a problem? Well, you move your ship-object and clone the CAMERAs position to it. So because you move the camera, those coordinates between x: -220...220 and y: -300...300 are left behind you. To find where in the world camera is located, use functions CameraX() and CameraY(). They return the world-coordinates (those that objects use).

In my opinion your code looks fine that way and there's not an actual need for functions yet.

EDIT: You can also use the ScreenPositionObject-command to position the newly created enemies according to screen coordinates. I'd use those CameraX() and CameraY() -functions, though.

Re: Moving the background

Posted: Tue Aug 16, 2011 10:32 pm
by cvirus
Once again back on the fire part, once again nothing works lol, here is my code for the enemies fire, don´t know what else should i do maybe the function parts would be nice to work on.

Code: Select all

    For newenemie.ENEMIES = Each ENEMIES
    ' PointObject newenemie\obj, ship
      MoveObject newenemie\obj, -0.5
      decreasehealth = False
If decreasehealth = False Then
    enemiebullet.ENEMBULLETS=new(ENEMBULLETS)   
            enemiebullet\obj = CloneObject(bullet)
            CloneObjectPosition enemiebullet\obj,newenemie\obj
            CloneObjectOrientation enemiebullet\obj,newenemie\obj
            
            MoveObject enemiebullet\obj,24
            
       EndIf
For enbullet.ENEMBULLETS = Each ENEMBULLETS
        MoveObject enbullet\obj,6
        PointObject enbullet\obj,ship
Next enbullet.ENEMBULLETS

Re: Moving the background

Posted: Tue Aug 16, 2011 10:38 pm
by cvirus
VesQ wrote:
EDIT: You can also use the ScreenPositionObject-command to position the newly created enemies according to screen coordinates. I'd use those CameraX() and CameraY() -functions, though.
Use this functions instead of

Code: Select all

 PositionObject newenemie\obj, Rand(-500,500),Rand(-450,450)
and this?

Code: Select all

 PositionObject newenemie\obj, Rand(-220,220),Rand(-300,300)

Any code example for that?

Thank you

Re: Moving the background

Posted: Wed Aug 17, 2011 9:51 am
by valscion
cvirus wrote:
VesQ wrote:
EDIT: You can also use the ScreenPositionObject-command to position the newly created enemies according to screen coordinates. I'd use those CameraX() and CameraY() -functions, though.
Use this functions instead of

Code: Select all

 PositionObject newenemie\obj, Rand(-500,500),Rand(-450,450)
and this?

Code: Select all

 PositionObject newenemie\obj, Rand(-220,220),Rand(-300,300)

Any code example for that?

Thank you
You need to add (or decrease, I'm not sure) the values of CameraX() and Y() from those coordinates. So like this:

Code: Select all

PositionObject newenemie\obj, Rand(-220,220) + CameraX(), Rand(-300,300) + CameraY()

Re: Moving the background

Posted: Wed Aug 17, 2011 3:27 pm
by cvirus
Better and faster now, the other problem now is the enemies shooting part.

my code for enemies shooting:

Code: Select all

[code]    For newenemie.ENEMIES = Each ENEMIES
    ' PointObject newenemie\obj, ship
      MoveObject newenemie\obj, -0.5
      decreasehealth = False
If decreasehealth = False Then
    enemiebullet.ENEMBULLETS=new(ENEMBULLETS)   
            enemiebullet\obj = CloneObject(bullet)
            CloneObjectPosition enemiebullet\obj,newenemie\obj
            CloneObjectOrientation enemiebullet\obj,newenemie\obj
            
            MoveObject enemiebullet\obj,24
            
       EndIf
       
For enbullet.ENEMBULLETS = Each ENEMBULLETS
        MoveObject enbullet\obj,6
        PointObject enbullet\obj,ship
      If Distance2(newenemie\obj,ship)< 50
      DeleteObject enbullet\obj
      Delete enbullet
      EndIf
Next enbullet.ENEMBULLETS
[/code]

the enemies can only shoot if neer the ship and not from any position but my code doesn´t work.

Re: Moving the background

Posted: Wed Aug 17, 2011 4:41 pm
by Jonez
I'm not sure what that code is supposed to do... But here's how I would do it:

Code: Select all

For ienemy.ENEMIES = Each ENEMIES
    If Distance2( ienemy\obj, playerShip ) < 500 //enemy is close enough
        PointObject ienemy\obj, playerShip
        
        nbullet.BULLETS = New( BULLETS )
        nbullet\obj = CloneObject( objMasterBullet )
        CloneObjectPosition nbullet\obj, ienemy\obj
        CloneObjectOrientation nbullet\obj, ienemy\obj
        
    Else
        MoveEnemyRandomly()
    EndIf
Next ienemy

For ibullet.BULLETS = Each BULLETS
    MoveObject ibullet\obj, 5
    
    If ObjectsOverlap( ibullet\obj, playerShip, 2 ) Then
        DeleteObject ibullet\obj
        Delete ibullet
        
        playerHealth - 5
    EndIf
Next ibullet
Oh yeah and of course the shooting would have a reload-function. Forgot that.

Re: Moving the background

Posted: Wed Aug 17, 2011 8:29 pm
by cvirus
I´m desperate because i have tried almost everything ans for some reason everything goes wrong, i´m confused a lot with this, where is my code, if any of you guys can point where in the code i´m doing it bad?

Code: Select all

SCREEN 640, 480, cbsizable

    FrameLimit 60
    
Type ENEMIES
    Field obj
    Field health As Integer
End Type

Type BULLETS
    Field obj
    
End Type

Type ENEMBULLETS
    Field obj
End Type


   
Gosub load
    
Repeat
SetWindow "Ship Battle Game 1"
    For newenemie.ENEMIES = Each ENEMIES
     PointObject newenemie\obj, ship
      MoveObject newenemie\obj, -0.5
      decreasehealth = False
If decreasehealth = False Then
    enemiebullet.ENEMBULLETS=new(ENEMBULLETS)   
            enemiebullet\obj = CloneObject(bullet)
            CloneObjectPosition enemiebullet\obj,newenemie\obj
            CloneObjectOrientation enemiebullet\obj,newenemie\obj
            
            MoveObject enemiebullet\obj,24
            
       EndIf
       
For enbullet.ENEMBULLETS = Each ENEMBULLETS
If Distance2(newenemie\obj,ship)< 500
      MoveObject enbullet\obj,6
      EndIf
Next enbullet.ENEMBULLETS

      For iBullet.BULLETS = Each BULLETS
         If ObjectsOverlap (newenemie\obj, ibullet\obj) Then
            decreasehealth = True
            DeleteObject ibullet\obj
            Delete ibullet
         EndIf
      Next ibullet
      If decreasehealth Then newenemie\health -1
      If newenemie\health < 1 Then 
      PlaySound boom,100
         DeleteObject newenemie\obj
         Delete newenemie
      EndIf
     Next newenemie.ENEMIES
     
       
    CloneCameraPosition ship
        TranslateObject floor, -1.4, 0 'move the floor
                If LeftKey() Then TurnObject ship, 5
                If RightKey() Then TurnObject ship,-5
                If UpKey() Then speed# = CurveValue(4.6, speed#, 13) 'if up arrow is pressed, speed# will be increased smoothly
                If DownKey() Then speed# = CurveValue(-3.5, speed#, 13) '...and if down arrow is pressed, it will be decreased smoothly
        speed# = CurveValue (0, speed#, 60) 'this will make sure the ship will eventually stop smoothly if no key is pressed
    MoveObject ship, speed# '...and finally, the ship is actually being moved
   
     If KeyDown(cbkeyspace) And reload =0
        PlaySound shoot,20
        newbullet.BULLETS = New(BULLETS)
        newbullet\obj = CloneObject(bullet)
        CloneObjectPosition newbullet\obj,ship
        CloneObjectOrientation newbullet\obj,ship
        MoveObject newbullet\obj,24
        reload=7  
     EndIf
     
SetWindow "Ship Battle Game 3"

   If Rand (1, 100) = 1 Then
      newenemie.ENEMIES = New(ENEMIES)
      newenemie\obj = CloneObject (enem)
      newenemie\health = 1
      PositionObject newenemie\obj, Rand(-220,220)+ CameraX(),Rand(-300,300)+ CameraY()
      SetupCollision ship,newenemie\obj,2,2,1
   EndIf
     SetWindow "Ship Battle Game 4"
b=0
    For ibullet.BULLETS = Each BULLETS
     MoveObject ibullet\obj,6 
    If Distance2 (ibullet\obj,ship)>400 Then
        DeleteObject ibullet\obj
        Delete ibullet
    EndIf
b=b+1
Next ibullet

If reload >0 Then reload=reload-1

Text 30,30,"Bullets: "+b
Text 0, 0, "Lives: "+lives
Text 30,42,"Coordinates: X"+Int(  (ship))+", Y"+Int(ObjectY (ship))
DrawScreen 

Until EscapeKey() Or lives = 0

load:
    shoot = LoadSound ("Blaster.wav")
    boom = LoadSound ("Boom.wav")
    
    enem = LoadObject("enemy2.png")
        ShowObject enem, 0

    bullet= LoadObject("bullet.png",72)
        ShowObject bullet, 0
        
    explosion = LoadObject("explosion.png", 72)
    ShowObject explosion, 0
    
    ship = LoadObject("nave.png", 72)
        PositionObject ship, 0, 0
       
    floor=MakeObjectFloor()
          fundo=LoadImage("fundo.bmp")
    PaintObject floor,fundo
        ClsColor cbBlack
        Color cbRed
        
For i = 1 To 15
    newenemie.ENEMIES = New(ENEMIES)
    newenemie\obj = CloneObject (enem)
    newenemie\health = 1
    PositionObject newenemie\obj, Rand(-500,500)+ CameraX(),Rand(-450,450)+CameraY()
    SetupCollision ship, newenemie\obj,2,2,1
Next i

    SetWindow "Ship Battle Game"     
x=0:y=0
lives =3
Return
:?:

Re: Moving the background

Posted: Sun Aug 21, 2011 6:16 pm
by cvirus
Any idea guys?

Thanks :D

Re: Moving the background

Posted: Sun Aug 21, 2011 8:02 pm
by Jonez
Well there really isn't a proper question in your post. I gave you a perfectly valid example on how to make a working model for shooting enemies. It seems you haven't even tried to use it in your code (I could be wrong). If my example isn't what you are looking for for your game, please tell us, and in more detail of what you really want.

We don't have the media for your game so we could try it (and no, we won't make our own). Moreover, your code isn't as clean as it could be. The indentation could be more systematic. This won't motivate us to search mistakes.

The point is: don't ask if we could tell everything that's wrong (and then possibly fix it for you). State your problem and ask a proper question about one topic only. There is no team of helpers behind the screen. We do this in our own time. If we're arsed to.

So, as far as I can see, you problem still lies in the enemy shooting (for one). And to that question I already gave an answer.

If your code is a complete mess, or it just could be better, there is no shame in starting all over. I mean, just look at the example Zero has given us with Coolbasic.

Re: Moving the background

Posted: Sun Aug 21, 2011 10:19 pm
by cvirus
Thank you for the reply Jonez, you are absolutely right, I will start all over with the examples you guys gave me here and thru the CoolBasic examples too, and then if the mistake continues i will open a new post with the proper questions.

Thanks ans sorry for the abuse.