News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

how can i get these invaders right ?

Started by RaverDave, February 26, 2011, 12:36:56 PM

Previous topic - Next topic

RaverDave

been messing around but they wont move far left before changing direction !
PlayBASIC Code: [Select]
; PROJECT : spaceinvaders
; AUTHOR : david wheeler
; CREATED : 27/01/2011
; EDITED : 23/02/2011
; ---------------------------------------------------------------------
OpenScreen 640,480,32,2
SetFPS 60
ScreenVsync On
Mouse Off
global quit=false
global invaderimage1=1
global invaderimage2=4
global shipimage=60
global numofinvaders=0
global shipsprite=60
global movem
global anim
global dropem
global tit
global changedir
Type tinvader
image
oldimage
Status
speed
Xpos,Ypos
animadd
count
EndType
type tship
image
xpos
ypos
speed
status

endtype

Dim invaders(100) As tinvader
dim ship(1) as tship
creategfx()
setupwave()
s$="Number of Invaders = "
s$=s$+str$(numofinvaders)
ship(1).speed=2
repeat
cls 0
print s$
print shipimage
tick=tick+1
if tick>40
tick=0
tit=1
movem=1
endif
for n=1 to numofinvaders
if movem=1

invaders(n).xpos=invaders(n).xpos+invaders(n).speed
tit=1


endif
if tit=1 and invaders(n).count=0
inc invaders(n).count
invaders(n).image=invaders(n).animadd
spriteimage n,invaders(inv).image

endif
if tit=1 and invaders(n).count>30
invaders(n).count=0
tit=0
invaders(n).image=invaders(n).image-invaders(n).animadd
spriteimage n,invaders(inv).image
endif
if invaders(n).xpos>608 or invaders(n).xpos<0 and drop=0
changedir=1
endif
if movem=0
invaders(n).image=1
endif
;spriteimage n,invaders(n).image
positionsprite n,invaders(n).xpos,invaders(n).ypos
;spriteimage n,invaders(inv).image

drawsprite n

next


if changedir = 1 and drop=0
changedir=0
drop=1
for n=1 to numofinvaders

invaders(n).speed = -invaders(n).speed

next
;movem=1
endif
if drop = 1 and changedir=0

for n = 1 to numofinvaders
if invaders(n).speed >0
invaders(n).xpos=invaders(n).xpos-32
endif

invaders(n).ypos = invaders(n).ypos+16

;print "poop"
next

endif
movem=0
drop=0
changedir=0
positionsprite shipsprite,ship(1).xpos,ship(1).ypos
drawsprite shipsprite

check_keys()
sync
until quit=true

function check_keys()
v=ScanCode()
print v
if v=203
ship(1).xpos=ship(1).xpos-ship(1).speed
endif
if v=205
ship(1).xpos=ship(1).xpos+ship(1).speed
endif
If v=16
quit=true
EndIf
endfunction

Function creategfx()

restore gfx
Invaderimage1=MakeImageFromData(13,10,32,20)
Invaderimage2=MakeImageFromData(13,10,32,20)
;********************** Create Players Ship Sprite *************

ShipImage=MakeImageFromData(13,10,32,20)

// This resource really should created dynamically
createsprite shipsprite
spriteimage shipsprite,shipimage
Login required to view complete source code



stevmjon

hey david

i fixed the problem for you.  it was easy, just a mistake easily made.  you had it set so if baddies speed is +x ,then head in -x direction after drop. i marked my change in the code for you.

hope this helps , stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

Big C.

#2
Its not so diffcult as you thought...

Here is another solution for you...

Before you change the new xpos of your aliens you should check whether the closest left or right alien reaches your borderlimits. If so then drop the complete alienwave and change the direction.

I use your code and marked the passage I have supplemented/edit. So keep in mind that your code isn't optimized and the way to coding this part of game routine could be made in a different way. But for the moment I hope it will helps you and I will be curious how your way will go on.

PlayBASIC Code: [Select]
; PROJECT : spaceinvaders
; AUTHOR : david wheeler edit by Big C.
; CREATED : 26.02.2011
; EDITED : 26.02.2011
; ---------------------------------------------------------------------
OpenScreen 640,480,32,2
SetFPS 60
ScreenVsync On
Mouse Off
Global quit=False
Global invaderimage1=1
Global invaderimage2=4
Global shipimage=60
Global numofinvaders=0
Global shipsprite=60
Global movem
Global anim
Global dropem
Global tit
Global changedir = 1 ; *** edit here and give a start direction; could be 1 or -1
Type tinvader
image
oldimage
Status
speed
Xpos,Ypos
animadd
count
EndType
Type tship
image
xpos
ypos
speed
status

EndType

Dim invaders(100) As tinvader
Dim ship(1) As tship
creategfx()
setupwave()
s$="Number of Invaders = "
s$=s$+Str$(numofinvaders)
ship(1).speed=2
Repeat
Cls 0
Print s$
Print shipimage
tick=tick+1
If tick>40
tick=0
tit=1
movem=1
EndIf
;*** this is to check wether to change dir and drop or not
For n=1 To numofinvaders
If invaders(n).xpos+invaders(n).speed > 608
drop = 1
changedir = -1
ExitFor
ElseIf invaders(n).xpos+invaders(n).speed < 0
drop = 1
changedir = 1
ExitFor
Else
drop = 0
EndIf
Next
;*** end edit part
For n=1 To numofinvaders
If movem=1

invaders(n).xpos=invaders(n).xpos+(invaders(n).speed*changedir) ; <-- here: calculate xpos with the changdir

;*** if drop = 1 the we calculate the new ypos
If drop = 1
invaders(n).ypos = invaders(n).ypos+16
EndIf
;*** end edit part
tit=1


EndIf
If tit=1 And invaders(n).count=0
Inc invaders(n).count
invaders(n).image=invaders(n).animadd
SpriteImage n,invaders(inv).image

EndIf
If tit=1 And invaders(n).count>30
invaders(n).count=0
tit=0
invaders(n).image=invaders(n).image-invaders(n).animadd
SpriteImage n,invaders(inv).image
EndIf
Remstart *** we dont need this
If invaders(n).xpos>608 Or invaders(n).xpos<0 And drop=0
changedir=1
EndIf
Remend

If movem=0
invaders(n).image=1
EndIf
;spriteimage n,invaders(n).image


PositionSprite n,invaders(n).xpos,invaders(n).ypos
;spriteimage n,invaders(inv).image

DrawSprite n

Next

RemStart *** we dont need this
If changedir = 1 And drop=0
changedir=0
drop=1
For n=1 To numofinvaders

invaders(n).speed = -invaders(n).speed

Next
;movem=1
EndIf


If drop = 1 And changedir=0

For n = 1 To numofinvaders

If invaders(n).speed >0
invaders(n).xpos=invaders(n).xpos-32
EndIf

invaders(n).ypos = invaders(n).ypos+16

;print "poop"
Next

EndIf
RemEnd


movem=0
drop=0
;changedir=0
PositionSprite shipsprite,ship(1).xpos,ship(1).ypos
DrawSprite shipsprite

Login required to view complete source code



RaverDave

Ah! thanks guys, I never knew about the exitfor command! Silly me, part ignorance and part hatred of reading online docs! And why I didnt think of using 3 values for changedir god knows! But thanks again, hopefully I can plod on with the other stuff now and dump all the code into the Resources forum on here! Yeh I will try and clean the code up also ...

RaverDave

#4
Already made improvements.. hopefully this is better :

(login required)

I've moved stuff in their own function and used the DrawAllSprites command instead of drawing em all separately .

Big C.

Some hint from me  ;)

Quote
Invaderimage1=MakeImageFromData(13,10,32,20)
Invaderimage2=MakeImageFromData(13,10,32,20)

At this point you create your two invaderimage and the function give the imagenumber in use back to the vars invaderimage1 and invaderimage2. How often do you use the vars invaderimage1 and invaderimage2 in your code (hint: use crtl+f and search  ;) )? What will happened if you use image creation commands before you create the image for your invader aliens (take a look to your function setupwave to the point where you will assign the alien sprites to the the alien type vars ;))?