TANK

Vragen en suggesties over het programmeren van spelletjes, sprites en dergelijke in Liberty BASIC

Moderator: anthonio

TANK

Berichtdoor bluatigro » vr apr 06, 2012 10:33 am

dit is n poging tot t maken van n tank game

sprites :
- zie post : rotating sprites
Code: Selecteer alles

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , px , py , pa , pb , pi , lastkey
winx = WindowWidth
winy = WindowHeight
pi = atn( 1 ) * 4
nomainwin
for i = 0 to 31
  loadbmp "u" ; i , DefaultDir$ + "\BMP\TANK_U" ; nr$( i ) ; ".bmp"
  u$ = u$ + " u" ; i
  loadbmp "gun";i , DefaultDir$ + "\BMP\TANK_GUN" ; nr$( i ) ; ".bmp"
  gun$ = gun$ + " gun" ; i
next i
open "Rotation Sprite Draw" for graphics as #m
  #m "trapclose [quit]"
  #m "addsprite under " ; u$
  #m "addsprite gun " ; gun$
  px = winx / 2 - 50
  py = winy / 2 - 50
  #m "setfocus"
  #m "when characterInput [key]"
  timer 40 , [tmr]
wait
function nr$( x )
  nr$ = right$( "00" ; x , 2 )
end function
function strad( st )
  strad = st * pi / 16
end function
[tmr]
  select case lastkey
    case _VK_UP
      py = py - cos( strad( pa ) )
      px = px - sin( strad( pa ) )
    case _VK_RIGHT
      pa = ( pa - 1 ) and 31
    case _VK_DOWN
      py = py + cos( strad( pa ) )
      px = px + sin( strad( pa ) )
    case _VK_LEFT
      pa = ( pa + 1 ) and 31
    case asc( "z" )
      pb = ( pb + 1 ) and 31
    case asc( "c" )
      pb = ( pb - 1 ) and 31
    case else
  end select
  lastkey = 32
  if px < -100 then px = winx
  if py < -100 then py = winy
  if px > winx then px = -100
  if py > winy then py = -100

  #m "spriteimage under u" ; pa
  #m "spritexy under " ; px ; " " ; py
  #m "spriteimage gun gun" ; ( pa + pb ) and 31
  #m "spritexy gun " ; px ; " " ; py

  #m "drawsprites"
wait
[key]
  lastkey = asc( right$( Inkey$ , 1 ) )
wait
[quit]
  for i = 0 to 31
    unloadbmp "u" ; i
    unloadbmp "gun" ; i
  next i
  close #m
end
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: TANK

Berichtdoor bluatigro » vr apr 06, 2012 10:40 am

update :
- vijand toegevoegt
- vuur
- vijand vuurt wilekurig

error :
- speler tank vuurt
- dat zou eigelijk aleen moeten gebeuren als de speler op de space bar drukt

toekomst :
- colision detectie
- vijand KI [ richten ]
- meer vijanden per next level
Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , px , py , pa , pb , pst , pi , lastkey
global ex , ey , ea , eb , est , fired
winx = WindowWidth
winy = WindowHeight
pi = atn( 1 ) * 4
fired = not( 0 )
nomainwin
for i = 0 to 31
  loadbmp "u" ; i , DefaultDir$ + "\BMP\TANK_U" ; nr$( i ) ; ".bmp"
  u$ = u$ + " u" ; i
  loadbmp "gun";i , DefaultDir$ + "\BMP\TANK_GUN" ; nr$( i ) ; ".bmp"
  gun$ = gun$ + " gun" ; i
next i
loadbmp "fire" , DefaultDir$ + "\BMP\fire.bmp"
open "Rotation Sprite Draw" for graphics as #m
  #m "trapclose [quit]"
  #m "fill black"
  #m "getbmp backbmp 0 0 2 3"
  #m "background backbmp"
  #m "addsprite underP " ; u$
  #m "addsprite gunP " ; gun$
  #m "addsprite fireP fire"
  #m "addsprite underE " ; u$
  #m "addsprite gunE " ; gun$
  #m "addsprite fireE fire"
  px = winx * 3 / 4 - 50
  py = winy / 2 - 50
  ex = winx / 4 - 50
  ey = winy / 2 - 50
  #m "setfocus"
  #m "when characterInput [key]"
  timer 40 , [tmr]
wait
function nr$( x )
  nr$ = right$( "00" ; x , 2 )
end function
function strad( st )
  strad = st * pi / 16
end function
[tmr]
  ''evaluate player
  select case lastkey
    case _VK_UP
      py = py - cos( strad( pa ) ) * 2
      px = px - sin( strad( pa ) ) * 2
    case _VK_RIGHT
      pa = ( pa - 1 ) and 31
    case _VK_DOWN
      py = py + cos( strad( pa ) ) * 2
      px = px + sin( strad( pa ) ) * 2
    case _VK_LEFT
      pa = ( pa + 1 ) and 31
    case _VK_SPACE
      if pst <> fired then
        #m "spritexy fireP " ; px ; " " ; py
        #m "spritemovexy fireP " _
        ; 0 - sin( strad( pb ) ) * 4 ; " " _
        ; 0 - cos( strad( pb ) ) * 4
        pst = fired
      end if
    case asc( "z" )
      pb = ( pb + 1 ) and 31
    case asc( "c" )
      pb = ( pb - 1 ) and 31
    case else
  end select
  lastkey = 32
  if px < -100 then px = winx
  if py < -100 then py = winy
  if px > winx then px = -100
  if py > winy then py = -100
  #m "spritexy? fireP fx fy"
  if fx < -100 _
  or fx > winx _
  or fy < -100 _
  or fy > winy then
    pst = not( fired )
    #m "spritemovexy fireP 0 0"
  end if
  #m "spriteimage underP u" ; pa
  #m "spritexy underP " ; px ; " " ; py
  #m "spriteimage gunP gun" ; ( pa + pb ) and 31
  #m "spritexy gunP " ; px ; " " ; py
  ''evaluate enemy
  select case int( rnd( 0 ) * 6 )
    case 0
      ex = ex - sin( strad( ea ) ) * 2
      ey = ey - cos( strad( ea ) ) * 2
    case 1
      ex = ex + sin( strad( ea ) ) * 2
      ey = ey + cos( strad( ea ) ) * 2
    case 2
      ea = ( ea + 1 ) and 31
    case 3
      ea = ( ea - 1 ) and 31
    case 4
      eb = ( eb + 1 ) and 31
    case 5
      eb = ( eb - 1 ) and 31
    case else
  end select
  if est <> fired then
    #m "spritexy fireE " ; ex ; " " ; ey
    #m "spritemovexy fireE " _
    ; 0 - sin( strad( eb ) ) * 4 ; " " _
    ; 0 - cos( strad( eb ) ) * 4
    est = fired
  end if
  if ex < -100 then ex = winx
  if ey < -100 then ey = winy
  if ex > winx then ex = -100
  if ey > winy then ey = -100
  #m "spritexy? fireE fx fy"
  if fx < -100 _
  or fx > winx _
  or fy < -100 _
  or fy > winy then
    est = not( fired )
    #m "spritemovexy fireE 0 0"
  end if
  #m "spriteimage underE u" ; ea
  #m "spritexy underE " ; ex ; " " ; ey
  #m "spriteimage gunE gun" ; ( ea + eb ) and 31
  #m "spritexy gunE " ; ex ; " " ; ey

  #m "drawsprites"
wait
[key]
  lastkey = asc( right$( Inkey$ , 1 ) )
wait
[quit]
  for i = 0 to 31
    unloadbmp "u" ; i
    unloadbmp "gun" ; i
  next i
  close #m
end
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: TANK

Berichtdoor bluatigro » vr apr 06, 2012 11:03 am

update :
- schieten in de juiste richting [ verlengde van kanon ]

Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , px , py , pa , pb , pst , pi , lastkey
global ex , ey , ea , eb , est , fired
winx = WindowWidth
winy = WindowHeight
pi = atn( 1 ) * 4
fired = not( 0 )
nomainwin
for i = 0 to 31
  loadbmp "u" ; i , DefaultDir$ + "\BMP\TANK_U" ; nr$( i ) ; ".bmp"
  u$ = u$ + " u" ; i
  loadbmp "gun" ; i , DefaultDir$ + "\BMP\TANK_GUN" ; nr$( i ) ; ".bmp"
  gun$ = gun$ + " gun" ; i
next i
loadbmp "fire" , DefaultDir$ + "\BMP\fire.bmp"
open "Rotation Sprite Draw" for graphics as #m
  #m "trapclose [quit]"
  #m "fill black"
  #m "getbmp backbmp 0 0 2 3"
  #m "background backbmp"
  #m "addsprite underP " ; u$
  #m "addsprite gunP " ; gun$
  #m "addsprite fireP fire"
  #m "addsprite underE " ; u$
  #m "addsprite gunE " ; gun$
  #m "addsprite fireE fire"
  px = winx * 3 / 4 - 50
  py = winy / 2 - 50
  ex = winx / 4 - 50
  ey = winy / 2 - 50
  #m "setfocus"
  #m "when characterInput [key]"
  timer 40 , [tmr]
wait
function nr$( x )
  nr$ = right$( "00" ; x , 2 )
end function
function strad( st )
  strad = st * pi / 16
end function
[tmr]
  ''evaluate player
  select case lastkey
    case _VK_UP
      py = py - cos( strad( pa ) ) * 2
      px = px - sin( strad( pa ) ) * 2
    case _VK_RIGHT
      pa = ( pa - 1 ) and 31
    case _VK_DOWN
      py = py + cos( strad( pa ) ) * 2
      px = px + sin( strad( pa ) ) * 2
    case _VK_LEFT
      pa = ( pa + 1 ) and 31
    case _VK_SPACE
      if pst <> fired then
        #m "spritexy fireP " ; px ; " " ; py
        #m "spritemovexy fireP " _
        ; 0 - sin( strad( pb + pa ) ) * 4 ; " " _
        ; 0 - cos( strad( pb + pa ) ) * 4
        pst = fired
      end if
    case asc( "z" )
      pb = ( pb + 1 ) and 31
    case asc( "c" )
      pb = ( pb - 1 ) and 31
    case else
  end select
  lastkey = 32
  if px < -100 then px = winx
  if py < -100 then py = winy
  if px > winx then px = -100
  if py > winy then py = -100
  #m "spritexy? fireP fx fy"
  if fx < -100 _
  or fx > winx _
  or fy < -100 _
  or fy > winy then
    pst = not( fired )
    #m "spritemovexy fireP 0 0"
  end if
  #m "spriteimage underP u" ; pa
  #m "spritexy underP " ; px ; " " ; py
  #m "spriteimage gunP gun" ; ( pa + pb ) and 31
  #m "spritexy gunP " ; px ; " " ; py
  ''evaluate enemy
  select case int( rnd( 0 ) * 6 )
    case 0
      ex = ex - sin( strad( ea ) ) * 2
      ey = ey - cos( strad( ea ) ) * 2
    case 1
      ex = ex + sin( strad( ea ) ) * 2
      ey = ey + cos( strad( ea ) ) * 2
    case 2
      ea = ( ea + 1 ) and 31
    case 3
      ea = ( ea - 1 ) and 31
    case 4
      eb = ( eb + 1 ) and 31
    case 5
      eb = ( eb - 1 ) and 31
    case else
  end select
  if est <> fired then
    #m "spritexy fireE " ; ex ; " " ; ey
    #m "spritemovexy fireE " _
    ; 0 - sin( strad( eb + ea ) ) * 4 ; " " _
    ; 0 - cos( strad( eb + ea ) ) * 4
    est = fired
  end if
  if ex < -100 then ex = winx
  if ey < -100 then ey = winy
  if ex > winx then ex = -100
  if ey > winy then ey = -100
  #m "spritexy? fireE fx fy"
  if fx < -100 _
  or fx > winx _
  or fy < -100 _
  or fy > winy then
    est = not( fired )
    #m "spritemovexy fireE 0 0"
  end if
  #m "spriteimage underE u" ; ea
  #m "spritexy underE " ; ex ; " " ; ey
  #m "spriteimage gunE gun" ; ( ea + eb ) and 31
  #m "spritexy gunE " ; ex ; " " ; ey

  #m "drawsprites"
wait
[key]
  lastkey = asc( right$( Inkey$ , 1 ) )
wait
[quit]
  for i = 0 to 31
    unloadbmp "u" ; i
    unloadbmp "gun" ; i
  next i
  close #m
end
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm


Keer terug naar Games

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron