News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

no media space invaders

Started by RaverDave, January 30, 2011, 07:51:59 PM

Previous topic - Next topic

RaverDave

Just a little something I am working on now n then, seems I have to relearn all this as I go along :/
Just a damned shame you cant code in the sounds also! can you?


PlayBASIC Code: [Select]
; PROJECT : spaceinvaders
; AUTHOR : david wheeler
; CREATED : 27/01/2011
; EDITED : 31/01/2011
; ---------------------------------------------------------------------
OpenScreen 640,480,32,2
SetFPS 60
ScreenVsync On
Mouse Off
global quit=false
global invader=1
global shipimage=60
global numofinvaders=0
global shipsprite=60
global dropem
global changedir
Type tinvader
image
Status
speed
Xpos,Ypos

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$
tick=tick+1
if tick>40
tick=0
movem=1
endif
for n=1 to numofinvaders
if movem=1

invaders(n).xpos=invaders(n).xpos+invaders(n).speed
endif
if invaders(n).xpos>600 or invaders(n).xpos<0
changedir=1
endif
positionsprite n,invaders(n).xpos,invaders(n).ypos
drawsprite n
;drawimage 1,invaders(n).xpos,invaders(n).ypos,1
next
movem=0
if changedir=1
changedir=0
for n2=1 to numofinvaders
invaders(n2).speed=-invaders(n2).speed
next
endif
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()
CreateImage invader,13,10
RenderToImage invader
restore gfx
for y=0 to 9
for x=0 to 12
p= readdata()
Ink rgb(0,255,0)
if p>0
dot x,y
endif
next x
next y
RenderToScreen
ScaleImage invader,32,20,1
;********************** Create Players Ship Sprite *************
createimage shipimage,13,10
rendertoimage shipimage
for y=0 to 9
for x=0 to 12
p= readdata()
Ink rgb(0,255,0)
if p>0
dot x,y
endif
next x
next y
rendertoscreen
scaleimage shipimage,32,20,1
createsprite shipsprite
spriteimage shipsprite,shipimage
ship(1).image=shipimage
ship(1).xpos=320
ship(1).ypos=400
cls 0
EndFunction


Function setupwave()
offsety=30
inv=1
for y=1 to 10
for x=1 to 13
t=readdata()
if t>0
invaders(inv).xpos=offsetx
invaders(inv).ypos=offsety
invaders(inv).image=t
invaders(inv).speed=4
createsprite inv
spriteimage inv,invaders(inv).image
inc inv
inc numofinvaders
endif
offsetx=offsetx+38
if x>11
offsety=offsety+15
offsetx=0
endif
next
next
endfunction
gfx1:
data 0,0,0,0,0,0,0,0,0,0,0,0,0
Login required to view complete source code


to do list:
more graphics!
players bullet
invaders bullets
ufo
barriers
score system
another loop for Title Screen
some game logic

BlinkOk

that looks great! all code too. nice work

kevin

#2
QuoteJust a damned shame you cant code in the sounds also! can you?

 Create the 'sample data' in a bank,  save the raw data + header to disc as a wav file and load it.    That's how this  works

 It looks good.  But skimming over the program there's a number of bits that can be improved, by breaking up into functions.  

eg,

PlayBASIC Code: [Select]
Function creategfx()

restore gfx
Invader=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

ship(1).image=shipimage
ship(1).xpos=320
ship(1).ypos=400
EndFunction




Function MakeImageFromData(DataWidth,DataHeight,PixelWidth,PixelHeight,ThisColour=$00ff00)
oldsurface=getsurface()
ThisImage=NewImage(DataWidth,DataHeight)
RenderToImage ThisIMage
Ink ThisColour
lockbuffer
for y=0 to DataHeight-1
for x=0 to DataWidth-1
if readdata()>0
dot x,y
endif
next x
next y
unlockbuffer
rendertoimage oldsurface
scaleimage ThisImage,PixelWidth,PixelHeight,1
EndFunction ThisImage








RaverDave

Yeh thanx kevin, I was thinking this myself as I was duplicating code for each time I created an image .