Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1945 simplified
#5
(11-28-2023, 06:17 PM)Marcus Wrote: Regarding collision, "just" check if two images overlap. It's easier to understand if you draw some on a paper Smile
...

Your collision detection code is wonderful, it works like magic. 
Now when a bullet hits an enemy, the enemy disappeares  Big Grin Big Grin Big Grin

Thanks also for your advice to avoid flickering graphics by 'set redraw off' and 'redraw'  Cool

Code:
'------ revised code to reflect some improvements -------

'INITIALIZATION
set window "1945 Simplified", 640, 480
set redraw off 'faster, removes flickering graphics
load image 1,"data_1945/plane.png"
load image 2,"data_1945/bullet.png"
load image 3,"data_1945/enemy.png"
load image 4,"data_1945/black.png"

speed=4
planeX=640/2        ; planeY=400
bulletX = planeX+15 ; bulletY = planeY
enemyX = []         ; enemyY = []
enemyX[0] = 40      ; enemyY[0] = -10
enemyX[1] = 300     ; enemyY[1] = -50
enemyX[2] = 400     ; enemyY[2] = -30

'MAIN PROGRAM
do
    'SET BACKGROUND
     cls
     draw image 4,0,0

     'THE PLANE
     draw image 1,planeX,planeY
                       
    'ESC TO QUIT, LEFT AND RIGHT KEYS TO MOVE THE PLANE
     if keydown(KEY_ESCAPE) then end
     if keydown(KEY_LEFT)   then planeX=planeX-speed
     if keydown(KEY_RIGHT)  then planeX=planeX+speed

     'ENEMIES
     for x=0 to 2
        'collision detection if bullet and enemy image overlap
        dead = bulletX + width(2) > enemyX[x] and bulletX < enemyX[x] + width(3) and
            bulletY + height(2) > enemyY[x] and bulletY < enemyY[x] + height(3)
        if enemyY[x]>480 or dead then
            enemyX[x]=rnd(640)
            enemyY[x]=-rnd(50)
         else
            enemyY[x]=enemyY[x]+speed
         endif
      next

      for x=0 to 2
        draw image 3,enemyX[x],enemyY[x]
      next
           
     'BULLET, AUTO SHOOT   
      if bulletY < 480 then
        bulletY   =   bulletY - speed*3
      endif
     
      if bulletY = 100 then
        bulletX = planeX+15
        bulletY = planeY
      endif         
                                 
      draw image 2,bulletX,bulletY

     'WAIT BEFORE LOOPING
      redraw 'faster, removes flickering graphics
      wait 40
loop
Reply


Messages In This Thread
1945 simplified - by 1micha.elok - 11-28-2023, 09:17 AM
RE: 1945 simplified - by Marcus - 11-28-2023, 04:46 PM
RE: 1945 simplified - by Marcus - 11-28-2023, 06:17 PM
RE: 1945 simplified - by 1micha.elok - 11-29-2023, 03:49 AM
RE: 1945 simplified - by Marcus - 11-29-2023, 06:01 PM
RE: 1945 simplified - by 1micha.elok - 12-20-2023, 10:18 AM
RE: 1945 simplified - by johnno56 - 11-28-2023, 09:08 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)