Page 1 of 1

Space Invaders (under construction)

Posted: Thu Aug 22, 2013 3:33 pm
by Logikmensch
Today I started a try to develop a small space invaders clone.
Move the ship by touching left or right bottom corner.
Fire up to 5 shots by touching above the ship.
Aliens are still missing. ;-)

Code: Select all

graphics
option base 1
stars=40
sx=screen_width()
sy=screen_height()
cx=sx/2
u=sx/20
dim stars_x(stars)
dim stars_y(stars)
dim step(stars)
dim shot_x(5)
dim shot_y(5)
gosub init_stars

loop:
  graphics lock
  graphics clear 0,0,0
  if sx<>screen_width() then
    sx=screen_width()
    sy=screen_height()
    gosub init_stars
  end if
  if cx<u then cx=u
  if cx>sx-u then cx=sx-u
  gosub stars
  gosub ship
' check left, right and shot touch
  ty=touch_y(0)
  if ty>=sy-5*u and ty<=sy then
    tx=touch_x(0)
    if tx<5*u then cx=cx-u/2
    if tx>sx-5*u then cx=cx+u/2
  else 
    if ty>0 then gosub add_shot
  end if
  gosub shot

  graphics unlock
goto loop

ship:
  draw color 1,1,1
  draw size 5
  draw line cx-u,sy to cx+u,sy
  draw line to cx,sy-2*u
  draw line to cx-u,sy
  return

stars:
  draw size 2
  for i=1 to stars
    draw color rnd(1),rnd(1),rnd(1)
    y=stars_y(i)
    draw pixel stars_x(i),y
    y=y+step(i)
    if y>sy then y=0
    stars_y(i)=y
  next i
  return
  
init_stars:
  for i=1 to stars
    step(i)=rnd(1)*2
    stars_x(i)=rnd(sx)
    stars_y(i)=rnd(sy)
  next i
  return

shot:
  for i=1 to 5
    x=shot_x(i)
    y=shot_y(i)
    if y>0 then 
      draw color rnd(1),rnd(1),rnd(1)
      draw line x,y+u to x,y
      y=y-u/3
      shot_y(i)=y
    end if
  next i
  return

add_shot:
' do not allow new shots if one lower shot is still visible
  found=0
  for i=1 to 5
    y=shot_y(i)
    if y<=0 then found=i
    if y>sy-5*u then return
  next i
  if found>0 then
    shot_x(found)=cx
    shot_y(found)=sy-2*u
  end if
  return

Re: Space Invaders (under construction)

Posted: Fri Aug 23, 2013 8:58 am
by Logikmensch
Now a further version which contains aliens that can be shot.
About user interface: press on the bottom area on the left or right side to position your ship; touch above the ship to shot. Try to kill all aliens!

still some things are missing...

Code: Select all

graphics
option base 1
randomize
pi=3.1415926

adjust_loop:
points=0
stars=40
shots=10
aliens=10
explosions=5
explosionpoints=20
sx=screen_width()
sy=screen_height()
cx=sx/2
u=(sx+sy)/40
dim stars_x(stars)
dim stars_y(stars)
dim step(stars)
dim shot_x(shots)
dim shot_y(shots)
dim shot_step(shots)
dim alien_x(aliens)
dim alien_y(aliens)
dim alien_step(aliens)
dim alien_angle(aliens)
dim alien_cnt(aliens)
dim explosion_x(explosions)
dim explosion_y(explosions)
dim explosion_size(explosions)
dim explosion_px(explosionpoints)
dim explosion_py(explosionpoints)

for i=1 to explosionpoints
  explosion_px(i)=rnd(1)-rnd(1)
  explosion_py(i)=rnd(1)-rnd(1)
next i

gosub init_stars
for lp=1 to 5
  gosub add_alien
next lp

loop:
  graphics lock
  graphics clear 0,0,0
  if cx<u then cx=u
  if cx>sx-u then cx=sx-u
  gosub stars
  gosub ship
  gosub alien
' check left, right and shot touch
  for t=0 to 1
    ty=touch_y(t)
    if ty>=sy-5*u and ty<=sy then
      tx=touch_x(t)
      if tx<5*u then 
        cx=cx-u/2
      else
        if tx>sx-5*u then cx=cx+u/2
      end if
    else 
      if ty>0 then 
        shotstep=-u/3
        shotx=cx
        shoty=sy-u*2
        gosub add_shot
      end if
    end if
  next t
  
  gosub shot
  gosub display
  gosub explosion
  graphics unlock
if sx=screen_width() then goto loop

goto adjust_loop

ship:
  draw color 1,1,1
  draw size 5
  draw line cx-u,sy to cx+u,sy
  draw line to cx,sy-2*u
  draw line to cx-u,sy
  return

stars:
  draw size 2
  for i=1 to stars
    draw color rnd(1),rnd(1),rnd(1)
    y=stars_y(i)
    draw pixel stars_x(i),y
    y=y+step(i)
    if y>sy then y=0
    stars_y(i)=y
  next i
  return
  
init_stars:
  for i=1 to stars
    step(i)=rnd(1)*2
    stars_x(i)=rnd(sx)
    stars_y(i)=rnd(sy)
  next i
  return

alien:
  for i=1 to aliens
    c=alien_cnt(i)
    if c>0 then
      x=alien_x(i)
      y=alien_y(i)
      draw color 1,rnd(0.5)+0.5,rnd(0.5)+0.5
      draw size 5
      draw line x-2*u,y to x,y-2*u
      draw line to x+2*u,y
      draw line to x,y+2*u
      draw line to x-2*u,y
      s=alien_step(i)
      h=(alien_angle(i)-90)/180*pi
      x=x+s*cos(h)
      y=y+s*sin(h)
      if x<0 then 
        x=sx
      else
        if x>sx then x=0
      end if
      alien_x(i)=x
      alien_y(i)=y
      c=c-1
      if c=0 then
        c=rnd(15*u)+1
        alien_angle(i)=rnd(360)
        alien_step(i)=rnd(1)*3
        shotx=x
        shoty=y
        shotstep=u/3
        gosub add_shot
      else
        if y<0 or y>sy/2 then
          a=alien_angle(i)-180
          if a<0 then a=a+360
          alien_angle(i)=a
        end if
      end if
      alien_cnt(i)=c
    end if
  next i
  return

add_alien:
  found=0
  for i=1 to aliens
    if alien_cnt(i)=0 and found=0 then found=i
  next i
  if found>0 then
    alien_x(found)=rnd(sx)
    alien_y(found)=0
    alien_angle(found)=rnd(90)+135
    alien_step(found)=rnd(1)*3
    alien_cnt(found)=rnd(u*15)+1
  end if
  return

add_explosion:
  found=0
  for ei=1 to explosions
    if explosion_size(ei)=0 then found=ei
  next ei
  if found>0 then
    explosion_x(found)=explosionx
    explosion_y(found)=explosiony
    explosion_size(found)=1
  end if
  return

explosion:
  for ei=1 to explosions
    s=explosion_size(ei)
    if s>0 then
      x=explosion_x(ei)
      y=explosion_y(ei)
      draw size 5
      for ex=1 to explosionpoints
        if rnd(1)<0.5 then 
          draw color 1,0,0
        else
          draw color 1,1,0
        end if
        draw pixel x+explosion_px(ex)*s,y+explosion_py(ex)*s
      next ex
      s=s+2
      if s>=u*4 then s=0
    end if
    explosion_size(ei)=s
  next ei
  return

shot:
  for i=1 to shots
    x=shot_x(i)
    y=shot_y(i)
    if y>0 and y<sy then 
      draw color rnd(1),rnd(1),rnd(1)
      draw line x,y+u to x,y
      stp=shot_step(i)
      y=y+stp
      if stp<0 then
        f=0
        for ai=1 to aliens
          if alien_cnt(ai)>0 and f=0 then
            ax=alien_x(ai)
            ay=alien_y(ai)
            if x>=ax-2*u and x<=ax+2*u and y>=ay-2*u and y<=ay+2*u then
              explosionx=x
              explosiony=y
              gosub add_explosion
              y=0
              alien_cnt(ai)=0
              f=ai
              points=points+1000
            end if
          end if
        next ai
      else
      end if
      shot_y(i)=y
    end if
  next i
  return

add_shot:
' do not allow new shots if one lower shot is still visible
  found=0
  for si=1 to shots
    y=shot_y(si)
    if y<=0 or y>sy then found=si
    if shot_step(si)<0 and y>sy-5*u then return
  next si
  if found>0 then
    shot_x(found)=shotx
    shot_y(found)=shoty
    shot_step(found)=shotstep
  end if
  return

display:
  fill color 1,1,0.5
  draw color 1,0,0
  fill rect 0,0 to sx,20
  draw text "SCORE: "& points at 10,0
  return

Re: Space Invaders (under construction)

Posted: Tue Sep 03, 2013 4:38 pm
by Logikmensch
I have made a version which contains the workaround for the DRAW PIXEL bug:

have fun shooting those aliens ;-)

Code: Select all

graphics
option base 1
randomize
pi=3.1415926

adjust_loop:
points=0
stars=20
shots=10
aliens=100
explosions=5
explosionpoints=20
sx=screen_width()
sy=screen_height()
cx=sx/2
u=(sx+sy)/40
dim stars_x(stars)
dim stars_y(stars)
dim step(stars)
dim shot_x(shots)
dim shot_y(shots)
dim shot_step(shots)
dim alien_x(aliens)
dim alien_y(aliens)
dim alien_step(aliens)
dim alien_angle(aliens)
dim alien_cnt(aliens)
dim explosion_x(explosions)
dim explosion_y(explosions)
dim explosion_size(explosions)
dim explosion_px(explosionpoints)
dim explosion_py(explosionpoints)

for i=1 to explosionpoints
  explosion_px(i)=rnd(1)-rnd(1)
  explosion_py(i)=rnd(1)-rnd(1)
next i

gosub init_stars
for lp=1 to 10
  gosub add_alien
next lp

loop:
  graphics lock
  graphics clear 0,0,0
  if cx<u then cx=u
  if cx>sx-u then cx=sx-u
  gosub stars
  gosub ship
  gosub alien
' check left, right and shot touch
  for t=0 to 1
    ty=touch_y(t)
    if ty>=sy-5*u and ty<=sy then
      tx=touch_x(t)
      if tx<5*u then 
        cx=cx-u/2
      else
        if tx>sx-5*u then cx=cx+u/2
      end if
    else 
      if ty>0 then 
        shotstep=-u/3
        shotx=cx
        shoty=sy-u*2
        gosub add_shot
      end if
    end if
  next t
  
  gosub shot
  gosub display
  gosub explosion
  graphics unlock
if sx=screen_width() then goto loop

goto adjust_loop

ship:
  draw color 1,1,1
  draw size 5
  draw line cx-u,sy to cx+u,sy
  draw line to cx,sy-2*u
  draw line to cx-u,sy
  return

stars:
  draw size 2
  for i=1 to stars
    draw color rnd(1),rnd(1),rnd(1)
    y=stars_y(i)
    draw pixel int(stars_x(i)),int(y)
    y=y+step(i)
    if y>sy then y=0
    stars_y(i)=y
  next i
  return
  
init_stars:
  for i=1 to stars
    step(i)=rnd(1)*2
    stars_x(i)=rnd(sx)
    stars_y(i)=rnd(sy)
  next i
  return

alien:
  for i=1 to aliens
    c=alien_cnt(i)
    if c>0 then
      x=alien_x(i)
      y=alien_y(i)
      draw color 1,rnd(0.5)+0.5,rnd(0.5)+0.5
      draw size 5
      draw line x-2*u,y to x,y-2*u
      draw line to x+2*u,y
      draw line to x,y+2*u
      draw line to x-2*u,y
      s=alien_step(i)
      h=(alien_angle(i)-90)/180*pi
      x=x+s*cos(h)
      y=y+s*sin(h)
      if x<0 then 
        x=sx
      else
        if x>sx then x=0
      end if
      alien_x(i)=x
      alien_y(i)=y
      c=c-1
      if c=0 then
        c=rnd(15*u)+1
        alien_angle(i)=rnd(360)
        alien_step(i)=rnd(1)*3
        shotx=x
        shoty=y
        shotstep=u/3
        gosub add_shot
      else
        if y<0 or y>sy/2 then
          a=alien_angle(i)-180
          if a<0 then a=a+360
          alien_angle(i)=a
        end if
      end if
      alien_cnt(i)=c
    end if
  next i
  return

add_alien:
  found=0
  for i=1 to aliens
    if alien_cnt(i)=0 and found=0 then found=i
  next i
  if found>0 then
    alien_x(found)=rnd(sx)
    alien_y(found)=0
    alien_angle(found)=rnd(90)+135
    alien_step(found)=rnd(1)*3
    alien_cnt(found)=rnd(u*15)+1
  end if
  return

add_explosion:
  found=0
  for ei=1 to explosions
    if explosion_size(ei)=0 then found=ei
  next ei
  if found>0 then
    explosion_x(found)=explosionx
    explosion_y(found)=explosiony
    explosion_size(found)=1
  end if
  return

explosion:
  for ei=1 to explosions
    s=explosion_size(ei)
    if s>0 then
      x=explosion_x(ei)
      y=explosion_y(ei)
   
      for ex=1 to explosionpoints
        draw size rnd(5)+1
        if rnd(1)<0.5 then 
          draw color 1,0,0
        else
          draw color 1,1,0
        end if
        draw pixel int(x+explosion_px(ex)*s),int(y+explosion_py(ex)*s)
      next ex
      s=s+2
      if s>=u*8 then s=0
    end if
    explosion_size(ei)=s
  next ei
  return

shot:
  for i=1 to shots
    x=shot_x(i)
    y=shot_y(i)
    if y>0 and y<sy then 
      draw color rnd(1),rnd(1),rnd(1)
      draw line x,y+u to x,y
      stp=shot_step(i)
      y=y+stp
      if stp<0 then
        f=0
        for ai=1 to aliens
          if alien_cnt(ai)>0 and f=0 then
            ax=alien_x(ai)
            ay=alien_y(ai)
            if x>=ax-2*u and x<=ax+2*u and y>=ay-2*u and y<=ay+2*u then
              explosionx=x
              explosiony=y
              gosub add_explosion
              y=0
              alien_cnt(ai)=0
              f=ai
              points=points+1000
            end if
          end if
        next ai
      else
      end if
      shot_y(i)=y
    end if
  next i
  return

add_shot:
' do not allow new shots if one lower shot is still visible
  found=0
  for si=1 to shots
    y=shot_y(si)
    if y<=0 or y>sy then found=si
    if shot_step(si)<0 and y>sy-5*u then return
  next si
  if found>0 then
    shot_x(found)=shotx
    shot_y(found)=shoty
    shot_step(found)=shotstep
  end if
  return

display:
  fill color 1,1,0.5
  draw color 1,0,0
  fill rect 0,0 to sx,20
  draw text "SCORE: "& points at 10,0
  return